diff options
Diffstat (limited to 'src')
3 files changed, 24 insertions, 11 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java b/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java index 1d93d68..38c063c 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java @@ -52,12 +52,15 @@ public class ConnectionFactory { private String dbname; private String dbuser; private String dbcr; + private String schema; + public ConnectionFactory() { Properties p = DmaapConfig.getConfig(); host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com"); dbname = p.getProperty("DB.name", "dmaap"); dbuser = p.getProperty("DB.user", "dmaap_admin"); dbcr = p.getProperty("DB.cred", "test234-ftl"); + schema = p.getProperty("DB.schema", "public"); } public static ConnectionFactory getDefaultInstance() { return(instance); @@ -77,6 +80,9 @@ public class ConnectionFactory { p.put("password", dbcr); return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p)); } + public String getSchema() { + return(schema); + } public void release(Connection c) { synchronized(this) { if (cur < pool.length) { diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/LoadSchema.java b/src/main/java/org/onap/dmaap/dbcapi/database/LoadSchema.java index 6e64c4a..2a7925b 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/LoadSchema.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/LoadSchema.java @@ -55,7 +55,7 @@ public class LoadSchema { Connection c = null; Statement stmt = null; InputStream is = null; - try { + try { c = cf.get(true); stmt = c.createStatement(); int newver = -1; @@ -63,11 +63,17 @@ public class LoadSchema { newver = getVer(stmt); } catch (Exception e) {} logger.info("Database schema currently at version " + newver++); + while ((is = LoadSchema.class.getClassLoader().getResourceAsStream("schema_" + newver + ".sql")) != null) { logger.info("Upgrading database schema to version " + newver); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s; - String sofar = null; + String sofar; + if ( newver > 0 ) { + sofar = null; + } else { + sofar = String.format( "SET search_path to %s;", cf.getSchema()); + } while ((s = br.readLine()) != null) { logger.info("SCHEMA: " + s); s = s.trim(); 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 b43edc5..66539ed 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/TableHandler.java @@ -68,7 +68,8 @@ class TableHandler<C> { private void setup(DatabaseMetaData dmd, Class<C> cls, String tabname, String keyname) throws Exception { this.cls = cls; Vector<DBFieldHandler> h = new Vector<DBFieldHandler>(); - ResultSet rs = dmd.getColumns("", "public", tabname, null); + 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(); @@ -100,15 +101,15 @@ class TableHandler<C> { if (haskey) { count++; h.add(new DBFieldHandler(cls, keyname, count, getSpecialCase(tabname, keyname))); - delstmt = "DELETE FROM " + tabname + " WHERE " + keyname + " = ?"; - insorreplstmt = "INSERT INTO " + tabname + " (" + clist + ", " + keyname + ") VALUES (" + qlist + ", ?) ON CONFLICT(" + keyname + ") DO UPDATE SET (" + clist + ") = (" + elist + ")"; - getstmt = "SELECT " + clist + ", " + keyname + " FROM " + tabname + " WHERE " + keyname + " = ?"; - liststmt = "SELECT " + clist + ", " + keyname + " FROM " + tabname; + 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; } else { - delstmt = "DELETE FROM " + tabname; - initstmt = "INSERT INTO " + tabname + " (" + clist + ") VALUES (" + qlist + ")"; - insorreplstmt = "UPDATE " + tabname + " SET (" + clist + ") = (" + qlist + ")"; - getstmt = "SELECT " + clist + ", " + keyname + " FROM " + tabname; + delstmt = "DELETE FROM " + qualifiedTableName; + initstmt = "INSERT INTO " + qualifiedTableName + " (" + clist + ") VALUES (" + qlist + ")"; + insorreplstmt = "UPDATE " + qualifiedTableName + " SET (" + clist + ") = (" + qlist + ")"; + getstmt = "SELECT " + clist + ", " + keyname + " FROM " + qualifiedTableName; } fields = h.toArray(new DBFieldHandler[h.size()]); } |