db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r359664 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform: JdbcModelReader.java derby/DerbyModelReader.java hsqldb/HsqlDbModelReader.java postgresql/PostgreSqlModelReader.java postgresql/PostgreSqlPlatform.java
Date Wed, 28 Dec 2005 23:35:14 GMT
Author: tomdz
Date: Wed Dec 28 15:35:05 2005
New Revision: 359664

URL: http://svn.apache.org/viewcvs?rev=359664&view=rev
Log:
Restructured checks for internal indices for primary/foreign keys
Added PostgreSql jdbc model reader

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java?rev=359664&r1=359663&r2=359664&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java Wed Dec 28
15:35:05 2005
@@ -503,6 +503,21 @@
      */
     protected void removeSystemIndices(Table table)
     {
+        removeInternalPrimaryKeyIndex(table);
+
+        for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
+        {
+            removeInternalForeignKeyIndex(table, table.getForeignKey(fkIdx));
+        }
+    }
+
+    /**
+     * Tries to remove the internal index for the table's primary key.
+     * 
+     * @param table The table
+     */
+    protected void removeInternalPrimaryKeyIndex(Table table)
+    {
         Column[] pks         = table.getPrimaryKeyColumns();
         List     columnNames = new ArrayList();
 
@@ -511,81 +526,101 @@
             columnNames.add(pks[columnIdx].getName());
         }
 
-        // Derby returns a unique index for the pk which we don't want however
-        int indexIdx = findMatchingInternalIndex(table, columnNames, true);
+        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
+        {
+            Index index = table.getIndex(indexIdx);
+
+            if (index.isUnique() && matches(index, columnNames) && 
+                isInternalPrimaryKeyIndex(table, index))
+            {
+                table.removeIndex(indexIdx);
+                break;
+            }
+        }
+    }
+
+    /**
+     * Tries to remove the internal index for the given foreign key.
+     * 
+     * @param table The table where the table is defined
+     * @param fk    The foreign key
+     */
+    protected void removeInternalForeignKeyIndex(Table table, ForeignKey fk)
+    {
+        List columnNames = new ArrayList();
 
-        if (indexIdx >= 0)
+        for (int columnIdx = 0; columnIdx < fk.getReferenceCount(); columnIdx++)
         {
-            table.removeIndex(indexIdx);
+            columnNames.add(fk.getReference(columnIdx).getLocalColumnName());
         }
 
-        // Likewise, Derby returns a non-unique index for every foreign key
-        for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
+        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
         {
-            ForeignKey fk = table.getForeignKey(fkIdx);
+            Index index = table.getIndex(indexIdx);
 
-            columnNames.clear();
-            for (int columnIdx = 0; columnIdx < fk.getReferenceCount(); columnIdx++)
-            {
-                columnNames.add(fk.getReference(columnIdx).getLocalColumnName());
-            }
-            indexIdx = findMatchingInternalIndex(table, columnNames, false);
-            if (indexIdx >= 0)
+            if (!index.isUnique() && matches(index, columnNames) && 
+                isInternalForeignKeyIndex(table, fk, index))
             {
                 table.removeIndex(indexIdx);
+                break;
             }
         }
     }
 
-
     /**
-     * Tries to find an internal index that matches the given columns.
+     * Checks whether the given index matches the column list.
      * 
-     * @param table              The table
+     * @param index              The index
      * @param columnsToSearchFor The names of the columns that the index should be for
-     * @param unique             Whether to search for an unique index
-     * @return The position of the index or <code>-1</code> if no such index
was found
+     * @return <code>true</code> if the index matches the columns
      */
-    protected int findMatchingInternalIndex(Table table, List columnsToSearchFor, boolean
unique)
+    protected boolean matches(Index index, List columnsToSearchFor)
     {
-        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
+        if (index.getColumnCount() != columnsToSearchFor.size())
         {
-            Index index = table.getIndex(indexIdx);
-
-            if ((unique == index.isUnique()) && (index.getColumnCount() == columnsToSearchFor.size()))
+            return false;
+        }
+        for (int columnIdx = 0; columnIdx < index.getColumnCount(); columnIdx++)
+        {
+            if (!columnsToSearchFor.get(columnIdx).equals(index.getColumn(columnIdx).getName()))
             {
-                boolean found = true;
-
-                for (int columnIdx = 0; found && (columnIdx < index.getColumnCount());
columnIdx++)
-                {
-                    if (!columnsToSearchFor.get(columnIdx).equals(index.getColumn(columnIdx).getName()))
-                    {
-                        found = false;
-                    }
-                }
-
-                // if the index seems to be internal, we immediately return it
-                if (found && mightBeInternalIndex(index))
-                {
-                    return indexIdx;
-                }
+                return false;
             }
         }
-        return -1;
+        return true;
     }
 
     /**
-     * Guesses whether the index might be an internal database-generated index.
-     * Note that only indices with the correct columns are fed to this method.
+     * Tries to determine whether the index is the internal database-generated index
+     * for the given table's primary key.
+     * Note that only unique indices with the correct columns are fed to this method.
      * Redefine this method for specific platforms if there are better ways
      * to determine internal indices.
      * 
+     * @param table The table owning the index
      * @param index The index to check
-     * @return <code>true</code> if the index seems to be an internal one
+     * @return <code>true</code> if the index seems to be an internal primary
key one
      */
