db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r630531 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/alteration/ java/org/apache/ddlutils/platform/db2/ java/org/apache/ddlutils/platform/mssql/ java/org/apache/ddlutils/platform/postgresql/ test/org/apache/ddlutils/io/
Date Sat, 23 Feb 2008 20:20:31 GMT
Author: tomdz
Date: Sat Feb 23 12:20:28 2008
New Revision: 630531

URL: http://svn.apache.org/viewvc?rev=630531&view=rev
Log:
Fixes to Db2 & Sql Server platforms and the model comparator

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlModelComparator.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlPlatform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/AddIndexChange.java?rev=630531&r1=630530&r2=630531&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 Sat Feb
23 12:20:28 2008
@@ -19,9 +19,11 @@
  * under the License.
  */
 
-import org.apache.ddlutils.DdlUtilsException;
+import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.IndexColumn;
+import org.apache.ddlutils.model.Table;
 
 /**
  * Represents the addition of an index to a table.
@@ -60,16 +62,15 @@
      */
     public void apply(Database model, boolean caseSensitive)
     {
-        Index newIndex = null;
+        Table table = findChangedTable(model, caseSensitive);
 
-        try
+        table.addIndex(_newIndex);
+        for (int idx = 0; idx < _newIndex.getColumnCount(); idx++)
         {
-            newIndex = (Index)_newIndex.clone();
-        }
-        catch (CloneNotSupportedException ex)
-        {
-            throw new DdlUtilsException(ex);
+            IndexColumn idxColumn = _newIndex.getColumn(idx);
+            Column      tmpColumn = idxColumn.getColumn();
+
+            idxColumn.setColumn(table.findColumn(tmpColumn.getName(), caseSensitive));
         }
-        findChangedTable(model, caseSensitive).addIndex(newIndex);
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java Sat Feb
23 12:20:28 2008
@@ -239,13 +239,15 @@
         {
             Table targetTable       = targetModel.getTable(tableIdx);
             Table intermediateTable = intermediateModel.findTable(targetTable.getName(),
_caseSensitive);
+            Table sourceTable       = sourceModel.findTable(targetTable.getName(), _caseSensitive);
 
             for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++)
             {
                 ForeignKey targetFk       = targetTable.getForeignKey(fkIdx);
                 ForeignKey intermediateFk = findCorrespondingForeignKey(intermediateTable,
targetFk);
+                ForeignKey sourceFk       = sourceTable == null ? null : findCorrespondingForeignKey(sourceTable,
targetFk);
 
-                if (intermediateFk == null)
+                if ((sourceFk == null) && (intermediateFk == null))
                 {
                     if (_log.isInfoEnabled())
                     {
@@ -515,10 +517,11 @@
 
         for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++)
         {
-            Index targetIndex = targetTable.getIndex(indexIdx);
-            Index sourceIndex = findCorrespondingIndex(intermediateTable, targetIndex);
+            Index targetIndex       = targetTable.getIndex(indexIdx);
+            Index intermediateIndex = findCorrespondingIndex(intermediateTable, targetIndex);
+            Index sourceIndex       = findCorrespondingIndex(sourceTable, targetIndex);
 
-            if (sourceIndex == null)
+            if ((sourceIndex == null) && (intermediateIndex == null))
             {
                 if (_log.isInfoEnabled())
                 {

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java Sat Feb 23
12:20:28 2008
@@ -23,6 +23,7 @@
 import java.sql.Types;
 
 import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.alteration.ColumnDefinitionChange;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Index;
 import org.apache.ddlutils.model.Table;
@@ -130,7 +131,8 @@
         String sourceNativeType = getBareNativeType(sourceColumn);
         String targetNativeType = getBareNativeType(targetColumn);
 
-        if (sourceNativeType.equals(targetNativeType))
+        if (sourceNativeType.equals(targetNativeType) &&
+            !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn))
         {
             printIdentifier(getColumnName(sourceColumn));
         }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java Sat Feb
23 12:20:28 2008
@@ -27,6 +27,7 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.alteration.ColumnDefinitionChange;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.ForeignKey;
@@ -510,5 +511,27 @@
         println("  DEALLOCATE refcursor");
         print("END");
         printEndOfStatement();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
+    {
+        boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn,
targetColumn);
+        boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn,
targetColumn);
+
+        if (sizeChanged || typeChanged)
+        {
+            print("CAST(");
+            printIdentifier(getColumnName(sourceColumn));
+            print(" AS ");
+            print(getSqlType(targetColumn));
+            print(")");
+        }
+        else
+        {
+            printIdentifier(getColumnName(sourceColumn));
+        }
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlModelComparator.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlModelComparator.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlModelComparator.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlModelComparator.java
Sat Feb 23 12:20:28 2008
@@ -36,6 +36,7 @@
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.IndexColumn;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.util.StringUtils;
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlPlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlPlatform.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mssql/MSSqlPlatform.java Sat Feb
23 12:20:28 2008
@@ -211,7 +211,10 @@
                     Column                 curColumn    = intermediateTable.findColumn(colDefChange.getChangedColumn(),
isDelimitedIdentifierModeOn());
 
                     // Sql Server has no way of adding or removing an IDENTITY constraint
-                    return curColumn.isAutoIncrement() == colDefChange.getNewColumn().isAutoIncrement();
+                    // Also, reducing the size of a column should be handled by recreation
to avoid truncation errors 
+                    return (curColumn.isAutoIncrement() == colDefChange.getNewColumn().isAutoIncrement())
&&
+                           (ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), curColumn,
colDefChange.getNewColumn()) ||
+                           !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), curColumn,
colDefChange.getNewColumn()));
                 }
                 else
                 {

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java
Sat Feb 23 12:20:28 2008
@@ -23,6 +23,7 @@
 import java.util.Map;
 
 import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.alteration.ColumnDefinitionChange;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.Index;
@@ -196,6 +197,28 @@
         if (column.isAutoIncrement())
         {
             dropAutoIncrementSequence(table, column);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
+    {
+        boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn,
targetColumn);
+        boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn,
targetColumn);
+
+        if (sizeChanged || typeChanged)
+        {
+            print("CAST(");
+            printIdentifier(getColumnName(sourceColumn));
+            print(" AS ");
+            print(getSqlType(targetColumn));
+            print(")");
+        }
+        else
+        {
+            printIdentifier(getColumnName(sourceColumn));
         }
     }
 }

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java?rev=630531&r1=630530&r2=630531&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java Sat Feb 23 12:20:28
2008
@@ -20,7 +20,6 @@
  */
 
 import java.math.BigDecimal;
-import java.math.BigInteger;
 import java.sql.Timestamp;
 import java.text.ParseException;
 import java.util.Date;
@@ -3107,6 +3106,9 @@
         Object    value = isSybase ? (Object)new BigDecimal("0") : new Integer(0);
         Timestamp time  = new Timestamp(new Date().getTime());
 
+        // to avoid problems with the database's time resolution
+        time.setNanos(0);
+
         insertRow("roundtrip", new Object[] { new Integer(1), value, time });
 
         alterDatabase(model2Xml);
@@ -3288,6 +3290,9 @@
         createDatabase(model1Xml);
 
         Timestamp time  = new Timestamp(new Date().getTime());
+
+        // to avoid problems with the database's time resolution
+        time.setNanos(0);
 
         insertRow("roundtrip", new Object[] { new Integer(1), null, time });
 



Mime
View raw message