From e949bab0978f7c1d9822d4030d9a00f72c2ee266 Mon Sep 17 00:00:00 2001 From: Arundathi Patil Date: Wed, 5 Sep 2018 18:54:54 +0530 Subject: TableHandler.java: Fixed sonar issues Fixed sonar code-smells/issues across this file Issue-ID: DMAAP-733 Change-Id: Ibea07e1f3904d66dc4d3ef31567472eb5ba2a3f8 Signed-off-by: Arundathi Patil --- .../onap/dmaap/dbcapi/database/TableHandler.java | 51 ++++++++++++---------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java b/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java index c83b6be..fc68aa2 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java @@ -40,22 +40,10 @@ class TableHandler { protected String initstmt; protected Class cls; protected DBFieldHandler[] fields; - private static Map> exceptions = new HashMap>(); - public static void setSpecialCase(String dbtabname, String dbfldname, DBFieldHandler.SqlOp handler) { - Map m = exceptions.get(dbtabname); - if (m == null) { - m = new HashMap(); - exceptions.put(dbtabname, m); - } - m.put(dbfldname, handler); - } - public static DBFieldHandler.SqlOp getSpecialCase(String dbtabname, String dbfldname) { - Map m = exceptions.get(dbtabname); - if (m != null) { - return(m.get(dbfldname)); - } - return(null); - } + private static Map> exceptions = new HashMap<>(); + private String select = "SELECT "; + private String from = " FROM "; + protected TableHandler(Class cls, String tabname, String keyname) throws Exception { this(ConnectionFactory.getDefaultInstance(), cls, tabname, keyname); } @@ -71,14 +59,31 @@ class TableHandler { } } } + + public static void setSpecialCase(String dbtabname, String dbfldname, DBFieldHandler.SqlOp handler) { + Map m = exceptions.get(dbtabname); + if (m == null) { + m = new HashMap<>(); + exceptions.put(dbtabname, m); + } + m.put(dbfldname, handler); + } + public static DBFieldHandler.SqlOp getSpecialCase(String dbtabname, String dbfldname) { + Map m = exceptions.get(dbtabname); + if (m != null) { + return(m.get(dbfldname)); + } + return(null); + } + private void setup(DatabaseMetaData dmd, Class cls, String tabname, String keyname) throws Exception { this.cls = cls; - Vector h = new Vector(); + Vector h = new Vector<>(); String qualifiedTableName = String.format( "%s.%s", cf.getSchema(), tabname ); ResultSet rs = dmd.getColumns("", cf.getSchema(), tabname, null); - StringBuffer sb1 = new StringBuffer(); - StringBuffer sb2 = new StringBuffer(); - StringBuffer sb3 = new StringBuffer(); + StringBuilder sb1 = new StringBuilder(); + StringBuilder sb2 = new StringBuilder(); + StringBuilder sb3 = new StringBuilder(); int count = 0; while (rs.next()) { if (!rs.getString(3).equals(tabname)) { @@ -109,13 +114,13 @@ class TableHandler { h.add(new DBFieldHandler(cls, keyname, count, getSpecialCase(tabname, keyname))); delstmt = "DELETE FROM " + qualifiedTableName + " WHERE " + keyname + " = ?"; insorreplstmt = "INSERT INTO " + qualifiedTableName + " (" + clist + ", " + keyname + ") VALUES (" + qlist + ", ?) ON CONFLICT(" + keyname + ") DO UPDATE SET (" + clist + ") = (" + elist + ")"; - getstmt = "SELECT " + clist + ", " + keyname + " FROM " + qualifiedTableName + " WHERE " + keyname + " = ?"; - liststmt = "SELECT " + clist + ", " + keyname + " FROM " + qualifiedTableName; + getstmt = select + clist + ", " + keyname + from + qualifiedTableName + " WHERE " + keyname + " = ?"; + liststmt = select + clist + ", " + keyname + from + qualifiedTableName; } else { delstmt = "DELETE FROM " + qualifiedTableName; initstmt = "INSERT INTO " + qualifiedTableName + " (" + clist + ") VALUES (" + qlist + ")"; insorreplstmt = "UPDATE " + qualifiedTableName + " SET (" + clist + ") = (" + qlist + ")"; - getstmt = "SELECT " + clist + ", " + keyname + " FROM " + qualifiedTableName; + getstmt = select + clist + ", " + keyname + from + qualifiedTableName; } fields = h.toArray(new DBFieldHandler[h.size()]); } -- cgit 1.2.3-korg