You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by to...@apache.org on 2006/01/01 21:26:30 UTC

svn commit: r360543 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils: PlatformInfo.java platform/SqlBuilder.java

Author: tomdz
Date: Sun Jan  1 12:26:25 2006
New Revision: 360543

URL: http://svn.apache.org/viewcvs?rev=360543&view=rev
Log:
Added support for databases that do not support non-unique indices (Mckoi)
Added support for specifying the auto-increment definition in the default value of the column specification
Added support for embedded indices

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java?rev=360543&r1=360542&r2=360543&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java Sun Jan  1 12:26:25 2006
@@ -44,6 +44,9 @@
     /** Whether foreign key constraints are embedded inside the create table statement. */
     private boolean _foreignKeysEmbedded = false;
 
+    /** Whether non-unique indices are supported. */
+    private boolean _supportingNonUniqueIndices = true;
+
     /** Whether indices are embedded inside the create table statement. */
     private boolean _indicesEmbedded = false;
 
@@ -53,6 +56,9 @@
     /** Whether identity specification is supported for non-primary key columns. */
     private boolean _supportingNonPKIdentityColumns = true;
 
+    /** Whether the auto-increment definition is done via the DEFAULT part of the column definition. */
+    private boolean _identitySpecUsesDefaultValue = false;
+
     /** Whether an ALTER TABLE is needed to drop indexes. */
     private boolean _useAlterTableForDrop = false;
 
