Revision: 3317
Author: [email protected]
Date: Sun Feb 21 04:13:41 2010
Log: Added new methods to DDLGenerator to rename tables, columns, indexes and relationships. Those new methods are not yet called from CompareSQLFormatter (as detecting the changes is not completely implemented)
http://code.google.com/p/power-architect/source/detail?r=3317

Modified:
 /trunk/src/ca/sqlpower/architect/ddl/DB2DDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/H2DDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/LiquibaseDDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/PostgresDDLGenerator.java
 /trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java

=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/DB2DDLGenerator.java Thu Jan 29 12:02:55 2009 +++ /trunk/src/ca/sqlpower/architect/ddl/DB2DDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -18,6 +18,10 @@
  */
 package ca.sqlpower.architect.ddl;

+import ca.sqlpower.sqlobject.SQLColumn;
+import ca.sqlpower.sqlobject.SQLIndex;
+import ca.sqlpower.sqlobject.SQLObject;
+import ca.sqlpower.sqlobject.SQLObjectException;
 import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.sql.Types;
@@ -26,6 +30,8 @@
 import ca.sqlpower.sqlobject.SQLRelationship;
 import ca.sqlpower.sqlobject.SQLRelationship.Deferrability;
 import ca.sqlpower.sqlobject.SQLRelationship.UpdateDeleteRule;
+import ca.sqlpower.sqlobject.SQLTable;
+import java.util.Map;

// TODO: override to_identifier routine to ensure identifier names are legal // and unique for DB2. See the Oracle, SQL Server, and Postgres DDL generators
@@ -122,4 +128,34 @@
             return "";
         }
     }
-}
+
+       @Override
+    public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>(0);
+               print("\nALTER TABLE ");
+               print(toQualifiedName(oldCol.getParent()));
+               print(" RENAME ");
+               print(createPhysicalName(colNameMap, oldCol));
+        print(" TO ");
+               print(createPhysicalName(colNameMap, oldCol));
+               endStatement(DDLStatement.StatementType.ALTER, oldCol);
+    }
+
+       @Override
+    public void renameTable(SQLTable oldTable, SQLTable newTable) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>(0);
+        println("RENAME TABLE " + createPhysicalName(colNameMap, oldTable)
+                 + " TO " + createPhysicalName(colNameMap, newTable));
+        endStatement(DDLStatement.StatementType.ALTER, newTable);
+    }
+
+       @Override
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException {
+               print("RENAME INDEX ");
+               print(toQualifiedName(oldIndex));
+               print(" TO ");
+               println(toQualifiedName(newIndex.getName()));
+               endStatement(DDLStatement.StatementType.ALTER, oldIndex);
+       }
+
+}
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java Thu Feb 18 09:29:28 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/DDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -92,6 +92,16 @@
      */
     public void modifyComment(SQLObject o);

+    /**
+     * Appends the DDL statement to rename "oldCol" to "newCol.
+     *
+     * If the DBMS does not support renaming columns the generator
+     * should create the approriate DROP/ADD sequence
+     *
+        * @param oldName the old definition of the column
+     * @param newColumn the new definition of the column
+     */
+    public void renameColumn(SQLColumn oldColumn, SQLColumn newColumn);

     /**
* Appends the DDL statement for dropping the given column from its parent
@@ -125,6 +135,15 @@
      */
     public void addRelationship(SQLRelationship r);

+    /**
+     * Appends the DDL statement for renaming the given FK relationship
+     * in this DDL Generator's target schema/catalog.
+     *
+     * @param oldFK The old relationship name
+     * @param newFK The new relationship name
+     */
+ public void renameRelationship(SQLRelationship oldFK, SQLRelationship newFK);
+
     /**
      * Appends the DDL statement for dropping the given FK relationship
      * in this DDL Generator's target schema/catalog.
@@ -133,6 +152,16 @@
      */
     public void dropRelationship(SQLRelationship r);

