commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jvan...@apache.org
Subject cvs commit: jakarta-commons-sandbox/sql/src/test-input datamodel.xml
Date Fri, 20 Sep 2002 20:34:39 GMT
jvanzyl     2002/09/20 13:34:38

  Modified:    sql/src/java/org/apache/commons/sql/builder SqlBuilder.java
               sql/src/java/org/apache/commons/sql/model Table.java
               sql/src/test/org/apache/commons/sql
                        TestDataModelRoundTrip.java
               sql/src/test/org/apache/commons/sql/builder TestBuilder.java
               sql/src/test-input datamodel.xml
  Log:
  o Applying John Thorhauer's patches for Postgresql:
  
  I have attatched a file that includes the patches that add support for
  Postgres and indexes along with unit tests for the commons-sql package.
  
  Revision  Changes    Path
  1.8       +76 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java
  
  Index: SqlBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SqlBuilder.java	16 Sep 2002 14:50:19 -0000	1.7
  +++ SqlBuilder.java	20 Sep 2002 20:34:38 -0000	1.8
  @@ -69,6 +69,8 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  +import org.apache.commons.sql.model.Index;
  +import org.apache.commons.sql.model.IndexColumn;
   import org.apache.commons.sql.model.Column;
   import org.apache.commons.sql.model.Database;
   import org.apache.commons.sql.model.ForeignKey;
  @@ -109,6 +111,9 @@
       /** Whether or not foreign key constraints are embedded inside the create table statement
*/
       private boolean foreignKeysEmbedded;
   
  +    /** Whether or not indexes are embedded inside the create table statement */
  +    private boolean indexesEmbedded;
  +
       /** Should foreign key constraints be explicitly named */
       private boolean foreignKeyConstraintsNamed;
   
  @@ -201,6 +206,9 @@
           if (isForeignKeysEmbedded()) {
               writeForeignKeys(table);
           }
  +        if (isIndexesEmbedded()) {
  +            writeEmbeddedIndexes(table);
  +        }
           println();
           print(")");
           printEndOfStatement();
  @@ -211,6 +219,9 @@
           if (!isForeignKeysEmbedded()) {
               writeForeignKeysAlterTable(table);
           }
  +        if (!isIndexesEmbedded()) {
  +            writeIndexes(table);
  +        }
       }
   
       /** 
  @@ -302,7 +313,23 @@
           this.foreignKeysEmbedded = foreignKeysEmbedded;
       }
   
  +    /**
  +     * @return whether the indexes are embedded in the create 
  +     * table clause or as seperate statements.
  +     * The default is false.
  +     */
  +    public boolean isIndexesEmbedded() {
  +        return indexesEmbedded;
  +    }
   
  +    /**
  +     * Sets whether the indexes are embedded in the create 
  +     * table clause or as seperate statements.
  +     * The default is false.
  +     */
  +    public void setIndexesEmbedded(boolean indexesEmbedded) {
  +        this.indexesEmbedded = indexesEmbedded;
  +    }
   
       /**
        * Returns whether foreign key constraints should be named when they are embedded inside
  @@ -504,6 +531,55 @@
                   printEndOfStatement();
               }
           }
  +    }
  +
  +    /**
  +     * Writes the indexes.
  +     */
  +    protected void writeIndexes(Table table) throws IOException{
  +        for (Iterator indexIter = table.getIndexes().iterator();
  +            indexIter.hasNext();
  +            ) {
  +            Index index = (Index) indexIter.next();
  +            if (index.getName() == null) {
  +                log.warn( "Index Name is null for index: " + index);
  +            }
  +            else {
  +                print("CREATE INDEX ");
  +                print(index.getName());
  +                print(" ON ");
  +                print(table.getName());
  +
  +                print(" (");
  +                
  +                for (Iterator idxColumnIter = index.getIndexColumns().iterator();
  +                    idxColumnIter.hasNext();
  +                    ) 
  +                {
  +                    IndexColumn idxColumn = (IndexColumn)idxColumnIter.next();
  +                    if (idxColumnIter.hasNext())
  +                    {
  +                        print(idxColumn.getName() + ", ");             
  +                    }
  +                    else
  +                    {
  +                        print(idxColumn.getName());
  +                    }
  +                }
  +
  +                print(")");
  +                printEndOfStatement();
  +            }
  +        }
  +    }
  +
  +
  +    /**
  +     * Writes the indexes embedded within the create table statement. not
  +     * yet implemented
  +     */
  +    protected void writeEmbeddedIndexes(Table table) throws IOException 
  +    {
       }
   
       /**
  
  
  
  1.5       +17 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java
  
  Index: Table.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Table.java	16 Sep 2002 15:18:29 -0000	1.4
  +++ Table.java	20 Sep 2002 20:34:38 -0000	1.5
  @@ -72,6 +72,8 @@
       private List columns = new ArrayList();
       
       private List foreignKeys = new ArrayList();
  +
  +    private List indexes = new ArrayList();
       
       public Table() 
       {
  @@ -117,6 +119,21 @@
           return (ForeignKey) foreignKeys.get(index);
       }        
       
  +    public void addIndex(Index index)
  +    {
  +        indexes.add(index);
  +    }
  +    
  +    public List getIndexes()
  +    {
  +        return indexes;
  +    }
  +
  +    public Index getIndex(int index)
  +    {
  +        return (Index) indexes.get(index);
  +    }
  +
       
       // Helper methods
       //-------------------------------------------------------------------------       
        
  
  
  
  1.4       +37 -10    jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/TestDataModelRoundTrip.java
  
  Index: TestDataModelRoundTrip.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/TestDataModelRoundTrip.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestDataModelRoundTrip.java	12 Sep 2002 17:56:50 -0000	1.3
  +++ TestDataModelRoundTrip.java	20 Sep 2002 20:34:38 -0000	1.4
  @@ -86,37 +86,64 @@
               assertTrue("Parsed a Database object", database != null);
               assertEquals("bookstore", database.getName());
               
  -            assertTrue("more than one table found", database.getTables().size() > 0
);
  +            assertTrue("More that one table should be found", 
  +                        database.getTables().size() > 1 );
               
               // Test our first table which is the 'book' table
               Table t1 = database.getTable(1);
               assertEquals("book", t1.getName());
               
  +            assertTrue("book table does not have primary", t1.hasPrimaryKey());
  +
  +            Index idx1 = (Index)t1.getIndex(0);
  +            assertTrue("Did not find an index", idx1 != null);
  +
  +            ForeignKey key = (ForeignKey) t1.getForeignKey(0);
  +            assertTrue("Did not find a foreign key", key != null);
  +
               Column c0 = t1.getColumn(0);
               assertEquals("book_id", c0.getName());
  -            assertTrue("book_id is required", c0.isRequired());
  -            assertTrue("book_id is primary key", c0.isPrimaryKey());
  +            assertTrue("book_id should be required", c0.isRequired());
  +            assertTrue("book_id should be primary key", c0.isPrimaryKey());
               
               Column c1 = t1.getColumn(1);
               assertEquals("isbn", c1.getName());
  -            assertTrue("isbn is required", c1.isRequired());
  -            assertTrue("isbn is not primary key", ! c1.isPrimaryKey());
  +            assertTrue("isbn should be required", c1.isRequired());
  +            assertTrue("isbn should not be primary key but is", 
  +                        ! c1.isPrimaryKey());
   
               List keyList1 = t1.getForeignKeys();
               assertEquals( "Foreign key count", 1, keyList1.size() );
               
               ForeignKey key0 = (ForeignKey) keyList1.get(0);
  -            assertEquals("foreignTable is correct", "author", key0.getForeignTable());
  +            assertEquals("foreignTable value correct", "author", 
  +                        key0.getForeignTable());
               
               List refList1 = key0.getReferences();
  -            assertEquals( "Reference count", 1, refList1.size() );
  +            assertEquals( "Reference count not correct", 1, refList1.size() );
               
               Reference r1 = (Reference) refList1.get(0);
  -            assertTrue("Found a reference", r1 != null);
  +            assertTrue("Could not find a reference", r1 != null);
                           
  -            assertEquals("local is correct", "author_id", r1.getLocal());
  -            assertEquals("foreign is correct", "author_id", r1.getForeign());
  +            assertEquals("local reference is incorrect", "author_id", 
  +                         r1.getLocal());
  +            assertEquals("foreign reference is incorrect", "author_id", 
  +                         r1.getForeign());
               
  +            List idxList = t1.getIndexes();
  +            assertEquals( "Index count", 1, idxList.size() );
  +
  +
  +            Index idx2 = (Index)idxList.get(0);
  +            assertTrue("Did not find an index", idx2 != null);
  +            assertEquals("Index name is incorrect", "book_isbn", idx2.getName());
  +
  +            List idxColumns = idx2.getIndexColumns();
  +            IndexColumn idxColumn = (IndexColumn) idxColumns.get(0);
  +            assertTrue("Did not find an index column", idxColumn != null);
  +            assertEquals("Index column name is incorrect", "isbn", 
  +                          idxColumn.getName());
  +
               // Write out the bean
               //writeBean(database);
           }
  
  
  
  1.5       +38 -1     jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/builder/TestBuilder.java
  
  Index: TestBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/builder/TestBuilder.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestBuilder.java	12 Sep 2002 17:56:49 -0000	1.4
  +++ TestBuilder.java	20 Sep 2002 20:34:38 -0000	1.5
  @@ -13,6 +13,8 @@
   import java.io.IOException;
   import java.io.FileInputStream;
   import java.io.FileWriter;
  +import java.io.StringWriter;
  +import java.io.Writer;
   
   import junit.framework.Test;
   import junit.framework.TestCase;
  @@ -68,8 +70,8 @@
           
           DatabaseReader reader = new DatabaseReader ();
           database = (Database) reader.parse(new FileInputStream(uri));
  -        
           assertTrue("Loaded a valid database", database != null);
  +
       }
   
       /**
  @@ -78,22 +80,57 @@
       public void testBuilders()
           throws Exception
       {
  +
           testBuilder( new AxionBuilder(), "axion.sql" );
           testBuilder( new HsqlDbBuilder(), "hsqldb.sql" );
           testBuilder( new MSSqlBuilder(), "mssql.sql" );        
           testBuilder( new MySqlBuilder(), "mysql.sql" );
           testBuilder( new OracleBuilder(), "oracle.sql" );
  +        testBuilder( new PostgreSqlBuilder(), "postgres.sql" );
           testBuilder( new SybaseBuilder(), "sybase.sql" );
  +
       }
       
  +    /**
  +     * A unit test for JUnit
  +     */
  +    public void testBaseBuilder()
  +        throws Exception
  +    {
  +    
  +        SqlBuilder builder = new SqlBuilder();
  +        StringWriter sw = new StringWriter();
  +        builder.setWriter(sw);
  +        builder.dropDatabase(database);       
  +
  +        String drop = sw.toString();
  +        int bookIdx = drop.indexOf("drop table book");
  +        int authIdx = drop.indexOf("drop table author");
  +
  +        assertTrue("dropDatabase Failed to create proper drop statement for " +
  +                    "book table. Here is the statment created:\n" + drop, 
  +                    bookIdx > 0);
  +        
  +        assertTrue("dropDatabase Failed to create proper drop statement for " +
  +                    "author table. Here is the statment created:\n" + drop,
  +                     authIdx > 0);
  +
  +        Writer wr = builder.getWriter();
  +        assertTrue("Couldnt find writer", wr != null);  
  +
  +
  +    }
  +
       protected void testBuilder(SqlBuilder builder, String fileName) throws Exception 
       {
  +
           String name = baseDir + "/target/" + fileName;
           
           FileWriter writer = new FileWriter( name );
           builder.setWriter( writer );
           builder.createDatabase( database );
           writer.close();
  +
       }
   }
   
  
  
  
  1.4       +8 -3      jakarta-commons-sandbox/sql/src/test-input/datamodel.xml
  
  Index: datamodel.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/test-input/datamodel.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- datamodel.xml	12 Sep 2002 17:56:49 -0000	1.3
  +++ datamodel.xml	20 Sep 2002 20:34:38 -0000	1.4
  @@ -21,9 +21,14 @@
       <column name="author_id" type="INTEGER" required="true"/>
       <column name="title" type="VARCHAR" size="255" required="true"/>
   	    
  -	  <foreign-key foreignTable="author">
  -	    <reference local="author_id" foreign="author_id"/>
  -	  </foreign-key>  
  +    <foreign-key foreignTable="author">
  +    <reference local="author_id" foreign="author_id"/>
  +    </foreign-key>  
  +
  +    <index name="book_isbn">
  +     <index-column name="isbn"/>
  +    </index>
  +              
     </table>
   
   </database>
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message