db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r279419 - /db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java
Date Wed, 07 Sep 2005 20:56:47 GMT
Author: tomdz
Date: Wed Sep  7 13:56:41 2005
New Revision: 279419

URL: http://svn.apache.org/viewcvs?rev=279419&view=rev
Log:
Extracted an alterTable method from the alterDatabase method to allow for easier database-specific
handling (eg. DDLUTILS-8)

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

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java?rev=279419&r1=279418&r2=279419&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java Wed Sep  7 13:56:41
2005
@@ -280,7 +280,6 @@
      *
      * @param currentModel  The current database schema
      * @param desiredModel  The desired database schema
-     * @param connection    A connection to the existing database that shall be modified
      * @param doDrops       Whether columns and indexes should be dropped if not in the
      *                      new schema
      * @param modifyColumns Whether columns should be altered for datatype, size as required
@@ -306,187 +305,169 @@
             }
             else
             {
-                for (int columnIdx = 0; columnIdx < desiredTable.getColumnCount(); columnIdx++)
-                {
-                    Column desiredColumn = desiredTable.getColumn(columnIdx);
-                    Column currentColumn = currentTable.findColumn(desiredColumn.getName());
+                alterTable(currentTable, desiredModel, desiredTable, doDrops, modifyColumns);
+            }
+        }
 
-                    if (null == currentColumn)
-                    {
-                        if (_log.isInfoEnabled())
-                        {
-                            _log.info("Creating column " + desiredTable.getName() + "." +
desiredColumn.getName());
-                        }
-                        writeColumnAlterStmt(desiredTable, desiredColumn, true);
-                    }
-                    else if (columnsDiffer(desiredColumn, currentColumn))
-                    {
-                        if (modifyColumns)
-                        {
-                            if (_log.isInfoEnabled())
-                            {
-                                _log.info("Altering column " + desiredTable.getName() + "."
+ desiredColumn.getName());
-                                _log.info("  desired = " + desiredColumn.toStringAll());
-                                _log.info("  current = " + currentColumn.toStringAll());
-                            }
-                            writeColumnAlterStmt(desiredTable, desiredColumn, false);
-                        }
-                        else
-                        {
-                            String text = "Column " + currentColumn.getName() + " in table
" + currentTable.getName() + " differs from current specification";
+        // generating deferred foreignkeys
+        //TODO should we try to generate new FKs on existing tables?
+        for (Iterator fkIt = newTables.iterator(); fkIt.hasNext();)
+        {
+            createExternalForeignKeys(desiredModel, (Table)fkIt.next());
+        }
 
-                            if (_log.isInfoEnabled())
-                            {
-                                _log.info(text);
-                            }
-                            printComment(text);
-                        }
-                    }
-                }
+        // check for table drops
+        for (int idx = 0; idx < currentModel.getTableCount(); idx++)
+        {
+            Table currentTable = currentModel.getTable(idx);
+            Table desiredTable = desiredModel.findTable(currentTable.getName());
 
-                // add fk constraints
-                for (int fkIdx = 0; fkIdx < desiredTable.getForeignKeyCount(); fkIdx++)
+            if ((desiredTable == null) && (currentTable.getName() != null) &&
(currentTable.getName().length() > 0))
+            {
+                if (doDrops)
                 {
-                    ForeignKey desiredFk = desiredTable.getForeignKey(fkIdx);
-                    ForeignKey currentFk = currentTable.findForeignKey(desiredFk);
-                    if ( currentFk == null ) {
-                        if (_log.isInfoEnabled())
-                        {
-                            _log.info("Creating foreign key " + desiredTable.getName() +
"." + desiredFk);
-                        }
-                        writeExternalForeignKeyCreateStmt(desiredModel, desiredTable, desiredFk);
+                    if (_log.isInfoEnabled())
+                    {
+                        _log.info("Dropping table " + currentTable.getName());
                     }
+                    dropTable(currentTable);
                 }
-
-                // TODO: should we check the index fields for differences?
-                //create new indexes
-                for (int indexIdx = 0; indexIdx < desiredTable.getIndexCount(); indexIdx++)
+                else
                 {
-                    Index desiredIndex = desiredTable.getIndex(indexIdx);
-                    Index currentIndex = currentTable.findIndex(desiredIndex.getName());
+                    String text = "Table " + currentTable.getName() + " can be dropped";
 
-                    if (null == currentIndex)
+                    if (_log.isInfoEnabled())
                     {
-                        if (_log.isInfoEnabled())
-                        {
-                            _log.info("Creating index " + desiredTable.getName() + "." +
desiredIndex.getName());
-                        }
-                        writeExternalIndexCreateStmt(desiredTable, desiredIndex);
+                        _log.info(text);
                     }
+                    printComment(text);
                 }
+            }
+        }
+    }
 
