db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rolf Jentsch <RJent...@electronicpartner.de>
Subject [PATCH] ant jdbc-task
Date Tue, 11 Mar 2003 13:17:50 GMT
Hallo,

while importing an existing database into a turbine/torque project, I found that with the
the ant jdbc-task information about indices is not written to the generated schema.xml.

The included patch adds support for this feature. 

The patch should work against torque-3.0 (src/java/org/apache/torque/task/TorqueJDBCTransformTask.java)
as well as torque-3.1-dev (src/generator/src/java/org/apache/torque/task/TorqueJDBCTransformTask.java)

So far it has only been tested with PostgreSQL 7.2.

Rolf Jentsch
Produktentwicklung EDV-Anwendungen für Mitglieder
ElectronicPartner GmbH & Co. KG
Düsseldorf

--- TorqueJDBCTransformTask.java.orig    2003-03-11 13:49:37.000000000 +0100
+++ TorqueJDBCTransformTask.java    2003-03-11 13:54:55.000000000 +0100
@@ -270,6 +270,7 @@
             List columns = getColumns(dbMetaData, curTable);
             List primKeys = getPrimaryKeys(dbMetaData, curTable);
             Collection forgnKeys = getForeignKeys(dbMetaData, curTable);
+            Collection indexData = getIndexData(dbMetaData, curTable);
 
             // Set the primary keys.
             primaryKeys = new Hashtable();
@@ -366,6 +367,35 @@
                 }
                 table.appendChild(fk);
             }
+            // Indices for this table.
+            for (Iterator l = indexData.iterator(); l.hasNext(); )
+            {
+                Object[] indexD = (Object[]) l.next();
+                Boolean notUnique = (Boolean) indexD[2];
+                List cols = (List) indexD[1];
+                Element ix;
+                if ( notUnique.booleanValue() )
+                    ix = doc.createElement("index");
+                else
+                {
+                    if ( checkPKey(cols,primKeys) )
+                        continue;
+                    ix = doc.createElement("unique");
+                }
+                //FIXME: find out the correct indexname
+                //ix.setAttribute("name",(String)indexD[0]);
+                for ( int m = 0 ;m < cols.size();m++ )
+                {
+                    Element col;
+                    if ( notUnique.booleanValue() )
+                        col = doc.createElement("index-column");
+                    else
+                        col = doc.createElement("unique-column");
+                    col.setAttribute("name",(String)cols.get(m));
+                    ix.appendChild(col);
+                }
+                table.appendChild(ix);
+            }
             databaseNode.appendChild(table);
         }
         doc.appendChild(databaseNode);
@@ -542,4 +572,79 @@
         }
         return fks.values();
     }
+    /**
+     * Retrieves a list of index definitions for a given table.
+     *
+     * @param dbMeta JDBC metadata.
+     * @param tableName Table from which to retrieve IX information.
+     * @return A list of index definitions in <code>tableName</code>.
+     * @throws SQLException
+     */
+    public Collection getIndexData(DatabaseMetaData dbMeta, String tableName)
+        throws SQLException
+    {
+        Hashtable idx = new Hashtable();
+        ResultSet indexInfo = null;
+        short indexType ;
+        try
+        {
+            indexInfo = dbMeta.getIndexInfo(null, dbSchema, tableName,false,true);
+            while (indexInfo.next())
+            {
+                indexType = indexInfo.getShort(7);
+                if ( indexType == dbMeta.tableIndexStatistic )
+                    continue;
+                String ixName = indexInfo.getString(6);
+                // if IX has no name - make it up (use tablename instead)
+                if (ixName == null)
+                {
+                    ixName = indexInfo.getString(3);
+                }
+                Object[] ix = (Object[]) idx.get(ixName);
+                List refs;
+                if (ix == null)
+                {
+                    ix = new Object[3];
+                    ix[0] = indexInfo.getString(3); //table name
+                    refs = new ArrayList();
+                    ix[1] = refs;
+                    ix[2] = new Boolean(indexInfo.getBoolean(4));
+                    idx.put(ixName, ix);
+                }
+                else
+                {
+                    refs = (ArrayList) ix[1];
+                }
+                String ref = indexInfo.getString(9); //column
+                refs.add(ref);
+            }
+        }
+        finally
+        {
+            if (indexInfo != null)
+            {
+                indexInfo.close();
+            }
+        }
+        return idx.values();
+    }
+    /**
+     * Checks wether or not an index may be the primary key
+     *
+     * An index is probably the primary key if it contains the
+     * same columns in the same order.
+     *
+     * @param cols Columns for the current index
+     * @param pKeyCols Columns for the primary key
+     * @return true if the index is the primary key
+     */
+    private boolean checkPKey(List cols,List pKeyCols )
+    {
+        if ( pKeyCols.size() != cols.size() )
+            return false;
+        for ( int m = 0 ; m < cols.size() ; m++ )
+            if (((String)cols.get(m)).compareTo((String)pKeyCols.get(m)) != 0)
+                return false;
+        return true;
+    }
 }


Mime
View raw message