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 to...@apache.org on 2006/01/01 21:28:28 UTC

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

Author: tomdz
Date: Sun Jan  1 12:28:19 2006
New Revision: 360546

URL: http://svn.apache.org/viewcvs?rev=360546&view=rev
Log:
Finished Mckoi support

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java
    db/ddlutils/trunk/src/test/jdbc.properties.mckoi
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiConstraints.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiDatatypes.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java?rev=360546&r1=360545&r2=360546&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java Sun Jan  1 12:28:19 2006
@@ -55,11 +55,18 @@
     /**
      * {@inheritDoc}
      */
-    protected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException
+    protected void writeColumnDefaultValue(Table table, Column column) throws IOException
     {
-        // we start at value 1 to avoid issues with jdbc
-        print("DEFAULT UNIQUEKEY(");
-        printIdentifier(getTableName(table));
-        print(") + 1");
+        if (column.isAutoIncrement())
+        {
+            // we start at value 1 to avoid issues with jdbc
+            print("UNIQUEKEY('");
+            print(getTableName(table));
+            print("') + 1");
+        }
+        else
+        {
+            super.writeColumnDefaultValue(table, column);
+        }
     }
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java?rev=360546&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java Sun Jan  1 12:28:19 2006
@@ -0,0 +1,119 @@
+package org.apache.ddlutils.platform.mckoi;
+
+/*
+ * Copyright 1999-2005 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.ResultSet;
+import java.sql.Statement;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.collections.map.ListOrderedMap;
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
+import org.apache.ddlutils.platform.JdbcModelReader;
+
+/**
+ * Reads a database model from a Mckoi database.
+ *
+ * @author Thomas Dudziak
+ * @version $Revision: $
+ */
+public class MckoiModelReader extends JdbcModelReader
+{
+    /**
+     * Creates a new model reader for Mckoi databases.
+     * 
+     * @param platformInfo The platform specific settings
+     */
+    public MckoiModelReader(PlatformInfo platformInfo)
+    {
+        super(platformInfo);
+        setDefaultCatalogPattern(null);
+        setDefaultSchemaPattern(null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Table table = super.readTable(metaData, values);
+
+        // Mckoi does not currently return unique indices in the metadata so we have to query
+        // internal tables to get this info
+        StringBuffer query = new StringBuffer();
+
+        query.append("SELECT uniqueColumns.column, uniqueColumns.seq_no, uniqueInfo.name");
+        query.append(" FROM SYS_INFO.sUSRUniqueColumns uniqueColumns, SYS_INFO.sUSRUniqueInfo uniqueInfo");
+        query.append(" WHERE uniqueColumns.un_id = uniqueInfo.id AND uniqueInfo.table = '");
+        query.append(table.getName());
+        if (table.getSchema() != null)
+        {
+            query.append("' AND uniqueInfo.schema = '");
+            query.append(table.getSchema());
+        }
+        query.append("'");
+
+        Statement stmt        = getConnection().createStatement();
+        ResultSet resultSet   = stmt.executeQuery(query.toString());
+        Map       indices     = new ListOrderedMap();
+        Map       indexValues = new HashMap();
+
+        indexValues.put("NON_UNIQUE", Boolean.FALSE);
+        while (resultSet.next())
+        {
+            indexValues.put("COLUMN_NAME",      resultSet.getString(1));
+            indexValues.put("ORDINAL_POSITION", new Short(resultSet.getShort(2)));
+            indexValues.put("INDEX_NAME",       resultSet.getString(3));
+
+            readIndex(metaData, indexValues, indices);
+        }
+        resultSet.close();
+
+        table.addIndices(indices.values());
+        
+        return table;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Column column = super.readColumn(metaData, values);
+
+        if (column.getSize() != null)
+        {
+            if (column.getSizeAsInt() <= 0)
+            {
+                column.setSize(null);
+            }
+        }
+
+        String defaultValue = column.getDefaultValue();
+
+        if ((defaultValue != null) && defaultValue.startsWith("UNIQUEKEY("))
+        {
+            column.setDefaultValue(null);
+            column.setAutoIncrement(true);
+        }
+        return column;
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java?rev=360546&r1=360545&r2=360546&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java Sun Jan  1 12:28:19 2006
@@ -16,8 +16,14 @@
  * limitations under the License.
  */
 
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
 import java.sql.Types;
+import java.util.Iterator;
+import java.util.Map;
 
+import org.apache.ddlutils.DynaSqlException;
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.platform.PlatformImplBase;
 
@@ -46,19 +52,27 @@
         info.setRequiringNullAsDefaultValue(false);
         info.setPrimaryKeyEmbedded(true);
         info.setForeignKeysEmbedded(false);
-        info.setIndicesEmbedded(false);
-
-        info.addNativeTypeMapping(Types.ARRAY,    "BLOB");
-        info.addNativeTypeMapping(Types.BIT,      "BOOLEAN");
-        info.addNativeTypeMapping(Types.DISTINCT, "BLOB");
-        info.addNativeTypeMapping(Types.FLOAT,    "DOUBLE");
-        info.addNativeTypeMapping(Types.NULL,     "BLOB");
-        info.addNativeTypeMapping(Types.OTHER,    "BLOB");
-        info.addNativeTypeMapping(Types.REF,      "BLOB");
-        info.addNativeTypeMapping(Types.STRUCT,   "BLOB");
-        info.addNativeTypeMapping("DATALINK", "BLOB");
-
+        info.setSupportingNonUniqueIndices(false);
+        info.setIndicesEmbedded(true);
+        info.setIdentitySpecUsesDefaultValue(true);
+
+        info.addNativeTypeMapping(Types.ARRAY,    "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping(Types.DISTINCT, "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping(Types.FLOAT,    "DOUBLE", Types.DOUBLE);
+        info.addNativeTypeMapping(Types.NULL,     "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping(Types.OTHER,    "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping(Types.REF,      "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping(Types.STRUCT,   "BLOB",   Types.BLOB);
+        info.addNativeTypeMapping("BIT",      "BOOLEAN", "BOOLEAN");
+        info.addNativeTypeMapping("DATALINK", "BLOB",    "BLOB");
+
+        info.addDefaultSize(Types.CHAR,      1024);
+        info.addDefaultSize(Types.VARCHAR,   1024);
+        info.addDefaultSize(Types.BINARY,    1024);
+        info.addDefaultSize(Types.VARBINARY, 1024);
+        
         setSqlBuilder(new MckoiBuilder(info));
+        setModelReader(new MckoiModelReader(info));
     }
 
     /**
@@ -67,5 +81,73 @@
     public String getName()
     {
         return DATABASENAME;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password, Map parameters) throws DynaSqlException, UnsupportedOperationException
+    {
+        // For McKoi, you create databases by simply appending "?create=true" to the connection url
+        if (JDBC_DRIVER.equals(jdbcDriverClassName))
+        {
+            StringBuffer creationUrl = new StringBuffer();
+            Connection   connection  = null;
+
+            creationUrl.append(connectionUrl);
+            // TODO: It might be safer to parse the URN and check whethere there is already a parameter there
+            //       (in which case e'd have to use '&' instead)
+            creationUrl.append("?create=true");
+            if ((parameters != null) && !parameters.isEmpty())
+            {
+                for (Iterator it = parameters.entrySet().iterator(); it.hasNext();)
+                {
+                    Map.Entry entry = (Map.Entry)it.next();
+
+                    // no need to specify create twice (and create=false wouldn't help anyway)
+                    if (!"create".equalsIgnoreCase(entry.getKey().toString()))
+                    {
+                        creationUrl.append("&");
+                        creationUrl.append(entry.getKey().toString());
+                        creationUrl.append("=");
+                        if (entry.getValue() != null)
+                        {
+                            creationUrl.append(entry.getValue().toString());
+                        }
+                    }
+                }
+            }
+            if (getLog().isDebugEnabled())
+            {
+                getLog().debug("About to create database using this URL: "+creationUrl.toString());
+            }
+            try
+            {
+                Class.forName(jdbcDriverClassName);
+
+                connection = DriverManager.getConnection(creationUrl.toString(), username, password);
+                logWarnings(connection);
+            }
+            catch (Exception ex)
+            {
+                throw new DynaSqlException("Error while trying to create a database", ex);
+            }
+            finally
+            {
+                if (connection != null)
+                {
+                    try
+                    {
+                        connection.close();
+                    }
+                    catch (SQLException ex)
+                    {}
+                }
+            }
+        }
+        else
+        {
+            throw new UnsupportedOperationException("Unable to create a Derby database via the driver "+jdbcDriverClassName);
+        }
     }
 }

Added: db/ddlutils/trunk/src/test/jdbc.properties.mckoi
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/jdbc.properties.mckoi?rev=360546&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/jdbc.properties.mckoi (added)
+++ db/ddlutils/trunk/src/test/jdbc.properties.mckoi Sun Jan  1 12:28:19 2006
@@ -0,0 +1,20 @@
+# JDBC properties for Mckoi 1.0.3
+
+# Use this property if ddlutils does not recognize the platform from the settings
+#ddlutils.platform=Mckoi
+
+# Properties starting with "datasource." will be fed into the datasource instance of the
+# class configured via the datasource.class property
+
+datasource.class=org.apache.commons.dbcp.BasicDataSource
+datasource.driverClassName=com.mckoi.JDBCDriver
+datasource.username=ddlutils
+datasource.password=ddlutils
+
+# Embedded mode
+
+#datasource.url=jdbc:mckoi:local://./target/database/ddlutils_mckoi
+
+# Client/server mode
+
+datasource.url=jdbc:mckoi://localhost

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiConstraints.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiConstraints.java?rev=360546&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiConstraints.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiConstraints.java Sun Jan  1 12:28:19 2006
@@ -0,0 +1,37 @@
+package org.apache.ddlutils.io.mckoi;
+
+/*
+ * Copyright 1999-2005 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.ConstraintsTestBase;
+import org.apache.ddlutils.platform.mckoi.MckoiPlatform;
+
+/**
+ * Performs the roundtrip constraint tests against a Mckoi database.
+ * 
+ * @author Thomas Dudziak
+ * @version $Revision: $
+ */
+public class TestMckoiConstraints extends ConstraintsTestBase
+{
+    /**
+     * {@inheritDoc}
+     */
+    protected String getPlatformName()
+    {
+        return MckoiPlatform.DATABASENAME;
+    }
+}

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiDatatypes.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiDatatypes.java?rev=360546&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiDatatypes.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/mckoi/TestMckoiDatatypes.java Sun Jan  1 12:28:19 2006
@@ -0,0 +1,37 @@
+package org.apache.ddlutils.io.mckoi;
+
+/*
+ * Copyright 1999-2005 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.DatatypesTestBase;
+import org.apache.ddlutils.platform.mckoi.MckoiPlatform;
+
+/**
+ * Performs the roundtrip datatype tests against a Mckoi database.
+ * 
+ * @author Thomas Dudziak
+ * @version $Revision: 289996 $
+ */
+public class TestMckoiDatatypes extends DatatypesTestBase
+{
+    /**
+     * {@inheritDoc}
+     */
+    protected String getPlatformName()
+    {
+        return MckoiPlatform.DATABASENAME;
+    }
+}