+    /**
+     * Appends the DDL statement for renaming the table.
+     * If the DBMS does not support renaming tables, the generator should
+     * create the approriate DROP/CREATE sequence.
+     *
+     * @param oldTable the old definition of the table
+        * @param newTable the new definition of the table
+     */
+    public void renameTable(SQLTable oldTable, SQLTable newTable);
+
     /**
* Appends the DDL statements for dropping the table in this DDL Generator's
      * current catalog and schema using SQLTable t's physical name.
@@ -150,7 +179,25 @@
      * current catalog and schema.
      */
     public void addIndex(SQLIndex idx) throws SQLObjectException;
-
+
+       /**
+ * Drops the specified index. Currently the CompareSQL does not detect index drops + * but I have added this for completeness (as it is needed e.g. for renaming an
+        * index with Liquibase)
+        *
+        * @param index
+        * @throws SQLObjectException
+        */
+       public void dropIndex(SQLIndex index) throws SQLObjectException;
+
+    /**
+     * Appends the DDL statements for renaming the given index
+        *
+        * @param oldIndex the old index definition
+        * @param newIndex the new index definition
+     */
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException;
+
     /**
* Returns the list of DDL statements that have been created so far. Call
      * {...@link #generateDDLStatements(Collection)} to populate this list.
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java Thu Feb 18 09:29:28 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/GenericDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -295,6 +295,11 @@
        public void writeCreateDB(SQLDatabase db) {
                println("-- Would Create Database "+db.getName()+" here. --");
        }
+
+ public void renameRelationship(SQLRelationship oldFK, SQLRelationship newFK) {
+        dropRelationship(oldFK);
+               addRelationship(newFK);
+       }

        public void dropRelationship(SQLRelationship r) {

@@ -576,6 +581,19 @@
             return true;
         }
     }
+
+       /**
+        * Generate the SQL to rename a table.
+        * <br/>
+        * The default implementation works for PostgreSQL, Oracle, HSQLDB, H2
+        *
+        * @param oldTable
+        * @param newTable
+        */
+    public void renameTable(SQLTable oldTable, SQLTable newTable) {
+ println("ALTER TABLE " + oldTable.getPhysicalName() + " RENAME TO " + newTable.getPhysicalName());
+        endStatement(DDLStatement.StatementType.ALTER, newTable);
+    }

     public void addComment(SQLObject o) {
// TODO: move remarks storage to SQLObject in order to support comments for all object types
@@ -639,9 +657,27 @@
                }
                print("\n");
        }
+
+       /**
+        * Generate the SQL to rename a column.
+        * <br/>
+        * The default implementation works for PostgreSQL, Oracle
+        * @param oldTable
+        * @param newTable
+        */
+       public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>();
+               print("\nALTER TABLE ");
+               print(toQualifiedName(oldCol.getParent()));
+               print(" RENAME COLUMN ");
+               print(createPhysicalName(colNameMap, oldCol));
+        print(" TO ");
+               print(createPhysicalName(colNameMap, newCol));
+               endStatement(DDLStatement.StatementType.ALTER, oldCol);
+    }

        public void addColumn(SQLColumn c) {
-               Map colNameMap = new HashMap();
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>();
                print("\nALTER TABLE ");
                print(toQualifiedName(c.getParent()));
                print(" ADD COLUMN ");
@@ -651,7 +687,7 @@
        }

        public void dropColumn(SQLColumn c) {
-               Map colNameMap = new HashMap();
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>();
                print("\nALTER TABLE ");
                print(toQualifiedName(c.getParent()));
                print(" DROP COLUMN ");
@@ -1336,6 +1372,35 @@
                        endStatement(DDLStatement.StatementType.CREATE,t);
                }
        }
