You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by fe...@apache.org on 2001/08/12 07:56:33 UTC
cvs commit: jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model TypeMap.java
fedor 01/08/11 22:56:33
Modified: src/java/org/apache/turbine/torque Tag: T_2_1_BRANCH
TorqueJDBCTransformTask.java
src/java/org/apache/turbine/torque/engine/database/model
Tag: T_2_1_BRANCH TypeMap.java
Log:
commiting my old JDBC task patches. JDBC task is now quite functional. This is already in the HEAD
Revision Changes Path
No revision
No revision
1.4.4.8 +90 -46 jakarta-turbine/src/java/org/apache/turbine/torque/Attic/TorqueJDBCTransformTask.java
Index: TorqueJDBCTransformTask.java
===================================================================
RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/torque/Attic/TorqueJDBCTransformTask.java,v
retrieving revision 1.4.4.7
retrieving revision 1.4.4.8
diff -u -r1.4.4.7 -r1.4.4.8
--- TorqueJDBCTransformTask.java 2001/06/04 23:53:27 1.4.4.7
+++ TorqueJDBCTransformTask.java 2001/08/12 05:56:33 1.4.4.8
@@ -69,9 +69,13 @@
import java.util.List;
import java.util.Properties;
import java.util.Vector;
+import java.util.Collection;
+import java.util.Iterator;
+import org.apache.turbine.torque.engine.database.model.TypeMap;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.xerces.dom.DocumentImpl;
+import org.apache.xerces.dom.DocumentTypeImpl;
import org.apache.xerces.dom.NodeImpl;
import org.apache.xml.serialize.BaseMarkupSerializer;
import org.apache.xml.serialize.OutputFormat;
@@ -87,13 +91,11 @@
* JDBC metadata.
*
* @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
- * @version $Id: TorqueJDBCTransformTask.java,v 1.4.4.7 2001/06/04 23:53:27 jvanzyl Exp $
+ * @author <a href="mailto:fedor.karpelevitch@barra.com">Fedor Karpelevitch</a>
+ * @version $Id: TorqueJDBCTransformTask.java,v 1.4.4.8 2001/08/12 05:56:33 fedor Exp $
*/
public class TorqueJDBCTransformTask extends Task
{
- /** Torque properties. */
- protected Properties props;
-
/** Name of XML database schema produced. */
protected String xmlSchema;
@@ -112,8 +114,7 @@
/** DOM document produced. */
protected DocumentImpl doc;
- /** Database Node to start things off. */
- protected Node database;
+ protected Node database, appData;
/** Hashtable of columns that have primary keys. */
protected Hashtable primaryKeys;
@@ -121,8 +122,7 @@
/** Hashtable to track what table a column belongs to. */
protected Hashtable columnTableMap;
- /** Map of java.sql.Types: integer -> string representation. */
- protected Properties sqlTypes;
+ protected boolean sameJavaName;
XMLSerializer xmlSerializer;
@@ -152,20 +152,21 @@
xmlSchema = v;
}
+ public void setSameJavaName(boolean v)
+ {
+ this.sameJavaName = v;
+ }
+
+ public boolean isSameJavaName()
+ {
+ return this.sameJavaName;
+ }
+
/**
* Default constructor.
*/
public void execute() throws BuildException
{
- props = new Properties();
- sqlTypes = new Properties();
-/*
- xmlSchema = props.getProperty("jdbcXMLSchema");
- dbUrl = props.getProperty("dbUrl");
- dbDriver = props.getProperty("dbDriver");
- dbUser = props.getProperty("dbUser");
- dbPassword = props.getProperty("dbPassword");
-*/
System.err.println("Torque - JDBCToXMLSchema starting\n");
System.err.println("Your DB settings are:");
System.err.println("driver : "+dbDriver);
@@ -173,7 +174,8 @@
System.err.println("user : "+dbUser);
System.err.println("password : "+dbPassword);
- doc = new DocumentImpl();
+ DocumentTypeImpl docType= new DocumentTypeImpl(null,"app-data", null, "http://jakarta.apache.org/turbine/dtd/database.dtd");
+ doc = new DocumentImpl(docType);
doc.appendChild(doc.createComment(" Autogenerated by JDBCToXMLSchema! "));
try
@@ -217,6 +219,7 @@
// The database map.
Vector tableList = getTableNames(dbMetaData);
+ appData = doc.createElement("app-data");
database = doc.createElement("database");
// Build a database-wide column -> table map.
@@ -244,13 +247,17 @@
Element table = doc.createElement("table");
table.setAttribute("name", curTable);
+ if (isSameJavaName())
+ {
+ table.setAttribute("javaName", curTable);
+ }
// Add Columns.
// TableMap tblMap = dbMap.getTable(curTable);
List columns = getColumns(dbMetaData, curTable);
List primKeys = getPrimaryKeys(dbMetaData, curTable);
- List forgnKeys = getForeignKeys(dbMetaData, curTable);
+ Collection forgnKeys = getForeignKeys(dbMetaData, curTable);
// Set the primary keys.
primaryKeys = new Hashtable();
@@ -261,21 +268,11 @@
primaryKeys.put(curPrimaryKey, curPrimaryKey);
}
- // Foreign keys for this table.
- for (int l = 0; l < forgnKeys.size(); l++)
- {
- String curForeignKey = (String) forgnKeys.get(l);
- String foreignKeyTable =
- (String) columnTableMap.get(curForeignKey);
- System.out.println(curForeignKey + " => " +
- foreignKeyTable);
- }
-
for (int j = 0; j < columns.size(); j++)
{
Vector v = (Vector) columns.get(j);
String name = (String) v.elementAt(0);
- int type = ((Integer) v.elementAt(1)).intValue();
+ Integer type = ((Integer) v.elementAt(1));
int size = ((Integer) v.elementAt(2)).intValue();
// From DatabaseMetaData.java
@@ -294,14 +291,16 @@
Element column = doc.createElement("column");
column.setAttribute("name", name);
- column.setAttribute("type",
- (String) sqlTypes.get(
- new Integer(type).toString()));
+ if (isSameJavaName())
+ {
+ column.setAttribute("javaName", name);
+ }
+ column.setAttribute("type", TypeMap.getTorqueType(type));
if (size > 0 &&
- (type == Types.CHAR ||
- type == Types.VARCHAR ||
- type == Types.LONGVARCHAR))
+ (type.intValue() == Types.CHAR ||
+ type.intValue() == Types.VARCHAR ||
+ type.intValue() == Types.LONGVARCHAR))
{
column.setAttribute("size",
new Integer(size).toString());
@@ -309,7 +308,7 @@
if (nullType.intValue() == 0)
{
- column.setAttribute("null", "false");
+ column.setAttribute("required", "true");
}
if (primaryKeys.containsKey(name))
@@ -319,9 +318,31 @@
table.appendChild(column);
}
+
+ // Foreign keys for this table.
+ for (Iterator l = forgnKeys.iterator(); l.hasNext();)
+ {
+ Object[] forKey = (Object[]) l.next();
+ String foreignKeyTable = (String)forKey[0];
+ Vector refs = (Vector)forKey[1];
+ Element fk = doc.createElement("foreign-key");
+ fk.setAttribute("foreignTable", foreignKeyTable);
+ for (int m=0; m<refs.size(); m++)
+ {
+ System.out.println(m);
+ Element ref = doc.createElement("reference");
+ String[] refData = (String[]) refs.get(m);
+ ref.setAttribute("local", refData[0]);
+ ref.setAttribute("foreign", refData[1]);
+ fk.appendChild(ref);
+ }
+ table.appendChild(fk);
+ }
+
database.appendChild(table);
}
- doc.appendChild(database);
+ appData.appendChild(database);
+ doc.appendChild(appData);
}
/**
@@ -335,7 +356,7 @@
public Vector getTableNames(DatabaseMetaData dbMeta)
throws SQLException
{
- ResultSet tableNames = dbMeta.getTables("",null, "%",null);
+ ResultSet tableNames = dbMeta.getTables(null,null, "%",null);
Vector tables = new Vector();
while (tableNames.next())
{
@@ -368,7 +389,7 @@
String tableName)
throws SQLException
{
- ResultSet columnSet = dbMeta.getColumns("",null, tableName, null);
+ ResultSet columnSet = dbMeta.getColumns(null,null, tableName, null);
Vector columns = new Vector();
while (columnSet.next())
{
@@ -398,7 +419,7 @@
public List getPrimaryKeys(DatabaseMetaData dbMeta, String tableName)
throws SQLException
{
- ResultSet parts = dbMeta.getPrimaryKeys("", null, tableName);
+ ResultSet parts = dbMeta.getPrimaryKeys(null, null, tableName);
List pk = new Vector();
while (parts.next())
{
@@ -414,15 +435,38 @@
* @param tableName Table from which to retrieve FK information.
* @return A list of foreign keys in <code>tableName</code>.
*/
- public List getForeignKeys(DatabaseMetaData dbMeta, String tableName)
+ public Collection getForeignKeys(DatabaseMetaData dbMeta, String tableName)
throws SQLException
{
- ResultSet foreignKeys = dbMeta.getImportedKeys("", null, tableName);
- List keys = new Vector();
+ ResultSet foreignKeys = dbMeta.getImportedKeys(null, null, tableName);
+ Hashtable fks = new Hashtable();
while (foreignKeys.next())
{
- keys.add(foreignKeys.getString(8));
+ String fkName = foreignKeys.getString(12);
+ // if FK has no name - make it up (use tablename instead)
+ if (fkName==null)
+ {
+ fkName = foreignKeys.getString(3);
+ }
+ Object[] fk = (Object[])fks.get(fkName);
+ Vector refs;
+ if (fk==null)
+ {
+ fk = new Object[2];
+ fk[0] = foreignKeys.getString(3); //referenced table name
+ refs = new Vector();
+ fk[1] = refs;
+ fks.put(fkName, fk);
+ }
+ else
+ {
+ refs = (Vector)fk[1];
+ }
+ String[] ref = new String[2];
+ ref[0] = foreignKeys.getString(8); //local column
+ ref[1] = foreignKeys.getString(4); //foreign column
+ refs.add(ref);
}
- return keys;
+ return fks.values();
}
}
No revision
No revision
1.2.10.1 +45 -1 jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Attic/TypeMap.java
Index: TypeMap.java
===================================================================
RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Attic/TypeMap.java,v
retrieving revision 1.2
retrieving revision 1.2.10.1
diff -u -r1.2 -r1.2.10.1
--- TypeMap.java 2001/03/06 06:13:10 1.2
+++ TypeMap.java 2001/08/12 05:56:33 1.2.10.1
@@ -56,6 +56,7 @@
import java.util.Hashtable;
import java.util.Date;
+import java.sql.Types;
import java.math.BigDecimal;
@@ -105,7 +106,7 @@
* BOOLEANINT | boolean | Integer
*
* @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
- * @version $Id: TypeMap.java,v 1.2 2001/03/06 06:13:10 chrise Exp $
+ * @version $Id: TypeMap.java,v 1.2.10.1 2001/08/12 05:56:33 fedor Exp $
*/
public class TypeMap
{
@@ -237,6 +238,7 @@
private static Hashtable jdbcToVillageMethodMap = null;
private static Hashtable jdbcToPPMethodMap = null;
private static Hashtable torqueTypeToJdbcTypeMap = null;
+ private static Hashtable jdbcToTorqueTypeMap = null;
private static boolean isInitialized = false;
/**
@@ -394,6 +396,33 @@
torqueTypeToJdbcTypeMap.put(TIMESTAMP, TIMESTAMP);
torqueTypeToJdbcTypeMap.put(BOOLEANCHAR, CHAR);
torqueTypeToJdbcTypeMap.put(BOOLEANINT, INTEGER);
+
+ /*
+ * Create JDBC type code to torque type map.
+ */
+ jdbcToTorqueTypeMap = new Hashtable();
+
+ jdbcToTorqueTypeMap.put(new Integer(Types.CHAR), CHAR);
+ jdbcToTorqueTypeMap.put(new Integer(Types.VARCHAR), VARCHAR);
+ jdbcToTorqueTypeMap.put(new Integer(Types.LONGVARCHAR), LONGVARCHAR);
+ jdbcToTorqueTypeMap.put(new Integer(Types.CLOB), CLOB);
+ jdbcToTorqueTypeMap.put(new Integer(Types.NUMERIC), NUMERIC);
+ jdbcToTorqueTypeMap.put(new Integer(Types.DECIMAL), DECIMAL);
+ jdbcToTorqueTypeMap.put(new Integer(Types.BIT), BIT);
+ jdbcToTorqueTypeMap.put(new Integer(Types.TINYINT), TINYINT);
+ jdbcToTorqueTypeMap.put(new Integer(Types.SMALLINT), SMALLINT);
+ jdbcToTorqueTypeMap.put(new Integer(Types.INTEGER), INTEGER);
+ jdbcToTorqueTypeMap.put(new Integer(Types.BIGINT), BIGINT);
+ jdbcToTorqueTypeMap.put(new Integer(Types.REAL), REAL);
+ jdbcToTorqueTypeMap.put(new Integer(Types.FLOAT), FLOAT);
+ jdbcToTorqueTypeMap.put(new Integer(Types.DOUBLE), DOUBLE);
+ jdbcToTorqueTypeMap.put(new Integer(Types.BINARY), BINARY);
+ jdbcToTorqueTypeMap.put(new Integer(Types.VARBINARY), VARBINARY);
+ jdbcToTorqueTypeMap.put(new Integer(Types.LONGVARBINARY), LONGVARBINARY);
+ jdbcToTorqueTypeMap.put(new Integer(Types.BLOB), BLOB);
+ jdbcToTorqueTypeMap.put(new Integer(Types.DATE), DATE);
+ jdbcToTorqueTypeMap.put(new Integer(Types.TIME), TIME);
+ jdbcToTorqueTypeMap.put(new Integer(Types.TIMESTAMP), TIMESTAMP);
isInitialized = true;
}
@@ -482,6 +511,21 @@
initialize();
return (String) torqueTypeToJdbcTypeMap.get(type);
+ }
+
+ /**
+ * Returns Torque type constant corresponding to JDBC type code.
+ * Used but Torque JDBC task.
+ */
+ public static String getTorqueType(Integer sqlType)
+ {
+ /*
+ * Make sure the we are initialized.
+ */
+ if (isInitialized == false)
+ initialize();
+
+ return (String)jdbcToTorqueTypeMap.get(sqlType);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org