You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by mv...@apache.org on 2006/01/04 01:36:33 UTC

svn commit: r365780 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/platform/mysql/ test/org/apache/ddlutils/io/mysql/

Author: mvdb
Date: Tue Jan  3 16:36:26 2006
New Revision: 365780

URL: http://svn.apache.org/viewcvs?rev=365780&view=rev
Log:
Adding mysqltests.
Somewhere hidden in there is http://issues.apache.org/jira/browse/DDLUTILS-26
a fix provided by : Xavier TROUSSICOT

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlModelReader.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Constraints.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Datatypes.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java?rev=365780&r1=365779&r2=365780&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java Tue Jan  3 16:36:26 2006
@@ -22,6 +22,7 @@
 
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.platform.SqlBuilder;
 
@@ -110,5 +111,20 @@
         super.writeTableCreationStmtEnding(table, parameters);
     }
 
+    /**
+     * @see org.apache.ddlutils.platform.SqlBuilder#writeExternalForeignKeyDropStmt(org.apache.ddlutils.model.Table, org.apache.ddlutils.model.ForeignKey)
+     */
+    protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey) throws IOException
+    {
+        writeTableAlterStmt(table);
+        print("DROP FOREIGN KEY ");
+        String foreignKeyName = foreignKey.getName();
+        if (foreignKeyName == null)
+        {
+            foreignKeyName = getConstraintName(null, table, "FK", getForeignKeyName(table, foreignKey));
+        }
+        printIdentifier(foreignKeyName);
+        printEndOfStatement();
+    }    
 
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlModelReader.java?rev=365780&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlModelReader.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlModelReader.java Tue Jan  3 16:36:26 2006
@@ -0,0 +1,137 @@
+package org.apache.ddlutils.platform.mysql;
+
+/*
+ * Copyright 1999-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.sql.SQLException;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.ForeignKey;
+import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
+import org.apache.ddlutils.platform.JdbcModelReader;
+
+/**
+ * Reads a database model from a MySql database.
+ *
+ * @author Martin van den Bemt
+ * @version $Revision: $
+ */
+public class MySqlModelReader extends JdbcModelReader
+{
+    /**
+     * Creates a new model reader for PostgreSql databases.
+     * 
+     * @param platformInfo The platform specific settings
+     */
+    public MySqlModelReader(PlatformInfo platformInfo)
+    {
+        super(platformInfo);
+        setDefaultCatalogPattern(null);
+        setDefaultSchemaPattern(null);
+        setDefaultTablePattern(null);
+    }
+
+    
+    
+    /**
+     * @see org.apache.ddlutils.platform.JdbcModelReader#readTable(org.apache.ddlutils.platform.DatabaseMetaDataWrapper, java.util.Map)
+     * @todo This needs some more work, since table names can be case sensitive or lowercase
+     *       depending on the platform (really cute).
+     *       See http://dev.mysql.com/doc/refman/4.1/en/name-case-sensitivity.html for more info.
+     */
+    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException {
+        Table table =  super.readTable(metaData, values);
+        List indexes = new ArrayList();
+        // this could be optimized, we need to check if an index can be named PRIMARY without
+        // being a primary key. Taking the safe path for now.
+        for (int i = 0; i < table.getIndexCount(); i++)
+        {
+            Index index = table.getIndex(i);
+            if ("PRIMARY".equals(index.getName()))
+            {
+                for (int c = 0; c < index.getColumnCount(); c++)
+                {
+                    String columnName = index.getColumn(c).getName();
+                    Column column  = table.findColumn(columnName);
+                    if (column.isPrimaryKey())
+                    {
+                        indexes.add(index);
+                    }
+                }
+            }
+            else
+            {
+                for (int f = 0; f < table.getForeignKeyCount(); f++)
+                {
+                    ForeignKey fk = table.getForeignKey(f);
+                    if (fk.getName().equals(index.getName()))
+                    {
+                        indexes.add(index);
+                    }
+                }
+            }
+        }
+        for (int i = 0; i < indexes.size(); i++)
+        {
+            table.removeIndex((Index) indexes.get(i));
+        }
+        return table;
+    }
+
+
+
+    /**
+     * @see org.apache.ddlutils.platform.JdbcModelReader#readColumn(org.apache.ddlutils.platform.DatabaseMetaDataWrapper, java.util.Map)
+     */
+    protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Column column = super.readColumn(metaData, values);
+
+        if ("".equals(column.getDescription()))
+        {
+            column.setDescription(null);
+        }
+        if ("".equals(column.getParsedDefaultValue()))
+        {
+            column.setDefaultValue(null);
+        }
+        if ("auto_increment".equals(column.getDescription()))
+        {
+            column.setAutoIncrement(true);
+        }
+        switch (column.getTypeCode())
+        {
+            case Types.INTEGER:
+                if ("0".equals(column.getDefaultValue()))
+                {
+                    column.setDefaultValue(null);
+                }
+            case Types.DOUBLE:
+                column.setSize(null);
+                break;
+        }
+        return column;
+    }
+
+
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java?rev=365780&r1=365779&r2=365780&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java Tue Jan  3 16:36:26 2006
@@ -50,6 +50,8 @@
         info.setPrimaryKeyEmbedded(true);
         info.setForeignKeysEmbedded(false);
         info.setIndicesEmbedded(false);