+
+       /**
+        * Drop the specified index.
+        * <br/>
+        * The default implementation should work for all databases.
+        *
+        * @param index the index to drop.
+        * @throws SQLObjectException
+        */
+       public void dropIndex(SQLIndex index) throws SQLObjectException {
+               print("DROP INDEX ");
+               println(toQualifiedName(index));
+               endStatement(DDLStatement.StatementType.DROP, index);
+       }
+
+       /**
+        * Rename an index.
+        * The default implementation works for PostgreSQL, Oracle, H2, HSQLDB
+        * @param oldIndex
+        * @param newIndex
+        * @throws SQLObjectException
+        */
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException {
+               print("ALTER INDEX ");
+               print(toQualifiedName(oldIndex));
+               print(" RENAME TO ");
+               println(toQualifiedName(newIndex.getName()));
+               endStatement(DDLStatement.StatementType.ALTER, oldIndex);
+       }

     /**
      * Adds a DDL statement to this generator that will create the
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/H2DDLGenerator.java Thu Jul 23 14:50:32 2009 +++ /trunk/src/ca/sqlpower/architect/ddl/H2DDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -26,10 +26,12 @@
 import java.util.HashMap;

 import ca.sqlpower.sqlobject.SQLColumn;
+import ca.sqlpower.sqlobject.SQLObject;
 import ca.sqlpower.sqlobject.SQLRelationship;
 import ca.sqlpower.sqlobject.SQLRelationship.Deferrability;
 import ca.sqlpower.sqlobject.SQLRelationship.UpdateDeleteRule;
 import ca.sqlpower.sqlobject.SQLType;
+import java.util.Map;

 /**
* Implements the quirks required for successful DDL generation that targets
@@ -151,5 +153,23 @@
     public String toString() {
         return "SQL Power H2 DDL Generator " + GENERATOR_VERSION;
     }
+
+       /**
+        * Generate the SQL to rename a column.
+        *
+        * @param oldTable
+        * @param newTable
+        */
+       public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>();
+               print("\nALTER TABLE ");
+               print(toQualifiedName(oldCol.getParent()));
+               print(" ALTER COLUMN ");
+               print(createPhysicalName(colNameMap, oldCol));
+        print(" RENAME TO ");
+               print(createPhysicalName(colNameMap, newCol));
+               endStatement(DDLStatement.StatementType.ALTER, newCol);
+    }
+

 }
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java Mon Jun 1 11:59:25 2009 +++ /trunk/src/ca/sqlpower/architect/ddl/HSQLDBDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -26,10 +26,12 @@
 import java.util.HashMap;

 import ca.sqlpower.sqlobject.SQLColumn;
+import ca.sqlpower.sqlobject.SQLObject;
 import ca.sqlpower.sqlobject.SQLRelationship;
 import ca.sqlpower.sqlobject.SQLTable;
 import ca.sqlpower.sqlobject.SQLRelationship.Deferrability;
 import ca.sqlpower.sqlobject.SQLRelationship.UpdateDeleteRule;
