db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r632715 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils: alteration/ColumnDefinitionChange.java platform/mssql/MSSqlBuilder.java platform/mssql/MSSqlModelComparator.java platform/mssql/MSSqlPlatform.java
Date Sun, 02 Mar 2008 06:32:48 GMT
Author: tomdz
Date: Sat Mar  1 22:32:46 2008
New Revision: 632715

URL: http://svn.apache.org/viewvc?rev=632715&view=rev
Log:
More fixes for the Sql Server platform

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefinitionChange.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

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefinitionChange.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefinitionChange.java?rev=632715&r1=632714&r2=632715&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefinitionChange.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ColumnDefinitionChange.java
Sat Mar  1 22:32:46 2008
@@ -125,6 +125,41 @@
     }
 
     /**
+     * Determines whether the size or precision/scale of the given target column is smaller
than that of the given source column.
+     * If size and precision/scale do not matter for the target column's type, then <code>false</code>
is returned. Note that for
+     * columns with precision & scale, it also counted as being smaller if the scale
of the target column is smaller than the
+     * one of the source column, regardless of whether the precision of the target column
is smaller than precision of the source
+     * column or equal to it or even bigger. The reason for this is that the reduced scale
would still potentially lead to truncation
+     * errors.
+     * 
+     * @param platformInfo The info object for the current platform
+     * @param sourceColumn The source column
+     * @param targetColumn The target column
+     * @return <code>true</code> if the size of the target column is smaller
+     */
+    public static boolean isSizeReduced(PlatformInfo platformInfo, Column sourceColumn, Column
targetColumn)
+    {
+        int     targetTypeCode = platformInfo.getTargetJdbcType(targetColumn.getTypeCode());
+        boolean sizeMatters    = platformInfo.hasSize(targetTypeCode);
+        boolean scaleMatters   = platformInfo.hasPrecisionAndScale(targetTypeCode);
+
+        if (sizeMatters && (sourceColumn.getSizeAsInt() > targetColumn.getSizeAsInt()))
+        {
+            return true;
+        }
+        else if (scaleMatters &&
+                 ((sourceColumn.getPrecisionRadix() > targetColumn.getPrecisionRadix())
||
+                  (sourceColumn.getScale() > targetColumn.getScale())))
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    /**
      * Determines whether the default value of the given target column is different from
the one of the given source column.
      * This method compares the parsed default values instead of their representations in
the columns.
      * 

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=632715&r1=632714&r2=632715&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 Mar
 1 22:32:46 2008
@@ -33,6 +33,7 @@
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.TypeMap;
 import org.apache.ddlutils.platform.SqlBuilder;
 
 /**
@@ -521,11 +522,25 @@
 
         if (sizeChanged || typeChanged)
         {
-            print("CAST(");
-            printIdentifier(getColumnName(sourceColumn));
-            print(" AS ");
-            print(getSqlType(targetColumn));
-            print(")");
+            if (TypeMap.isTextType(targetColumn.getTypeCode()) &&
+                sizeChanged && (sourceColumn.getSizeAsInt() > targetColumn.getSizeAsInt()))
+            {
+                print("SUBSTRING(CAST(");
+                printIdentifier(getColumnName(sourceColumn));
+                print(" AS ");
+                print(getNativeType(targetColumn));
+                print("),1,");
+                print(targetColumn.getSize());
+                print(")");
+            }
+            else
+            {
+                print("CAST(");
+                printIdentifier(getColumnName(sourceColumn));
+                print(" AS ");
+                print(getSqlType(targetColumn));
+                print(")");
+            }
         }
         else
         {

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=632715&r1=632714&r2=632715&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 Mar  1 22:32:46 2008
@@ -27,6 +27,7 @@
 import org.apache.ddlutils.alteration.AddForeignKeyChange;
 import org.apache.ddlutils.alteration.AddIndexChange;
 import org.apache.ddlutils.alteration.AddPrimaryKeyChange;
+import org.apache.ddlutils.alteration.ColumnDefinitionChange;
 import org.apache.ddlutils.alteration.ModelComparator;
 import org.apache.ddlutils.alteration.RemoveForeignKeyChange;
 import org.apache.ddlutils.alteration.RemoveIndexChange;
@@ -36,9 +37,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.StringUtilsExt;
 
 /**
  * A model comparator customized for Sql Server.
@@ -293,29 +292,10 @@
 
             if (sourceColumn != null)
             {
-                boolean hasChange      = false;
-                int     targetTypeCode = getPlatformInfo().getTargetJdbcType(targetColumn.getTypeCode());
-                boolean sizeMatters    = getPlatformInfo().hasSize(targetTypeCode);
-                boolean scaleMatters   = getPlatformInfo().hasPrecisionAndScale(targetTypeCode);
+                int targetTypeCode = getPlatformInfo().getTargetJdbcType(targetColumn.getTypeCode());
 
-                if (targetTypeCode != sourceColumn.getTypeCode())
-                {
-                    hasChange = true;
-                }
-                else
-                {
-                    if (sizeMatters && !StringUtilsExt.equals(sourceColumn.getSize(),
targetColumn.getSize()))
-                    {
-                        hasChange = true;
-                    }
-                    else if (scaleMatters &&
-                             (!StringUtilsExt.equals(sourceColumn.getSize(), targetColumn.getSize())
||
-                              (sourceColumn.getScale() != targetColumn.getScale())))
-                    {
-                        hasChange = true;
-                    }
-                }
-                if (hasChange)
+                if ((targetTypeCode != sourceColumn.getTypeCode()) ||
+                    ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn,
targetColumn))
                 {
                     result.add(targetColumn);
                 }

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=632715&r1=632714&r2=632715&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 Mar
 1 22:32:46 2008
@@ -209,12 +209,13 @@
                 {
                     ColumnDefinitionChange colDefChange = (ColumnDefinitionChange)change;
                     Column                 curColumn    = intermediateTable.findColumn(colDefChange.getChangedColumn(),
isDelimitedIdentifierModeOn());
+                    Column                 newColumn    = colDefChange.getNewColumn();
 
                     // Sql Server has no way of adding or removing an IDENTITY constraint
-                    // Also, reducing the size of a column should be handled by recreation
to avoid truncation errors 
+                    // Also, Sql Server cannot handle reducing the size (even with the CAST
in place)
                     return (curColumn.isAutoIncrement() == colDefChange.getNewColumn().isAutoIncrement())
&&
-                           (ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), curColumn,
colDefChange.getNewColumn()) ||
-                           !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), curColumn,
colDefChange.getNewColumn()));
+                           (curColumn.isRequired() || (curColumn.isRequired() == newColumn.isRequired()))
&&
+                           !ColumnDefinitionChange.isSizeReduced(getPlatformInfo(), curColumn,
newColumn);
                 }
                 else
                 {



Mime
View raw message