db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r504804 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/platform/sybase/ test/org/apache/ddlutils/io/
Date Thu, 08 Feb 2007 06:33:51 GMT
Author: tomdz
Date: Wed Feb  7 22:33:51 2007
New Revision: 504804

URL: http://svn.apache.org/viewvc?view=rev&rev=504804
Log:
Fixed identity handling for Sybase
Made tests more robust

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybaseBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybasePlatform.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybaseBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybaseBuilder.java?view=diff&rev=504804&r1=504803&r2=504804
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybaseBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybaseBuilder.java Wed
Feb  7 22:33:51 2007
@@ -184,15 +184,65 @@
     }
 
     /**
-     * Writes the statement that turns on the ability to write delimited identifiers.
+     * Returns the SQL to enable identity override mode.
+     * 
+     * @param table The table to enable the mode for
+     * @return The SQL
      */
-    private void writeQuotationOnStatement() throws IOException
+    protected String getEnableIdentityOverrideSql(Table table)
+    {
+        StringBuffer result = new StringBuffer();
+
+        result.append(getQuotationOnStatement());
+        result.append("SET IDENTITY_INSERT ");
+        result.append(getDelimitedIdentifier(getTableName(table)));
+        result.append(" ON");
+
+        return result.toString();
+    }
+
+    /**
+     * Returns the SQL to disable identity override mode.
+     * 
+     * @param table The table to disable the mode for
+     * @return The SQL
+     */
+    protected String getDisableIdentityOverrideSql(Table table)
+    {
+        StringBuffer result = new StringBuffer();
+
+        result.append(getQuotationOnStatement());
+        result.append("SET IDENTITY_INSERT ");
+        result.append(getDelimitedIdentifier(getTableName(table)));
+        result.append(" OFF");
+
+        return result.toString();
+    }
+
+    /**
+     * Returns the statement that turns on the ability to write delimited identifiers.
+     * 
+     * @return The quotation-on statement
+     */
+    private String getQuotationOnStatement()
     {
         if (getPlatform().isDelimitedIdentifierModeOn())
         {
-            print("SET quoted_identifier on");
-            printEndOfStatement();
+            return "SET quoted_identifier on";
         }
+        else
+        {
+            return "";
+        }
+    }
+
+    /**
+     * Writes the statement that turns on the ability to write delimited identifiers.
+     */
+    private void writeQuotationOnStatement() throws IOException
+    {
+        print(getQuotationOnStatement());
+        printEndOfStatement();
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybasePlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybasePlatform.java?view=diff&rev=504804&r1=504803&r2=504804
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybasePlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sybase/SybasePlatform.java Wed
Feb  7 22:33:51 2007
@@ -255,4 +255,60 @@
 		setTextSize(MAX_TEXT_SIZE);
 		return super.query(model, sql, queryHints);
 	}