+import java.util.Map;

 /**
* Implements the quirks required for successful DDL generation that targets
@@ -195,4 +197,23 @@
         print(c.getRemarks().replaceAll(REGEX_CRLF, "\n-- "));
         endStatement(DDLStatement.StatementType.COMMENT, c);
     }
-}
+
+       /**
+        * Generate the SQL to rename a column.
+        * <br/>
+        * The default implementation works for PostgreSQL, Oracle
+        * @param oldTable
+        * @param newTable
+        */
+       public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>();
+               print("\nALTER TABLE ");
+               print(toQualifiedName(oldCol.getParent()));
+               print(" ALTER COLUMN ");
+               print(createPhysicalName(colNameMap, oldCol));
+        print(" RENAME TO ");
+               print(createPhysicalName(colNameMap, newCol));
+               endStatement(DDLStatement.StatementType.ALTER, newCol);
+    }
+
+}
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/LiquibaseDDLGenerator.java Fri Feb 19 13:16:46 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/LiquibaseDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -619,11 +619,11 @@
        }

        public String getCatalogTerm() {
-               return "Catalog";
+               return null;
        }

        public String getSchemaTerm() {
-               return "Schema";
+               return null;
        }

     /**
@@ -672,6 +672,22 @@
                println(sql.toString());
                endStatement(DDLStatement.StatementType.XMLTAG,t);
        }
+
+       @Override
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException {
+               // renaming an index is currently not supported in Liquibase 1.8
+               dropIndex(oldIndex);
+               addIndex(newIndex);
+       }
+
+       public void dropIndex(SQLIndex index) throws SQLObjectException {
+               print("<dropIndex indexName=\"");
+               print(getName(index));
+               print("\" tableName=\"");
+               print(getName(index.getParent()));
+               println("\"/>");
+               endStatement(DDLStatement.StatementType.XMLTAG, index);
+       }

     /**
      * Adds the necessary <createIndex> tag.
@@ -697,7 +713,7 @@
             println("  <column name=\"" + getName(c) + "\"/>") ;
         }
         println("</createIndex>");
-        endStatement(DDLStatement.StatementType.CREATE, index);
+        endStatement(DDLStatement.StatementType.XMLTAG, index);
     }

     private String getTableQualifier(SQLObject o) {
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java Fri Feb 5 10:47:31 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/MySqlDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -364,6 +364,51 @@
         return null;
     }

+       @Override
+    public void renameTable(SQLTable oldTable, SQLTable newTable) {
+               Map<String, SQLObject> colNameMap = new HashMap<String, 
SQLObject>(0);
+        println("RENAME TABLE "
+                               + createPhysicalName(colNameMap, oldTable)
+                               + " TO "
+                               + createPhysicalName(colNameMap, newTable));
+        endStatement(DDLStatement.StatementType.ALTER, newTable);
+    }
+
+       @Override
+       public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> empty = new HashMap<String, 
SQLObject>(0);
+               print("ALTER TABLE ");
+               print(createPhysicalName(empty, oldCol.getParent()));
+               print(" CHANGE ");
+
+               Map<String, SQLObject> cols = new HashMap<String, SQLObject>();
+               try {
+                       for (SQLColumn col : oldCol.getParent().getColumns()) {
+                               cols.put(col.getPhysicalName(), col);
+                       }
+               } catch (SQLObjectException e) {
+                       // can't do anything...
+               }
+               print(createPhysicalName(cols, newCol));
+               print(" ");
+               print(columnDefinition(newCol, cols));
+               endStatement(DDLStatement.StatementType.ALTER, newCol);
+       }
+
+       @Override
+ public void renameRelationship(SQLRelationship oldFK, SQLRelationship newFK) { + println("/* Renaming foreign key " + oldFK.getPhysicalName() + " to " + newFK.getPhysicalName() + " */");
+               dropRelationship(oldFK);
+               addRelationship(newFK);
+       }
+
+       @Override
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException { + println("/* Renaming index " + oldIndex.getPhysicalName() + " to " + newIndex.getPhysicalName() + " */");
+               dropIndex(oldIndex);
+               addIndex(newIndex);
+       }
+
     /**
* 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
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/PostgresDDLGenerator.java Fri Feb 5 10:47:31 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/PostgresDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -14,7 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 package ca.sqlpower.architect.ddl;

@@ -331,7 +331,7 @@
      */
     @Override
     public void addIndex(SQLIndex index) throws SQLObjectException {
-
+
         createPhysicalName(topLevelNames, index);
         println("");
         print("CREATE ");
@@ -342,7 +342,7 @@
         print(toIdentifier(index.getName()));
         print("\n ON ");
         print(toQualifiedName(index.getParent()));
-        if(index.getType() != null) {
+        if(index.getType() != null) {
             print(" USING "+ index.getType());
         }
         print("\n ( ");
@@ -366,7 +366,7 @@
addCluster(index, toIdentifier(index.getName()), index.getParent().getName());
         }
     }
-
+
     /**
      * This will create a clustered index on a given table.
      */
@@ -375,10 +375,10 @@
         print("CLUSTER " + indexName + " ON " + table);
         endStatement(DDLStatement.StatementType.CREATE, index);
     }
-
+
     @Override
public void addTable(SQLTable t) throws SQLException, SQLObjectException {
-
+
// Create all the sequences that will be needed for auto-increment cols in this table
         for (SQLColumn c : t.getColumns()) {
             if (c.isAutoIncrement()) {
@@ -388,9 +388,9 @@
                 endStatement(StatementType.CREATE, seq);
             }
         }
-
+
         super.addTable(t);
-
+
         // attach sequences to columns
         for (SQLColumn c : t.getColumns()) {
             if (c.isAutoIncrement()) {
@@ -400,7 +400,7 @@
             }
         }
     }
