commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql TestDataModelRoundTrip.java
Date Thu, 12 Sep 2002 17:56:50 GMT
jstrachan    2002/09/12 10:56:50

  Modified:    sql/src/test/org/apache/commons/sql/builder TestBuilder.java
               sql/src/java/org/apache/commons/sql/builder
                        AxionBuilder.java SqlBuilder.java
               sql/src/java/org/apache/commons/sql/dynabean
                        SqlDynaBean.java DynaSql.java
               sql      maven.xml project.xml project.properties
               sql/src/test-input datamodel.xml
               sql/src/test/org/apache/commons/sql
                        TestDataModelRoundTrip.java
  Added:       sql/src/test/org/apache/commons/sql/dynabean
                        TestDynaSql.java
               sql/src/java/org/apache/commons/sql/util
                        DataSourceWrapper.java DDLExecutor.java
                        JdbcSupport.java
  Removed:     sql/src/java/org/apache/commons/sql/dynabean
                        DataSourceDynaSql.java
  Log:
  * Working test cases that demonstrate the use of the SqlBuilder classes showing them working on both Axion and HsqlDb.
  
  * Added a fully working simple database access API using a DynaBean to map to a row in a table. 
  
  * From the DyanSql you configure the Database, give it a DataSource and you can add/update/remove rows via DyanBeans
  
  	DynaBean author = dynaSql.newInstance( "author" );
  	author.set("name", "James");
  	dynaSql.insert(author);
  
  etc.		
  
  * Added a helper class, DDLExecutor which will create databases or drop databases using the relational model and a specific SqlBuilder implementation.
  
  * There's now a simple JdbcSupport helper class which contains some common methods for closing down statements & result sets and working with DataSource instances to pool JDBC connections.
  
  * for testing purposes mostly, there's a DataSourceWrapper that allows regular JDBC Connections to be used inside a DataSource without any pooling.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/dynabean/TestDynaSql.java
  
  Index: TestDynaSql.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.dynabean;
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.FileInputStream;
  import java.io.FileWriter;
  
  import javax.sql.DataSource;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.beanutils.DynaClass;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.commons.logging.impl.SimpleLog;
  
  import org.apache.commons.sql.builder.*;
  import org.apache.commons.sql.dynabean.DynaSql;
  import org.apache.commons.sql.io.DatabaseReader;
  import org.apache.commons.sql.model.*;
  import org.apache.commons.sql.util.DataSourceWrapper;
  import org.apache.commons.sql.util.DDLExecutor;
  
  import org.axiondb.jdbc.AxionDriver;
  
  /**
   * Test harness for the SqlBuilder for various databases.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.3 $
   */
  public class TestDynaSql extends TestCase
  {
      private Database database;
      private String baseDir;
   
      public static void main( String[] args ) 
      {
          TestRunner.run( suite() );
      }
      
      /**
       * A unit test suite for JUnit
       */
      public static Test suite()
      {
          return new TestSuite(TestDynaSql.class);
      }
  
      /**
       * Constructor for the TestDynaSql object
       *
       * @param testName
       */
      public TestDynaSql(String testName)
      {
          super(testName);
      }
  
      /**
       * The JUnit setup method
       */
      protected void setUp() throws Exception
      {
          super.setUp();
          
          baseDir = System.getProperty("basedir", ".");
          String uri = baseDir + "/src/test-input/datamodel.xml";
          
          DatabaseReader reader = new DatabaseReader ();
          database = (Database) reader.parse(new FileInputStream(uri));
          
          assertTrue("Loaded a valid database", database != null);
      }
  
      /**
       * Tests the Axion database
       */
      public void testAxion() throws Exception
      {
          DataSource dataSource = createDataSource(
              "org.axiondb.jdbc.AxionDriver", 
              "jdbc:axiondb:diskdb:target/axiondb"
          );
  
          testDDLExecutor(dataSource, new AxionBuilder() );        
      }
  
      /**
       * Tests the HsqlDb database
       */
      public void testHsqlDb() throws Exception
      {
          DataSource dataSource = createDataSource(
              "org.hsqldb.jdbcDriver", 
              "jdbc:hsqldb:target/hsqldb", 
              "sa", 
              ""
          );
          
          testDDLExecutor(dataSource, new HsqlDbBuilder() );        
      }
  
      /**
       * Creates the database on the given data source with the given SQL builder
       */    
      protected void testDDLExecutor(DataSource dataSource, SqlBuilder builder) throws Exception 
      {
          DDLExecutor executor = new DDLExecutor(dataSource, builder);
          executor.createDatabase(database, true);
          
          testDynaSql(dataSource);
      }
      
      /**
       * Perform some database operations on the data source using DynaBeans
       */    
      protected void testDynaSql(DataSource dataSource) throws Exception 
      {
          // first lets check that the tables are available in our database
          
          assertTrue( "Database contains a table 'author'", database.findTable("author") != null );
          assertTrue( "Database contains a table 'book'", database.findTable("book") != null );
          
          DynaSql dynaSql = new DynaSql(dataSource, database);
  
          DynaBean author = dynaSql.newInstance("author");
  
          assertTrue("Found an author", author != null);
          
          author.set("author_id", new Integer(1));
          author.set("name", "Oscar Wilde");
          dynaSql.insert(author);
          
          System.out.println( "Inserted author: " + author );
          
          DynaBean book = dynaSql.newInstance("book");
          
          assertTrue("Found an book", book != null);
          
          book.set("author_id", new Integer(1));
          book.set("isbn", "ISBN-ABCDEF");
          book.set("title", "The Importance of being Earnest");
          dynaSql.insert(book);
          
          System.out.println( "Inserted book: " + book );
      }
  
      /**
       * Creates a new DataSource for the given JDBC URI
       */    
      protected DataSource createDataSource(String className, String connectURL) throws Exception 
      {
          return createDataSource(className, connectURL, null, null);
      }
      
      /**
       * Creates a new DataSource for the given JDBC URI
       */    
      protected DataSource createDataSource(String className, String connectURL, String userName, String password) throws Exception 
      {
          DataSourceWrapper wrapper = new DataSourceWrapper();
          wrapper.setDriverClassName(className);
          wrapper.setJdbcURL(connectURL);
          wrapper.setUserName(userName);
          wrapper.setPassword(password);
          return wrapper;
      }
  }
  
  
  
  
  1.4       +5 -3      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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestBuilder.java	10 Sep 2002 15:56:23 -0000	1.3
  +++ TestBuilder.java	12 Sep 2002 17:56:49 -0000	1.4
  @@ -78,10 +78,12 @@
       public void testBuilders()
           throws Exception
       {
  -        testBuilder( new SybaseBuilder(), "sybase.sql" );
  -        testBuilder( new OracleBuilder(), "oracle.sql" );
  -        testBuilder( new MySqlBuilder(), "mysql.sql" );
  +        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 SybaseBuilder(), "sybase.sql" );
       }
       
       protected void testBuilder(SqlBuilder builder, String fileName) throws Exception 
  
  
  
  1.2       +21 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/AxionBuilder.java
  
  Index: AxionBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/AxionBuilder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AxionBuilder.java	12 Sep 2002 15:03:59 -0000	1.1
  +++ AxionBuilder.java	12 Sep 2002 17:56:49 -0000	1.2
  @@ -82,4 +82,25 @@
       public AxionBuilder() {
           setForeignKeysEmbedded(true);
       }    
  +
  +
  +    protected String getSqlType(Column column) {
  +        // Axion doesn't support text width specification 
  +        return column.getType();
  +    }
  +    protected void writePrimaryKeys(Table table) throws IOException {
  +        // disable primary key constraints
  +    }
  +    
  +    protected void writeForeignKeys(Table table) throws IOException {
  +        // disable foreign key constraints
  +    }
  +    
  +    protected void printAutoIncrementColumn() throws IOException {
  +        //print( "IDENTITY" );
  +    }
  +
  +    protected void printNotNullable() throws IOException {
  +        //print("NOT NULL");
  +    }
   }
  
  
  
  1.6       +54 -13    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SqlBuilder.java	12 Sep 2002 15:03:59 -0000	1.5
  +++ SqlBuilder.java	12 Sep 2002 17:56:49 -0000	1.6
  @@ -126,12 +126,21 @@
        * Outputs the DDL required to drop and recreate the database 
        */
       public void createDatabase(Database database, boolean dropTable) throws IOException {
  +        
  +        // lets drop the tables in reverse order as its less likely to cause
  +        // problems with referential constraints
  +        
  +        if (dropTable) {
  +            List tables = database.getTables();
  +            for (int i = tables.size() - 1; i >= 0; i-- ) {
  +                Table table = (Table) tables.get(i);
  +                dropTable(table);
  +            }
  +        }
  +            
           for (Iterator iter = database.getTables().iterator(); iter.hasNext(); ) {
               Table table = (Table) iter.next();
               tableComment(table);
  -            if (dropTable) {
  -                dropTable(table);
  -            }
               createTable(table);
           }
       }
  @@ -140,8 +149,11 @@
        * Outputs the DDL required to drop the database 
        */
       public void dropDatabase(Database database) throws IOException {
  -        for (Iterator iter = database.getTables().iterator(); iter.hasNext(); ) {
  -            Table table = (Table) iter.next();
  +        
  +        // lets drop the tables in reverse order
  +        List tables = database.getTables();
  +        for (int i = tables.size() - 1; i >= 0; i-- ) {
  +            Table table = (Table) tables.get(i);
               tableComment(table);
               dropTable(table);
           }
  @@ -203,16 +215,17 @@
           print(getSqlType(column));
           print(" ");
           if (column.isRequired()) {
  -            print("NOT NULL");
  +            printNotNullable();
           }
           else {
  -            print("NULL");
  +            printNullable();
           }
           print(" ");
           if (column.isAutoIncrement()) {
               printAutoIncrementColumn();
           }
       }
  +    
   
       // Properties
       //-------------------------------------------------------------------------                
  @@ -306,6 +319,22 @@
       //-------------------------------------------------------------------------                
   
       /**
  +     * @return true if we should generate a primary key constraint for the given
  +     *  primary key columns. By default if there are no primary keys or the column(s) are 
  +     *  all auto increment (identity) columns then there is no need to generate a primary key 
  +     *  constraint.
  +     */
  +    protected boolean shouldGeneratePrimaryKeys(List primaryKeyColumns) {
  +        for (Iterator iter = primaryKeyColumns.iterator(); iter.hasNext(); ) {
  +            Column column = (Column) iter.next();
  +            if (! column.isAutoIncrement()) {
  +                return true;
  +            }
  +        }
  +        return false;
  +    }
  +
  +    /**
        * @return the full SQL type string including the size
        */
       protected String getSqlType(Column column) {
  @@ -335,7 +364,7 @@
        */
       protected void writePrimaryKeys(Table table) throws IOException {
           List primaryKeyColumns = table.getPrimaryKeyColumns();
  -        if (primaryKeyColumns.size() > 0) {
  +        if (primaryKeyColumns.size() > 0 && shouldGeneratePrimaryKeys(primaryKeyColumns)) {
               println(",");
               printIndent();
               writePrimaryKeyStatement(primaryKeyColumns);
  @@ -347,7 +376,7 @@
        */
       protected void writePrimaryKeysAlterTable(Table table) throws IOException {
           List primaryKeyColumns = table.getPrimaryKeyColumns();
  -        if (primaryKeyColumns.size() > 0) {
  +        if (primaryKeyColumns.size() > 0 && shouldGeneratePrimaryKeys(primaryKeyColumns)) {
               print("ALTER TABLE ");
               println(table.getName());
   
  @@ -391,8 +420,7 @@
               ) {
               ForeignKey key = (ForeignKey) keyIter.next();
               if (key.getForeignTable() == null) {
  -                System.err.println(
  -                    "WARN: foreign key table is null for key: " + key);
  +                log.warn( "Foreign key table is null for key: " + key);
               }
               else {
                   println(",");
  @@ -430,8 +458,7 @@
               ) {
               ForeignKey key = (ForeignKey) keyIter.next();
               if (key.getForeignTable() == null) {
  -                System.err.println(
  -                    "WARN: foreign key table is null for key: " + key);
  +                log.warn( "Foreign key table is null for key: " + key);
               }
               else {
                   print("ALTER TABLE ");
  @@ -505,6 +532,20 @@
           println( text );
       }
   
  +    /**
  +     * Prints that a column is nullable 
  +     */
  +    protected void printNullable() throws IOException {
  +        print("NULL");
  +    }
  +    
  +    /**
  +     * Prints that a column is not nullable
  +     */
  +    protected void printNotNullable() throws IOException {
  +        print("NOT NULL");
  +    }
  +    
       /** 
        * Prints the end of statement text, which is typically a semi colon followed by 
        * a carriage return
  
  
  
  1.2       +2 -1      jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/SqlDynaBean.java
  
  Index: SqlDynaBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/SqlDynaBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SqlDynaBean.java	11 Sep 2002 22:39:15 -0000	1.1
  +++ SqlDynaBean.java	12 Sep 2002 17:56:49 -0000	1.2
  @@ -62,6 +62,7 @@
   package org.apache.commons.sql.dynabean;
   
   import org.apache.commons.beanutils.BasicDynaBean;
  +import org.apache.commons.beanutils.DynaClass;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -78,7 +79,7 @@
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog( SqlDynaBean.class );
       
  -    public SqlDynaBean(SqlDynaClass dynaClass) {
  +    public SqlDynaBean(DynaClass dynaClass) {
           super(dynaClass);
       }
   
  
  
  
  1.3       +49 -63    jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/DynaSql.java
  
  Index: DynaSql.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/DynaSql.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DynaSql.java	11 Sep 2002 23:00:29 -0000	1.2
  +++ DynaSql.java	12 Sep 2002 17:56:49 -0000	1.3
  @@ -73,6 +73,8 @@
   import java.util.List;
   import java.util.Map;
   
  +import javax.sql.DataSource;
  +
   import org.apache.commons.beanutils.DynaBean;
   import org.apache.commons.beanutils.DynaClass;
   
  @@ -81,6 +83,7 @@
   
   import org.apache.commons.sql.model.Database;
   import org.apache.commons.sql.model.Table;
  +import org.apache.commons.sql.util.JdbcSupport;
   
   /**
    * DynaSql provides simple access to relational data
  @@ -90,7 +93,7 @@
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public abstract class DynaSql {
  +public class DynaSql extends JdbcSupport {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog( DynaSql.class );
  @@ -104,7 +107,8 @@
       public DynaSql() {
       }
   
  -    public DynaSql(Database database) {
  +    public DynaSql(DataSource dataSource, Database database) {
  +        super(dataSource);
           this.database = database;
       }
   
  @@ -113,12 +117,36 @@
        * @return the new empty DynaBean for the given tableName or null if the
        *  table does not exist in the current Database model.
        */
  -    public DynaBean newInstance(String tableName) {
  -        SqlDynaClass dynaClass = getSqlDynaClass(tableName);
  +    public DynaBean newInstance(String tableName) throws IllegalAccessException, InstantiationException {
  +        SqlDynaClass dynaClass = getDynaClass(tableName);
  +        if (dynaClass != null) {
  +            return dynaClass.newInstance();
  +        }
           return null;
       }
       
       /**
  +     * @return the SqlDynaClass for the given table name. If the SqlDynaClass does not exist
  +     * then create a new one based on the Table definition
  +     */
  +    public SqlDynaClass getDynaClass(String tableName) {
  +        SqlDynaClass answer = (SqlDynaClass) dynaClassCache.get(tableName);
  +        if (answer == null) {
  +            Table table = getDatabase().findTable(tableName);
  +            if (table != null) {
  +                answer = createSqlDynaClass(table);
  +                dynaClassCache.put(tableName, dynaClassCache);
  +            }
  +            else {
  +                log.warn( "No such table: " + tableName );
  +                System.out.println( "Couldn't find table: " + tableName );
  +            }
  +        }
  +        return answer;
  +    }
  +    
  +    
  +    /**
        * Stores the given DyanBean in the database, inserting it if there is no primary key
        * otherwise the bean is updated in the database.
        */
  @@ -179,7 +207,9 @@
               }
               String sql = createDeleteSql(dynaClass, primaryKeys);
   
  -            log.info( "About to execute SQL: " + sql );
  +            if (log.isDebugEnabled()) {
  +                log.debug( "About to execute SQL: " + sql );
  +            }
               
               statement = connection.prepareStatement( sql );   
               
  @@ -227,50 +257,12 @@
       //-------------------------------------------------------------------------                
   
       /**
  -     * @return a new JDBC connection from the pool
  -     */
  -    protected abstract Connection borrowConnection() throws SQLException;
  -    
  -    /**
  -     * Returns a JDBC connection back into the pool
  -     */
  -    protected abstract void returnConnection(Connection connection) throws SQLException;
  -    
  -    /**
        * @return true if this dynaBean has a primary key
        */
       protected boolean exists(DynaBean dynaBean, Connection connection) {
           return false;
       }
   
  -    /**
  -     * Closes the given result set down.
  -     */
  -    protected void closeResultSet(ResultSet resultSet) {
  -        if ( resultSet != null ) {
  -            try {
  -                resultSet.close();
  -            }
  -            catch (Exception e) {
  -                log.warn("Ignoring exception closing result set: " + e, e);
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Closes the given statement down.
  -     */
  -    protected void closeStatement(Statement statement) {
  -        if ( statement != null ) {
  -            try {
  -                statement.close();
  -            }
  -            catch (Exception e) {
  -                log.warn("Ignoring exception closing statement: " + e, e);
  -            }
  -        }
  -    }
  -    
        /**
        * Creates a new primary key value, inserts the bean and returns the new item.
        */
  @@ -285,7 +277,9 @@
           
           String sql = createInsertSql(dynaClass, properties);
           
  -        log.info( "About to execute SQL: " + sql );
  +        if (log.isDebugEnabled()) {
  +            log.debug( "About to execute SQL: " + sql );
  +        }
           
           PreparedStatement statement = null;
           try {
  @@ -323,8 +317,10 @@
           
           String sql = createUpdateSql(dynaClass, primaryKeys, properties);
           
  -        log.info( "About to execute SQL: " + sql );
  -
  +        if (log.isDebugEnabled()) {
  +            log.debug( "About to execute SQL: " + sql );
  +        }
  +        
           PreparedStatement statement = null;
           try {
               statement = connection.prepareStatement( sql );   
  @@ -444,22 +440,6 @@
       }
   
       /**
  -     * @return the SqlDynaClass for the given table name. If the SqlDynaClass does not exist
  -     * then create a new one based on the Table definition
  -     */
  -    protected SqlDynaClass getSqlDynaClass(String tableName) {
  -        SqlDynaClass answer = (SqlDynaClass) dynaClassCache.get(tableName);
  -        if (answer == null) {
  -            Table table = getDatabase().findTable(tableName);
  -            if (table != null) {
  -                answer = createSqlDynaClass(table);
  -                dynaClassCache.put(tableName, dynaClassCache);
  -            }
  -        }
  -        return answer;
  -    }
  -    
  -    /**
        * A Factory method to create a new SqlDynaClass instance for the given table name If the SqlDynaClass does not exist
        * then create a new one based on the Table definition
        */
  @@ -470,7 +450,13 @@
       /**
        * Sets property value with the given prepared statement, doing any type specific conversions or swizzling.
        */
  -    protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException {
  +    protected void setObject(
  +        PreparedStatement statement, 
  +        int sqlIndex, 
  +        DynaBean dynaBean, 
  +        SqlDynaProperty property
  +    ) throws SQLException {
  +        
           Object value = dynaBean.get(property.getName());    
           statement.setObject(sqlIndex, value);
       }
  
  
  
  1.2       +14 -8     jakarta-commons-sandbox/sql/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/maven.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- maven.xml	31 Jul 2002 16:59:59 -0000	1.1
  +++ maven.xml	12 Sep 2002 17:56:49 -0000	1.2
  @@ -1,16 +1,16 @@
   <project default="java:jar" xmlns:j="jelly:core">
   
  -  <goal name="jelly-task" prereqs="java:compile, test:compile">
  -	<path id="test.classpath">
  -	    <pathelement path="${maven.build.dest}"/>
  +  <goal name="setclasspath" prereqs="java:compile, test:compile">
  +		<path id="test.classpath">
  +		  <pathelement path="${maven.build.dest}"/>
   	    <pathelement path="target/classes"/>
   	    <pathelement path="target/test-classes"/>
   	    <path refid="maven.dependency.classpath"/>
  -	</path>
  +		</path>
     </goal>
   
  -  <goal name="sql:generate-sql" prereqs="jelly-task" 
  -	description="Generates the SQL DDL for creating and dropping the database">
  +  <goal name="sql:generate-sql" prereqs="setclasspath" 
  +		description="Generates the SQL DDL for creating and dropping the database">
   
   <!--
   	if we could set the threads context class loader to ${test.classpath} 
  @@ -26,13 +26,19 @@
     </goal>
     
     <goal name="sql:create-db" prereqs="sql:generate-sql" 
  -	description="Runs the SQL to create a new database, dropping the previous tables">
  +		description="Runs the SQL to create a new database, dropping the previous tables">
   
       <java classname="org.apache.commons.jelly.Jelly" fork="yes">
         <classpath refid="test.classpath"/>
   	  <arg value="src/jelly/createDb.jelly"/> 
       </java>
     </goal>
  -  
  +
  +  <goal name="test:dynasql" prereqs="setclasspath" 
  +		description="Runs the DynaSql test case">
  +    <java classname="org.apache.commons.sql.dynabean.TestDynaSql" fork="yes">
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </goal>
     
   </project>
  
  
  
  1.5       +4 -8      jakarta-commons-sandbox/sql/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/project.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- project.xml	11 Sep 2002 22:39:41 -0000	1.4
  +++ project.xml	12 Sep 2002 17:56:49 -0000	1.5
  @@ -102,7 +102,7 @@
   
       <dependency>
         <id>commons-collections</id>
  -      <version>2.0</version>
  +      <version>2.1-dev</version>
       </dependency>
   
       <dependency>
  @@ -142,18 +142,14 @@
         <version>2.0.0</version>
       </dependency>
   
  -		<!-- for test cases -->
  +    <!-- database used for testing -->
       <dependency>
         <id>axion</id>
         <version>1.0-dev.20020830.060007</version>
       </dependency>
       <dependency>
  -      <id>commons-pool</id>
  -      <version>1.1-dev</version>
  -    </dependency>
  -    <dependency>
  -      <id>commons-dbcp</id>
  -      <version>1.0-dev-20020806</version>
  +      <id>hsqldb</id>
  +      <version>1.7.0</version>
       </dependency>
   
   	  <!-- no longer requird -->    
  
  
  
  1.2       +2 -0      jakarta-commons-sandbox/sql/project.properties
  
  Index: project.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/project.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.properties	31 Jul 2002 16:59:59 -0000	1.1
  +++ project.properties	12 Sep 2002 17:56:49 -0000	1.2
  @@ -8,6 +8,8 @@
   
   jarResources.basedir = src/java
   
  +maven.junit.fork = true
  +
   compile.debug = on
   compile.optimize = off
   compile.deprecation = off
  
  
  
  1.3       +9 -9      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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- datamodel.xml	10 Sep 2002 13:49:46 -0000	1.2
  +++ datamodel.xml	12 Sep 2002 17:56:49 -0000	1.3
  @@ -2,6 +2,15 @@
   
   <database name="bookstore">
   
  +	<!-- =================================================== -->
  +  <!-- A U T H O R  T A B L E                              -->
  +  <!-- =================================================== -->
  +
  +  <table name="author">
  +    <column name="author_id" type="INTEGER" primaryKey="true" required="true"/>
  +    <column name="name" type="VARCHAR" size="50" required="true"/>
  +	</table>
  +
     <!-- =================================================== -->
     <!-- B O O K  T A B L E                                  -->
     <!-- =================================================== -->
  @@ -16,14 +25,5 @@
   	    <reference local="author_id" foreign="author_id"/>
   	  </foreign-key>  
     </table>
  -
  -	<!-- =================================================== -->
  -  <!-- A U T H O R  T A B L E                              -->
  -  <!-- =================================================== -->
  -
  -  <table name="author">
  -    <column name="author_id" type="INTEGER" primaryKey="true" required="true"/>
  -    <column name="name" type="VARCHAR" size="50" required="true"/>
  -	</table>
   
   </database>
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DataSourceWrapper.java
  
  Index: DataSourceWrapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 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", "Tomcat", 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/>.
   *
   */
  
  package org.apache.commons.sql.util;
  
  import java.io.*;
  import java.sql.*;
  import java.util.*;
  import javax.sql.DataSource;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * <p>A simple <code>DataSource</code> wrapper for the standard
   * <code>DriverManager</code> class.
   * 
   * @author Hans Bergsten
   */
  public class DataSourceWrapper implements DataSource {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(DataSourceWrapper.class);
  
      private String driverClassName;
      private String jdbcURL;
      private String userName;
      private String password;
  
      public void setDriverClassName(String driverClassName)
          throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  
          if (log.isDebugEnabled()) {
              log.debug("Loading JDBC driver: [" + driverClassName + "]");
          }
  
          this.driverClassName = driverClassName;
          getClass().getClassLoader().loadClass(driverClassName).newInstance();
      }
  
      public void setJdbcURL(String jdbcURL) {
          this.jdbcURL = jdbcURL;
      }
  
      public void setUserName(String userName) {
          this.userName = userName;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      /**
       * Returns a Connection using the DriverManager and all
       * set properties.
       */
      public Connection getConnection() throws SQLException {
          Connection conn = null;
          if (userName != null) {
              if (log.isDebugEnabled()) {
                  log.debug(
                      "Creating connection from url: " + jdbcURL + " userName: " + userName);
              }
  
              conn = DriverManager.getConnection(jdbcURL, userName, password);
          }
          else {
              if (log.isDebugEnabled()) {
                  log.debug("Creating connection from url: " + jdbcURL);
              }
  
              conn = DriverManager.getConnection(jdbcURL);
          }
          if (log.isDebugEnabled()) {
              log.debug(
                  "Created connection: " + conn );
          }
          return conn;
      }
  
      /**
       * Always throws a SQLException. Username and password are set
       * in the constructor and can not be changed.
       */
      public Connection getConnection(String username, String password)
          throws SQLException {
          throw new SQLException("Not Supported");
      }
  
      /**
       * Always throws a SQLException. Not supported.
       */
      public int getLoginTimeout() throws SQLException {
          throw new SQLException("Not Supported");
      }
  
      /**
       * Always throws a SQLException. Not supported.
       */
      public PrintWriter getLogWriter() throws SQLException {
          throw new SQLException("Not Supported");
      }
  
      /**
       * Always throws a SQLException. Not supported.
       */
      public void setLoginTimeout(int seconds) throws SQLException {
          throw new SQLException("Not Supported");
      }
  
      /**
       * Always throws a SQLException. Not supported.
       */
      public synchronized void setLogWriter(PrintWriter out) throws SQLException {
          throw new SQLException("Not Supported");
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DDLExecutor.java
  
  Index: DDLExecutor.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.util;
  
  import java.io.IOException;
  import java.io.StringWriter;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.Statement;
  import java.sql.SQLException;
  import java.sql.SQLWarning;
  import java.util.StringTokenizer;
  
  import javax.sql.DataSource;
  
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.commons.sql.builder.SqlBuilder;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.model.Table;
  import org.apache.commons.sql.util.JdbcSupport;
  
  /**
   * DDLExecutor is a utility class which is capable of performing DDL
   * on a database connection such as to create a database, or drop it or alter it..
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.14 $
   */
  public class DDLExecutor extends JdbcSupport {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog( DDLExecutor.class );
      
      /** The Strategy for building SQL for the current physical database */
      private SqlBuilder sqlBuilder;
      
      /** Should we continue if a command fails */
      private boolean continueOnError = true;
      
      public DDLExecutor() {
      }
  
      public DDLExecutor(DataSource dataSource, SqlBuilder sqlBuilder) {
          super(dataSource);
          this.sqlBuilder = sqlBuilder;
      }
  
  
      /**
       * Creates the given database using the current DataSource, optionally 
       * dropping tables first.
       */
      public void createDatabase(Database database, boolean dropTablesFirst) throws SQLException {
          String sql = null;
          try {
              StringWriter buffer = new StringWriter();
              getSqlBuilder().setWriter(buffer);
              getSqlBuilder().createDatabase(database, dropTablesFirst);
              sql = buffer.toString();
          }
          catch (IOException e) {
              throw new SQLException( "We should never get this exception!!: " + e );
          }
          evaluateBatch(sql);
      }
      
      /**
       * Drops the given database using the current DataSource
       */
      public void dropDatabase(Database database) throws SQLException {
          String sql = null;
          try {
              StringWriter buffer = new StringWriter();
              getSqlBuilder().setWriter(buffer);
              getSqlBuilder().dropDatabase(database);
              sql = buffer.toString();
          }
          catch (IOException e) {
              throw new SQLException( "We should never get this exception!!: " + e );
          }
          evaluateBatch(sql);
      }
      
  
      // Properties
      //-------------------------------------------------------------------------                
  
      /**
       * Returns the sqlBuilder.
       * @return SqlBuilder
       */
      public SqlBuilder getSqlBuilder() {
          return sqlBuilder;
      }
  
      /**
       * Sets the sqlBuilder.
       * @param sqlBuilder The sqlBuilder to set
       */
      public void setSqlBuilder(SqlBuilder sqlBuilder) {
          this.sqlBuilder = sqlBuilder;
      }
  
      /**
       * Returns the continueOnError.
       * @return boolean
       */
      public boolean isContinueOnError() {
          return continueOnError;
      }
  
      /**
       * Sets the continueOnError.
       * @param continueOnError The continueOnError to set
       */
      public void setContinueOnError(boolean continueOnError) {
          this.continueOnError = continueOnError;
      }
  
      // Implementation methods
      //-------------------------------------------------------------------------                
  
      protected void evaluateBatch(String sql) throws SQLException {
          Connection connection = borrowConnection();
          Statement statement = null;
          int errors = 0;
          int commandCount = 0;
          try {
              statement = connection.createStatement();
              
              StringTokenizer tokenizer = new StringTokenizer( sql, ";" );
              while (tokenizer.hasMoreTokens()) {
                  String command = tokenizer.nextToken();
                  
                  // ignore whitespace
                  command = command.trim();
                  if ( command.length() == 0 ) {
                      continue;
                  }
                  
                  commandCount++;
                  
                  if (log.isDebugEnabled() ) {
                      log.debug( "About to execute sql: " + command );
                  }
                  
                  if (continueOnError) {                                
                      try {
                          int results = statement.executeUpdate(command);
                          if (log.isDebugEnabled()) {
                              log.debug( "returned: " + results + " row(s) changed" );
                          }
                      }
                      catch (SQLException e) {
                          log.error( "Command failed: " + command + ". Reason: " + e );
                          errors++;
                      }
                  }
                  else {
                      int results = statement.executeUpdate(command);
                      if (log.isDebugEnabled()) {
                          log.debug( "returned: " + results + " row(s) changed" );
                      }
                  }
                  
                  // lets display any warnings
                  SQLWarning warning = connection.getWarnings();
                  while (warning != null ) {
                      log.warn( warning.toString() );
                      warning = warning.getNextWarning();
                  }
                  connection.clearWarnings();
              }
              log.info( "Executed: "+ commandCount + " statement(s) with " + errors + " error(s)" );
          }
          finally {
              closeStatement(statement);
              returnConnection(connection);
          }
      }
      
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/JdbcSupport.java
  
  Index: JdbcSupport.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.util;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.Statement;
  import java.sql.SQLException;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * JdbcSupport is an abstract base class for objects which need to 
   * perform JDBC operations. It contains a number of useful methods 
   * for implementation inheritence..
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.14 $
   */
  public abstract class JdbcSupport {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog( JdbcSupport.class );
      
      private DataSource dataSource;
      
      public JdbcSupport() {
      }
  
      public JdbcSupport(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      // Properties
      //-------------------------------------------------------------------------                
      
      /**
       * Returns the DataSource used to pool JDBC Connections.
       * @return DataSource
       */
      public DataSource getDataSource() {
          return dataSource;
      }
  
      /**
       * Sets the DataSource used to pool JDBC Connections.
       * @param dataSource The dataSource to set
       */
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      // Implementation methods    
      //-------------------------------------------------------------------------                
  
      /**
       * @return a new JDBC connection from the pool
       */
      protected Connection borrowConnection() throws SQLException {
          return getDataSource().getConnection();
      }
      
      /**
       * Returns a JDBC connection back into the pool
       */
      protected void returnConnection(Connection connection) throws SQLException {
          try {
              connection.close();
          }
          catch (Exception e) {
              log.error( "Caught exception while returning connection to pool: " + e, e);
          }
      }
  
      /**
       * Closes the given result set down.
       */
      protected void closeResultSet(ResultSet resultSet) {
          if ( resultSet != null ) {
              try {
                  resultSet.close();
              }
              catch (Exception e) {
                  log.warn("Ignoring exception closing result set: " + e, e);
              }
          }
      }
  
      /**
       * Closes the given statement down.
       */
      protected void closeStatement(Statement statement) {
          if ( statement != null ) {
              try {
                  statement.close();
              }
              catch (Exception e) {
                  log.warn("Ignoring exception closing statement: " + e, e);
              }
          }
      }
  }
  
  
  
  1.3       +13 -13    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestDataModelRoundTrip.java	10 Sep 2002 13:49:46 -0000	1.2
  +++ TestDataModelRoundTrip.java	12 Sep 2002 17:56:50 -0000	1.3
  @@ -89,33 +89,33 @@
               assertTrue("more than one table found", database.getTables().size() > 0 );
               
               // Test our first table which is the 'book' table
  -            Table t0 = database.getTable(0);
  -            assertEquals("book", t0.getName());
  +            Table t1 = database.getTable(1);
  +            assertEquals("book", t1.getName());
               
  -            Column c0 = t0.getColumn(0);
  +            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());
               
  -            Column c1 = t0.getColumn(1);
  +            Column c1 = t1.getColumn(1);
               assertEquals("isbn", c1.getName());
               assertTrue("isbn is required", c1.isRequired());
               assertTrue("isbn is not primary key", ! c1.isPrimaryKey());
   
  -            List keyList0 = t0.getForeignKeys();
  -            assertEquals( "Foreign key count", 1, keyList0.size() );
  +            List keyList1 = t1.getForeignKeys();
  +            assertEquals( "Foreign key count", 1, keyList1.size() );
               
  -            ForeignKey key0 = (ForeignKey) keyList0.get(0);
  +            ForeignKey key0 = (ForeignKey) keyList1.get(0);
               assertEquals("foreignTable is correct", "author", key0.getForeignTable());
               
  -            List refList0 = key0.getReferences();
  -            assertEquals( "Reference count", 1, refList0.size() );
  +            List refList1 = key0.getReferences();
  +            assertEquals( "Reference count", 1, refList1.size() );
               
  -            Reference r0 = (Reference) refList0.get(0);
  -            assertTrue("Found a reference", r0 != null);
  +            Reference r1 = (Reference) refList1.get(0);
  +            assertTrue("Found a reference", r1 != null);
                           
  -            assertEquals("local is correct", "author_id", r0.getLocal());
  -            assertEquals("foreign is correct", "author_id", r0.getForeign());
  +            assertEquals("local is correct", "author_id", r1.getLocal());
  +            assertEquals("foreign is correct", "author_id", r1.getForeign());
               
               // Write out the bean
               //writeBean(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