Author: jfuerth
Date: Wed Apr 22 10:40:15 2009
New Revision: 2987
Modified:
trunk/regress/ca/sqlpower/architect/swingui/TestTableEditPane.java
trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java
trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java
trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java
trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java
Log:
I'm about to remove the physicalPrimaryKeyName property from SQLTable, and
these changes accommodate that.
As a nice side effect, I found and fixed bugs in adding and dropping keys
in MySQL, PostgreSQL, and SQL Server (some syntax errors, and some use of
the logical key name where physical should have been used)
Modified: trunk/regress/ca/sqlpower/architect/swingui/TestTableEditPane.java
==============================================================================
--- trunk/regress/ca/sqlpower/architect/swingui/TestTableEditPane.java
(original)
+++ trunk/regress/ca/sqlpower/architect/swingui/TestTableEditPane.java Wed
Apr 22 10:40:15 2009
@@ -74,7 +74,8 @@
tep.setPhysicalNameText("New Name");
tep.setPkNameText("New PK Name");
tep.applyChanges();
- assertEquals ("New PK Name", t.getPhysicalPrimaryKeyName());
+ assertEquals ("New PK Name", t.getPrimaryKeyName());
+ assertEquals ("New PK Name",
t.getPrimaryKeyIndex().getPhysicalName());
}
}
Modified: trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java (original)
+++ trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java Wed Apr 22
10:40:15 2009
@@ -244,7 +244,7 @@
*/
public String getSchemaTerm();
- public void dropPrimaryKey(SQLTable t);
+ public void dropPrimaryKey(SQLTable t) throws SQLObjectException;
public void addPrimaryKey(SQLTable t) throws SQLObjectException;
Modified: trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java
(original)
+++ trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java Wed Apr 22
10:40:15 2009
@@ -36,12 +36,11 @@
import ca.sqlpower.architect.ArchitectUtils;
import ca.sqlpower.architect.DepthFirstSearch;
import ca.sqlpower.architect.profile.ProfileFunctionDescriptor;
-import ca.sqlpower.sqlobject.SQLObjectException;
-import ca.sqlpower.sqlobject.SQLObjectRuntimeException;
import ca.sqlpower.sqlobject.SQLColumn;
import ca.sqlpower.sqlobject.SQLDatabase;
import ca.sqlpower.sqlobject.SQLIndex;
import ca.sqlpower.sqlobject.SQLObject;
+import ca.sqlpower.sqlobject.SQLObjectException;
import ca.sqlpower.sqlobject.SQLObjectUtils;
import ca.sqlpower.sqlobject.SQLRelationship;
import ca.sqlpower.sqlobject.SQLSequence;
@@ -735,7 +734,12 @@
firstCol = false;
}
- addPrimaryKeysToCreateTable(t);
+ SQLIndex pk = t.getPrimaryKeyIndex();
+ if (pk != null) {
+ print(",\n");
+ print(" ");
+ writePKConstraintClause(pk);
+ }
print("\n)");
endStatement(DDLStatement.StatementType.CREATE, t);
@@ -748,60 +752,48 @@
protected Object getDefaultType() {
return Types.VARCHAR;
}
-
- protected void addPrimaryKeysToCreateTable(SQLTable t) throws
SQLObjectException {
- logger.debug("Adding Primary keys");
-
- Iterator it = t.getColumns().iterator();
- boolean firstCol = true;
- while (it.hasNext()) {
- SQLColumn col = (SQLColumn) it.next();
- if (col.getPrimaryKeySeq() == null) break;
- if (firstCol) {
- // generate a unique primary key name
- createPhysicalPrimaryKeyName(t);
- print(",\n");
- print(" CONSTRAINT ");
- print(t.getPhysicalPrimaryKeyName());
- print(" PRIMARY KEY (");
- firstCol = false;
- } else {
- print(", ");
- }
- print(col.getPhysicalName());
- }
- if (!firstCol) {
- print(")");
- }
+
+ /**
+ * Writes out the primary key constraint clause for the given primary
key
+ * index. The clause begins with the word "CONSTRAINT" and has no
leading
+ * space, so you must write a newline, space, and/or indentation before
+ * calling this method.
+ * <p>
+ * Side effect: the PK object's name will be initialized if necessary.
+ *
+ * @param pk
+ * The primary key index
+ * @throws SQLObjectException
+ * If the pk object wasn't already populated and the
populate
+ * attempt fails.
+ */
+ protected void writePKConstraintClause(SQLIndex pk) throws
SQLObjectException {
+ if (!pk.isPrimaryKeyIndex()) {
+ throw new IllegalArgumentException("The given index is not a
primary key");
+ }
+ createPhysicalName(topLevelNames, pk);
+ print("CONSTRAINT ");
+ print(pk.getPhysicalName());
+ print(" PRIMARY KEY (");
+
+ boolean firstCol = true;
+ for (SQLIndex.Column col : pk.getChildren()) {
+ if (!firstCol) print(", ");
+ print(col.getPhysicalName());
+ firstCol = false;
+ }
+ print(")");
}
protected void writePrimaryKey(SQLTable t) throws SQLObjectException {
- boolean firstCol = true;
- Iterator it = t.getColumns().iterator();
- while (it.hasNext()) {
- SQLColumn col = (SQLColumn) it.next();
- if (col.getPrimaryKeySeq() == null) break;
- if (firstCol) {
- // generate a unique primary key name
- createPhysicalPrimaryKeyName(t);
- println("");
- print("ALTER TABLE ");
- print( toQualifiedName(t) );
- print(" ADD CONSTRAINT ");
- print(t.getPrimaryKeyName());
- println("");
- print("PRIMARY KEY (");
- firstCol = false;
- } else {
- print(", ");
- }
- print(col.getPhysicalName());
- }
- if (!firstCol) {
- print(")");
- endStatement(DDLStatement.StatementType.ADD_PK, t);
-
- }
+ println("");
+ print("ALTER TABLE ");
+ print( toQualifiedName(t) );
+ print(" ADD ");
+
+ writePKConstraintClause(t.getPrimaryKeyIndex());
+
+ endStatement(DDLStatement.StatementType.ADD_PK, t);
}
/**
@@ -1203,22 +1195,6 @@
return seq.getPhysicalName();
}
- /**
- * Generate, set, and return a physicalPrimaryKeyName which is just the
- * logical primary key name run through "toIdentifier()".
- * Before returning it, run it past checkDupName to check in and add
- * it to the topLevelNames Map.
- * @throws SQLObjectException
- */
-
- private String createPhysicalPrimaryKeyName(SQLTable t) throws
SQLObjectException {
- logger.debug("The physical primary key name of the table is: " +
t.getPhysicalPrimaryKeyName());
- String physName = toIdentifier(t.getPrimaryKeyName());
- t.setPhysicalPrimaryKeyName(physName);
- createPhysicalName(topLevelNames, t.getPrimaryKeyIndex());
- return physName;
- }
-
/**
* Generates a standard <code>DROP TABLE $tablename</code> command.
Should work on most platforms.
*/
@@ -1241,14 +1217,10 @@
return ddlStatements;
}
- public void dropPrimaryKey(SQLTable t) {
-
- try {
- print("\nALTER TABLE " + toQualifiedName(t.getName())
- + " DROP PRIMARY KEY " + t.getPrimaryKeyName());
- } catch (SQLObjectException e) {
- throw new SQLObjectRuntimeException(e);
- }
+ public void dropPrimaryKey(SQLTable t) throws SQLObjectException {
+ SQLIndex pk = t.getPrimaryKeyIndex();
+ print("\nALTER TABLE " + toQualifiedName(t.getName())
+ + " DROP CONSTRAINT " + pk.getPhysicalName());
endStatement(DDLStatement.StatementType.DROP, t);
}
Modified: trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java (original)
+++ trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java Wed Apr 22
10:40:15 2009
@@ -24,16 +24,15 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
-import ca.sqlpower.sqlobject.SQLObject;
-import ca.sqlpower.sqlobject.SQLObjectException;
import ca.sqlpower.sqlobject.SQLColumn;
import ca.sqlpower.sqlobject.SQLIndex;
+import ca.sqlpower.sqlobject.SQLObject;
+import ca.sqlpower.sqlobject.SQLObjectException;
import ca.sqlpower.sqlobject.SQLObjectRuntimeException;
import ca.sqlpower.sqlobject.SQLRelationship;
import ca.sqlpower.sqlobject.SQLTable;
@@ -365,29 +364,34 @@
return null;
}
+ /**
+ * Overridden because MySQL doesn't allow the naming of PK
constraints. This
+ * version's text begins with "PRIMARY KEY" and is otherwise the same
as the
+ * generic method it overrides.
+ */
@Override
- protected void addPrimaryKeysToCreateTable(SQLTable t) throws
SQLObjectException {
- logger.debug("Adding Primary keys");
+ protected void writePKConstraintClause(SQLIndex pk) throws
SQLObjectException {
+ if (!pk.isPrimaryKeyIndex()) {
+ throw new IllegalArgumentException("The given index is not a
primary key");
+ }
+ createPhysicalName(topLevelNames, pk);
+ print("PRIMARY KEY (");
- Iterator it = t.getColumns().iterator();
boolean firstCol = true;
- while (it.hasNext()) {
- SQLColumn col = (SQLColumn) it.next();
- if (col.getPrimaryKeySeq() == null)
- break;
- if (firstCol) {
- print(",\n PRIMARY KEY (");
- firstCol = false;
- } else {
- print(", ");
- }
+ for (SQLIndex.Column col : pk.getChildren()) {
+ if (!firstCol) print(", ");
print(col.getPhysicalName());
+ firstCol = false;
}
- if (!firstCol) {
- print(")");
- }
+ print(")");
}
+ @Override
+ public void dropPrimaryKey(SQLTable t) {
+ print("\nALTER TABLE " + toQualifiedName(t.getName()) + " DROP
PRIMARY KEY");
+ endStatement(DDLStatement.StatementType.DROP, t);
+ }
+
public void dropRelationship(SQLRelationship r) {
print("\nALTER TABLE ");
@@ -396,42 +400,6 @@
print(" DROP FOREIGN KEY ");
print(r.getName());
endStatement(DDLStatement.StatementType.DROP, r);
- }
-
- /*
- * The primary key name in MySQL is completely ignored. Every primary
key is
- * named PRIMARY so we don't have to do any checking of name conflicts
for
- * the primary key. We don't even have to specify a name for the
primary
- * key.
- *
- * @see
ca.sqlpower.architect.ddl.GenericDDLGenerator#writePrimaryKey(ca.sqlpower.sqlpower.SQLTable)
- */
- @Override
- protected void writePrimaryKey(SQLTable t) throws SQLObjectException {
- logger.debug("Writing the primary key of " + t.getName());
- boolean firstCol = true;
- Iterator it = t.getColumns().iterator();
- while (it.hasNext()) {
- SQLColumn col = (SQLColumn) it.next();
- if (col.getPrimaryKeySeq() == null)
- break;
- if (firstCol) {
- // generate a unique primary key name
- println("");
- print("ALTER TABLE ");
- print(toQualifiedName(t));
- print(" ADD CONSTRAINT ");
- print("PRIMARY KEY (");
- firstCol = false;
- } else {
- print(", ");
- }
- print(col.getPhysicalName());
- }
- if (!firstCol) {
- print(")");
- endStatement(DDLStatement.StatementType.ADD_PK, t);
- }
}
/**
Modified: trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java
(original)
+++ trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java Wed Apr
22 10:40:15 2009
@@ -454,11 +454,15 @@
}
}
+ /**
+ * Overrides the syntax to omit the "constraint" keyword.
+ */
@Override
public void dropPrimaryKey(SQLTable t) {
try {
+ SQLIndex pk = t.getPrimaryKeyIndex();
print("\nALTER TABLE " + toQualifiedName(t.getName())
- + " DROP " + t.getPrimaryKeyName());
+ + " DROP " + pk.getPhysicalName());
} catch (SQLObjectException e) {
throw new SQLObjectRuntimeException(e);
}