db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r419021 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase: InterbaseBuilder.java InterbaseModelReader.java InterbasePlatform.java
Date Tue, 04 Jul 2006 14:36:50 GMT
Author: tomdz
Date: Tue Jul  4 07:36:50 2006
New Revision: 419021

URL: http://svn.apache.org/viewvc?rev=419021&view=rev
Log:
Enhanced Interbase platform

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseBuilder.java?rev=419021&r1=419020&r2=419021&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseBuilder.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseBuilder.java
Tue Jul  4 07:36:50 2006
@@ -76,41 +76,13 @@
     public void createTable(Database database, Table table, Map parameters) throws IOException
     {
         super.createTable(database, table, parameters);
-        print("COMMIT");
-        printEndOfStatement();
 
         // creating generator and trigger for auto-increment
         Column[] columns = table.getAutoIncrementColumns();
 
         for (int idx = 0; idx < columns.length; idx++)
         {
-            print("CREATE GENERATOR ");
-            printIdentifier(getConstraintName("gen", table, columns[idx].getName(), null));
-            printEndOfStatement();
-            print("COMMIT");
-            printEndOfStatement();
-            print("SET TERM !!");
-            printEndOfStatement();
-            print("CREATE TRIGGER ");
-            printIdentifier(getConstraintName("trg", table, columns[idx].getName(), null));
-            print(" FOR ");
-            printlnIdentifier(getTableName(table));
-            println("ACTIVE BEFORE INSERT POSITION 0 AS");
-            println("BEGIN");
-            print("IF (NEW.");
-            printIdentifier(getColumnName(columns[idx]));
-            println(" IS NULL) THEN");
-            print("NEW.");
-            printIdentifier(getColumnName(columns[idx]));
-            print(" = GEN_ID(");
-            printIdentifier(getConstraintName("gen", table, columns[idx].getName(), null));
-            println(", 1);");
-            println("END !!");
-            print("SET TERM ");
-            print(getPlatformInfo().getSqlCommandDelimiter());
-            println(" !!");
-            print("COMMIT");
-            printEndOfStatement();
+            writeAutoIncrementCreateStmts(database, table, columns[idx]);
         }
     }
 
@@ -140,15 +112,66 @@
 
         for (int idx = 0; idx < columns.length; idx++)
         {
-            print("DELETE FROM RDB$GENERATOR WHERE RDB$GENERATOR_NAME = ");
-            printIdentifier(getConstraintName("gen", table, columns[idx].getName(), null));
-            printEndOfStatement();
-            print("COMMIT");
-            printEndOfStatement();
+            writeAutoIncrementDropStmts(table, columns[idx]);
         }
         super.dropTable(table);
-        print("COMMIT");
+    }
+
+    /**
+     * Writes the creation statements to make the given column an auto-increment column.
+     * 
+     * @param database The database model
+     * @param table    The table
+     * @param column   The column to make auto-increment
+     */
+    private void writeAutoIncrementCreateStmts(Database database, Table table, Column column)
throws IOException
+    {
+        print("CREATE GENERATOR ");
+        printIdentifier(getGeneratorName(table, column));
         printEndOfStatement();
+
+        print("CREATE TRIGGER ");
+        printIdentifier(getConstraintName("trg", table, column.getName(), null));
+        print(" FOR ");
+        printlnIdentifier(getTableName(table));
+        println("ACTIVE BEFORE INSERT POSITION 0 AS");
+        print("BEGIN IF (NEW.");
+        printIdentifier(getColumnName(column));
+        print(" IS NULL) THEN NEW.");
+        printIdentifier(getColumnName(column));
+        print(" = GEN_ID(");
+        printIdentifier(getGeneratorName(table, column));
+        print(", 1); END");
+        printEndOfStatement();
+    }
+
+    /**
+     * Writes the statements to drop the auto-increment status for the given column.
+     * 
+     * @param table  The table
+     * @param column The column to remove the auto-increment status for
+     */
+    private void writeAutoIncrementDropStmts(Table table, Column column) throws IOException
+    {
+        print("DROP TRIGGER ");
+        printIdentifier(getConstraintName("trg", table, column.getName(), null));
+        printEndOfStatement();
+
+        print("DROP GENERATOR ");
+        printIdentifier(getGeneratorName(table, column));
+        printEndOfStatement();
+    }
+
+    /**
+     * Determines the name of the generator for an auto-increment column.
+     * 
+     * @param table  The table
+     * @param column The auto-increment column
+     * @return The generator name
+     */
+    protected String getGeneratorName(Table table, Column column)
+    {
+        return getConstraintName("gen", table, column.getName(), null);
     }
 
     /**
@@ -178,7 +201,7 @@
             for (int idx = 0; idx < columns.length; idx++)
             {
                 result.append("GEN_ID(");
-                result.append(getConstraintName("gen", table, columns[idx].getName(), null));
+                result.append(getGeneratorName(table, columns[idx]));
                 result.append(", 0)");
             }
             result.append(" FROM RDB$DATABASE");

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java?rev=419021&r1=419020&r2=419021&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
Tue Jul  4 07:36:50 2006
@@ -16,11 +16,14 @@
  * limitations under the License.
  */
 
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -28,6 +31,7 @@
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Index;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.TypeMap;
 import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
 import org.apache.ddlutils.platform.JdbcModelReader;
 
