You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Rolf Jentsch <RJ...@electronicpartner.de> on 2003/03/11 14:17:50 UTC
[PATCH] ant jdbc-task
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;
+ }
}
Re: [PATCH] ant jdbc-task
Posted by Rolf Jentsch <RJ...@electronicpartner.de>.
Am Dienstag, 11. März 2003 14:39 schrieb Emmanuel Florent:
> This is very nice from you . Thanks a lot.
> Please let me know, I'm having trouble with jdbc & pgsql. This is
> mentioned on Torque website. What version of postgresql do you use ,
> what version of jdbc ?
>
> Thanks a lot again.
>
> Emmanuel Florent
> eflorent@devaki.org
> http://www.devaki.org/
>
The server i'm using, is a postgresql-7.2.2 installed from the rpms contained
in SuSE 81. The jdbc-driver was compiled from the 7.2-source distributen from
postgresql.org.
The example application from the tdk builds and runs without problems, the
jdbc-task needs some tuning. It looks like that decimal/numeric types loose
there size.
Off-topic: does anybody know a ER-modelling-tool that runs on linux and is
usable with postgresql ?
Rolf Jentsch
Produktentwicklung EDV-Anwendungen für Mitglieder
ElectronicPartner GmbH & Co. KG
Düsseldorf
rjentsch@electronicpartner.de
Re: [PATCH] ant jdbc-task
Posted by Emmanuel Florent <ef...@formaguide.com>.
This is very nice from you . Thanks a lot.
Please let me know, I'm having trouble with jdbc & pgsql. This is
mentioned on Torque website. What version of postgresql do you use ,
what version of jdbc ?
Thanks a lot again.
Emmanuel Florent
eflorent@devaki.org
http://www.devaki.org/
On 2003.03.11 14:17 Rolf Jentsch wrote:
> 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;
> + }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>
>
>