-                // drop fk constraints
-                for (int fkIdx = 0; fkIdx < currentTable.getForeignKeyCount(); fkIdx++)
-                {
-                    ForeignKey currentFk = currentTable.getForeignKey(fkIdx);
-                    ForeignKey desiredFk = desiredTable.findForeignKey(currentFk);
+    /**
+     * Alters the given currently existing table object to match the given desired table
object.
+     * 
+     * @param currentTable The current table definition
+     * @param desiredModel The desired model
+     * @param desiredTable The desired table definition
+     * @param doDrops       Whether columns and indexes should be dropped if not in the
+     *                      new schema
+     * @param modifyColumns Whether columns should be altered for datatype, size as required
+     */
+    protected void alterTable(Table currentTable, Database desiredModel, Table desiredTable,
boolean doDrops, boolean modifyColumns) throws IOException
+    {
+        for (int columnIdx = 0; columnIdx < desiredTable.getColumnCount(); columnIdx++)
+        {
+            Column desiredColumn = desiredTable.getColumn(columnIdx);
+            Column currentColumn = currentTable.findColumn(desiredColumn.getName());
 
-                    if ( desiredFk == null ) {
-                        if (_log.isInfoEnabled())
-                        {
-                            _log.info((doDrops ? "" : "Not ") + "Dropping foreign key " +
currentTable.getName() + "." + currentFk);
-                        }
-                        if ( doDrops ) {
-                            writeExternalForeignKeyDropStmt(currentTable, currentFk);
-                        }
-                    }
+            if (null == currentColumn)
+            {
+                if (_log.isInfoEnabled())
+                {
+                    _log.info("Creating column " + desiredTable.getName() + "." + desiredColumn.getName());
                 }
-
-
-                //Drop columns
-                for (int columnIdx = 0; columnIdx < currentTable.getColumnCount(); columnIdx++)
+                writeColumnAlterStmt(desiredTable, desiredColumn, true);
+            }
+            else if (columnsDiffer(desiredColumn, currentColumn))
+            {
+                if (modifyColumns)
                 {
-                    Column currentColumn = currentTable.getColumn(columnIdx);
-                    Column desiredColumn = desiredTable.findColumn(currentColumn.getName());
-
-                    if (null == desiredColumn)
+                    if (_log.isInfoEnabled())
                     {
-                        if (doDrops)
-                        {
-                            if (_log.isInfoEnabled())
-                            {
-                                _log.info("Dropping column " + currentTable.getName() + "."
+ currentColumn.getName());
-                            }
-                            writeColumnDropStmt(currentTable, currentColumn);
-                        }
-                        else
-                        {
-                            String text = "Column " + currentColumn.getName() + " can be
dropped from table " + currentTable.getName();
-
-                            if (_log.isInfoEnabled())
-                            {
-                                _log.info(text);
-                            }
-                            printComment(text);
-                        }
+                        _log.info("Altering column " + desiredTable.getName() + "." + desiredColumn.getName());
+                        _log.info("  desired = " + desiredColumn.toStringAll());
+                        _log.info("  current = " + currentColumn.toStringAll());
                     }
+                    writeColumnAlterStmt(desiredTable, desiredColumn, false);
                 }
-
-                //Drop indexes
-                for (int indexIdx = 0; indexIdx < currentTable.getIndexCount(); indexIdx++)
+                else
                 {
-                    Index currentIndex = currentTable.getIndex(indexIdx);
-                    Index desiredIndex = desiredTable.findIndex(currentIndex.getName());
+                    String text = "Column " + currentColumn.getName() + " in table " + currentTable.getName()
+ " differs from current specification";
 
-                    if (null == desiredIndex)
+                    if (_log.isInfoEnabled())
                     {
-                        // make sure this isn't the primary key index
-                        boolean  isPk = true;
+                        _log.info(text);
+                    }
+                    printComment(text);
+                }
+            }
+        }
 
-                        for (int columnIdx = 0; columnIdx < currentIndex.getColumnCount();
columnIdx++)
-                        {
-                            IndexColumn indexColumn = currentIndex.getColumn(columnIdx);
-                            Column      column      = currentTable.findColumn(indexColumn.getName());
+        // add fk constraints
+        for (int fkIdx = 0; fkIdx < desiredTable.getForeignKeyCount(); fkIdx++)
+        {
+            ForeignKey desiredFk = desiredTable.getForeignKey(fkIdx);
+            ForeignKey currentFk = currentTable.findForeignKey(desiredFk);
 
-                            if (column != null && !column.isPrimaryKey())
-                            {
-                                isPk = false;
-                                break;
-                            }
-                        }
-                        if (!isPk)
-                        {
-                            if (_log.isInfoEnabled())
-                            {
-                                _log.info((doDrops ? "" : "Not ") + "Dropping non-primary
index " + currentTable.getName() + "." + currentIndex.getName());
-                            }
-                            if ( doDrops )
-                            {
-                                writeExternalIndexDropStmt(currentTable, currentIndex);
-                            }
-                        }
-                    }
+            if (currentFk == null)
+            {
+                if (_log.isInfoEnabled())
+                {
+                    _log.info("Creating foreign key " + desiredTable.getName() + "." + desiredFk);
                 }
+                writeExternalForeignKeyCreateStmt(desiredModel, desiredTable, desiredFk);
+            }
+        }
+
+        // TODO: should we check the index fields for differences?
+        //create new indexes
+        for (int indexIdx = 0; indexIdx < desiredTable.getIndexCount(); indexIdx++)
+        {
+            Index desiredIndex = desiredTable.getIndex(indexIdx);
+            Index currentIndex = currentTable.findIndex(desiredIndex.getName());
 
-            } 
+            if (currentIndex == null)
+            {
+                if (_log.isInfoEnabled())
+                {
+                    _log.info("Creating index " + desiredTable.getName() + "." + desiredIndex.getName());
+                }
+                writeExternalIndexCreateStmt(desiredTable, desiredIndex);
+            }
         }
 
-        // generating deferred foreignkeys
-        //TODO should we try to generate new FKs on existing tables?
-        for (Iterator fkIt = newTables.iterator(); fkIt.hasNext();)
+        // drop fk constraints
+        for (int fkIdx = 0; fkIdx < currentTable.getForeignKeyCount(); fkIdx++)
         {
-            createExternalForeignKeys(desiredModel, (Table)fkIt.next());
+            ForeignKey currentFk = currentTable.getForeignKey(fkIdx);
+            ForeignKey desiredFk = desiredTable.findForeignKey(currentFk);
+
+            if (desiredFk == null)
+            {
+                if (_log.isInfoEnabled())
+                {
+                    _log.info((doDrops ? "" : "Not ") + "Dropping foreign key " + currentTable.getName()
+ "." + currentFk);
+                }
+                if (doDrops)
+                {
+                    writeExternalForeignKeyDropStmt(currentTable, currentFk);
+                }
+            }
         }
 
-        // check for table drops
-        for (int idx = 0; idx < currentModel.getTableCount(); idx++)
+
+        //Drop columns
+        for (int columnIdx = 0; columnIdx < currentTable.getColumnCount(); columnIdx++)
         {
-            Table currentTable = currentModel.getTable(idx);
-            Table desiredTable = desiredModel.findTable(currentTable.getName());
+            Column currentColumn = currentTable.getColumn(columnIdx);
+            Column desiredColumn = desiredTable.findColumn(currentColumn.getName());
 
-            if ((desiredTable == null) && (currentTable.getName() != null) &&
(currentTable.getName().length() > 0))
+            if (desiredColumn == null)
             {
                 if (doDrops)
                 {
                     if (_log.isInfoEnabled())
                     {
-                        _log.info("Dropping table " + currentTable.getName());
+                        _log.info("Dropping column " + currentTable.getName() + "." + currentColumn.getName());
                     }
-                    dropTable(currentTable);
+                    writeColumnDropStmt(currentTable, currentColumn);
                 }
                 else
                 {
-                    String text = "Table " + currentTable.getName() + " can be dropped";
+                    String text = "Column " + currentColumn.getName() + " can be dropped
from table " + currentTable.getName();
 
                     if (_log.isInfoEnabled())
                     {
@@ -496,8 +477,45 @@
                 }
             }
         }
-    }
 
+        //Drop indexes
+        for (int indexIdx = 0; indexIdx < currentTable.getIndexCount(); indexIdx++)
+        {
+            Index currentIndex = currentTable.getIndex(indexIdx);
+            Index desiredIndex = desiredTable.findIndex(currentIndex.getName());
+
+            if (desiredIndex == null)
+            {
+                // make sure this isn't the primary key index
+                boolean  isPk = true;
+
+                for (int columnIdx = 0; columnIdx < currentIndex.getColumnCount(); columnIdx++)
+                {
+                    IndexColumn indexColumn = currentIndex.getColumn(columnIdx);
+                    Column      column      = currentTable.findColumn(indexColumn.getName());
+
+                    if ((column != null) && !column.isPrimaryKey())
+                    {
+                        isPk = false;
+                        break;
+                    }
+                }
+                if (!isPk)
+                {
+                    if (_log.isInfoEnabled())
+                    {
+                        _log.info((doDrops ? "" : "Not ") + "Dropping non-primary index "
+ currentTable.getName() + "." + currentIndex.getName());
+                    }
+                    if (doDrops)
+                    {
+                        writeExternalIndexDropStmt(currentTable, currentIndex);
+                    }
+                }
+            }
+        }
+
+    } 
+    
     /** 
      * Outputs the DDL to create the table along with any non-external constraints as well
      * as with external primary keys and indices (but not foreign keys).



Mime
View raw message