-    protected boolean mightBeInternalIndex(Index index)
+    protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
     {
-        return true;
+        return false;
+    }
+
+    /**
+     * Tries to determine whether the index is the internal database-generated index
+     * for the given foreign key.
+     * Note that only non-unique indices with the correct columns are fed to this method.
+     * Redefine this method for specific platforms if there are better ways
+     * to determine internal indices.
+     * 
+     * @param table The table owning the index and foreign key
+     * @param fk    The foreign key
+     * @param index The index to check
+     * @return <code>true</code> if the index seems to be an internal primary
key one
+     */
+    protected boolean isInternalForeignKeyIndex(Table table, ForeignKey fk, Index index)
+    {
+        return false;
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyModelReader.java?rev=359664&r1=359663&r2=359664&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyModelReader.java Wed
Dec 28 15:35:05 2005
@@ -21,7 +21,9 @@
 
 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;
 
@@ -66,12 +68,28 @@
     }
 
     /**
-     * Guesses whether the index might be an internal index, i.e. one created by Derby.
+     * {@inheritDoc}
+     */
+    protected boolean isInternalForeignKeyIndex(Table table, ForeignKey fk, Index index)
+    {
+        return isInternalIndex(index);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
+    {
+        return isInternalIndex(index);
+    }
+
+    /**
+     * Determines whether the index is an internal index, i.e. one created by Derby.
      * 
      * @param index The index to check
      * @return <code>true</code> if the index seems to be an internal one
      */
-    protected boolean mightBeInternalIndex(Index index)
+    private boolean isInternalIndex(Index index)
     {
         String name = index.getName();
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java?rev=359664&r1=359663&r2=359664&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbModelReader.java
Wed Dec 28 15:35:05 2005
@@ -17,7 +17,9 @@
  */
 
 import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.platform.JdbcModelReader;
 
 /**
@@ -43,12 +45,20 @@
     /**
      * {@inheritDoc}
      */
-    protected boolean mightBeInternalIndex(Index index)
+    protected boolean isInternalForeignKeyIndex(Table table, ForeignKey fk, Index index)
     {
         String name = index.getName();
 
-        // Internal names normally have the form "SYS_PK_ROUNDTRIP" or "SYS_IDX_11"
-        return (name != null) &&
-               (name.startsWith("SYS_PK_") || name.startsWith("SYS_IDX_"));
+        return (name != null) && name.startsWith("SYS_IDX_");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
+    {
+        String name = index.getName();
+
+        return (name != null) && name.startsWith("SYS_PK_");
     }
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java?rev=359664&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
(added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
Wed Dec 28 15:35:05 2005
@@ -0,0 +1,64 @@
+package org.apache.ddlutils.platform.postgresql;
+
+/*
+ * 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.PlatformInfo;
+import org.apache.ddlutils.model.ForeignKey;
+import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.JdbcModelReader;
+
+/**
+ * Reads a database model from a PostgreSql database.
+ *
+ * @author Thomas Dudziak
+ * @version $Revision: $
+ */
+public class PostgreSqlModelReader extends JdbcModelReader
+{
+    /**
+     * Creates a new model reader for PostgreSql databases.
+     * 
+     * @param platformInfo The platform specific settings
+     */
+    public PostgreSqlModelReader(PlatformInfo platformInfo)
+    {
+        super(platformInfo);
+        setDefaultCatalogPattern(null);
+        setDefaultSchemaPattern(null);
+        setDefaultTablePattern(null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected boolean isInternalForeignKeyIndex(Table table, ForeignKey fk, Index index)
+    {
+        // TODO Auto-generated method stub
+        return super.isInternalForeignKeyIndex(table, fk, index);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
+    {
+        // PostgreSql uses the form "<tablename>_pkey"
+        return (table.getName() + "_pkey").equals(index.getName());
+    }
+
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java?rev=359664&r1=359663&r2=359664&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
Wed Dec 28 15:35:05 2005
@@ -82,6 +82,7 @@
         info.setHasSize(Types.VARBINARY, false);
 
         setSqlBuilder(new PostgreSqlBuilder(info));
+        setModelReader(new PostgreSqlModelReader(info));
     }
 
     /**



Mime
View raw message