matth       2003/12/16 08:03:07

  Modified:    sql      build.xml
  Added:       sql/src/java/org/apache/commons/sql/model Unique.java
                        UniqueColumn.java
               sql/src/java/org/apache/commons/sql/task DatabaseTask.java
               sql/src/test/org/apache/commons/sql/builder
                        TestEverything.java
               sql/src/test-input test-start.xml test.properties
  Log:
  I forgot to add these files from the last patch.  Maven also include absolute paths 
in the build.xml -- I thought this was fixed?
  
  Revision  Changes    Path
  1.6       +4 -4      jakarta-commons-sandbox/sql/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/build.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- build.xml 16 Dec 2003 15:19:15 -0000      1.5
  +++ build.xml 16 Dec 2003 16:03:07 -0000      1.6
  @@ -33,7 +33,7 @@
       </mkdir>
       <javac destdir="${classesdir}" deprecation="true" debug="true" optimize="false" 
excludes="**/package.html">
         <src>
  -        <pathelement 
location="/home/subterfusion/files/tech/appdev/projects/apache/jakarta-commons-sandbox/sql/src/java">
  +        <pathelement location="src/java">
           </pathelement>
         </src>
         <classpath>
  @@ -89,7 +89,7 @@
           </pathelement>
         </classpath>
         <batchtest todir="${testreportdir}">
  -        <fileset 
dir="/home/subterfusion/files/tech/appdev/projects/apache/jakarta-commons-sandbox/sql/src/test">
  +        <fileset dir="src/test">
             <include name="**/Test*.java">
             </include>
           </fileset>
  @@ -101,7 +101,7 @@
       </mkdir>
       <javac destdir="${testclassesdir}" deprecation="true" debug="true" 
optimize="false" excludes="**/package.html">
         <src>
  -        <pathelement 
location="/home/subterfusion/files/tech/appdev/projects/apache/jakarta-commons-sandbox/sql/src/test">
  +        <pathelement location="src/test">
           </pathelement>
         </src>
         <classpath>
  @@ -125,7 +125,7 @@
       </property>
       <property name="title" value="commons-sql 1.0-dev API">
       </property>
  -    <javadoc use="true" private="true" destdir="${javadocdir}" author="true" 
version="true" 
sourcepath="/home/subterfusion/files/tech/appdev/projects/apache/jakarta-commons-sandbox/sql/src/java"
 packagenames="org.apache.commons.sql.*">
  +    <javadoc use="true" private="true" destdir="${javadocdir}" author="true" 
version="true" sourcepath="src/java" packagenames="org.apache.commons.sql.*">
         <classpath>
           <fileset dir="${libdir}">
             <include name="*.jar">
  
  
  
  1.1                  
jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Unique.java
  
  Index: Unique.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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 [EMAIL PROTECTED]
   *
   * 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: Unique.java,v 1.1 2003/12/16 16:03:07 matth Exp $
   */
  package org.apache.commons.sql.model;
  
  import java.util.Iterator;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Provides compatibility with Torque-style xml with separate &lt;index&gt; and
   * &lt;unique&gt; tags, but adds no functionality.  All indexes are treated the
   * same by the Table.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">John Marshall</a>
   * @version $Revision: 1.1 $
   */
  public class Unique extends Index
  {
      public Unique() {
          setUnique( true );
      }
      
      public void setUnique(boolean unique) {
          if ( unique == false ) {
              throw new IllegalArgumentException( "Unique index cannot be made 
non-unique" );
          }
          super.setUnique(unique);
      }
  
      public boolean isUnique() {
          return true;
      }
  
      public void addUniqueColumn(UniqueColumn indexColumn)
      {
          super.addIndexColumn(indexColumn);
      }
      
      public List getUniqueColumns()
      {
          return super.getIndexColumns();
      }
      
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/UniqueColumn.java
  
  Index: UniqueColumn.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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 [EMAIL PROTECTED]
   *
   * 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: UniqueColumn.java,v 1.1 2003/12/16 16:03:07 matth Exp $
   */
  package org.apache.commons.sql.model;
  
  /**
   * Provides compatibility with Torque-style xml with separate &lt;index&gt; and
   * &lt;unique&gt; tags, but adds no functionality.  All indexes are treated the
   * same by the Table.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">John Marshall</a>
   * @version $Revision: 1.1 $
   */
  public class UniqueColumn extends IndexColumn
  {
      public UniqueColumn() {}
      
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/task/DatabaseTask.java
  
  Index: DatabaseTask.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/task/DatabaseTask.java,v
 1.1 2003/12/16 16:03:07 matth Exp $
   * $Revision: 1.1 $
   * $Date: 2003/12/16 16:03:07 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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 [EMAIL PROTECTED]
   *
   * 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.task;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.sql.io.JdbcModelReader;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.util.DataSourceWrapper;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  
  /**
   * This is an abstract class for tasks that need to access a database through
   * a connection.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">J. Russell Smyth</a>
   * @author John Marshall/Connectria
   * @version $Id: DatabaseTask.java,v 1.1 2003/12/16 16:03:07 matth Exp $
   */
  public abstract class DatabaseTask extends Task
  {
      /** JDBC URL. */
      protected String dbUrl;
  
      /** JDBC driver. */
      protected String dbDriver;
  
      /** JDBC user name. */
      protected String dbUser;
  
      /** JDBC password. */
      protected String dbPassword;
  
      /** DB catalog to use. */
      protected String dbCatalog;
  
      /** DB schema to use. */
      protected String dbSchema;
  
      public String getDbSchema()
      {
          return dbSchema;
      }
  
      public void setDbCatalog(String dbCatalog)
      {
          this.dbCatalog = dbCatalog;
      }
  
      public void setDbSchema(String dbSchema)
      {
          this.dbSchema = dbSchema;
      }
  
      public void setDbUrl(String v)
      {
          dbUrl = v;
      }
  
      public String getDbUrl()
      {
          return dbUrl;
      }
  
      public void setDbDriver(String v)
      {
          dbDriver = v;
      }
  
      public void setDbUser(String v)
      {
          dbUser = v;
      }
  
      public void setDbPassword(String v)
      {
          dbPassword = v;
      }
  
      /**
       * Prints the connection settings to stderr.
       */
      protected void printDbSettings() throws BuildException
      {
          System.err.println("Your DB settings are:");
          System.err.println("driver : " + dbDriver);
          System.err.println("URL : " + dbUrl);
          System.err.println("user : " + dbUser);
          System.err.println("password : " + dbPassword);
          System.err.println("catalog : " + dbCatalog);
          System.err.println("schema : " + dbSchema);
      }
  
      /**
       * Gets a Connection as specified
       * 
       * @return a Connection to the database
       * 
       * @throws ClassNotFoundException if dbDriver cannot be loaded
       * @throws SQLException if the database cannot be connected to
       */
      protected DataSource getDataSource() throws ClassNotFoundException, SQLException
      {
          // Load the database Driver.
          DataSourceWrapper wrapper = new DataSourceWrapper(dbDriver, dbUrl, dbUser, 
dbPassword);
          return wrapper;
      }
  
      /**
       * Retrievs the database specification from a connection
       * @param con The database connection
       * @return the Database schema
       * 
       * @throws SQLException if the schema cannot be read
       */
      protected Database getDbFromConnection(Connection con) throws SQLException
      {
          JdbcModelReader reader = new JdbcModelReader(con);
          if ( dbCatalog!=null ) {
              reader.setCatalog(dbCatalog);
          }
          if ( dbSchema!=null ) {
              reader.setSchema(dbSchema);
          }
  
          Database db = reader.getDatabase();
          return db;
      } 
  }
  
  
  1.1                  
jakarta-commons-sandbox/sql/src/test/org/apache/commons/sql/builder/TestEverything.java
  
  Index: TestEverything.java
  ===================================================================
  package org.apache.commons.sql.builder;
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   *
   * $Id: TestEverything.java,v 1.1 2003/12/16 16:03:07 matth Exp $
   */
  import java.beans.IntrospectionException;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.StringWriter;
  import java.sql.SQLException;
  import java.sql.Types;
  import java.util.Iterator;
  import java.util.Properties;
  
  import javax.sql.DataSource;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.sql.dynabean.DynaSql;
  import org.apache.commons.sql.io.DatabaseReader;
  import org.apache.commons.sql.model.Column;
  import org.apache.commons.sql.model.Database;
  import org.apache.commons.sql.model.Table;
  import org.apache.commons.sql.util.DDLExecutor;
  import org.apache.commons.sql.util.DataSourceWrapper;
  import org.xml.sax.SAXException;
  
  /**
   * Test harness for the SqlBuilder for various databases.
   * Tests: create table, create index, drop index, pk create, add column,
   * drop column, modify column, drop table, default values, unique indexes
   *
   * @author John Marshall/Connectria
   * @version $Revision: 1.1 $
   */
  public class TestEverything extends TestCase
  {
      private String baseDir;
      private Properties props;
      private DataSource dataSource;
  
      /**
       * A unit test suite for JUnit
       */
      public static Test suite()
      {
          return new TestSuite(TestEverything.class);
      }
  
      public static void main(String[] args) {
          junit.textui.TestRunner.run(TestEverything.class);
      }
  
      /**
       * Constructor for the TestEverything object
       *
       * @param testName
       */
      public TestEverything(String testName)
      {
          super(testName);
      }
  
      /**
       * The JUnit setup method
       */
  
       // TODO: Uncomment after other tests are added
      /*
      protected void setUp() throws Exception
      {
          super.setUp();
  
          baseDir = System.getProperty("basedir", ".");
          String uri = baseDir + "/src/test-input/test.properties";
  
          props = new Properties();
          props.load( new FileInputStream(uri) );
  
          assertTrue("Loaded valid properties", !props.isEmpty() );
  
          dataSource = getDataSource();
      }
      */
  
      /**
       * Gets a DataSource from the configuration in properties
       */
      private DataSource getDataSource() throws ClassNotFoundException {
          String driver = props.getProperty( "dbDriver" );
          String url = props.getProperty( "dbUrl" );
          String user = props.getProperty( "dbUser" );
          String password = props.getProperty( "dbPassword" );
  
          return new DataSourceWrapper( driver, url, user, password );
      }
  
      /**
       * Gets a Database from the filesystem
       */
      private Database getDatabase( String name ) throws IntrospectionException, 
FileNotFoundException, SAXException, IOException {
          String uri = baseDir + "/src/test-input/" + name + ".xml";
  
          DatabaseReader reader = new DatabaseReader();
          Database database = (Database) reader.parse( new FileInputStream(uri) );
          assertTrue("Loaded a valid database", database != null);
  
          return database;
      }
  
      // TODO: Remove after other tests have been added.
      // This class needs at least one test or it fails
      public void testFake() {}
  
      /**
       * A unit test for JUnit
       */
      public void XtestEverything()
          throws Exception
      {
          // TODO: Reinsert test after env has been configured
          String tableName = "test_table";
  
          Integer testId = new Integer(1);
          String testName = "UniqueName";
  
          Database db = getDatabase( "test-start" );
          assertEquals( "Correct version", "1.0", db.getVersion() );
  
          updateDatabase( db, true );
          Table table = db.findTable( tableName );
  
          DynaSql dynaSql = new DynaSql( dataSource, db );
          DynaBean test = dynaSql.newInstance(tableName);
  
          assertTrue("Test not null", test != null);
  
          test.set("id", testId);
          test.set("name", testName);
          dynaSql.insert(test);
  
          //test inserted
          //test default value
          Iterator iter = dynaSql.query( "select * from " + tableName + " where id = 
1" );
          assertTrue("Found at least one row", iter.hasNext());
  
          test = (DynaBean) iter.next();
  
          assertTrue("Found a dynaBean row", test != null);
  
          assertEquals( "bean has correct id", testId, test.get("id") );
          assertEquals( "bean has correct name", testName, test.get("name") );
  //dynasql forces null into column
  //        assertEquals( "bean has correct default number", new Integer(25), 
test.get("number") );
  
          //test unique index
          test = dynaSql.newInstance(tableName);
          test.set("id", new Integer(2));
          test.set("name", testName);
          boolean insertError = false;
          try {
              dynaSql.insert(test);
          } catch ( SQLException e ) {
              insertError = true;
          }
  
          assertTrue( "Unique index violation on insert", insertError );
  
          //test pk
          test = dynaSql.newInstance(tableName);
          test.set("id", testId);
          test.set("name", "another name");
          insertError = false;
          try {
              dynaSql.insert(test);
          } catch ( SQLException e ) {
              insertError = true;
          }
  
          assertTrue( "PK violation on insert", insertError );
  
  
          //check adding column with default
          Column newCol = new Column( "defaulted", Types.INTEGER, 11, true, false, 
false, "50" );
          table.addColumn( newCol );
          updateDatabase( db, false );
  
  
          iter = dynaSql.query( "select * from " + tableName + " where id = 1" );
          assertTrue("Found at least one row", iter.hasNext());
          test = (DynaBean) iter.next();
          assertTrue("Found a dynaBean row", test != null);
          assertEquals( "bean has correct id", testId, test.get("id") );
          assertEquals( "bean has correct name", testName, test.get("name") );
          assertEquals( "bean has correct default number", new Integer(50), 
test.get("defaulted") );
  
          //change column type
          table.getColumns().remove( newCol );
          newCol.setTypeCode( Types.VARCHAR );
          table.addColumn( newCol );
          updateDatabase( db, false );
  
          iter = dynaSql.query( "select * from " + tableName + " where id = 1" );
          assertTrue("Found at least one row", iter.hasNext());
          test = (DynaBean) iter.next();
          assertTrue("Found a dynaBean row", test != null);
          assertEquals( "bean has correct id", testId, test.get("id") );
          assertEquals( "bean has correct name", testName, test.get("name") );
          //the following statement does fail with new Integer(50) as the expected 
value, so type check is right
          assertEquals( "bean has correct type", "50", test.get("defaulted") );
  
  
          //try drop column
          table.getColumns().remove( newCol );
          updateDatabase( db, false );
  
          iter = dynaSql.query( "select * from " + tableName + " where id = 1" );
          assertTrue("Found at least one row", iter.hasNext());
          test = (DynaBean) iter.next();
          assertTrue("Found a dynaBean row", test != null);
          assertEquals( "bean has correct id", testId, test.get("id") );
          assertEquals( "bean has correct name", testName, test.get("name") );
          assertTrue( "bean has no property", 
test.getDynaClass().getDynaProperty("defaulted") == null );
  
  
          //try drop index
          table.getIndexes().clear();
          assertEquals( "No table indexes", 0, table.getIndexes().size() );
          updateDatabase( db, false );
  //org.apache.commons.sql.io.DatabaseWriter writer = new 
org.apache.commons.sql.io.DatabaseWriter(System.err);
  //writer.write(db);
  
          test = dynaSql.newInstance(tableName);
          test.set("id", new Integer(5));
          test.set("name", testName);
          insertError = false;
          try {
              dynaSql.insert(test);
          } catch ( SQLException e ) {
              e.printStackTrace();
              insertError = true;
          }
  
          assertFalse( "No Unique index violation on insert", insertError );
  
          //try drop table
          db.getTables().remove(table);
          updateDatabase( db, false );
          boolean error = false;
          try {
              iter = dynaSql.query( "select * from " + tableName + " where id = 1" );
          } catch ( SQLException e ) {
              error = true;
          }
  
          assertTrue( "Had no table error", error );
      }
  
      /**
       * Updates the current database to match the desired schema
       *
       * @param create true if drop/create, false if alter
       */
      private void updateDatabase( Database db, boolean create ) throws 
InstantiationException, SQLException, IllegalAccessException, IOException {
          StringWriter writer = new StringWriter();
          SqlBuilder builder = SqlBuilderFactory.newSqlBuilder(props.getProperty( 
"dbType" ) );
          builder.setWriter(writer);
          if ( create ) {
              builder.createDatabase( db, true );
          } else {
              builder.alterDatabase( db, dataSource.getConnection(), true, true);
          }
  
          System.err.println( writer.toString() );
  
          DDLExecutor exec = new DDLExecutor(dataSource);
          exec.setContinueOnError(true);
          exec.evaluateBatch(writer.toString());
      }
  }
  
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/test-input/test-start.xml
  
  Index: test-start.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <database name="bookstore" version="1.0">
  
    <table name="test_table" description="Test Table">
      <column
        name="id"
        required="true"
        primaryKey="true"
        type="INTEGER"
        size="11"
        description="Unique id"
        autoIncrement="true"
      />
      <column
        name="name"
        required="true"
        type="VARCHAR"
        size="50"
        description="Name"
        defaultValue=""
      />
      <column
        name="number"
        required="false"
        type="INTEGER"
        size="11"
        defaultValue="25"
      />
  
      <index name="test_index" unique="true">
          <index-column name="name"/>
      </index>
  
  
    </table>
  
  </database>
  
  
  
  1.1                  jakarta-commons-sandbox/sql/src/test-input/test.properties
  
  Index: test.properties
  ===================================================================
  dbType=mysql
  dbDriver=com.mysql.jdbc.Driver
  dbUrl=jdbc:mysql://localhost:3306/sqltest
  dbUser=
  dbPassword=
  
  
  

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

Reply via email to