Martin Poeschl wrote:

the unit tests!!
i don't want to check in anything which could break the current stuff before the 3.0 release is done ...
ok...here are the latest u-diffs and the output of the unit tests...
C:\java\projects\jakarta-turbine-torque\jakarta-turbine-torque>maven java:jar
 __  __
|  \/  |__ Jakarta _ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|   v. 1.0-beta-7

C:\java\projects\jakarta-turbine-torque\jakarta-turbine-torque

java:prepare-filesystem:

java:compile:
    [javac] Compiling 26 source files to C:\java\projects\jakarta-turbine-torque
\jakarta-turbine-torque\target\classes

java:jar-resources:

test:prepare-filesystem:

test:test-resources:

test:compile:

test:test:
    [junit] dir attribute ignored if running in the same VM
    [junit] Running org.apache.torque.engine.database.model.NameFactoryTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0,24 sec
    [junit] dir attribute ignored if running in the same VM
    [junit] Running org.apache.torque.om.ComboKeyTest
    [junit] Tests run: 9, Failures: 0, Errors: 0, Time elapsed: 0,03 sec
    [junit] dir attribute ignored if running in the same VM
    [junit] Running org.apache.torque.om.NumberKeyTest
    [junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0,02 sec
    [junit] dir attribute ignored if running in the same VM
    [junit] Running org.apache.torque.util.CriteriaTest
    [junit] Tests run: 10, Failures: 0, Errors: 0, Time elapsed: 1,032 sec

java:jar:
    [jar] Building jar: C:\java\projects\jakarta-turbine-torque\jakarta-turbine-
torque\target\torque-3.0-rc2.jar

BUILD SUCCESSFUL
Total time:  23 seconds
Index: ./src/java/org/apache/torque/engine/database/transform/SQLToAppData.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/engine/database/transform/SQLToAppData.java,v
retrieving revision 1.7
diff -u -r1.7 SQLToAppData.java
--- ./src/java/org/apache/torque/engine/database/transform/SQLToAppData.java    8 Oct 
2002 18:17:06 -0000       1.7
+++ ./src/java/org/apache/torque/engine/database/transform/SQLToAppData.java    29 Nov 
+2002 16:47:41 -0000
@@ -64,6 +64,8 @@
 import org.apache.torque.engine.database.model.Database;
 import org.apache.torque.engine.database.model.ForeignKey;
 import org.apache.torque.engine.database.model.Table;
+import org.apache.torque.engine.database.model.Unique;
+import org.apache.torque.engine.database.model.Index;
 import org.apache.torque.engine.sql.ParseException;
 import org.apache.torque.engine.sql.SQLScanner;
 import org.apache.torque.engine.sql.Token;
@@ -74,9 +76,12 @@
  * structure.  The class makes use of SQL Scanner to get
  * sql tokens and the parses these to create the AppData
  * class. SQLToAppData is in effect a simplified sql parser.
- *
+ * It supports CREATE INDEXE, ALTER TABLE, the UNIQUE keyword
+ * and CONSTRAINTS in CREATE TABLE
+ * TODO: does not recognize long varchar as type (maybe assign it to VARCHAR without 
+length)
  * @author <a href="mailto:[EMAIL PROTECTED]";>Leon Messerschmidt</a>
  * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Andreou</a>
  * @version $Id: SQLToAppData.java,v 1.7 2002/10/08 18:17:06 mpoeschl Exp $
  */
 public class SQLToAppData
@@ -176,7 +181,7 @@
     }
 
     /**
-     * Parses a CREATE TABLE FOO command.
+     * Parses a CREATE TABLE/INDEX FOO command.
      *
      * @throws ParseException
      */
@@ -187,6 +192,10 @@
         {
             Create_Table();
         }
+        if (token.getStr().toUpperCase().equals("INDEX"))
+        {
+            Create_Index();
+        }
     }
 
     /**
@@ -247,6 +256,11 @@
         {
             Create_Table_Column_Unique(tbl);
         }
+        else if(token.getStr().toUpperCase().equals("CONSTRAINT"))
+        {
+            next();
+            Add_Constraint(tbl);
+        }
         else
         {
             Create_Table_Column_Data(tbl);
@@ -299,7 +313,7 @@
     }
 
     /**
-     * Parses UNIQUE (NAME,FOO,BAR) statement
+     * Parses UNIQUE (NAME) statement
      */
     private void Create_Table_Column_Unique(Table tbl) throws ParseException
     {
@@ -319,7 +333,7 @@
                 {
                     err("Invalid column name: " + colName);
                 }
-                c.setUnique(true);
+                Add_Unique(tbl,c,null);
             }
             next();
         }
