commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder SqlBuilder.java HsqlDbBuilder.java
Date Thu, 15 Jul 2004 21:45:57 GMT
tomdz       2004/07/15 14:45:56

  Modified:    sql/src/java/org/apache/commons/sql/builder SqlBuilder.java
                        HsqlDbBuilder.java
  Log:
  Moved foreignkey generation at the end of the generated sql (for dbs that want external
foreignkeys) to avoid references to yet-undefined tables
  
  Revision  Changes    Path
  1.17      +26 -5     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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- SqlBuilder.java	2 Mar 2004 13:18:31 -0000	1.16
  +++ SqlBuilder.java	15 Jul 2004 21:45:56 -0000	1.17
  @@ -19,6 +19,7 @@
   import java.io.Writer;
   import java.sql.Connection;
   import java.sql.SQLException;
  +import java.util.ArrayList;
   import java.util.Iterator;
   import java.util.List;
   
  @@ -111,6 +112,10 @@
               tableComment(table);
               createTable(table);
           }
  +        // we're writing the foreignkeys last to ensure that all referenced tables are
already defined
  +        for (Iterator iter = database.getTables().iterator(); iter.hasNext(); ) {
  +            createExternalForeignKey((Table) iter.next());
  +        }
       }
   
       /**
  @@ -147,7 +152,7 @@
       }
   
       /** 
  -     * Outputs the DDL to create the table along with any constraints
  +     * Outputs the DDL to create the table along with any non-external constraints
        */
       public void createTable(Table table) throws IOException {
           print("create table ");
  @@ -172,15 +177,21 @@
           if (!isPrimaryKeyEmbedded()) {
               writePrimaryKeysAlterTable(table);
           }
  -        if (!isForeignKeysEmbedded()) {
  -            writeForeignKeysAlterTable(table);
  -        }
           if (!isIndexesEmbedded()) {
               writeIndexes(table);
           }
       }
   
       /** 
  +     * Creates an external foreignkey definition.
  +     */
  +    public void createExternalForeignKey(Table table) throws IOException {
  +        if (!isForeignKeysEmbedded()) {
  +            writeForeignKeysAlterTable(table);
  +        }
  +    }
  +
  +    /** 
        * Outputs the DDL to add a column to a table.
        */
       public void createColumn(Table table, Column column) throws IOException {
  @@ -698,7 +709,8 @@
        */
       public void alterDatabase(Database desiredDb, Connection cn, boolean doDrops, boolean
modifyColumns) throws IOException, SQLException {
   
  -        Database currentDb = new JdbcModelReader(cn).getDatabase();
  +        Database  currentDb      = new JdbcModelReader(cn).getDatabase();
  +        ArrayList deferredTables = new ArrayList();
   
           for (Iterator iter = desiredDb.getTables().iterator(); iter.hasNext(); ) {
               Table desiredTable = (Table) iter.next();
  @@ -711,6 +723,8 @@
               if ( currentTable == null ) {
                   log.info( "creating table " + desiredTable.getName() );
                   createTable( desiredTable );
  +                // we're deferring foreignkey generation
  +                deferredTables.add(desiredTable);
               } else {
                   //add any columns, indices, or constraints
   
  @@ -797,6 +811,13 @@
   
               } //table exists?
           } //for tables create
  +
  +        // generating deferred foreignkeys
  +        for (Iterator iter = deferredTables.iterator(); iter.hasNext();)
  +        {
  +            createExternalForeignKey((Table)iter.next());
  +        }
  +        
   
           //check for table drops
           for (Iterator iter = currentDb.getTables().iterator(); iter.hasNext(); ) {
  
  
  
  1.5       +0 -4      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/HsqlDbBuilder.java
  
  Index: HsqlDbBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/HsqlDbBuilder.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HsqlDbBuilder.java	28 Feb 2004 03:35:47 -0000	1.4
  +++ HsqlDbBuilder.java	15 Jul 2004 21:45:56 -0000	1.5
  @@ -27,8 +27,4 @@
    * @version $Revision$
    */
   public class HsqlDbBuilder extends SqlBuilder {
  -    
  -    public HsqlDbBuilder() {
  -        setForeignKeysEmbedded(true);
  -    }
   }
  
  
  

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


Mime
View raw message