db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r366027 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform: JdbcModelReader.java hsqldb/HsqlDbModelReader.java
Date Wed, 04 Jan 2006 23:32:59 GMT
Author: tomdz
Date: Wed Jan  4 15:32:54 2006
New Revision: 366027

URL: http://svn.apache.org/viewcvs?rev=366027&view=rev
Log:
Extracted utility method for determining the auto-increment/identity property of a column
via the ResultSetMetaData

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java?rev=366027&r1=366026&r2=366027&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java Wed Jan 
4 15:32:54 2006
@@ -18,7 +18,9 @@
 
 import java.sql.Connection;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -909,5 +911,59 @@
             values.put(descriptor.getName(), descriptor.readColumn(resultSet));
         }
         return values;
+    }
+
+    /**
+     * Helper method that determines the auto increment status for the given columns via
the
+     * {@link ResultSetMetaData#isAutoIncrement(int)} method.
+     * 
+     * @param table          The table
+     * @param columnsToCheck The columns to check (e.g. the primary key columns)
+     */
+    protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck)
throws SQLException
+    {
+        StringBuffer query = new StringBuffer();
+    
+        query.append("SELECT ");
+        for (int idx = 0; idx < columnsToCheck.length; idx++)
+        {
+            if (idx > 0)
+            {
+                query.append(",");
+            }
+            if (getPlatformInfo().isUseDelimitedIdentifiers())
+            {
+                query.append(getPlatformInfo().getDelimiterToken());
+            }
+            query.append(columnsToCheck[idx].getName());
+            if (getPlatformInfo().isUseDelimitedIdentifiers())
+            {
+                query.append(getPlatformInfo().getDelimiterToken());
+            }
+        }
+        query.append(" FROM ");
+        if (getPlatformInfo().isUseDelimitedIdentifiers())
+        {
+            query.append(getPlatformInfo().getDelimiterToken());
+        }
+        query.append(table.getName());
+        if (getPlatformInfo().isUseDelimitedIdentifiers())
+        {
+            query.append(getPlatformInfo().getDelimiterToken());
+        }
+        query.append(" WHERE 1 = 0");
+        
+        Statement         stmt       = getConnection().createStatement();
+        ResultSet         rs         = stmt.executeQuery(query.toString());
+        ResultSetMetaData rsMetaData = rs.getMetaData();
+    
+        for (int idx = 0; idx < columnsToCheck.length; idx++)
+        {
+            if (rsMetaData.isAutoIncrement(idx + 1))
+            {
+                columnsToCheck[idx].setAutoIncrement(true);
+            }
+        }
+        stmt.close();
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java?rev=366027&r1=366026&r2=366027&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
Wed Jan  4 15:32:54 2006
@@ -16,14 +16,10 @@
  * limitations under the License.
  */
 
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.Map;
 
 import org.apache.ddlutils.PlatformInfo;
-import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
 import org.apache.ddlutils.model.Table;
@@ -62,50 +58,7 @@
         // into the database metadata
         // Since Hsqldb only allows IDENTITY for primary key columns, we restrict
         // our search to those columns
-        Column[]     pks   = table.getPrimaryKeyColumns();
-        StringBuffer query = new StringBuffer();
-
-        query.append("SELECT ");
-        for (int idx = 0; idx < pks.length; idx++)
-        {
-            if (idx > 0)
-            {
-                query.append(",");
-            }
-            if (getPlatformInfo().isUseDelimitedIdentifiers())
-            {
-                query.append("\"");
-            }
-            query.append(pks[idx].getName());
-            if (getPlatformInfo().isUseDelimitedIdentifiers())
-            {
-                query.append("\"");
-            }
-        }
-        query.append(" FROM ");
-        if (getPlatformInfo().isUseDelimitedIdentifiers())
-        {
-            query.append("\"");
-        }
-        query.append(table.getName());
-        if (getPlatformInfo().isUseDelimitedIdentifiers())
-        {
-            query.append("\"");
-        }
-        query.append(" WHERE 1 = 0");
-        
-        Statement         stmt       = getConnection().createStatement();
-        ResultSet         rs         = stmt.executeQuery(query.toString());
-        ResultSetMetaData rsMetaData = rs.getMetaData();
-
-        for (int idx = 0; idx < pks.length; idx++)
-        {
-            if (rsMetaData.isAutoIncrement(idx + 1))
-            {
-                pks[idx].setAutoIncrement(true);
-            }
-        }
-        stmt.close();
+        determineAutoIncrementFromResultSetMetaData(table, table.getPrimaryKeyColumns());
         
         return table;
     }



Mime
View raw message