Hi all,

It appeared that I goofed up. Missed one condition. Here's the improved patch.

Thanks,

Wilfred




Wilfred Springer | Software Architect | TomTom | [EMAIL PROTECTED]
Index: src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java
===================================================================
--- src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java	(revision 387801)
+++ src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java	(working copy)
@@ -21,6 +21,10 @@
 import org.apache.ddlutils.Platform;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.platform.SqlBuilder;
+import org.apache.ddlutils.DynaSqlException;
+import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.IndexColumn;
+import org.apache.ddlutils.model.Column;
 
 /**
  * The SQL Builder for the HsqlDb database.
@@ -60,4 +64,41 @@
     {
         return "CALL IDENTITY()";
     }
+
+    /**
+     * [EMAIL PROTECTED]
+     *
+     * In case of a unique index, this implementation will generate an
+     * ALTER TABLE ADD CONSTRAINT statement instead, in line with <a
+     * href="http://www.hsqldb.org/doc/guide/ch09.html#create_index-section";>the
+     * HSQLDB documentation</a>.
+     *
+     * @see http://www.hsqldb.org/doc/guide/ch09.html#create_index-section
+     */
+    protected void writeExternalIndexCreateStmt(Table table, Index index) throws IOException {
+        if (index.isUnique()) {
+            print("ALTER TABLE ");
+            printIdentifier(getTableName(table));
+            print(" ADD CONSTRAINT ");
+            printIdentifier(getIndexName(index));
+            print(" UNIQUE (");
+            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(")");
+            printEndOfStatement();
+        } else {
+            super.writeExternalIndexCreateStmt(table, index);
+        }
+    }
+
 }

Reply via email to