You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/09/12 17:04:00 UTC

cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder AxionBuilder.java HsqlDbBuilder.java Db2Builder.java MySqlBuilder.java SqlBuilderFactory.java SqlBuilder.java OracleBuilder.java MSSqlBuilder.java SybaseBuilder.java

jstrachan    2002/09/12 08:04:00

  Modified:    sql/src/java/org/apache/commons/sql/builder
                        MySqlBuilder.java SqlBuilderFactory.java
                        SqlBuilder.java OracleBuilder.java
                        MSSqlBuilder.java SybaseBuilder.java
  Added:       sql/src/java/org/apache/commons/sql/builder
                        AxionBuilder.java HsqlDbBuilder.java
                        Db2Builder.java
  Log:
  Patched SqlBuilder so that its base its very SQL92-like and then just extended for enhancements and non standard database extensions.
  
  Also added SqlBuilder implementations for Axion, HsqlDb and DB2
  
  Revision  Changes    Path
  1.3       +1 -1      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MySqlBuilder.java
  
  Index: MySqlBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MySqlBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MySqlBuilder.java	10 Sep 2002 13:49:44 -0000	1.2
  +++ MySqlBuilder.java	12 Sep 2002 15:03:59 -0000	1.3
  @@ -90,6 +90,6 @@
       }
       
       protected void printAutoIncrementColumn() throws IOException { 
  -        print( "AUTO_INCREMENT " );
  +        print( "AUTO_INCREMENT" );
       }
   }
  
  
  
  1.3       +15 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilderFactory.java
  
  Index: SqlBuilderFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilderFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SqlBuilderFactory.java	11 Sep 2002 15:24:38 -0000	1.2
  +++ SqlBuilderFactory.java	12 Sep 2002 15:03:59 -0000	1.3
  @@ -63,6 +63,7 @@
   
   import java.io.IOException;
   import java.io.Writer;
  +import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.List;
  @@ -111,6 +112,17 @@
           return null;
       }
   
  +    /**
  +     * @return a List of currently registered database types for which there is a
  +     * specific SqlBuilder.
  +     */
  +    public static synchronized List getDatabaseTypes() {
  +        // return a copy to prevent modification
  +        List answer = new ArrayList();
  +        answer.addAll( databases.keySet());
  +        return answer;
  +    }
  +
   
       /**
        * Register the common builders
  @@ -123,6 +135,9 @@
        * Register the common builders
        */
       protected static void registerDatabases() {
  +        registerDatabase("axion", AxionBuilder.class);
  +        registerDatabase("db2", Db2Builder.class);
  +        registerDatabase("hsqldb", HsqlDbBuilder.class);
           registerDatabase("mssql", MSSqlBuilder.class);
           registerDatabase("mysql", MySqlBuilder.class);
           registerDatabase("oracle", OracleBuilder.class);
  
  
  
  1.5       +53 -12    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SqlBuilder.java	11 Sep 2002 15:24:38 -0000	1.4
  +++ SqlBuilder.java	12 Sep 2002 15:03:59 -0000	1.5
  @@ -109,6 +109,9 @@
       /** Whether or not foreign key constraints are embedded inside the create table statement */
       private boolean foreignKeysEmbedded;
   
  +    /** Should foreign key constraints be explicitly named */
  +    private boolean foreignKeyConstraintsNamed;
  +    
       public SqlBuilder() {
       }
   
  @@ -116,10 +119,19 @@
        * Outputs the DDL required to drop and recreate the database 
        */
       public void createDatabase(Database database) throws IOException {
  +        createDatabase(database, true);
  +    }
  +
  +    /**
  +     * Outputs the DDL required to drop and recreate the database 
  +     */
  +    public void createDatabase(Database database, boolean dropTable) throws IOException {
           for (Iterator iter = database.getTables().iterator(); iter.hasNext(); ) {
               Table table = (Table) iter.next();
               tableComment(table);
  -            dropTable(table);
  +            if (dropTable) {
  +                dropTable(table);
  +            }
               createTable(table);
           }
       }
  @@ -196,6 +208,7 @@
           else {
               print("NULL");
           }
  +        print(" ");
           if (column.isAutoIncrement()) {
               printAutoIncrementColumn();
           }
  @@ -268,6 +281,27 @@
           this.foreignKeysEmbedded = foreignKeysEmbedded;
       }
   
  +
  +
  +    /**
  +     * Returns whether foreign key constraints should be named when they are embedded inside
  +     * a create table clause.
  +     * @return boolean
  +     */
  +    public boolean isForeignKeyConstraintsNamed() {
  +        return foreignKeyConstraintsNamed;
  +    }
  +
  +    /**
  +     * Sets whether foreign key constraints should be named when they are embedded inside
  +     * a create table clause.
  +     * @param foreignKeyConstraintsNamed The foreignKeyConstraintsNamed to set
  +     */
  +    public void setForeignKeyConstraintsNamed(boolean foreignKeyConstraintsNamed) {
  +        this.foreignKeyConstraintsNamed = foreignKeyConstraintsNamed;
  +    }
  +
  +
       // Implementation methods
       //-------------------------------------------------------------------------                
   
  @@ -364,13 +398,16 @@
                   println(",");
   
                   printIndent();
  -                print("CONSTRAINT ");
  -                print(table.getName());
  -                print("_FK_");
  -                print(Integer.toString(++counter));
  -                print(" FOREIGN KEY (");
  +                
  +                if (isForeignKeyConstraintsNamed()) {
  +                    print("CONSTRAINT ");
  +                    print(table.getName());
  +                    print("_FK_");
  +                    print(Integer.toString(++counter));
  +                    print(" " );
  +                }
  +                print("FOREIGN KEY (");
                   writeLocalReferences(key);
  -                print(key.getForeignTable());
                   println(")");
   
                   printIndent();
  @@ -458,9 +495,14 @@
       /**
        * Prints an SQL comment to the current stream
        */
  -    protected void printComment(String text) throws IOException {
  -        print("# ");
  -        println(text);
  +    protected void printComment(String text) throws IOException { 
  +        print( "--" );
  +        
  +       // MySql insists on a space after the first 2 dashes.
  +       // http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#Comments
  +       // dunno if this is a common thing
  +        print(" ");
  +        println( text );
       }
   
       /** 
  @@ -505,8 +547,7 @@
        * Outputs the fact that this column is an auto increment column.
        */ 
       protected void printAutoIncrementColumn() throws IOException {
  +        print( "IDENTITY" );
       }
  -
  -
   
   }
  
  
  
  1.3       +8 -1      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/OracleBuilder.java
  
  Index: OracleBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/OracleBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- OracleBuilder.java	10 Sep 2002 13:49:44 -0000	1.2
  +++ OracleBuilder.java	12 Sep 2002 15:03:59 -0000	1.3
  @@ -81,6 +81,7 @@
       
       public OracleBuilder() {
           setPrimaryKeyEmbedded(false);
  +        setForeignKeyConstraintsNamed(true);
       }
       
       public void dropTable(Table table) throws IOException { 
  @@ -89,12 +90,18 @@
           print( " CASCADE CONSTRAINTS" );
           printEndOfStatement();
       }
  -    
  +
  +    // there's no real need to print comments like this, just preserving
  +    // backwards compatibility with the old Torque Velocity scripts
       protected void printComment(String text) throws IOException { 
           print( "--" );
           if (! text.startsWith( "-" ) ) {
               print(" ");
           }                
           println( text );
  +    }
  +    
  +    protected void printAutoIncrementColumn() throws IOException { 
  +        //print( "AUTO_INCREMENT" );
       }
   }
  
  
  
  1.3       +6 -0      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MSSqlBuilder.java
  
  Index: MSSqlBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MSSqlBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MSSqlBuilder.java	10 Sep 2002 13:49:44 -0000	1.2
  +++ MSSqlBuilder.java	12 Sep 2002 15:03:59 -0000	1.3
  @@ -81,6 +81,7 @@
   public class MSSqlBuilder extends SqlBuilder {
       
       public MSSqlBuilder() {
  +        setForeignKeyConstraintsNamed(true);
       }
       
       public void dropTable(Table table) throws IOException { 
  @@ -128,6 +129,11 @@
           println( "     DROP TABLE " + tableName );
           print( "END" );
           printEndOfStatement();
  +    }
  +    
  +    protected void printComment(String text) throws IOException {
  +        print("# ");
  +        println(text);
       }
       
       protected void printAutoIncrementColumn() throws IOException { 
  
  
  
  1.3       +5 -0      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SybaseBuilder.java
  
  Index: SybaseBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SybaseBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SybaseBuilder.java	10 Sep 2002 13:49:44 -0000	1.2
  +++ SybaseBuilder.java	12 Sep 2002 15:03:59 -0000	1.3
  @@ -81,6 +81,7 @@
   public class SybaseBuilder extends SqlBuilder {
       
       public SybaseBuilder() {
  +        setForeignKeyConstraintsNamed(true);
       }
       
       public void dropTable(Table table) throws IOException { 
  @@ -115,5 +116,9 @@
           print( "/* " );
           print( text );
           println( " */" );
  +    }
  +    
  +    protected void printAutoIncrementColumn() throws IOException { 
  +        //print( "AUTO_INCREMENT" );
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/AxionBuilder.java
  
  Index: AxionBuilder.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   */
  
  package org.apache.commons.sql.builder;
  
  import java.io.IOException;
  import java.io.Writer;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.sql.model.Column;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.model.Table;
  
  /**
   * An SQL Builder for the <a href="http://axion.tigris.org/">Axion</a> JDBC database.
   * 
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.14 $
   */
  public class AxionBuilder extends SqlBuilder {
      
      public AxionBuilder() {
          setForeignKeysEmbedded(true);
      }    
  }
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/HsqlDbBuilder.java
  
  Index: HsqlDbBuilder.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   */
  
  package org.apache.commons.sql.builder;
  
  import java.io.IOException;
  import java.io.Writer;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.sql.model.Column;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.model.Table;
  
  /**
   * An SQL Builder for the <a href="http://hsqldb.sourceforge.net/">HsqlDb</a> JDBC database.
   * This builder was primarily written to be used as another test case. If you want an open source,
   * non-GPL pure Java JDBC implementation we highly recommend you try
   * <a href="http://axion.tigris.org/">Axion</a> instead.
   * 
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.14 $
   */
  public class HsqlDbBuilder extends SqlBuilder {
      
      public HsqlDbBuilder() {
          setForeignKeysEmbedded(true);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/Db2Builder.java
  
  Index: Db2Builder.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   */
  
  package org.apache.commons.sql.builder;
  
  import java.io.IOException;
  import java.io.Writer;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.sql.model.Column;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.model.Table;
  
  /**
   * An SQL Builder for Oracle
   * 
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.14 $
   */
  public class Db2Builder extends SqlBuilder {
      
      public Db2Builder() {
          setPrimaryKeyEmbedded(false);
          setForeignKeyConstraintsNamed(true);
      }
      
      public void dropTable(Table table) throws IOException { 
          super.dropTable(table);
          print( "drop sequence if exists  " );
          print( table.getName() );
          print( ".SequenceName" );
          printEndOfStatement();
      }
      
      protected void printAutoIncrementColumn() throws IOException { 
          print( "GENERATED ALWAYS AS IDENTITY" );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>