@@ -515,7 +529,7 @@
                 }
                 else if (token.getStr().toUpperCase().equals("UNIQUE"))
                 {
-                    col.setUnique(true);
+                    Add_Unique(tbl,col,null);
                     next();
                 }
                 else if (token.getStr().toUpperCase().equals("NULL"))
@@ -528,6 +542,11 @@
                     col.setAutoIncrement(true);
                     next();
                 }
+                else if(token.getStr().toUpperCase().equals("CHECK"))
+                {
+                        Add_Check(col);
+                }
+
                 else if (token.getStr().toUpperCase().equals("DEFAULT"))
                 {
                     next();
@@ -548,6 +567,335 @@
     }
 
     /**
+     * Parses an ALTER FOO command.
+     *
+     * @throws ParseException
+     */
+    private void Alter() throws ParseException
+    {
+        next();
+        if (token.getStr().toUpperCase().equals("TABLE"))
+        {
+            Alter_Table();
+        }
+    }
+
+    /**
+     * Parses an ALTER TABLE FOO command
+     */
+    private void Alter_Table() throws ParseException
+    {
+        next();
+        String tableName = token.getStr(); // name of the table
+        next();
+        if (!token.getStr().equals("ADD"))
+        {
+            err("ADD expected");
+        }
+        next();
+
+        Table tbl=appDataDB.getTable(tableName);
+        if (tbl==null)
+        {
+            err("Invalid table name: " + tableName);
+        }
+
+        //tbl.setIdMethod("none");
+        if (token.getStr().toUpperCase().equals("CONSTRAINT"))
+        {
+                next();
+                Add_Constraint(tbl);
+        }
+        else if (token.getStr().equals("("))
+        {
+                next();
+                if (token.getStr().toUpperCase().equals("CONSTRAINT"))
+                {
+                    next();
+                }
+                Add_Constraint(tbl);
+                next(); // skip the )
+        }
+    }
+
+    /**
+     * Parses a CONSTRAINT command
+     */
+    private void Add_Constraint(Table tbl) throws ParseException
+    {
+        String allConstraints="UNIQUE*PRIMARY*FOREIGN";
+        String str=token.getStr();
+        String constraintName=null;
+
+        if (allConstraints.indexOf(str.toUpperCase())<0)
+        {
+            constraintName=str;
+            next();
+            str=token.getStr();
+        }
+
+        if (str.toUpperCase().equals("UNIQUE"))
+        {
+            Add_Constraint_Unique(tbl,constraintName);
+        }
+
+        if (str.toUpperCase().equals("PRIMARY"))
+        {
+            Add_Constraint_Primary(tbl,constraintName);
+        }
+
+        if (str.toUpperCase().equals("FOREIGN"))
+        {
+            Add_Constraint_Foreign(tbl,constraintName);
+        }
+    }
+
+    /**
+     * Parses a CONSTRAINT FOREIGN KEY command
+     */
+    private void Add_Constraint_Foreign(Table tbl,String constrName) throws 
+ParseException
+    {
+        if (constrName==null)
+            constrName="FK_UNDEF_"+token.getLine()+"_"+token.getCol();
+
+        next();
+        if (!token.getStr().toUpperCase().equals("KEY"))
+        {
+            err("KEY expected");
+        }
+        next();
+        if (!token.getStr().equals("("))
+        {
+            err("( expected");
+        }
+        next();
+
+        String colName = token.getStr();
+        Column c=tbl.getColumn(colName);
+        if (c == null)
+        {
+            err("Invalid column name: " + colName);
+        }
+
+        next();
+        if (!token.getStr().equals(")"))
+        {
+            err(") expected");
+        }
+        next();
+        if (!token.getStr().toUpperCase().equals("REFERENCES"))
+        {
+            err("REFERENCES expected");
+        }
+        next();
+
+        String referenceTable=token.getStr();
+        Table refTbl=appDataDB.getTable(referenceTable);
+        if (refTbl==null)
+        {
+            err("Invalid table name: "+referenceTable);
+        }
+        next();
+        String refColumn=colName;
+        if (token.getStr().equals("("))
+        {
+            next();
+            refColumn=token.getStr();
+            next();
+            if (!token.getStr().equals(")"))
+            {
+                err(") expected");
+            }
+        }
+
+        Column refC=refTbl.getColumn(refColumn);
+        if (refC==null)
+        {
+            err("Invalid column name: " + refColumn);
+        }
+
+        ForeignKey fk=new ForeignKey();
+
+        if (!token.getStr().equals(")"))
+        {
+            if (!token.getStr().toUpperCase().equals("ON"))
+            {
+                err("ON expected");
+            }
+            next();
+            if (token.getStr().toUpperCase().equals("DELETE"))
+            {
+                next();
+                String onDelete=token.getStr();
+                fk.setOnDelete(onDelete);
+                next();
+            }
+            else if (token.getStr().toUpperCase().equals("UPDATE"))
+            {
+                next();
+                String onUpdate=token.getStr();
+                fk.setOnUpdate(onUpdate);
+                next();
+            }
+        }
+        //next(); // skip the )
+
+        fk.setName(constrName);
+        fk.setForeignTableName(referenceTable);
+        fk.addReference(colName,refColumn);
+        tbl.addForeignKey(fk);
+    }
+
+    /**
+     * Parses a CONSTRAINT PRIMARY KEY command
+     */
+    private void Add_Constraint_Primary(Table tbl,String constrName) throws 
+ParseException
+    {
+        if (constrName==null)
+            constrName="PK_UNDEF_"+token.getLine()+"_"+token.getCol();
+
+        next();
+        if (!token.getStr().toUpperCase().equals("KEY"))
+        {
+            err("KEY expected");
+        }
+        next();
+        if (!token.getStr().equals("("))
+        {
+            err("( expected");
+        }
+        next();
+        String colName;
+        while (!(colName = token.getStr()).equals(")"))
+        {
+            Column c=tbl.getColumn(colName);
+            if (c == null)
+            {
+                err("Invalid column name: " + colName);
+            }
+            c.setPrimaryKey(true);
+            next();
+            if (token.getStr().equals(","))
+                next();
+        }
+        if (!token.getStr().equals(")"))
+        {
+            err(") expected");
+        }
+        next(); // skip the )
+    }
+
+    /**
+     * Parses a CONSTRAINT UNIQUE command
+     */
+    private void Add_Constraint_Unique(Table tbl,String constrName) throws 
+ParseException
+    {
+        /*if (constrName==null)
+            constrName="UNIQUE_UNDEF_"+token.getLine()+"_"+token.getCol();*/
+
+        next();
+        if (!token.getStr().equals("("))
+        {
+            err("( expected");
+        }
+        next();
+        String colName=token.getStr();
+        Column c=tbl.getColumn(colName);
+        if (c == null)
+        {
+            err("Invalid column name: " + colName);
+        }
+        Add_Unique(tbl,c,constrName);
+        next();
+        if (!token.getStr().toUpperCase().equals(")"))
+        {
+            err(") expected");
+        }
+        next(); // skip the )
+    }
+
+    /**
+     * Parses a CHECK command in CREATE TABLE.
+     * For now, just skip to the end of the check command.
+     */
+    private void Add_Check(Column col) throws ParseException
+    {
+        next();
+        int iCount=0;
+        while (!token.getStr().equals(";"))
+        {
+            if (token.getStr().equals("("))
+                iCount++;
+            else if (token.getStr().equals(")"))
+                iCount--;
+            next();
+            if (iCount==0)
+            {
+                //next();
+                return;
+            }
+        }
+    }
+
+    /**
+     * Mark a column as unique
+     */
+    private void Add_Unique(Table tbl,Column col,String constrName)
+    {
+        col.setUnique(true);
+        Unique unique=new Unique();
+        unique.setName(constrName);
+        unique.addColumn(col);
+        tbl.addUnique(unique);
+    }
+
+    /**
+     * Parses a CREATE INDEX command
+     */
+    private void Create_Index() throws ParseException
+    {
+        next();
+        String indexName = token.getStr(); // name of the index
+        next();
+        if (!token.getStr().toUpperCase().equals("ON"))
+        {
+            err("on expected");
+        }
+        next();
+        String tableName = token.getStr(); // name of the table
+        next();
+        if (!token.getStr().equals("("))
+        {
+            err("( expected");
+        }
+        next();
+        String columnName = token.getStr(); // name of the column
+        next();
+        String indexType="";
+        if (!token.getStr().equals(")"))
+        {
+            indexType=token.getStr();
+            next();
+        }
+        next(); // skip )
+
+        Table tbl=appDataDB.getTable(tableName);
+        if (tbl==null)
+        {
+            err("Invalid table name: "+tableName);
+        }
+        Column col=tbl.getColumn(columnName);
+        if (col==null)
+        {
+            err("Invalid column name: "+columnName);
+        }
+        Index index=new Index();
+        index.setName(indexName);
+        index.addColumn(col);
+        tbl.addIndex(index);
+    }
+
+    /**
      * Execute the parser.
      */
     public AppData execute() throws IOException, ParseException
@@ -575,6 +923,10 @@
             if (token.getStr().toUpperCase().equals("CREATE"))
             {
                 Create();
+            }
+            if (token.getStr().toUpperCase().equals("ALTER"))
+            {
+                Alter();
             }
             if (hasTokens())
             {

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to