@@ -198,6 +204,27 @@
     }
 
     /**
+     * Determines whether non-unique indices are supported.
+     *
+     * @return <code>true</code> if non-unique indices are supported
+     */
+    public boolean isSupportingNonUniqueIndices()
+    {
+        return _supportingNonUniqueIndices;
+    }
+
+    /**
+     * Specifies whether non-unique indices are supported.
+     *
+     * @param supportingNonUniqueIndices <code>true</code> if non-unique indices
+     *                                   are supported
+     */
+    public void setSupportingNonUniqueIndices(boolean supportingNonUniqueIndices)
+    {
+        _supportingNonUniqueIndices = supportingNonUniqueIndices;
+    }
+
+    /**
      * Determines whether the indices are embedded in the create table clause
      * or as seperate statements. Per default, indices are external.
      * 
@@ -258,6 +285,29 @@
     public void setSupportingNonPKIdentityColumns(boolean supportingNonPKIdentityColumns)
     {
         _supportingNonPKIdentityColumns = supportingNonPKIdentityColumns;
+    }
+
+    /**
+     * Determines whether the auto-increment specification uses the DEFAULT value of the
+     * column definition.
+     *
+     * @return <code>true</code> if the auto-increment spec is done via the DEFAULT value
+     */
+    public boolean isIdentitySpecUsesDefaultValue()
+    {
+        return _identitySpecUsesDefaultValue;
+    }
+
+    /**
+     * Specifies whether the auto-increment specification uses the DEFAULT value of the
+     * column definition.
+     *
+     * @param identitySpecUsesDefaultValue <code>true</code> if the auto-increment spec is
+     *                                     done via the DEFAULT value
+     */
+    public void setIdentitySpecUsesDefaultValue(boolean identitySpecUsesDefaultValue)
+    {
+        _identitySpecUsesDefaultValue = identitySpecUsesDefaultValue;
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=360543&r1=360542&r2=360543&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Sun Jan  1 12:26:25 2006
@@ -591,7 +591,7 @@
         println("(");
 
         writeColumns(table);
-
+        
         if (getPlatformInfo().isPrimaryKeyEmbedded())
         {
             writeEmbeddedPrimaryKeysStmt(table);
@@ -1113,7 +1113,8 @@
         print(" ");
         print(getSqlType(column));
 
-        if (column.getDefaultValue() != null)
+        if ((column.getDefaultValue() != null) ||
+            (getPlatformInfo().isIdentitySpecUsesDefaultValue() && column.isAutoIncrement()))
         {
             print(" DEFAULT ");
             writeColumnDefaultValue(table, column);
@@ -1129,7 +1130,7 @@
             print(" ");
             writeColumnNullableStmt();
         }
-        if (column.isAutoIncrement())
+        if (column.isAutoIncrement() && !getPlatformInfo().isIdentitySpecUsesDefaultValue())
         {
             if (!getPlatformInfo().isSupportingNonPKIdentityColumns() && !column.isPrimaryKey())
             {
@@ -1398,8 +1399,7 @@
 
         if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns))
         {
-            println(",");
-            printIndent();
+            printStartOfEmbeddedStatement();
             writePrimaryKeyStmt(table, primaryKeyColumns);
         }
     }
@@ -1489,7 +1489,13 @@
     {
         for (int idx = 0; idx < table.getIndexCount(); idx++)
         {
-            writeExternalIndexCreateStmt(table, table.getIndex(idx));
+            Index index = table.getIndex(idx);
+
+            if (!index.isUnique() && !getPlatformInfo().isSupportingNonUniqueIndices())
+            {
+                throw new DynaSqlException("Platform does not support non-unique indices");
+            }
+            writeExternalIndexCreateStmt(table, index);
         }
     }
 
@@ -1500,7 +1506,17 @@
      */
     protected void writeEmbeddedIndicesStmt(Table table) throws IOException 
     {
-        // TODO
+        for (int idx = 0; idx < table.getIndexCount(); idx++)
+        {
+            Index index = table.getIndex(idx);
+
+            if (!index.isUnique() && !getPlatformInfo().isSupportingNonUniqueIndices())
+            {
+                throw new DynaSqlException("Platform does not support non-unique indices");
+            }
+            printStartOfEmbeddedStatement();
+            writeEmbeddedIndexCreateStmt(table, index);
+        }
     }
 
     /**
@@ -1551,6 +1567,49 @@
     }
 
     /**
+     * Writes the given embedded index of the table.
+     * 
+     * @param table The table
+     * @param index The index
+     */
+    protected void writeEmbeddedIndexCreateStmt(Table table, Index index) throws IOException
+    {
+        if ((index.getName() != null) && (index.getName().length() > 0))
+        {
+            print(" CONSTRAINT ");
+            printIdentifier(getIndexName(index));
+        }
+        if (index.isUnique())
+        {
+            print(" UNIQUE");
+        }
+        else
+        {
+            print(" INDEX ");
+        }
+        print(" (");
+
+        for (int idx = 0; idx < index.getColumnCount(); idx++)
+        {
+            IndexColumn idxColumn = index.getColumn(idx);
+            Column      col       = table.findColumn(idxColumn.getName());
+
+            if (col == null)
+            {
+                //would get null pointer on next line anyway, so throw exception
+                throw new DynaSqlException("Invalid column '" + idxColumn.getName() + "' on index " + index.getName() + " for table " + table.getName());
+            }
+            if (idx > 0)
+            {
+                print(", ");
+            }
+            printIdentifier(getColumnName(col));
+        }
+
+        print(")");
+    }
+
+    /**
      * Generates the statement to drop a non-embedded index from the database.
      *
      * @param table The table the index is on
@@ -1591,9 +1650,7 @@
             }
             else
             {
-                println(",");
-                printIndent();
-                
+                printStartOfEmbeddedStatement();
                 if (getPlatformInfo().isEmbeddedForeignKeysNamed())
                 {
                     print("CONSTRAINT ");
@@ -1712,7 +1769,16 @@
             println();
         }
     }
-    
+
+    /** 
+     * Prints the start of an embedded statement.
+     */
+    protected void printStartOfEmbeddedStatement() throws IOException
+    {
+        println(",");
+        printIndent();
+    }
+
     /** 
      * Prints the end of statement text, which is typically a semi colon followed by 
      * a carriage return.