SapDB/MaxDB fixes

git-svn-id: https://svn.apache.org/repos/asf/db/ddlutils/trunk@893941 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/ddlutils/platform/SqlBuilder.java b/src/main/java/org/apache/ddlutils/platform/SqlBuilder.java
index c391f52..b630fff 100644
--- a/src/main/java/org/apache/ddlutils/platform/SqlBuilder.java
+++ b/src/main/java/org/apache/ddlutils/platform/SqlBuilder.java
@@ -448,8 +448,8 @@
     /**
      * Writes a statement that copies the data from the source to the target table. Note
      * that this copies only those columns that are in both tables.
-     * Database-specific implementations might redefine this method though they usually
-     * it suffices to redefine the {@link #writeCastExpression(Column, Column)} method.
+     * Database-specific implementations might redefine this method though it usually
+     * suffices to redefine the {@link #writeCastExpression(Column, Column)} method.
      * 
      * @param sourceTable The source table
      * @param targetTable The target table
@@ -1956,7 +1956,7 @@
      * @param table      The table
      * @param foreignKey The foreignkey
      */
-    private void writeForeignKeyOnDeleteAction(Table table, ForeignKey foreignKey) throws IOException
+    protected void writeForeignKeyOnDeleteAction(Table table, ForeignKey foreignKey) throws IOException
     {
         CascadeActionEnum action = foreignKey.getOnDelete();
 
@@ -2006,7 +2006,7 @@
      * @param table      The table
      * @param foreignKey The foreignkey
      */
-    private void writeForeignKeyOnUpdateAction(Table table, ForeignKey foreignKey) throws IOException
+    protected void writeForeignKeyOnUpdateAction(Table table, ForeignKey foreignKey) throws IOException
     {
         CascadeActionEnum action = foreignKey.getOnUpdate();
 
diff --git a/src/main/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java b/src/main/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java
index b69fed5..9296764 100644
--- a/src/main/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java
+++ b/src/main/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java
@@ -23,7 +23,6 @@
 

 import org.apache.ddlutils.Platform;

 import org.apache.ddlutils.model.Column;

-import org.apache.ddlutils.model.Database;

 import org.apache.ddlutils.model.ForeignKey;

 import org.apache.ddlutils.model.Table;

 import org.apache.ddlutils.platform.sapdb.SapDbBuilder;

@@ -66,32 +65,6 @@
     /**

      * {@inheritDoc}

      */

-    public void createForeignKey(Database database, Table table, ForeignKey foreignKey) throws IOException

-    {

-        if (foreignKey.getForeignTableName() == null)

-        {

-            _log.warn("Foreign key table is null for key " + foreignKey);

-        }

-        else

-        {

-            writeTableAlterStmt(table);

-

-            print("ADD CONSTRAINT ");

-            printIdentifier(getForeignKeyName(table, foreignKey));

-            print(" FOREIGN KEY (");

-            writeLocalReferences(foreignKey);

-            print(") REFERENCES ");

-            printIdentifier(getTableName(database.findTable(foreignKey.getForeignTableName())));

-            print(" (");

-            writeForeignReferences(foreignKey);

-            print(")");

-            printEndOfStatement();

-        }

-    }

-

-    /**

-     * {@inheritDoc}

-     */

     public void dropForeignKey(Table table, ForeignKey foreignKey) throws IOException

     {

         writeTableAlterStmt(table);

diff --git a/src/main/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java b/src/main/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java
index bb3f285..0ab0bee 100644
--- a/src/main/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java
+++ b/src/main/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java
@@ -22,11 +22,15 @@
 import java.io.IOException;
 
 import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.alteration.ColumnDefinitionChange;
+import org.apache.ddlutils.model.CascadeActionEnum;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.TypeMap;
 import org.apache.ddlutils.platform.SqlBuilder;
+import org.apache.ddlutils.util.StringUtilsExt;
 
 /**
  * The SQL Builder for SapDB.
@@ -85,26 +89,10 @@
     /**
      * {@inheritDoc}
      */
-    public void createForeignKey(Database database, Table table, ForeignKey foreignKey) throws IOException
+    protected void writeForeignKeyOnDeleteAction(Table table, ForeignKey foreignKey) throws IOException
     {
-        if (foreignKey.getForeignTableName() == null)
-        {
-            _log.warn("Foreign key table is null for key " + foreignKey);
-        }
-        else
-        {
-            writeTableAlterStmt(table);
-
-            print(" ADD FOREIGN KEY ");
-            printIdentifier(getForeignKeyName(table, foreignKey));
-            print(" (");
-            writeLocalReferences(foreignKey);
-            print(") REFERENCES ");
-            printIdentifier(getTableName(database.findTable(foreignKey.getForeignTableName())));
-            print(" (");
-            writeForeignReferences(foreignKey);
-            print(")");
-            printEndOfStatement();
+        if (foreignKey.getOnDelete() != CascadeActionEnum.NONE) {
+            super.writeForeignKeyOnDeleteAction(table, foreignKey);
         }
     }
 
@@ -236,4 +224,27 @@
         }
         printEndOfStatement();
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
+    {
+        boolean charSizeChanged = TypeMap.isTextType(targetColumn.getTypeCode()) &&
+                                  TypeMap.isTextType(targetColumn.getTypeCode()) &&
+                                  ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) &&
+                                  !StringUtilsExt.isEmpty(targetColumn.getSize());
+
+        if (charSizeChanged)
+        {
+            print("SUBSTR(");
+        }
+        printIdentifier(getColumnName(sourceColumn));
+        if (charSizeChanged)
+        {
+            print(",1,");
+            print(targetColumn.getSize());
+            print(")");
+        }
+    }
 }
diff --git a/src/test/java/org/apache/ddlutils/platform/TestSapDbPlatform.java b/src/test/java/org/apache/ddlutils/platform/TestSapDbPlatform.java
index d373bcd..b203556 100644
--- a/src/test/java/org/apache/ddlutils/platform/TestSapDbPlatform.java
+++ b/src/test/java/org/apache/ddlutils/platform/TestSapDbPlatform.java
@@ -136,8 +136,8 @@
             "    \"COL_FK\" INTEGER NOT NULL,\n"+

             "    PRIMARY KEY (\"COL_PK\")\n"+

             ");\n"+

-            "ALTER TABLE \"table2\" ADD FOREIGN KEY \"table2_FK_COL_FK_COL_FK_2_table1\" (\"COL_FK_1\", \"COL_FK_2\") REFERENCES \"table1\" (\"COL_PK_2\", \"COL_PK_1\");\n"+

-            "ALTER TABLE \"table3\" ADD FOREIGN KEY \"testfk\" (\"COL_FK\") REFERENCES \"table2\" (\"COL_PK\");\n",

+            "ALTER TABLE \"table2\" ADD CONSTRAINT \"table2_FK_COL_FK_COL_FK_2_table1\" FOREIGN KEY(\"COL_FK_1\", \"COL_FK_2\") REFERENCES \"table1\" (\"COL_PK_2\", \"COL_PK_1\");\n"+

+            "ALTER TABLE \"table3\" ADD CONSTRAINT \"testfk\" FOREIGN KEY(\"COL_FK\") REFERENCES \"table2\" (\"COL_PK\");\n",

             getTableConstraintTestDatabaseCreationSql());

     }