-
+
     /**
* Augments the default columnDefinition behaviour by adding the correct * default value clause for auto-increment columns. For non-autoincrement
@@ -409,7 +409,7 @@
     @Override
     protected String columnDefinition(SQLColumn c, Map colNameMap) {
         String nameAndType = super.columnDefinition(c, colNameMap);
-
+
         if (c.isAutoIncrement()) {
SQLSequence seq = new SQLSequence(toIdentifier(c.getAutoIncrementSequenceName())); return nameAndType + " DEFAULT nextval(" + SQL.quote(toQualifiedName(seq.getName())) + ")";
@@ -418,4 +418,27 @@
         }
     }

-}
+       @Override
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException {
+               print("ALTER INDEX ");
+               print(toQualifiedName(oldIndex));
+               print(" RENAME TO ");
+               println(toQualifiedName(newIndex.getName()));
+               endStatement(DDLStatement.StatementType.ALTER, oldIndex);
+       }
+
+       /**
+        * Drop the specified index.
+        * <br/>
+        * The default implementation should work for all databases.
+        *
+        * @param index the index to drop.
+        * @throws SQLObjectException
+        */
+       public void dropIndex(SQLIndex index) throws SQLObjectException {
+               print("DROP INDEX ");
+               print(toQualifiedName(index));
+               println(" CASCADE");
+               endStatement(DDLStatement.StatementType.DROP, index);
+       }
+}
=======================================
--- /trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java Mon Feb 8 10:12:47 2010 +++ /trunk/src/ca/sqlpower/architect/ddl/SQLServerDDLGenerator.java Sun Feb 21 04:13:41 2010
@@ -30,12 +30,14 @@

 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;
 import ca.sqlpower.sqlobject.SQLType;
 import ca.sqlpower.sqlobject.SQLRelationship.Deferrability;
+import java.util.Map;

 /**
* The base class for version-specific SQL Server DDL generators. This class is
@@ -520,4 +522,52 @@
         +" DROP CONSTRAINT "
         +fkName;
     }
-}
+
+       @Override
+       public void renameColumn(SQLColumn oldCol, SQLColumn newCol) {
+               Map<String, SQLObject> empty = new HashMap<String, 
SQLObject>(0);
+               print("sp_rename @objname='");
+               print(toQualifiedName(oldCol.getParent()));
+               print(".");
+               print(createPhysicalName(empty, oldCol));
+               print("', @newname='");
+               print(createPhysicalName(empty, newCol));
+               print("', @objtype='COLUMN'");
+               endStatement(DDLStatement.StatementType.ALTER, newCol);
+       }
+
+       @Override
+ public void renameIndex(SQLIndex oldIndex, SQLIndex newIndex) throws SQLObjectException {
+               Map<String, SQLObject> empty = new HashMap<String, 
SQLObject>(0);
+               print("sp_rename @objname='");
+               print(toQualifiedName(oldIndex));
+               print("', @newname='");
+               print(toQualifiedName(newIndex));
+               print("', @objtype='INDEX'");
+               endStatement(DDLStatement.StatementType.ALTER, newIndex);
+       }
+
+       @Override
+ public void renameRelationship(SQLRelationship oldFK, SQLRelationship newFK) {
+               Map<String, SQLObject> empty = new HashMap<String, 
SQLObject>(0);
+               print("sp_rename @objname='");
+               print(createPhysicalName(empty, oldFK));
+               print("', @newname='");
+               print(createPhysicalName(empty, newFK));
+               println("', @objtype='OBJECT'");
+               endStatement(DDLStatement.StatementType.ALTER, newFK);
+       }
+
+       @Override
+       public void renameTable(SQLTable oldTable, SQLTable newTable) {
+               Map<String, SQLObject> empty = new HashMap<String, 
SQLObject>(0);
+               print("sp_rename @objname='");
+               print(createPhysicalName(empty, oldTable));
+               print("', @newname='");
+               print(createPhysicalName(empty, newTable));
+               println("'");
+               endStatement(DDLStatement.StatementType.ALTER, newTable);
+       }
+
+
+}

Reply via email to