Revision: 3704
Author: [email protected]
Date: Fri Jul  9 09:41:20 2010
Log: Fixed a bug with the HSQLDB DDL generator where check constraint names must be unique, regardless of which level it is applied on. e.g. the check constraint name on one column must not be the same as the check constraint name on another column. Similarly with H2 and SQL Server, the column name is prepended to the check constraint name for DDL generation.
http://code.google.com/p/power-architect/source/detail?r=3704

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java Wed Jul 7 11:16:36 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java Fri Jul 9 09:41:20 2010
@@ -24,9 +24,14 @@
 import java.sql.Types;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;

 import ca.sqlpower.architect.ddl.DDLStatement.StatementType;
+import ca.sqlpower.object.SPResolverRegistry;
+import ca.sqlpower.object.SPVariableHelper;
+import ca.sqlpower.object.SPVariableResolver;
+import ca.sqlpower.sqlobject.SQLCheckConstraint;
 import ca.sqlpower.sqlobject.SQLColumn;
 import ca.sqlpower.sqlobject.SQLObject;
 import ca.sqlpower.sqlobject.SQLRelationship;
@@ -221,5 +226,40 @@
                print(createPhysicalName(colNameMap, newCol));
                endStatement(StatementType.ALTER, newCol);
     }
+
+    /**
+ * Overridden because check constraints must have unique names regardless of + * which level it is applied on. Each constraint name is prepended by the
+     * column name the check constraint is actually being applied to. e.g.
+     * col_<column-name>_<constraint-name>.
+     */
+    @Override
+ protected String columnCheckConstraint(SQLColumn c, List<SQLCheckConstraint> checkConstraints) {
+        if (!supportsCheckConstraint() ||
+                c == null ||
+                checkConstraints == null ||
+                checkConstraints.isEmpty()) {
+            return "";
+        }
+
+        SPVariableResolver resolver = c.getVariableResolver();
+        SPVariableHelper helper = new SPVariableHelper(c);
+        SPResolverRegistry.register(c, resolver);
+
+        StringBuilder sb = new StringBuilder();
+        for (SQLCheckConstraint constraint : checkConstraints) {
+            if (sb.length() > 0) {
+                sb.append(" ");
+            }
+            sb.append(String.format("CONSTRAINT col_%s_%s CHECK (%s)",
+                    c.getPhysicalName(),
+                    constraint.getName(),
+                    helper.substitute(constraint.getConstraint())));
+        }
+
+        SPResolverRegistry.deregister(c, resolver);
+
+        return sb.toString();
+    }

 }

Reply via email to