+        // see http://dev.mysql.com/doc/refman/4.1/en/example-auto-increment.html
+        info.setSupportingNonPKIdentityColumns(false);
         info.setCommentPrefix("#");
         // Double quotes are only allowed for delimiting identifiers if the server SQL mode includes ANSI_QUOTES 
         info.setDelimiterToken("`");
@@ -80,8 +82,11 @@
 
         info.addDefaultSize(Types.BINARY,    254);
         info.addDefaultSize(Types.VARBINARY, 254);
+        // VARCHAR needs a size specified. It's best to sepecify one in your model
+        info.addDefaultSize(Types.VARCHAR, 50);
         
         setSqlBuilder(new MySqlBuilder(info));
+        setModelReader(new MySqlModelReader(info));
     }
 
     /**

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Constraints.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Constraints.java?rev=365780&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Constraints.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Constraints.java Tue Jan  3 16:36:26 2006
@@ -0,0 +1,227 @@
+package org.apache.ddlutils.io.mysql;
+
+/*
+ * Copyright 1999-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.io.TestConstraints;
+import org.apache.ddlutils.platform.mysql.MySqlPlatform;
+
+/**
+ * Performs the roundtrip constraint tests against a PostgreSql database.
+ * NOTE: On windows you have to set ower_case_table_names=2 in you my.ini to
+ *       pass this test.
+ * 
+ * As a work around I've overrien the test_index_model, since above is not happening
+ * in a consequent manner.
+ *
+ * @author Martin van den Bemt
+ * @version $Revision: $
+ */
+public class TestMySql41Constraints extends TestConstraints
+{
+
+    /** 
+     * Test model with a simple index.
+     * Added OVERRIDE to the name to minimize confusion.
+     */
+    public static final String TEST_INDEX_MODEL_OVERRIDE = 
+        "<?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='VALUE' type='DOUBLE'/>\n"+
+        "    <index name='TEST_INDEX'>\n"+
+        "      <index-column name='VALUE'/>\n"+
+        "    </index>\n"+
+        "  </table>\n"+
+        "</database>";
+
+    /** Test model with a not-nullable column. */
+    protected static final String TEST_NOT_NULL_MODEL_OVERRIDE = 
+        "<?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='VALUE' type='VARCHAR' required='true'/>\n"+
+        "  </table>\n"+
+        "</database>";
+
+    /** Test model with an unique index with two columns. */
+    protected static final String TEST_UNIQUE_INDEX_MODEL_OVERRIDE = 
+        "<?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='VALUE1' type='DOUBLE'/>\n"+
+        "    <column name='VALUE2' type='VARCHAR'/>\n"+
+        "    <unique name='TEST_INDEX'>\n"+
+        "      <unique-column name='VALUE2'/>\n"+
+        "      <unique-column name='VALUE1'/>\n"+
+        "    </unique>\n"+
+        "  </table>\n"+
+        "</database>";
+
+    /** Test model with an index with two columns, one of which a pk field. */
+    protected static final String TEST_PRIMARY_KEY_INDEX_MODEL_OVERRIDE = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='roundtrip'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE' type='DOUBLE'/>\n"+
+        "    <index name='TEST_INDEX'>\n"+
+        "      <index-column name='VALUE'/>\n"+
+        "      <index-column name='PK_1'/>\n"+
+        "    </index>\n"+
+        "  </table>\n"+
+        "</database>";
+    /** Test model with two tables and a simple foreign key relationship between them. */
+    protected static final String TEST_SIMPLE_FOREIGN_KEY_MODEL_OVERRIDE = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='roundtrip_1'>\n"+
+        "    <column name='PK' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "  </table>\n"+
+        "  <table name='roundtrip_2'>\n"+
+        "    <column name='PK' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE' type='INTEGER' required='true'/>\n"+
+        "    <foreign-key foreignTable='roundtrip_1'>\n"+
+        "      <reference local='VALUE' foreign='PK'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
+    /** Test model with two tables and overlapping foreign keys between them. */
+    protected static final String TEST_OVERLAPPING_FOREIGN_KEYS_MODEL_OVERRIDE = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='roundtrip_1'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "  </table>\n"+
+        "  <table name='roundtrip_2'>\n"+
+        "    <column name='PK' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='INTEGER' required='true'/>\n"+
+        "    <column name='VALUE_2' type='INTEGER'/>\n"+
+        "    <column name='VALUE_3' type='VARCHAR'/>\n"+
+        "    <foreign-key name='FK_1' foreignTable='roundtrip_1'>\n"+
+        "      <reference local='VALUE_1' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_3' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "    <foreign-key foreignTable='roundtrip_1'>\n"+
+        "      <reference local='VALUE_2' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_3' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
+    /** Test model with two tables and circular foreign key relationships between them. */
+    protected static final String TEST_CIRCULAR_FOREIGN_KEYS_MODEL_OVERRIDE = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='roundtrip_1'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='INTEGER'/>\n"+
+        "    <column name='VALUE_2' type='VARCHAR'/>\n"+
+        "    <foreign-key foreignTable='roundtrip_2'>\n"+
+        "      <reference local='VALUE_1' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_2' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "  <table name='roundtrip_2'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='VARCHAR' required='true'/>\n"+
+        "    <column name='VALUE_2' type='INTEGER' required='true'/>\n"+
+        "    <foreign-key foreignTable='roundtrip_1'>\n"+
+        "      <reference local='VALUE_2' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_1' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
+
+
+    /**
+     * Tests a simple index.
+     * This test actually shouldn't pass :( 
+     */
+    public void testIndex()
+    {
+        if (getPlatformInfo().isSupportingNonUniqueIndices())
+        {
+            performConstraintsTest(TEST_INDEX_MODEL_OVERRIDE);
+        }
+    }
+
+    /**
+     * Tests a not-nullable column. 
+     */
+    public void testNotNullableColumn()
+    {
+        performConstraintsTest(TEST_NOT_NULL_MODEL_OVERRIDE);
+    }
+
+    /**
+     * Tests an unique index for two columns. 
+     */
+    public void testUniqueIndex()
+    {
+        performConstraintsTest(TEST_UNIQUE_INDEX_MODEL_OVERRIDE);
+    }
+
+    /**
+     * Tests an index for two columns, one of which a pk column. 
+     */
+    public void testPrimaryKeyIndex()
+    {
+        if (getPlatformInfo().isSupportingNonUniqueIndices())
+        {
+            performConstraintsTest(TEST_PRIMARY_KEY_INDEX_MODEL_OVERRIDE);
+        }
+    }
+
+    /**
+     * Tests two tables with a simple foreign key relationship between them. 
+     */
+    public void testSimpleForeignKey()
+    {
+        performConstraintsTest(TEST_SIMPLE_FOREIGN_KEY_MODEL_OVERRIDE);
+    }
+
+    /**
+     * @see org.apache.ddlutils.io.RoundtripTestBase#getPlatformName()
+     */
+    protected String getPlatformName()
+    {
+        return MySqlPlatform.DATABASENAME;
+    }
+
+    /**
+     * Tests two tables with overlapping foreign key relationships between them. 
+     */
+    public void testOverlappingForeignKeys()
+    {
+        performConstraintsTest(TEST_OVERLAPPING_FOREIGN_KEYS_MODEL_OVERRIDE);
+    }
+
+    /**
+     * Tests two tables with circular foreign key relationships between them. 
+     */
+    public void testCircularForeignKeys()
+    {
+        performConstraintsTest(TEST_CIRCULAR_FOREIGN_KEYS_MODEL_OVERRIDE);
+    }
+
+}

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Datatypes.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Datatypes.java?rev=365780&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Datatypes.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mysql/TestMySql41Datatypes.java Tue Jan  3 16:36:26 2006
@@ -0,0 +1,37 @@
+package org.apache.ddlutils.io.mysql;
+
+/*
+ * Copyright 1999-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.io.TestDatatypes;
+import org.apache.ddlutils.platform.mysql.MySqlPlatform;
+
+/**
+ * Performs the roundtrip datatype tests against a PostgreSql database.
+ * 
+ * @author Martin van den Bemt
+ * @version $Revision: 289996 $
+ */
+public class TestMySql41Datatypes extends TestDatatypes
+{
+    /**
+     * @see org.apache.ddlutils.io.RoundtripTestBase#getPlatformName()
+     */
+    protected String getPlatformName()
+    {
+        return MySqlPlatform.DATABASENAME;
+    }
+}