db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r398033 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/alteration/ java/org/apache/ddlutils/platform/ java/org/apache/ddlutils/util/ test/org/apache/ddlutils/alteration/
Date Fri, 28 Apr 2006 23:18:03 GMT
Author: tomdz
Date: Fri Apr 28 16:17:59 2006
New Revision: 398033

URL: http://svn.apache.org/viewcvs?rev=398033&view=rev
Log:
More implementation of the new alteration algorithm

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChangeImplBase.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/util/CallbackClosure.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/util/MultiInstanceofPredicate.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddColumnChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddForeignKeyChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddPrimaryKeyChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddTableChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDataTypeChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefaultValueChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnRequiredChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnSizeChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/PrimaryKeyChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveColumnChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveForeignKeyChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveIndexChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemovePrimaryKeyChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTableChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestModelComparator.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddColumnChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddColumnChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddColumnChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddColumnChange.java Fri Apr
28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class AddColumnChange
+public class AddColumnChange extends TableChangeImplBase
 {
-    /** The table to add the column to. */
-    private Table _table;
     /** The new column. */
     private Column _newColumn;
 
@@ -39,7 +37,7 @@
      */
     public AddColumnChange(Table table, Column newColumn)
     {
-        _table     = table;
+        super(table);
         _newColumn = newColumn;
     }
 
@@ -51,15 +49,5 @@
     public Column getNewColumn()
     {
         return _newColumn;
-    }
-
-    /**
-     * Returns the table to add the column to.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddForeignKeyChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddForeignKeyChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddForeignKeyChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddForeignKeyChange.java Fri
Apr 28 16:17:59 2006
@@ -20,14 +20,14 @@
 import org.apache.ddlutils.model.Table;
 
 /**
- * Represents the addition of a foreign key to a table.
+ * Represents the addition of a foreign key to a table. Note that for
+ * simplicity and because it fits the model, this change actually implements
+ * table change for the table that the new foreign key will originate.
  * 
  * @version $Revision: $
  */
-public class AddForeignKeyChange
+public class AddForeignKeyChange extends TableChangeImplBase
 {
-    /** The table to add the foreign key to. */
-    private Table _table;
     /** The new foreign key. */
     private ForeignKey _newForeignKey;
 
@@ -39,7 +39,7 @@
      */
     public AddForeignKeyChange(Table table, ForeignKey newForeignKey)
     {
-        _table         = table;
+        super(table);
         _newForeignKey = newForeignKey;
     }
 
@@ -51,15 +51,5 @@
     public ForeignKey getNewForeignKey()
     {
         return _newForeignKey;
-    }
-
-    /**
-     * Returns the table where the foreign key is to be added.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java Fri Apr
28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class AddIndexChange
+public class AddIndexChange extends TableChangeImplBase
 {
-    /** The table to add the index to. */
-    private Table _table;
     /** The new index. */
     private Index _newIndex;
 
@@ -39,7 +37,7 @@
      */
     public AddIndexChange(Table table, Index newIndex)
     {
-        _table    = table;
+        super(table);
         _newIndex = newIndex;
     }
 
@@ -51,15 +49,5 @@
     public Index getNewIndex()
     {
         return _newIndex;
-    }
-
-    /**
-     * Returns the table where the index is to be added.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddPrimaryKeyChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddPrimaryKeyChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddPrimaryKeyChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddPrimaryKeyChange.java Fri
Apr 28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class AddPrimaryKeyChange
+public class AddPrimaryKeyChange extends TableChangeImplBase
 {
-    /** The table to add the primary key to. */
-    private Table    _table;
     /** The columns making up the primary key. */
     private Column[] _primaryKeyColumns;
 
@@ -39,7 +37,7 @@
      */
     public AddPrimaryKeyChange(Table table, Column[] primaryKeyColumns)
     {
-        _table             = table;
+        super(table);
         _primaryKeyColumns = primaryKeyColumns;
     }
 
@@ -51,15 +49,5 @@
     public Column[] getPrimaryKeyColumns()
     {
         return _primaryKeyColumns;
-    }
-
-    /**
-     * Returns the table to add the primary key to.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddTableChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddTableChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddTableChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddTableChange.java Fri Apr
28 16:17:59 2006
@@ -24,7 +24,7 @@
  * 
  * @version $Revision: $
  */
-public class AddTableChange
+public class AddTableChange implements ModelChange
 {
     /** The new table. */
     private Table _newTable;

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.java
Fri Apr 28 16:17:59 2006
@@ -25,10 +25,8 @@
  * 
  * @version $Revision: $
  */
-public class ColumnAutoIncrementChange
+public class ColumnAutoIncrementChange extends TableChangeImplBase
 {
-    /** The table of the column. */
-    private Table _table;
     /** The column. */
     private Column _column;
 
@@ -40,7 +38,7 @@
      */
     public ColumnAutoIncrementChange(Table table, Column column)
     {
-        _table  = table;
+        super(table);
         _column = column;
     }
 
@@ -52,15 +50,5 @@
     public Column getColumn()
     {
         return _column;
-    }
-
-    /**
-     * Returns the table of the column.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDataTypeChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDataTypeChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDataTypeChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDataTypeChange.java Fri
Apr 28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class ColumnDataTypeChange
+public class ColumnDataTypeChange extends TableChangeImplBase
 {
-    /** The table of the column. */
-    private Table _table;
     /** The column. */
     private Column _column;
     /** The JDBC type code of the new type. */
@@ -42,7 +40,7 @@
      */
     public ColumnDataTypeChange(Table table, Column column, int newTypeCode)
     {
-        _table       = table;
+        super(table);
         _column      = column;
         _newTypeCode = newTypeCode;
     }
@@ -65,15 +63,5 @@
     public int getNewTypeCode()
     {
         return _newTypeCode;
-    }
-
-    /**
-     * Returns the table of the column.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefaultValueChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefaultValueChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefaultValueChange.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefaultValueChange.java
Fri Apr 28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class ColumnDefaultValueChange
+public class ColumnDefaultValueChange extends TableChangeImplBase
 {
-    /** The table of the column. */
-    private Table _table;
     /** The column. */
     private Column _column;
     /** The new default value. */
@@ -42,7 +40,7 @@
      */
     public ColumnDefaultValueChange(Table table, Column column, String newDefaultValue)
     {
-        _table           = table;
+        super(table);
         _column          = column;
         _newDefaultValue = newDefaultValue;
     }
@@ -65,15 +63,5 @@
     public String getNewDefaultValue()
     {
         return _newDefaultValue;
-    }
-
-    /**
-     * Returns the table of the column.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnRequiredChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnRequiredChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnRequiredChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnRequiredChange.java Fri
Apr 28 16:17:59 2006
@@ -25,10 +25,8 @@
  * 
  * @version $Revision: $
  */
-public class ColumnRequiredChange
+public class ColumnRequiredChange extends TableChangeImplBase
 {
-    /** The table of the column. */
-    private Table _table;
     /** The column. */
     private Column _column;
 
@@ -40,7 +38,7 @@
      */
     public ColumnRequiredChange(Table table, Column column)
     {
-        _table  = table;
+        super(table);
         _column = column;
     }
 
@@ -52,15 +50,5 @@
     public Column getColumn()
     {
         return _column;
-    }
-
-    /**
-     * Returns the table of the column.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnSizeChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnSizeChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnSizeChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnSizeChange.java Fri Apr
28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class ColumnSizeChange
+public class ColumnSizeChange extends TableChangeImplBase
 {
-    /** The table of the column. */
-    private Table _table;
     /** The column. */
     private Column _column;
     /** The new size. */
@@ -45,7 +43,7 @@
      */
     public ColumnSizeChange(Table table, Column column, int newSize, int newScale)
     {
-        _table    = table;
+        super(table);
         _column   = column;
         _newSize  = newSize;
         _newScale = newScale;
@@ -79,15 +77,5 @@
     public int getNewScale()
     {
         return _newScale;
-    }
-
-    /**
-     * Returns the table of the column.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelChange.java?rev=398033&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelChange.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelChange.java Fri Apr 28
16:17:59 2006
@@ -0,0 +1,26 @@
+package org.apache.ddlutils.alteration;
+
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Marker interface for changes to a database model element.
+ * 
+ * @version $Revision: $
+ */
+public interface ModelChange
+{
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/PrimaryKeyChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/PrimaryKeyChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/PrimaryKeyChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/PrimaryKeyChange.java Fri Apr
28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class PrimaryKeyChange
+public class PrimaryKeyChange extends TableChangeImplBase
 {
-    /** The table to where the primary key is defined. */
-    private Table _table;
     /** The columns making up the original primary key. */
     private Column[] _oldPrimaryKeyColumns;
     /** The columns making up the new primary key. */
@@ -42,7 +40,7 @@
      */
     public PrimaryKeyChange(Table table, Column[] oldPrimaryKeyColumns, Column[] newPrimaryKeyColumns)
     {
-        _table                = table;
+        super(table);
         _oldPrimaryKeyColumns = oldPrimaryKeyColumns;
         _newPrimaryKeyColumns = newPrimaryKeyColumns;
     }
@@ -65,15 +63,5 @@
     public Column[] getNewPrimaryKeyColumns()
     {
         return _newPrimaryKeyColumns;
-    }
-
-    /**
-     * Returns the table whose primary key is to be changed.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveColumnChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveColumnChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveColumnChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveColumnChange.java Fri
Apr 28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class RemoveColumnChange
+public class RemoveColumnChange extends TableChangeImplBase
 {
-    /** The table to remove the column from. */
-    private Table _table;
     /** The column. */
     private Column _column;
 
@@ -39,7 +37,7 @@
      */
     public RemoveColumnChange(Table table, Column column)
     {
-        _table  = table;
+        super(table);
         _column = column;
     }
 
@@ -51,15 +49,5 @@
     public Column getColumn()
     {
         return _column;
-    }
-
-    /**
-     * Returns the table to remove the column from.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveForeignKeyChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveForeignKeyChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveForeignKeyChange.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveForeignKeyChange.java
Fri Apr 28 16:17:59 2006
@@ -20,14 +20,14 @@
 import org.apache.ddlutils.model.Table;
 
 /**
- * Represents the removal of a foreign key from a table.
+ * Represents the removal of a foreign key from a table. Note that for
+ * simplicity and because it fits the model, this change actually implements
+ * table change for the table that the foreign key originates.
  * 
  * @version $Revision: $
  */
-public class RemoveForeignKeyChange
+public class RemoveForeignKeyChange extends TableChangeImplBase
 {
-    /** The table to add the foreign key to. */
-    private Table _table;
     /** The foreign key. */
     private ForeignKey _foreignKey;
 
@@ -39,7 +39,7 @@
      */
     public RemoveForeignKeyChange(Table table, ForeignKey foreignKey)
     {
-        _table      = table;
+        super(table);
         _foreignKey = foreignKey;
     }
 
@@ -51,15 +51,5 @@
     public ForeignKey getForeignKey()
     {
         return _foreignKey;
-    }
-
-    /**
-     * Returns the table where the foreign key is to be removed.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveIndexChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveIndexChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveIndexChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveIndexChange.java Fri Apr
28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class RemoveIndexChange
+public class RemoveIndexChange extends TableChangeImplBase
 {
-    /** The table to add the index to. */
-    private Table _table;
     /** The index to be removed. */
     private Index _index;
 
@@ -39,7 +37,7 @@
      */
     public RemoveIndexChange(Table table, Index index)
     {
-        _table = table;
+        super(table);
         _index = index;
     }
 
@@ -51,15 +49,5 @@
     public Index getIndex()
     {
         return _index;
-    }
-
-    /**
-     * Returns the table where the index is to be removed from.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemovePrimaryKeyChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemovePrimaryKeyChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemovePrimaryKeyChange.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemovePrimaryKeyChange.java
Fri Apr 28 16:17:59 2006
@@ -24,10 +24,8 @@
  * 
  * @version $Revision: $
  */
-public class RemovePrimaryKeyChange
+public class RemovePrimaryKeyChange extends TableChangeImplBase
 {
-    /** The table to remove the primary key from. */
-    private Table    _table;
     /** The columns making up the primary key. */
     private Column[] _primaryKeyColumns;
 
@@ -39,7 +37,7 @@
      */
     public RemovePrimaryKeyChange(Table table, Column[] primaryKeyColumns)
     {
-        _table             = table;
+        super(table);
         _primaryKeyColumns = primaryKeyColumns;
     }
 
@@ -51,15 +49,5 @@
     public Column[] getPrimaryKeyColumns()
     {
         return _primaryKeyColumns;
-    }
-
-    /**
-     * Returns the table to remove the primary key from.
-     *
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTableChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTableChange.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTableChange.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTableChange.java Fri Apr
28 16:17:59 2006
@@ -23,11 +23,8 @@
  * 
  * @version $Revision: $
  */
-public class RemoveTableChange
+public class RemoveTableChange extends TableChangeImplBase
 {
-    /** The table. */
-    private Table _table;
-
     /**
      * Creates a new change object.
      * 
@@ -35,16 +32,6 @@
      */
     public RemoveTableChange(Table table)
     {
-        _table = table;
-    }
-
-    /**
-     * Returns the table.
-     * 
-     * @return The table
-     */
-    public Table getTable()
-    {
-        return _table;
+        super(table);
     }
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChange.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChange.java?rev=398033&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChange.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChange.java Fri Apr 28
16:17:59 2006
@@ -0,0 +1,34 @@
+package org.apache.ddlutils.alteration;
+
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.ddlutils.model.Table;
+
+/**
+ * Represents a change to a table or sub-element of a table (e.g. a column).
+ * 
+ * @version $Revision: $
+ */
+public interface TableChange extends ModelChange
+{
+    /**
+     * Returns the affected table from the original model.
+     * 
+     * @return The affected table
+     */
+    public Table getChangedTable();
+}

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChangeImplBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChangeImplBase.java?rev=398033&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChangeImplBase.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/TableChangeImplBase.java Fri
Apr 28 16:17:59 2006
@@ -0,0 +1,50 @@
+package org.apache.ddlutils.alteration;
+
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.ddlutils.model.Table;
+
+/**
+ * Base class for change implementations.
+ * 
+ * @version $Revision: $
+ */
+public abstract class TableChangeImplBase implements TableChange
+{
+    /** The affected table. */
+    private Table _table;
+
+    /**
+     * Creates a new change object.
+     * 
+     * @param table The table
+     */
+    public TableChangeImplBase(Table table)
+    {
+        _table = table;
+    }
+
+    /**
+     * Returns the affected table.
+     * 
+     * @return The table
+     */
+    public Table getChangedTable()
+    {
+        return _table;
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Fri Apr 28 16:17:59
2006
@@ -22,11 +22,15 @@
 import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import org.apache.commons.collections.Closure;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.Predicate;
 import org.apache.commons.collections.map.ListOrderedMap;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
@@ -34,6 +38,23 @@
 import org.apache.ddlutils.DynaSqlException;
 import org.apache.ddlutils.Platform;
 import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.alteration.AddColumnChange;
+import org.apache.ddlutils.alteration.AddForeignKeyChange;
+import org.apache.ddlutils.alteration.AddIndexChange;
+import org.apache.ddlutils.alteration.AddPrimaryKeyChange;
+import org.apache.ddlutils.alteration.AddTableChange;
+import org.apache.ddlutils.alteration.ColumnAutoIncrementChange;
+import org.apache.ddlutils.alteration.ColumnDataTypeChange;
+import org.apache.ddlutils.alteration.ColumnDefaultValueChange;
+import org.apache.ddlutils.alteration.ColumnRequiredChange;
+import org.apache.ddlutils.alteration.ColumnSizeChange;
+import org.apache.ddlutils.alteration.ModelComparator;
+import org.apache.ddlutils.alteration.PrimaryKeyChange;
+import org.apache.ddlutils.alteration.RemoveColumnChange;
+import org.apache.ddlutils.alteration.RemoveForeignKeyChange;
+import org.apache.ddlutils.alteration.RemoveIndexChange;
+import org.apache.ddlutils.alteration.RemovePrimaryKeyChange;
+import org.apache.ddlutils.alteration.RemoveTableChange;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.ForeignKey;
@@ -42,6 +63,8 @@
 import org.apache.ddlutils.model.Reference;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.model.TypeMap;
+import org.apache.ddlutils.util.CallbackClosure;
+import org.apache.ddlutils.util.MultiInstanceofPredicate;
 
 /**
  * This class is a collection of Strategy methods for creating the DDL required to create
and drop 
@@ -310,6 +333,209 @@
 
         // we're writing the external foreignkeys last to ensure that all referenced tables
are already defined
         createExternalForeignKeys(database);
+    }
+
+    /**
+     * Generates the DDL to modify an existing database so the schema matches
+     * the specified database schema by using drops, modifications and additions.
+     * Database-specific implementations can change aspect of this algorithm by
+     * redefining the individual methods that compromise it.
+     *
+     * @param currentModel  The current database schema
+     * @param desiredModel  The desired database schema
+     */
+    public void alterDatabase2(Database currentModel, Database desiredModel) throws IOException
+    {
+        ModelComparator comparator = new ModelComparator(getPlatform().isDelimitedIdentifierModeOn());
+        List            changes    = comparator.compare(currentModel, desiredModel);
+
+        processChanges(currentModel, desiredModel, changes);
+    }
+
+    /**
+     * Calls the given closure for all changes that are of one of the given types, and
+     * then removes them from the changes collection.
+     * 
+     * @param changes     The changes
+     * @param changeTypes The types to search for
+     * @param closure     The closure to invoke
+     */
+    protected void applyForSelectedChanges(Collection changes, Class[] changeTypes, final
Closure closure)
+    {
+        final Predicate predicate = new MultiInstanceofPredicate(changeTypes);
+
+        // basically we filter the changes for all objects where the above predicate
+        // returns true, and for these filtered objects we invoke the given closure
+        CollectionUtils.filter(changes,
+                               new Predicate()
+                               {
+                                   public boolean evaluate(Object obj)
+                                   {
+                                       if (predicate.evaluate(obj))
+                                       {
+                                           closure.execute(obj);
+                                           return false;
+                                       }
+                                       else
+                                       {
+                                           return true;
+                                       }
+                                   }
+                               });
+    }
+    
+    /**
+     * Processes the changes. The default argument performs several passes:
+     * <ol>
+     * <li>{@link org.apache.ddlutils.alteration.RemoveForeignKeyChange} and
+     *     {@link org.apache.ddlutils.alteration.RemoveIndexChange} come first
+     *     to allow for e.g. subsequent primary key changes or column removal.</li>
+     * <li>{@link org.apache.ddlutils.alteration.RemoveTableChange}
+     *     comes after the removal of foreign keys and indices.</li> 
+     * <li>These are all handled together:<br/>
+     *     {@link org.apache.ddlutils.alteration.RemovePrimaryKeyChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.AddPrimaryKeyChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.PrimaryKeyChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.RemoveColumnChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.AddColumnChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.ColumnAutoIncrementChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.ColumnDefaultValueChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.ColumnRequiredChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.ColumnDataTypeChange}<br/>
+     *     {@link org.apache.ddlutils.alteration.ColumnSizeChange}<br/>
+     *     The reason for this is that the default algorithm rebuilds the table for these
+     *     changes and thus their order is irrelevant.</li>
+     * <li>{@link org.apache.ddlutils.alteration.AddTableChange}<br/>
+     *     needs to come after the table removal (so that tables of the same name are
+     *     removed) and before the addition of foreign keys etc.</li>
+     * <li>{@link org.apache.ddlutils.alteration.AddForeignKeyChange} and
+     *     {@link org.apache.ddlutils.alteration.AddIndexChange} come last
+     *     after table/column/primary key additions or changes.</li>
+     * </ol>
+     * 
+     * @param currentModel The current database schema
+     * @param desiredModel The desired database schema
+     * @param changes      The changes
+     */
+    protected void processChanges(Database currentModel, Database desiredModel, List changes)
throws IOException
+    {
+        CallbackClosure callbackClosure = new CallbackClosure(this, "processChange");
+
+        // 1st pass: removing external constraints and indices
+        applyForSelectedChanges(changes,
+                                new Class[] { RemoveForeignKeyChange.class,
+                                              RemoveIndexChange.class },
+                                callbackClosure);
+
+        // 2nd pass: removing tables
+        applyForSelectedChanges(changes,
+                                new Class[] { RemoveTableChange.class },
+                                callbackClosure);
+
+        // 3rd pass: changing the structure of tables
+        Predicate predicate = new MultiInstanceofPredicate(new Class[] { RemovePrimaryKeyChange.class,
+                                                                         AddPrimaryKeyChange.class,
+                                                                         PrimaryKeyChange.class,
+                                                                         RemoveColumnChange.class,
+                                                                         AddColumnChange.class,
+                                                                         ColumnAutoIncrementChange.class,
+                                                                         ColumnDefaultValueChange.class,
+                                                                         ColumnRequiredChange.class,
+                                                                         ColumnDataTypeChange.class,
+                                                                         ColumnSizeChange.class
});
+
+        processTableStructureChanges(CollectionUtils.select(changes, predicate));
+
+        // 4th pass: adding tables
+        applyForSelectedChanges(changes,
+                                new Class[] { AddTableChange.class },
+                                callbackClosure);
+        // 5th pass: adding external constraints and indices
+        applyForSelectedChanges(changes,
+                                new Class[] { AddForeignKeyChange.class,
+                                              AddIndexChange.class },
+                                callbackClosure);
+    }
+
+    /**
+     * Processes the change representing the removal of a foreign key.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(RemoveForeignKeyChange change)
+    {
+        // TODO: ALTER TABLE DROP FOREIGN KEY
+    }
+
+    /**
+     * Processes the change representing the removal of an index.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(RemoveIndexChange change)
+    {
+        // TODO: DROP INDEX
+    }
+
+    /**
+     * Processes the change representing the removal of a table.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(RemoveTableChange change)
+    {
+        // TODO: DROP TABLE
+    }
+
+    /**
+     * Processes the change representing the addition of a table.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(AddTableChange change)
+    {
+        // TODO: CREATE TABLE
+    }
+
+    /**
+     * Processes the change representing the addition of a foreign key.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(AddForeignKeyChange change)
+    {
+        // TODO: ALTER TABLE ADD FOREIGN KEY
+    }
+
+    /**
+     * Processes the change representing the addition of an index.
+     * 
+     * @param change The change object
+     */
+    protected void processChange(AddIndexChange change)
+    {
+        // TODO: CREATE INDEX
+    }
+
+    /**
+     * Processes the changes to the structure of tables.
+     * 
+     * @param changes The change objects
+     */
+    protected void processTableStructureChanges(Collection changes)
+    {
+        // TODO:
+        // * sort the changes according to the affected tables and columns
+        // * for each affected table ...
+        // It might be possible to use the target table directly instead of the
+        // changes, even for datatype changes where we have to create casts in
+        // the INSERT statement (simply compare the native datatypes and create
+        // casts as needed)
+        // Subclasses would then filter through the change collection before (or
+        // after ? -> auto-increment) calling this method in order to use db-specific
+        // statements where possible which might reduce in the number of changes
+        // tables; we however have to take the thus processed changes into account
+        // when creating tables new (i.e. no need for casts when datatype changes)
     }
 
     /**

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/CallbackClosure.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/CallbackClosure.java?rev=398033&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/CallbackClosure.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/CallbackClosure.java Fri Apr 28 16:17:59
2006
@@ -0,0 +1,124 @@
+package org.apache.ddlutils.util;
+
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Queue;
+
+import org.apache.commons.collections.Closure;
+import org.apache.ddlutils.DdlUtilsException;
+
+/**
+ * A closure that determines a callback for the type of the object and calls it.
+ * Note that inheritance is also taken into account. I.e. if the object is of
+ * type B which is a subtype of A, and there is only a callback for type A,
+ * then this one will be invoked. If there is however also a callback for type B,
+ * then only this callback for type B will be invoked and not the one for type A. 
+ * 
+ * @version $Revision: $
+ */
+public class CallbackClosure implements Closure
+{
+    /** The object on which the callbacks will be invoked. */
+    private Object _callee;
+    /** The cached callbacks. */
+    private Map _callbacks = new HashMap();
+
+    /**
+     * Creates a new closure object.
+     * 
+     * @param callee       The object on which the callbacks will be invoked
+     * @param callbackName The name of the callback method
+     */
+    public CallbackClosure(Object callee, String callbackName)
+    {
+        _callee = callee;
+
+        Class type = callee.getClass();
+
+        // we're caching the callbacks
+        do
+        {
+            Method[] methods = type.getMethods();
+
+            if (methods != null)
+            {
+                for (int idx = 0; idx < methods.length; idx++)
+                {
+                    if (methods[idx].getName().equals(callbackName) &&
+                        (methods[idx].getParameterTypes() != null) &&
+                        (methods[idx].getParameterTypes().length == 1))
+                    {
+                        _callbacks.put(methods[idx].getParameterTypes()[0], methods[idx]);
+                    }
+                }
+            }
+            type = type.getSuperclass();
+        }
+        while ((type != null) && !type.equals(Object.class));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void execute(Object obj) throws DdlUtilsException
+    {
+        Queue queue = new LinkedList();
+
+        queue.add(obj.getClass());
+        while (!queue.isEmpty())
+        {
+            Class  type     = (Class)queue.poll();
+            Method callback = (Method)_callbacks.get(type);
+
+            if (callback != null)
+            {
+                try
+                {
+                    callback.invoke(_callee, new Object[] { obj });
+                    return;
+                }
+                catch (InvocationTargetException ex)
+                {
+                    throw new DdlUtilsException(ex.getTargetException());
+                }
+                catch (IllegalAccessException ex)
+                {
+                    throw new DdlUtilsException(ex);
+                }
+            }
+            if ((type.getSuperclass() != null) && !type.getSuperclass().equals(Object.class))
+            {
+                queue.add(type.getSuperclass());
+            }
+
+            Class[] baseInterfaces = type.getInterfaces();
+
+            if (baseInterfaces != null)
+            {
+                for (int idx = 0; idx < baseInterfaces.length; idx++)
+                {
+                    queue.add(baseInterfaces[idx]);
+                }
+            }
+        }
+    }
+}

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/MultiInstanceofPredicate.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/MultiInstanceofPredicate.java?rev=398033&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/MultiInstanceofPredicate.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/MultiInstanceofPredicate.java Fri
Apr 28 16:17:59 2006
@@ -0,0 +1,65 @@
+package org.apache.ddlutils.util;
+
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.commons.collections.Predicate;
+
+/**
+ * A predicate that tests whether the object is of one of the configured types. 
+ * 
+ * @version $Revision: $
+ */
+public class MultiInstanceofPredicate implements Predicate
+{
+    /** The types to check. */
+    private Class[] _typesToCheck;
+
+    /**
+     * Creates a new predicate.
+     * 
+     * @param typesToCheck The types to check
+     */
+    public MultiInstanceofPredicate(Class[] typesToCheck)
+    {
+        _typesToCheck = typesToCheck;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean evaluate(Object obj)
+    {
+        if ((_typesToCheck == null) || (_typesToCheck.length == 0))
+        {
+            return true;
+        }
+        else
+        {
+            Class typeOfObj = obj.getClass();
+    
+            for (int idx = 0; idx < _typesToCheck.length; idx++)
+            {
+                if (_typesToCheck[idx].isAssignableFrom(typeOfObj))
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+
+}

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestModelComparator.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestModelComparator.java?rev=398033&r1=398032&r2=398033&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestModelComparator.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestModelComparator.java Fri
Apr 28 16:17:59 2006
@@ -117,7 +117,7 @@
         RemoveTableChange change = (RemoveTableChange)changes.get(0);
 
         assertEquals("TableA",
-                     change.getTable().getName());
+                     change.getChangedTable().getName());
     }
 
     /**
@@ -153,7 +153,7 @@
         assertEquals("TABLEA",
                      change1.getNewTable().getName());
         assertEquals("TableA",
-                     change2.getTable().getName());
+                     change2.getChangedTable().getName());
     }
 
     /**



Mime
View raw message