Index: src/java/org/apache/turbine/torque/TorqueJDBCTransformTask.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine/src/java/org/apache/turbine/torque/TorqueJDBCTransformTask.java,v
retrieving revision 1.4.4.7
diff -u -r1.4.4.7 TorqueJDBCTransformTask.java
--- src/java/org/apache/turbine/torque/TorqueJDBCTransformTask.java	2001/06/04 23:53:27	1.4.4.7
+++ src/java/org/apache/turbine/torque/TorqueJDBCTransformTask.java	2001/07/10 17:49:00
@@ -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>
+ *  @author <a href="mailto:fedor.karpelevitch@barra.com">Fedor Karpelevitch</a>
  *  @version $Id: TorqueJDBCTransformTask.java,v 1.4.4.7 2001/06/04 23:53:27 jvanzyl 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();
     }
 }