@@ -56,6 +60,24 @@
     /**
      * {@inheritDoc}
      */
+    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Table table = super.readTable(metaData, values);
+
+        if (table != null)
+        {
+            determineColumnSizes(table);
+            determineColumnDefaultValues(table);
+            determineAutoIncrementColumns(table);
+            adjustColumns(table);
+        }
+
+        return table;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     protected Collection readColumns(DatabaseMetaDataWrapper metaData, String tableName)
throws SQLException
     {
         ResultSet columnData = null;
@@ -105,17 +127,163 @@
     }
 
     /**
+     * Helper method that determines the column default values using Interbase's system tables.
+     *
+     * @param table The table
+     */
+    protected void determineColumnDefaultValues(Table table) throws SQLException
+    {
+        PreparedStatement prepStmt = getConnection().prepareStatement("SELECT RDB$FIELD_NAME,
RDB$DEFAULT_SOURCE FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME=?");
+
+        try
+        {
+            prepStmt.setString(1, getPlatform().isDelimitedIdentifierModeOn() ? table.getName()
: table.getName().toUpperCase());
+
+            ResultSet rs = prepStmt.executeQuery();
+
+            while (rs.next())
+            {
+                String columnName = rs.getString(1).trim();
+                Column column     = table.findColumn(columnName, getPlatform().isDelimitedIdentifierModeOn());
+
+                if (column != null)
+                {
+                    String defaultValue = rs.getString(2);
+
+                    if (!rs.wasNull() && (defaultValue != null))
+                    {
+                        defaultValue = defaultValue.trim();
+                        if (defaultValue.startsWith("DEFAULT "))
+                        {
+                            defaultValue = defaultValue.substring("DEFAULT ".length());
+                        }
+                        column.setDefaultValue(defaultValue);
+                    }
+                }
+            }
+            rs.close();
+        }
+        finally
+        {
+            prepStmt.close();
+        }
+    }
+
+    /**
+     * Helper method that determines the column sizes (precision, scale) using Interbase's
system tables.
+     *
+     * @param table The table
+     */
+    protected void determineColumnSizes(Table table) throws SQLException
+    {
+        PreparedStatement prepStmt = getConnection().prepareStatement("SELECT a.RDB$FIELD_NAME,
b.RDB$FIELD_PRECISION, b.RDB$FIELD_SCALE FROM RDB$RELATION_FIELDS a, RDB$FIELDS b WHERE a.RDB$RELATION_NAME=?
AND a.RDB$FIELD_SOURCE=b.RDB$FIELD_NAME");
+
+        try
+        {
+            prepStmt.setString(1, getPlatform().isDelimitedIdentifierModeOn() ? table.getName()
: table.getName().toUpperCase());
+
+            ResultSet rs = prepStmt.executeQuery();
+
+            while (rs.next())
+            {
+                String columnName = rs.getString(1).trim();
+                Column column     = table.findColumn(columnName, getPlatform().isDelimitedIdentifierModeOn());
+
+                if (column != null)
+                {
+                    short   precision          = rs.getShort(2);
+                    boolean precisionSpecified = !rs.wasNull();
+                    short   scale              = rs.getShort(3);
+                    boolean scaleSpecified     = !rs.wasNull();
+
+                    if (precisionSpecified)
+                    {
+                        // for some reason, Interbase stores the negative scale
+                        column.setSizeAndScale(precision, scaleSpecified ? -scale : 0);
+                    }
+                }
+            }
+            rs.close();
+        }
+        finally
+        {
+            prepStmt.close();
+        }
+    }
+
+    /**
+     * Helper method that determines the auto increment status using Interbase's system tables.
+     *
+     * @param table The table
+     */
+    protected void determineAutoIncrementColumns(Table table) throws SQLException
+    {
+        // Since for long table and column names, the generator name will be shortened
+        // we have to determine for each column whether there is a generator for it
+        InterbaseBuilder builder = (InterbaseBuilder)getPlatform().getSqlBuilder();
+        Column[]         columns = table.getColumns();
+        HashMap          names   = new HashMap();
+        String           name;
+
+        for (int idx = 0; idx < columns.length; idx++)
+        {
+            name = builder.getGeneratorName(table, columns[idx]);
+            if (!getPlatform().isDelimitedIdentifierModeOn())
+            {
+                name = name.toUpperCase();
+            }
+            names.put(name, columns[idx]);
+        }
+
+        Statement stmt = getConnection().createStatement();
+
+        try
+        {
+            ResultSet rs = stmt.executeQuery("SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS");
+
+            while (rs.next())
+            {
+                String generatorName = rs.getString(1).trim();
+                Column column        = (Column)names.get(generatorName);
+
+                if (column != null)
+                {
+                    column.setAutoIncrement(true);
+                }
+            }
+            rs.close();
+        }
+        finally
+        {
+            stmt.close();
+        }
+    }
+
+    /**
      * {@inheritDoc}
      */
-    protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    protected void adjustColumns(Table table)
     {
-        Column column = super.readColumn(metaData, values);
+        Column[] columns = table.getColumns();
 
-        if (column.getTypeCode() == Types.FLOAT)
+        for (int idx = 0; idx < columns.length; idx++)
         {
-            column.setTypeCode(Types.REAL);
+            if (columns[idx].getTypeCode() == Types.FLOAT)
+            {
+                columns[idx].setTypeCode(Types.REAL);
+            }
+            else if ((columns[idx].getTypeCode() == Types.NUMERIC) || (columns[idx].getTypeCode()
== Types.DECIMAL))
+            {
+                if ((columns[idx].getTypeCode() == Types.NUMERIC) && (columns[idx].getSizeAsInt()
== 18) && (columns[idx].getScale() == 0))
+                {
+                    columns[idx].setTypeCode(Types.BIGINT);
+                }
+            }
+            else if (TypeMap.isTextType(columns[idx].getTypeCode()))
+            {
+                columns[idx].setDefaultValue(unescape(columns[idx].getDefaultValue(), "'",
"''"));
+            }
         }
-        return column;
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java?rev=419021&r1=419020&r2=419021&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
Tue Jul  4 07:36:50 2006
@@ -52,26 +52,27 @@
         info.setCommentSuffix("*/");
 
         // BINARY and VARBINARY are also handled by the InterbaseBuilder.getSqlType method
-        info.addNativeTypeMapping(Types.ARRAY,         "BLOB");
+        info.addNativeTypeMapping(Types.ARRAY,         "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.BIGINT,        "NUMERIC(18,0)");
-        info.addNativeTypeMapping(Types.BINARY,        "CHAR {0} CHARACTER SET OCTETS");
-        info.addNativeTypeMapping(Types.BIT,           "SMALLINT",         Types.SMALLINT);
-        info.addNativeTypeMapping(Types.CLOB,          "BLOB SUB_TYPE TEXT");
-        info.addNativeTypeMapping(Types.DISTINCT,      "BLOB");
+        info.addNativeTypeMapping(Types.BINARY,        "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.BIT,           "SMALLINT",           Types.SMALLINT);
+        info.addNativeTypeMapping(Types.BLOB,          "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.CLOB,          "BLOB SUB_TYPE TEXT", Types.VARCHAR);
+        info.addNativeTypeMapping(Types.DISTINCT,      "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.DOUBLE,        "DOUBLE PRECISION");
-        info.addNativeTypeMapping(Types.FLOAT,         "DOUBLE PRECISION", Types.DOUBLE);
-        info.addNativeTypeMapping(Types.JAVA_OBJECT,   "BLOB");
-        info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB");
-        info.addNativeTypeMapping(Types.LONGVARCHAR,   "BLOB SUB_TYPE TEXT");
-        info.addNativeTypeMapping(Types.NULL,          "BLOB");
-        info.addNativeTypeMapping(Types.OTHER,         "BLOB");
+        info.addNativeTypeMapping(Types.FLOAT,         "DOUBLE PRECISION",   Types.DOUBLE);
+        info.addNativeTypeMapping(Types.JAVA_OBJECT,   "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.LONGVARCHAR,   "BLOB SUB_TYPE TEXT", Types.VARCHAR);
+        info.addNativeTypeMapping(Types.NULL,          "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.OTHER,         "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.REAL,          "FLOAT");
-        info.addNativeTypeMapping(Types.REF,           "BLOB");
-        info.addNativeTypeMapping(Types.STRUCT,        "BLOB");
-        info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT",         Types.SMALLINT);
-        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} CHARACTER SET OCTETS");
+        info.addNativeTypeMapping(Types.REF,           "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.STRUCT,        "BLOB",               Types.LONGVARBINARY);
+        info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT",           Types.SMALLINT);
+        info.addNativeTypeMapping(Types.VARBINARY,     "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping("BOOLEAN",  "SMALLINT", "SMALLINT");
-        info.addNativeTypeMapping("DATALINK", "BLOB");
+        info.addNativeTypeMapping("DATALINK", "BLOB",     "LONGVARBINARY");
 
         setSqlBuilder(new InterbaseBuilder(this));
         setModelReader(new InterbaseModelReader(this));



Mime
View raw message