+
+
+    /**
+     * Determines whether we need to use identity override mode for the given table.
+     * 
+     * @param table The table
+     * @return <code>true</code> if identity override mode is needed
+     */
+    private boolean useIdentityOverrideFor(Table table)
+    {
+        return isIdentityOverrideOn() &&
+               getPlatformInfo().isIdentityOverrideAllowed() &&
+               (table.getAutoIncrementColumns().length > 0);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void beforeInsert(Connection connection, Table table) throws SQLException
+    {
+        if (useIdentityOverrideFor(table))
+        {
+            SybaseBuilder builder = (SybaseBuilder)getSqlBuilder();
+    
+            connection.createStatement().execute(builder.getEnableIdentityOverrideSql(table));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void afterInsert(Connection connection, Table table) throws SQLException
+    {
+        if (useIdentityOverrideFor(table))
+        {
+            SybaseBuilder builder = (SybaseBuilder)getSqlBuilder();
+    
+            connection.createStatement().execute(builder.getDisableIdentityOverrideSql(table));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void beforeUpdate(Connection connection, Table table) throws SQLException
+    {
+        beforeInsert(connection, table);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void afterUpdate(Connection connection, Table table) throws SQLException
+    {
+        afterInsert(connection, table);
+    }
 }

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java?view=diff&rev=504804&r1=504803&r2=504804
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java Wed Feb  7 22:33:51
2007
@@ -499,8 +499,8 @@
             (expected.getTypeCode() == Types.DECIMAL))
         {
             assertEquals("Precision not the same for column "+actual.getName()+".",
-                         expected.getSize(),
-                         actual.getSize());
+                         expected.getSizeAsInt(),
+                         actual.getSizeAsInt());
             assertEquals("Scale not the same for column "+actual.getName()+".",
                          expected.getScale(),
                          actual.getScale());

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java?view=diff&rev=504804&r1=504803&r2=504804
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java Wed Feb  7 22:33:51
2007
@@ -545,59 +545,69 @@
         {
             return;
         }
-
-        boolean isSybase  = SybasePlatform.DATABASENAME.equals(getPlatform().getName());
-        String  model1Xml = 
-            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
-            "<database name='roundtriptest'>\n"+
-            "  <table name='roundtrip'>\n"+
-            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
-            "    <column name='avalue' type='INTEGER'/>\n"+
-            "  </table>\n"+
-            "</database>";
-        String model2Xml;
-
-        // since some databases require/automatically make the column required
-        // we also make the column required in order to avoid problems when
-        // comparing the model from the live database with our assumed one
-        if (isSybase)
-        {
-            model2Xml = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
-                        "<database name='roundtriptest'>\n"+
-                        "  <table name='roundtrip'>\n"+
-                        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
-                        "    <column name='avalue' type='NUMERIC' size='12,0' autoIncrement='true'
required='true'/>\n"+
-                        "  </table>\n"+
-                        "</database>";
-        }
-        else
+        // Sybase does not like INTEGER auto-increment columns
+        if (SybasePlatform.DATABASENAME.equals(getPlatform().getName()))
         {
-            model2Xml = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
-                        "<database name='roundtriptest'>\n"+
-                        "  <table name='roundtrip'>\n"+
-                        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
-                        "    <column name='avalue' type='INTEGER' autoIncrement='true'
required='true'/>\n"+
-                        "  </table>\n"+
-                        "</database>";
-        }
+            String  model1Xml = 
+                "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+                "<database name='roundtriptest'>\n"+
+                "  <table name='roundtrip'>\n"+
+                "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+                "    <column name='avalue' type='NUMERIC' size='12,0'/>\n"+
+                "  </table>\n"+
+                "</database>";
+            String model2Xml =
+                "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+                "<database name='roundtriptest'>\n"+
+                "  <table name='roundtrip'>\n"+
+                "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+                "    <column name='avalue' type='NUMERIC' size='12,0' autoIncrement='true'
required='true'/>\n"+
+                "  </table>\n"+
+                "</database>";
+
+            createDatabase(model1Xml);
 
-        createDatabase(model1Xml);
+            insertRow("roundtrip", new Object[] { new Integer(1), new BigDecimal(2) });
 
-        insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) });
+            alterDatabase(model2Xml);
 
-    	alterDatabase(model2Xml);
+            assertEquals(getAdjustedModel(),
+                         readModelFromDatabase("roundtriptest"));
 
-        assertEquals(getAdjustedModel(),
-                     readModelFromDatabase("roundtriptest"));
-
-        List beans = getRows("roundtrip");
+            List beans = getRows("roundtrip");
 
-        if (isSybase)
-        {
             assertEquals(new BigDecimal(2), beans.get(0), "avalue");
         }
         else
         {
+            String  model1Xml = 
+                "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+                "<database name='roundtriptest'>\n"+
+                "  <table name='roundtrip'>\n"+
+                "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+                "    <column name='avalue' type='INTEGER'/>\n"+
+                "  </table>\n"+
+                "</database>";
+            String model2Xml=
+                "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+                "<database name='roundtriptest'>\n"+
+                "  <table name='roundtrip'>\n"+
+                "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+                "    <column name='avalue' type='INTEGER' autoIncrement='true' required='true'/>\n"+
+                "  </table>\n"+
+                "</database>";
+
+            createDatabase(model1Xml);
+
+            insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) });
+
+            alterDatabase(model2Xml);
+
+            assertEquals(getAdjustedModel(),
+                         readModelFromDatabase("roundtriptest"));
+
+            List beans = getRows("roundtrip");
+
             assertEquals(new Integer(2), beans.get(0), "avalue");
         }
     }
@@ -1964,7 +1974,7 @@
 
         createDatabase(model1Xml);
 
-        insertRow("roundtrip1", new Object[] { new Integer(1) });
+        insertRow("roundtrip1", new Object[] { "1" });
 
         alterDatabase(model2Xml);
 
@@ -2079,7 +2089,7 @@
 
         createDatabase(model1Xml);
 
-        insertRow("roundtrip", new Object[] { null, new Integer(1) });
+        insertRow("roundtrip", new Object[] { null, "1" });
 
         alterDatabase(model2Xml);
 



Mime
View raw message