db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r359917 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform: JdbcModelReader.java hsqldb/HsqlDbModelReader.java
Date Thu, 29 Dec 2005 22:43:23 GMT
Author: tomdz
Date: Thu Dec 29 14:43:18 2005
New Revision: 359917

URL: http://svn.apache.org/viewcvs?rev=359917&view=rev
Log:
Added workaround for determining auto-increment columns from Hsqldb databases

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=359917&r1=359916&r2=359917&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 Thu Dec 29
14:43:18 2005
@@ -76,6 +76,8 @@
     private String _defaultTablePattern = "%";
     /** The table types to recognize per default. */
     private String[] _defaultTableTypes = { "TABLE" };
+    /** The active connection while reading a database model. */
+    private Connection _connection;
 
     /**
      * Creates a new model reader instance.
@@ -356,6 +358,17 @@
     }
 
     /**
+     * Returns the active connection. Note that this is only set during a call to
+     * {@link #readTables(String, String, String[])}.
+     *
+     * @return The connection or <code>null</code> if there is no active connection
+     */
+    protected Connection getConnection()
+    {
+        return _connection;
+    }
+
+    /**
      * Reads the database model from the given connection.
      * 
      * @param connection The connection
@@ -402,20 +415,27 @@
         {
             db.setName(name);
         }
-        db.addTables(readTables(connection, catalog, schema, tableTypes));
+        try
+        {
+            _connection = connection;
+            db.addTables(readTables(catalog, schema, tableTypes));
+        }
+        finally
+        {
+            _connection = null;
+        }
         return db;
     }
 
     /**
      * Reads the tables from the database metadata.
      * 
-     * @param connection    The connection
      * @param catalog       The catalog to acess in the database; use <code>null</code>
for the default value
      * @param schemaPattern The schema(s) to acess in the database; use <code>null</code>
for the default value
      * @param tableTypes    The table types to process; use <code>null</code>
or an empty list for the default ones
      * @return The tables
      */
-    protected Collection readTables(Connection connection, String catalog, String schemaPattern,
String[] tableTypes) throws SQLException
+    protected Collection readTables(String catalog, String schemaPattern, String[] tableTypes)
throws SQLException
     {
         ResultSet tableData = null;
 
@@ -423,7 +443,7 @@
         {
             DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();
 
-            metaData.setMetaData(connection.getMetaData());
+            metaData.setMetaData(_connection.getMetaData());
             metaData.setCatalog(catalog == null ? getDefaultCatalogPattern() : catalog);
             metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() :
schemaPattern);
             metaData.setTableTypes((tableTypes == null) || (tableTypes.length == 0) ? getDefaultTableTypes()
: tableTypes);

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=359917&r1=359916&r2=359917&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
Thu Dec 29 14:43:18 2005
@@ -16,10 +16,18 @@
  * 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;
+import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
 import org.apache.ddlutils.platform.JdbcModelReader;
 
 /**
@@ -40,6 +48,66 @@
         super(platformInfo);
         setDefaultCatalogPattern(null);
         setDefaultSchemaPattern(null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Table table = super.readTable(metaData, values);
+
+        // For at least version 1.7.2 we have to determine the auto-increment columns
+        // from a result set meta data because the database does not put this info
+        // 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();
+        
+        return table;
     }
 
     /**



Mime
View raw message