]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-4633: When changing field (column) datatypes in the database during services...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 22 Mar 2012 23:38:06 +0000 (16:38 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 22 Mar 2012 23:38:06 +0000 (16:38 -0700)
services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java

index 5ab9040cbd4aa71f6697bdf1ad6466c9f3418bab..328cadfa278b7cdb4324a19476d7f735d862e5e1 100644 (file)
@@ -142,21 +142,20 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler {
         Statement stmt = null;
         ResultSet rs = null;
 
-        if (databaseProductType == DatabaseProductType.MYSQL) {
-            sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
-                    + "WHERE TABLE_SCHEMA = '" + getDatabaseName(field) + "'"
+        try {
+            conn = JDBCTools.getConnection(dataSource);
+            stmt = conn.createStatement();
+            if (databaseProductType == DatabaseProductType.MYSQL) {
+                sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
+                    + "WHERE TABLE_SCHEMA = '" + JDBCTools.getDatabaseName(conn) + "'"
                     + " AND TABLE_NAME = '" + getTableName(field) + "'"
                     + " AND COLUMN_NAME = '" + field.getCol() + "'";
-        } else if (databaseProductType == DatabaseProductType.POSTGRESQL) {
-            sql = "SELECT data_type FROM information_schema.columns "
-                    + "WHERE table_catalog = '" + getDatabaseName(field) + "'"
+            } else if (databaseProductType == DatabaseProductType.POSTGRESQL) {
+                sql = "SELECT data_type FROM information_schema.columns "
+                    + "WHERE table_catalog = '" + JDBCTools.getDatabaseName(conn) + "'"
                     + " AND table_name = '" + getTableName(field) + "'"
                     + " AND column_name = '" + field.getCol() + "'";
-        }
-
-        try {
-            conn = JDBCTools.getConnection(dataSource);
-            stmt = conn.createStatement();
+            }
             rs = stmt.executeQuery(sql);
             while (rs.next()) {
                 currentDatatype = rs.getString(1);
index bf9e5310c99074971a11a35b6f7e683860d5bba3..ab5909642c41e1d3bf83bcdf24c5659a2d3cb736 100644 (file)
@@ -52,6 +52,7 @@ public class JDBCTools {
 \r
     //todo: make sure this will get instantiated in the right order\r
     final static Logger logger = LoggerFactory.getLogger(JDBCTools.class);\r
+    private static String JDBC_URL_DATABASE_SEPARATOR = "\\/";\r
         \r
     public static DataSource getDataSource(String repositoryName) throws NamingException {\r
        DataSource result = null;\r
@@ -253,6 +254,36 @@ public class JDBCTools {
     private static String getDefaultRepositoryName() {\r
         return DEFAULT_REPOSITORY_NAME;\r
     }\r
+    \r
+    /**\r
+     * Returns the catalog name for an open JDBC connection.\r
+     * \r
+     * @param conn an open JDBC Connection\r
+     * @return the catalog name.\r
+     * @throws SQLException \r
+     */\r
+    public static String getDatabaseName(Connection conn) throws Exception {\r
+        String databaseName = "";\r
+        if (conn == null) {\r
+            return databaseName;\r
+        }\r
+        DatabaseMetaData metadata = conn.getMetaData();\r
+        String urlStr = metadata.getURL();\r
+        \r
+        // Format of the PostgreSQL JDBC URL:\r
+        // http://jdbc.postgresql.org/documentation/80/connect.html\r
+        if (getDatabaseProductType() == DatabaseProductType.POSTGRESQL) {\r
+            String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR);\r
+            databaseName = tokens[tokens.length - 1];\r
+            // Format of the MySQL JDBC URL:\r
+            // http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html\r
+            // FIXME: the last token could contain optional parameters, not accounted for here.\r
+        } else if (getDatabaseProductType() == DatabaseProductType.MYSQL) {\r
+            String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR);\r
+            databaseName = tokens[tokens.length - 1];\r
+        }\r
+        return databaseName;\r
+    }\r
 \r
     /**\r
      * Prints metadata, such as database username and connection URL,\r