Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 73603 invoked from network); 16 Dec 2003 15:09:59 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 16 Dec 2003 15:09:59 -0000 Received: (qmail 57275 invoked by uid 500); 16 Dec 2003 15:09:47 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 57249 invoked by uid 500); 16 Dec 2003 15:09:47 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 57236 invoked by uid 500); 16 Dec 2003 15:09:47 -0000 Received: (qmail 57233 invoked from network); 16 Dec 2003 15:09:47 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 16 Dec 2003 15:09:47 -0000 Received: (qmail 73518 invoked by uid 1744); 16 Dec 2003 15:09:51 -0000 Date: 16 Dec 2003 15:09:51 -0000 Message-ID: <20031216150951.73517.qmail@minotaur.apache.org> From: matth@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util DDLExecutor.java DataSourceWrapper.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N matth 2003/12/16 07:09:51 Modified: sql project.xml sql/src/java/org/apache/commons/sql/builder AxionBuilder.java Db2Builder.java MSSqlBuilder.java MckoiSqlBuilder.java MySqlBuilder.java OracleBuilder.java PostgreSqlBuilder.java SqlBuilder.java SybaseBuilder.java sql/src/java/org/apache/commons/sql/io DatabaseWriter.java JdbcModelReader.java sql/src/java/org/apache/commons/sql/model Column.betwixt Column.java Database.java Index.java Table.betwixt Table.java sql/src/java/org/apache/commons/sql/type TypesWriter.java sql/src/java/org/apache/commons/sql/util DDLExecutor.java DataSourceWrapper.java Log: Obtained from: Gus Heck Submitted by: John Marshall Committed patch to builders and a bunch of other classes. Some tests are commented out until the test environment can be better configured. All other tests pass, look ok. Revision Changes Path 1.15 +7 -3 jakarta-commons-sandbox/sql/project.xml Index: project.xml =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/project.xml,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- project.xml 3 Dec 2003 21:03:00 -0000 1.14 +++ project.xml 16 Dec 2003 15:09:50 -0000 1.15 @@ -113,12 +113,14 @@ - commons-betwixt - 1.0-beta-1-dev + commons-betwixt + commons-betwixt + 20030211.133854 - commons-jelly + commons-jelly + commons-jelly 20030902.160215 @@ -127,10 +129,12 @@ 1.0-beta-2 + f ant 1.9 +4 -5 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.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- AxionBuilder.java 13 Oct 2003 08:30:54 -0000 1.8 +++ AxionBuilder.java 16 Dec 2003 15:09:50 -0000 1.9 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -93,7 +93,7 @@ // disable foreign key constraints } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { //print( "IDENTITY" ); } @@ -118,8 +118,7 @@ * does not support default values so we are removing * default from the Axion column builder. */ - public void createColumn(Column column) throws IOException { - this.column = column; + public void createColumn(Table table, Column column) throws IOException { print(column.getName()); print(" "); print(getSqlType(column)); @@ -133,7 +132,7 @@ } print(" "); if (column.isAutoIncrement()) { - printAutoIncrementColumn(); + printAutoIncrementColumn(table, column); } } } 1.4 +3 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/Db2Builder.java Index: Db2Builder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/Db2Builder.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- Db2Builder.java 13 Oct 2003 08:30:54 -0000 1.3 +++ Db2Builder.java 16 Dec 2003 15:09:50 -0000 1.4 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -64,6 +64,7 @@ import java.io.IOException; +import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Table; /** @@ -87,7 +88,7 @@ printEndOfStatement(); } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { print( "GENERATED ALWAYS AS IDENTITY" ); } } 1.6 +3 -2 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MSSqlBuilder.java 13 Oct 2003 08:30:54 -0000 1.5 +++ MSSqlBuilder.java 16 Dec 2003 15:09:50 -0000 1.6 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -65,6 +65,7 @@ import java.io.IOException; import java.util.Iterator; +import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.ForeignKey; import org.apache.commons.sql.model.Table; @@ -132,7 +133,7 @@ println(text); } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { print( "IDENTITY (1,1) " ); } } 1.5 +4 -3 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MckoiSqlBuilder.java Index: MckoiSqlBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MckoiSqlBuilder.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- MckoiSqlBuilder.java 13 Oct 2003 08:30:54 -0000 1.4 +++ MckoiSqlBuilder.java 16 Dec 2003 15:09:50 -0000 1.5 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -64,6 +64,7 @@ import java.io.IOException; +import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Table; /** @@ -84,7 +85,7 @@ printEndOfStatement(); } - protected void printAutoIncrementColumn() throws IOException { - print( "DEFAULT UNIQUEKEY('" + getTable().getName() + "')"); + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { + print( "DEFAULT UNIQUEKEY('" + table.getName() + "')"); } } 1.6 +23 -2 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MySqlBuilder.java 13 Oct 2003 08:30:54 -0000 1.5 +++ MySqlBuilder.java 16 Dec 2003 15:09:50 -0000 1.6 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -63,13 +63,16 @@ package org.apache.commons.sql.builder; import java.io.IOException; +import java.util.List; +import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Table; /** * An SQL Builder for MySQL * * @author James Strachan + * @author John Marshall/Connectria * @version $Revision$ */ public class MySqlBuilder extends SqlBuilder { @@ -84,7 +87,25 @@ printEndOfStatement(); } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { print( "AUTO_INCREMENT" ); + } + + protected boolean shouldGeneratePrimaryKeys(List primaryKeyColumns) { + /* + * mySQL requires primary key indication for autoincrement key columns + * I'm not sure why the default skips the pk statement if all are identity + */ + return true; + } + + protected String getNativeType(Column column){ + if ( "timestamp".equalsIgnoreCase( column.getType() ) ) { + return "DATETIME"; + } else if ( "longvarchar".equalsIgnoreCase( column.getType() ) ) { + return "TEXT"; + } else { + return column.getType(); + } } } 1.7 +2 -2 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- OracleBuilder.java 13 Oct 2003 08:30:54 -0000 1.6 +++ OracleBuilder.java 16 Dec 2003 15:09:50 -0000 1.7 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -110,7 +110,7 @@ } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { //print( "AUTO_INCREMENT" ); } 1.6 +9 -7 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java Index: PostgreSqlBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- PostgreSqlBuilder.java 13 Oct 2003 08:30:55 -0000 1.5 +++ PostgreSqlBuilder.java 16 Dec 2003 15:09:50 -0000 1.6 @@ -3,7 +3,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -61,6 +61,9 @@ import java.io.IOException; import org.apache.commons.sql.model.Column; +import org.apache.commons.sql.model.Table; + +import java.io.IOException; /** * An SQL Builder for PostgresSqlL @@ -75,7 +78,7 @@ } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { print(" "); print("serial"); print(" "); @@ -85,12 +88,11 @@ /** * Outputs the DDL to add a column to a table. */ - public void createColumn(Column column) throws IOException { - this.column = column; + public void createColumn(Table table, Column column) throws IOException { print(column.getName()); print(" "); if (column.isAutoIncrement()) { - printAutoIncrementColumn(); + printAutoIncrementColumn(table, column); } else { 1.14 +387 -80 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.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- SqlBuilder.java 13 Oct 2003 08:30:55 -0000 1.13 +++ SqlBuilder.java 16 Dec 2003 15:09:50 -0000 1.14 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -63,11 +63,16 @@ import java.io.IOException; import java.io.Writer; +import java.sql.Connection; +import java.sql.SQLException; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.commons.sql.io.JdbcModelReader; +import org.apache.commons.sql.model.Index; +import org.apache.commons.sql.model.IndexColumn; import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Database; import org.apache.commons.sql.model.ForeignKey; @@ -89,10 +94,13 @@ * Hopefully only a small amount code needs to be changed on a per database basis. * * @author James Strachan + * @author John Marshall/Connectria * @version $Revision$ */ public class SqlBuilder { + private static final String LINE_SEP = System.getProperty( "line.separator", "\n" ); + /** The Log to which logging calls will be made. */ private static final Log log = LogFactory.getLog(SqlBuilder.class); @@ -117,12 +125,9 @@ /** Should foreign key constraints be explicitly named */ private boolean foreignKeyConstraintsNamed; - /** The current Table we're working on */ - protected Table table; - - /** The current Column we're working on */ - protected Column column; - + /** Is an ALTER TABLE needed to drop indexes? */ + private boolean alterTableForDrop; + public SqlBuilder() { } @@ -144,13 +149,13 @@ if (dropTable) { List tables = database.getTables(); for (int i = tables.size() - 1; i >= 0; i-- ) { - table = (Table) tables.get(i); + Table table = (Table) tables.get(i); dropTable(table); } } for (Iterator iter = database.getTables().iterator(); iter.hasNext(); ) { - table = (Table) iter.next(); + Table table = (Table) iter.next(); tableComment(table); createTable(table); } @@ -164,7 +169,7 @@ // lets drop the tables in reverse order List tables = database.getTables(); for (int i = tables.size() - 1; i >= 0; i-- ) { - table = (Table) tables.get(i); + Table table = (Table) tables.get(i); tableComment(table); dropTable(table); } @@ -193,7 +198,6 @@ * Outputs the DDL to create the table along with any constraints */ public void createTable(Table table) throws IOException { - this.table = table; print("create table "); println(table.getName()); println("("); @@ -227,8 +231,9 @@ /** * Outputs the DDL to add a column to a table. */ - public void createColumn(Column column) throws IOException { - this.column = column; + public void createColumn(Table table, Column column) throws IOException { + //see comments in columnsDiffer about null/"" defaults + print(column.getName()); print(" "); print(getSqlType(column)); @@ -246,7 +251,7 @@ } print(" "); if (column.isAutoIncrement()) { - printAutoIncrementColumn(); + printAutoIncrementColumn(table, column); } } @@ -359,20 +364,6 @@ //------------------------------------------------------------------------- /** - * @return the current Table we're working on - */ - protected Table getTable() { - return table; - } - - /** - * @return the current Column we're working on - */ - protected Column getColumn() { - return column; - } - - /** * @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 @@ -419,7 +410,7 @@ println(","); } printIndent(); - createColumn(column); + createColumn(table, column); } } @@ -521,31 +512,36 @@ keyIter.hasNext(); ) { ForeignKey key = (ForeignKey) keyIter.next(); - if (key.getForeignTable() == null) { - log.warn( "Foreign key table is null for key: " + key); - } - else { - print("ALTER TABLE "); - println(table.getName()); + writeForeignKeyAlterTable( table, key ); + } + } - printIndent(); - print("ADD CONSTRAINT "); - print(table.getName()); - print("_FK_"); - print(Integer.toString(++counter)); - print(" FOREIGN KEY ("); - writeLocalReferences(key); - println(")"); + protected void writeForeignKeyAlterTable( Table table, ForeignKey key ) throws IOException { + if (key.getForeignTable() == null) { + log.warn( "Foreign key table is null for key: " + key); + } + else { + print("ALTER TABLE "); + println(table.getName()); - printIndent(); - print("REFERENCES "); - print(key.getForeignTable()); - print(" ("); - writeForeignReferences(key); - println(")"); - printEndOfStatement(); - } + printIndent(); + print("ADD CONSTRAINT "); + print(table.getName()); + print("_FK_"); + print(Integer.toString(++counter)); + print(" FOREIGN KEY ("); + writeLocalReferences(key); + println(")"); + + printIndent(); + print("REFERENCES "); + print(key.getForeignTable()); + print(" ("); + writeForeignReferences(key); + println(")"); + printEndOfStatement(); } + } /** @@ -556,39 +552,49 @@ indexIter.hasNext(); ) { Index index = (Index) indexIter.next(); - if (index.getName() == null) { - log.warn( "Index Name is null for index: " + index); - } - else { - print("CREATE INDEX "); - print(index.getName()); - print(" ON "); - print(table.getName()); + writeIndex( table, index ); + } + } - print(" ("); - - for (Iterator idxColumnIter = index.getIndexColumns().iterator(); - idxColumnIter.hasNext(); - ) + /** + * Writes one index for a table + */ + protected void writeIndex( Table table, Index index ) throws IOException { + if (index.getName() == null) { + log.warn( "Index Name is null for index: " + index); + } + else { + print("CREATE"); + if ( index.isUnique() ) { + print( " UNIQUE" ); + } + print(" INDEX "); + print(index.getName()); + print(" ON "); + print(table.getName()); + + print(" ("); + + for (Iterator idxColumnIter = index.getIndexColumns().iterator(); + idxColumnIter.hasNext(); + ) + { + IndexColumn idxColumn = (IndexColumn)idxColumnIter.next(); + if (idxColumnIter.hasNext()) { - IndexColumn idxColumn = (IndexColumn)idxColumnIter.next(); - if (idxColumnIter.hasNext()) - { - print(idxColumn.getName() + ", "); - } - else - { - print(idxColumn.getName()); - } + print(idxColumn.getName() + ", "); + } + else + { + print(idxColumn.getName()); } - - print(")"); - printEndOfStatement(); } - } - } + print(")"); + printEndOfStatement(); + } + } /** * Writes the indexes embedded within the create table statement. not * yet implemented @@ -672,7 +678,7 @@ * Prints a new line */ protected void println() throws IOException { - print("\n"); + print( LINE_SEP ); } /** @@ -700,12 +706,313 @@ /** * Outputs the fact that this column is an auto increment column. */ - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { print( "IDENTITY" ); } protected String getNativeType(Column column){ return column.getType(); } + + + /** + * Generates the DDL to modify an existing database so the schema matches + * the current specified database schema. Drops and modifications will + * not be made. + * + * @param desiredDb The desired database schema + * @param cn A connection to the existing database that should be modified + * + * @throws IOException if the ddl cannot be output + * @throws SQLException if there is an error reading the current schema + */ + public void alterDatabase(Database desiredDb, Connection cn) throws IOException, SQLException { + alterDatabase( desiredDb, cn, false, false ); + } + + /** + * Generates the DDL to modify an existing database so the schema matches + * the current specified database schema. + * + * @param desiredDb The desired database schema + * @param cn A connection to the existing database that should be modified + * @param doDrops true if columns and indexes should be dropped, false if + * just a message should be output + * @param modifyColumns true if columns should be altered for datatype, size, etc., + * false if just a message should be output + * + * @throws IOException if the ddl cannot be output + * @throws SQLException if there is an error reading the current schema + */ + public void alterDatabase(Database desiredDb, Connection cn, boolean doDrops, boolean modifyColumns) throws IOException, SQLException { + + Database currentDb = new JdbcModelReader(cn).getDatabase(); + + for (Iterator iter = desiredDb.getTables().iterator(); iter.hasNext(); ) { + Table desiredTable = (Table) iter.next(); + Table currentTable = currentDb.findTable( desiredTable.getName() ); + +//took out because if there were no changes to be made the execution had +//errors because it tries to execute the comments as a statement +// tableComment(desiredTable); + + if ( currentTable == null ) { + log.info( "creating table " + desiredTable.getName() ); + createTable( desiredTable ); + } else { + //add any columns, indices, or constraints + + Iterator desiredColumns = desiredTable.getColumns().iterator(); + while ( desiredColumns.hasNext() ) { + Column desiredColumn = (Column) desiredColumns.next(); + Column currentColumn = currentTable.findColumn(desiredColumn.getName()); + if ( null == currentColumn ) { + log.info( "creating column " + desiredTable.getName() + "." + desiredColumn.getName() ); + alterColumn( desiredTable, desiredColumn, true ); + } else if ( columnsDiffer( desiredColumn, currentColumn ) ) { + if ( modifyColumns ) { + log.info( "altering column " + desiredTable.getName() + "." + desiredColumn.getName() ); + log.info( " desiredColumn=" + desiredColumn.toStringAll() ); + log.info( " currentColumn=" + currentColumn.toStringAll() ); + alterColumn( desiredTable, desiredColumn, false ); + } else { + String text = "Column " + currentColumn.getName() + " in table " + currentTable.getName() + " differs from current specification"; + log.info( text ); + printComment( text ); + } + } + } //for columns + + //@todo add constraints here... + + //hmm, m-w.com says indices and indexes are both okay + //@todo should we check the index fields for differences? + Iterator desiredIndexes = desiredTable.getIndexes().iterator(); + while ( desiredIndexes.hasNext() ) { + Index desiredIndex = (Index) desiredIndexes.next(); + Index currentIndex = currentTable.findIndex(desiredIndex.getName()); + if ( null == currentIndex ) { + log.info( "creating index " + desiredTable.getName() + "." + desiredIndex.getName() ); + writeIndex( desiredTable, desiredIndex ); + } + } + + // Drops /////////////////////// + //@todo drop constraints - probably need names on them for this + + //do any drops of columns + Iterator currentColumns = currentTable.getColumns().iterator(); + while ( currentColumns.hasNext() ) { + Column currentColumn = (Column) currentColumns.next(); + Column desiredColumn = desiredTable.findColumn(currentColumn.getName()); + if ( null == desiredColumn ) { + if ( doDrops ) { + log.info( "dropping column " + currentTable.getName() + "." + currentColumn.getName() ); + dropColumn( currentTable, currentColumn ); + } else { + String text = "Column " + currentColumn.getName() + " can be dropped from table " + currentTable.getName(); + log.info( text ); + printComment( text ); + } + } + } //for columns + + //drop indexes + Iterator currentIndexes = currentTable.getIndexes().iterator(); + while ( currentIndexes.hasNext() ) { + Index currentIndex = (Index) currentIndexes.next(); + Index desiredIndex = desiredTable.findIndex(currentIndex.getName()); + if ( null == desiredIndex ) { + //make sure this isn't the primary key index (mySQL reports this at least) + + Iterator indexColumns = currentIndex.getIndexColumns().iterator(); + boolean isPk = true; + while ( indexColumns.hasNext() ) { + IndexColumn ic = (IndexColumn) indexColumns.next(); + Column c = currentTable.findColumn( ic.getName() ); + if ( !c.isPrimaryKey() ) { + isPk = false; + break; + } + } + + if ( !isPk ) { + log.info( "dropping non-primary index " + currentTable.getName() + "." + currentIndex.getName() ); + dropIndex( currentTable, currentIndex ); + } + } + } + + } //table exists? + } //for tables create + + //check for table drops + for (Iterator iter = currentDb.getTables().iterator(); iter.hasNext(); ) { + Table currentTable = (Table) iter.next(); + Table desiredTable = desiredDb.findTable( currentTable.getName() ); + + if ( desiredTable == null ) { + if ( doDrops ) { + log.info( "dropping table " + currentTable.getName() ); + dropTable( currentTable ); + } else { + String text = "Table " + currentTable.getName() + " can be dropped"; + log.info( text ); + printComment( text ); + } + } + + } //for tables drops + + } + + /** + * Generates the alter statement to add or modify a single column on a table. + * + * @param table The table the index is on + * @param column The column to drop + * @param add true if the column is new, false if it is to be changed + * + * @throws IOException if the statement cannot be written + */ + public void alterColumn( Table table, Column column, boolean add ) throws IOException { + + writeAlterHeader( table ); + + print( add ? "ADD " : "MODIFY " ); + createColumn( table, column ); + printEndOfStatement(); + } + + /** + * Generates the statement to drop an column from a table. + * + * @param table The table the index is on + * @param column The column to drop + * + * @throws IOException if the statement cannot be written + */ + public void dropColumn( Table table, Column column ) throws IOException { + + writeAlterHeader( table ); + + print( "DROP COLUMN " ); + print( column.getName() ); + printEndOfStatement(); + } + + /** + * Generates the first part of the ALTER TABLE statement including the + * table name. + * + * @param table The table being altered + * + * @throws IOException if the statement cannot be written + */ + protected void writeAlterHeader( Table table ) throws IOException { + print("ALTER TABLE "); + println(table.getName()); + + printIndent(); + + } + + /** + * Generates the statement to drop an index from the database. The + * alterTableForDrop property is checked to determine what + * style of drop is generated. + * + * @param table The table the index is on + * @param index The index to drop + * + * @throws IOException if the statement cannot be written + * + * @see SqlBuilder#useAlterTableForDrop + */ + public void dropIndex( Table table, Index index ) throws IOException { + + if ( useAlterTableForDrop() ) { + writeAlterHeader( table ); + } + + print( "DROP INDEX " ); + print( index.getName() ); + + if ( ! useAlterTableForDrop() ) { + print( " ON " ); + print( table.getName() ); + } + + printEndOfStatement(); + } + + /** + * Helper method to determine if two column specifications represent + * different types. Type, nullability, size, scale, default value, + * and precision radix are the attributes checked. Currently default + * values are compared where null and empty string are considered equal. + * See comments in the method body for explanation. + * + * + * @param first First column to compare + * @param second Second column to compare + * @return true if the columns differ + */ + protected boolean columnsDiffer( Column desired, Column current ) { + boolean result = false; + + //The createColumn method leaves off the default clause if column.getDefaultValue() + //is null. mySQL interprets this as a default of "" or 0, and thus the columns + //are always different according to this method. alterDatabase will generate + //an alter statement for the column, but it will be the exact same definition + //as before. In order to avoid this situation I am ignoring the comparison + //if the desired default is null. In order to "un-default" a column you'll + //have to have a default="" or default="0" in the schema xml. + //If this is bad for other databases, it is recommended that the createColumn + //method use a "DEFAULT NULL" statement if that is what is needed. + //A good way to get this would be to require a defaultValue="" in the + //schema xml if you really want null and not just unspecified. + String desiredDefault = desired.getDefaultValue(); + String currentDefault = current.getDefaultValue(); + boolean defaultsEqual = desiredDefault == null || + desiredDefault.equals(currentDefault); + + boolean sizeMatters = desired.getSize() > 0; + + if ( desired.getTypeCode() != current.getTypeCode() || + desired.isRequired() != current.isRequired() || + (sizeMatters && desired.getSize() != current.getSize()) || + desired.getScale() != current.getScale() || + !defaultsEqual || + desired.getPrecisionRadix() != current.getPrecisionRadix() ) + { + result = true; + } + + return result; + } + + /** + * Whether an ALTER TABLE statement is necessary when dropping indexes + * or constraints. The default is false. + * @return true if ALTER TABLE is required + */ + public boolean useAlterTableForDrop() { + return alterTableForDrop; + } + + /** + * Whether an ALTER TABLE statement is necessary when dropping indexes + * or constraints. The default is false. + * @param alterTableForDrop The new value + */ + public void setAlterTableForDrop(boolean alterTableForDrop) { + this.alterTableForDrop = alterTableForDrop; + } + +//used to check for code to be changed when changing signatures +//protected final void printAutoIncrementColumn() throws IOException {}; +//protected final void createColumn(Column column) throws IOException {}; + } 1.6 +3 -2 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- SybaseBuilder.java 13 Oct 2003 08:30:55 -0000 1.5 +++ SybaseBuilder.java 16 Dec 2003 15:09:50 -0000 1.6 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -65,6 +65,7 @@ import java.io.IOException; import java.util.Iterator; +import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.ForeignKey; import org.apache.commons.sql.model.Table; @@ -114,7 +115,7 @@ println( " */" ); } - protected void printAutoIncrementColumn() throws IOException { + protected void printAutoIncrementColumn(Table table, Column column) throws IOException { //print( "AUTO_INCREMENT" ); } } 1.5 +4 -1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/DatabaseWriter.java Index: DatabaseWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/DatabaseWriter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DatabaseWriter.java 3 Dec 2003 21:03:00 -0000 1.4 +++ DatabaseWriter.java 16 Dec 2003 15:09:50 -0000 1.5 @@ -89,6 +89,9 @@ private void init() { setXMLIntrospector( DatabaseReader.newXMLIntrospector() ); enablePrettyPrint(); - getBindingConfiguration().setMapIDs(false); + + // TODO: Remove deprecated call once Betwixt is updated on ibiblio + setWriteIDs(false); + //getBindingConfiguration().setMapIDs(false); } } 1.5 +59 -1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/JdbcModelReader.java Index: JdbcModelReader.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/JdbcModelReader.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- JdbcModelReader.java 13 Oct 2003 08:30:13 -0000 1.4 +++ JdbcModelReader.java 16 Dec 2003 15:09:50 -0000 1.5 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -67,8 +67,10 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Vector; import org.apache.commons.logging.Log; @@ -76,6 +78,8 @@ import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Database; import org.apache.commons.sql.model.ForeignKey; +import org.apache.commons.sql.model.Index; +import org.apache.commons.sql.model.IndexColumn; import org.apache.commons.sql.model.Reference; import org.apache.commons.sql.model.Table; @@ -217,6 +221,13 @@ while (fkIterator.hasNext()) { t.addForeignKey((ForeignKey) fkIterator.next()); } + + Iterator idxIterator = + getIndexesForTable(t.getName()).iterator(); + while (idxIterator.hasNext()) { + t.addIndex((Index) idxIterator.next()); + } + } return tables; } @@ -524,4 +535,51 @@ } return fks; } + + + private List getIndexesForTable(String tableName) throws SQLException { + DatabaseMetaData dbmd = connection.getMetaData(); + + Map indexesByName = new HashMap(); + + ResultSet columnData = null; + try { + columnData = dbmd.getIndexInfo(catalog, schema, tableName, false, false); + } catch ( SQLException e ) { + log.trace("database does not support getIndexInfo()", e); + } + + if ( columnData != null ) { + try { + //can be multiple columns per index + while ( columnData.next() ) { + + String indexName = columnData.getString("INDEX_NAME"); + boolean unique = !columnData.getBoolean("NON_UNIQUE"); + String column = columnData.getString("COLUMN_NAME"); + + Index index = (Index) indexesByName.get(indexName); + if ( index == null ) { + index = new Index(); + index.setName( indexName ); + indexesByName.put( indexName, index ); + index.setUnique( unique ); + } + + IndexColumn ic = new IndexColumn(); + ic.setName( column ); + index.addIndexColumn( ic ); + } + } + finally { + if (columnData != null) { + columnData.close(); + } + } + } + + return new Vector(indexesByName.values()); + } + + } 1.2 +2 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.betwixt Index: Column.betwixt =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.betwixt,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Column.betwixt 28 Oct 2002 16:14:15 -0000 1.1 +++ Column.betwixt 16 Dec 2003 15:09:50 -0000 1.2 @@ -1,7 +1,7 @@ - + - \ No newline at end of file + 1.10 +46 -30 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.java Index: Column.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Column.java 13 Oct 2003 08:30:03 -0000 1.9 +++ Column.java 16 Dec 2003 15:09:50 -0000 1.10 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,13 +56,13 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . - * + * * $Id$ */ package org.apache.commons.sql.model; -public class Column +public class Column { private String name; private boolean primaryKey = false; @@ -75,14 +75,14 @@ private int scale = 0; private int precisionRadix = 10; private int ordinalPosition = 0; - - public Column() + + public Column() { } - - public Column(String name, int typeCode, int size, boolean required, boolean - primaryKey, boolean autoIncrement, String defaultValue) + + public Column(String name, int typeCode, int size, boolean required, boolean + primaryKey, boolean autoIncrement, String defaultValue) { this.name = name; this.typeCode = typeCode; @@ -94,15 +94,15 @@ this.defaultValue = defaultValue; } - public Column(String name, String type, int size, boolean required, boolean - primaryKey, boolean autoIncrement, String defaultValue ) + public Column(String name, String type, int size, boolean required, boolean + primaryKey, boolean autoIncrement, String defaultValue ) { this(name, TypeMap.getJdbcTypeCode(type), size, required, primaryKey, autoIncrement, defaultValue); } - public Column(String name, int typeCode, int size, boolean required, boolean - primaryKey, boolean autoIncrement, String defaultValue, - int scale) + public Column(String name, int typeCode, int size, boolean required, boolean + primaryKey, boolean autoIncrement, String defaultValue, + int scale) { this.name = name; this.typeCode = typeCode; @@ -119,27 +119,43 @@ { return super.toString() + "[name=" + name + ";type=" + type + "]"; } - + + public String toStringAll() + { + return "Column[name=" + name + + ";type=" + type + + ";typeCode=" + typeCode + + ";size=" + size + + ";required=" + required + + ";pk=" + primaryKey + + ";auto=" + autoIncrement + + ";default=" + defaultValue + + ";scale=" + scale + + ";prec=" + precisionRadix + + ";ord=" + ordinalPosition + + "]"; + } + public String getName() { return name; } - + public void setName(String name) { this.name = name; } - + public boolean isPrimaryKey() { return primaryKey; } - + public void setPrimaryKey(boolean primaryKey) { this.primaryKey = primaryKey; - } - + } + public boolean isRequired() { return required; @@ -149,7 +165,7 @@ { this.required = required; } - + public boolean isAutoIncrement() { return autoIncrement; @@ -159,23 +175,23 @@ { this.autoIncrement = autoIncrement; } - + public int getTypeCode() { return typeCode; } - + public void setTypeCode(int typeCode) { this.typeCode = typeCode; this.type = TypeMap.getJdbcTypeName(typeCode); } - public String getType() + public String getType() { return type; } - + /** * Set this columns type by name */ @@ -184,7 +200,7 @@ this.type = type; this.typeCode = TypeMap.getJdbcTypeCode(type); } - + public int getSize() { return size; @@ -199,7 +215,7 @@ { return this.scale; } - + public void setScale(int scale) { this.scale = scale; @@ -209,7 +225,7 @@ { return defaultValue; } - + public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; @@ -219,7 +235,7 @@ { return this.precisionRadix; } - + public void setPrecisionRadix(int precisionRadix) { this.precisionRadix = precisionRadix; @@ -229,12 +245,12 @@ { return this.ordinalPosition; } - + public void setOrdinalPosition(int ordinalPosition) { this.ordinalPosition = ordinalPosition; } - + } 1.6 +46 -25 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Database.java Index: Database.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Database.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Database.java 13 Oct 2003 08:30:03 -0000 1.5 +++ Database.java 16 Dec 2003 15:09:50 -0000 1.6 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,7 +56,7 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . - * + * * $Id$ */ package org.apache.commons.sql.model; @@ -65,38 +65,59 @@ import java.util.Iterator; import java.util.List; -public class Database +/** + * Models a database. + * + * @version $Id$ + * @author John Marshall/Connectria + * @author Matthew Hawthorne + */ +public class Database { private String name; - + private String idMethod; - + + /** Database version id */ + private String version; + private List tables = new ArrayList(); - + public Database() { } - + public String getName() { return name; } - + public void setName(String name) { this.name=name; } - + + public String getVersion() + { + return version; + } + + public void setVersion(String ver) + { + version = ver; + } + + public void setIdMethod(String idMethod) { this.idMethod=idMethod; } - - + + public void addTable(Table table) { tables.add(table); - } + } public List getTables() { @@ -104,42 +125,42 @@ } // Helper methods - + /** * Finds the table with the specified name, using case insensitive matching. - * Note that this method is not called getTable(String) to avoid introspection + * Note that this method is not called getTable(String) to avoid introspection * problems. */ public Table findTable(String name) { - for (Iterator iter = tables.iterator(); iter.hasNext(); ) + for (Iterator iter = tables.iterator(); iter.hasNext(); ) { Table table = (Table) iter.next(); - + // table names are typically case insensitive - if (table.getName().equalsIgnoreCase( name )) + if (table.getName().equalsIgnoreCase( name )) { return table; } } return null; } - - + + // Additions for PropertyUtils - + public void setTable(int index, Table table) { addTable(table); - } + } public Table getTable(int index) { return (Table) tables.get(index); } - - - public String toString() + + + public String toString() { return super.toString() + "[name=" + name + ";tableCount=" + tables.size() + "]"; } 1.4 +13 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Index.java Index: Index.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Index.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- Index.java 13 Oct 2003 08:30:03 -0000 1.3 +++ Index.java 16 Dec 2003 15:09:50 -0000 1.4 @@ -3,7 +3,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -66,6 +66,8 @@ private List indexColumns = new ArrayList(); + private boolean unique = false; + public Index() {} public String getName() @@ -87,4 +89,13 @@ { return indexColumns; } + + public boolean isUnique() { + return unique; + } + + public void setUnique(boolean unique) { + this.unique = unique; + } + } 1.3 +8 -8 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.betwixt Index: Table.betwixt =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.betwixt,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Table.betwixt 28 Oct 2002 19:42:32 -0000 1.2 +++ Table.betwixt 16 Dec 2003 15:09:50 -0000 1.3 @@ -1,8 +1,8 @@ - - - - - - - - \ No newline at end of file + + + + + + + + 1.11 +96 -39 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java Index: Table.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- Table.java 13 Oct 2003 08:30:03 -0000 1.10 +++ Table.java 16 Dec 2003 15:09:50 -0000 1.11 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,7 +56,7 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . - * + * * $Id$ */ package org.apache.commons.sql.model; @@ -65,25 +65,41 @@ import java.util.Iterator; import java.util.List; -public class Table +/** + * Models a table. + * + * @version $Id$ + * @author John Marshall/Connectria + * @author Matthew Hawthorne + */ +import org.apache.commons.collections.iterators.FilterIterator; +import org.apache.commons.collections.Predicate; + +public class Table { + private static final Predicate UNIQUE_PREDICATE = new Predicate() { + public boolean evaluate(Object input) { + return ((Index)input).isUnique(); + } + }; + private String catalog = null; - + private String name = null; private String schema = null; private String remarks = null; - + private String type = null; - + private List columns = new ArrayList(); - + private List foreignKeys = new ArrayList(); private List indexes = new ArrayList(); - - public Table() + + public Table() { } @@ -91,7 +107,7 @@ { return this.catalog; } - + public void setCatalog(String catalog) { this.catalog = catalog; @@ -101,7 +117,7 @@ { return this.remarks; } - + public void setRemarks(String remarks) { this.remarks = remarks; @@ -111,12 +127,12 @@ { return this.schema; } - + public void setSchema(String schema) { this.schema = schema; } - + public String getType() { return (type == null) ? "(null)" : type; @@ -126,17 +142,17 @@ { this.type = type; } - - public String getName() + + public String getName() { return name; } - + public void setName(String name) { this.name=name; } - + public void addColumn(Column column) { columns.add(column); @@ -158,17 +174,17 @@ } } } - + public List getColumns() { return columns; } - + public void addForeignKey(ForeignKey foreignKey) { foreignKeys.add(foreignKey); } - + public List getForeignKeys() { return foreignKeys; @@ -177,18 +193,18 @@ public Column getColumn(int index) { return (Column) columns.get(index); - } + } public ForeignKey getForeignKey(int index) { return (ForeignKey) foreignKeys.get(index); - } - + } + public void addIndex(Index index) { indexes.add(index); } - + public List getIndexes() { return indexes; @@ -199,17 +215,38 @@ return (Index) indexes.get(index); } - +//take this out of Unique is annoying +//this is in here to support in the xml + /** + * Add a unique index to this table + * @param index The unique index + */ + public void addUnique(Unique index) + { + addIndex(index); + } + + /** + * Gets a list of unique indexes on this table. + * @return an Iterator of Index objects where isUnique == true + */ + public Iterator getUniques() + { + return new FilterIterator( indexes.iterator(), UNIQUE_PREDICATE ); + } +//end unique + + // Helper methods - //------------------------------------------------------------------------- - + //------------------------------------------------------------------------- + /** * @return true if there is at least one primary key column * on this table */ - public boolean hasPrimaryKey() + public boolean hasPrimaryKey() { - for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) + for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) { Column column = (Column) iter.next(); if ( column.isPrimaryKey() ) @@ -219,20 +256,20 @@ } return false; } - + /** * Finds the table with the specified name, using case insensitive matching. - * Note that this method is not called getColumn(String) to avoid introspection + * Note that this method is not called getColumn(String) to avoid introspection * problems. */ public Column findColumn(String name) { - for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) + for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) { Column column = (Column) iter.next(); - + // column names are typically case insensitive - if (column.getName().equalsIgnoreCase( name )) + if (column.getName().equalsIgnoreCase( name )) { return column; } @@ -241,13 +278,33 @@ } /** + * Finds the index with the specified name, using case insensitive matching. + * Note that this method is not called getIndex(String) to avoid introspection + * problems. + */ + public Index findIndex(String name) + { + for (Iterator iter = getIndexes().iterator(); iter.hasNext(); ) + { + Index index = (Index) iter.next(); + + // column names are typically case insensitive + if (index.getName().equalsIgnoreCase( name )) + { + return index; + } + } + return null; + } + + /** * @return a List of primary key columns or an empty list if there are no * primary key columns for this Table - */ - public List getPrimaryKeyColumns() + */ + public List getPrimaryKeyColumns() { List answer = new ArrayList(); - for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) + for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) { Column column = (Column) iter.next(); if ( column.isPrimaryKey() ) @@ -261,9 +318,9 @@ /** * @return the auto increment column, if there is one, otherwise null is returned */ - public Column getAutoIncrementColumn() + public Column getAutoIncrementColumn() { - for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) + for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) { Column column = (Column) iter.next(); if ( column.isAutoIncrement() ) 1.4 +6 -4 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/type/TypesWriter.java Index: TypesWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/type/TypesWriter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TypesWriter.java 3 Dec 2003 21:03:00 -0000 1.3 +++ TypesWriter.java 16 Dec 2003 15:09:51 -0000 1.4 @@ -1,7 +1,7 @@ /* - * $Header$ - * $Revision$ - * $Date$ + * /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/type/TypesWriter.java,v 1.3 2003/12/03 21:03:00 matth Exp + * 1.3 + * 2003/12/03 21:03:00 * * ==================================================================== * @@ -91,6 +91,8 @@ private void init() { setXMLIntrospector(TypesReader.newXMLIntrospector()); enablePrettyPrint(); - getBindingConfiguration().setMapIDs(false); + // TODO: Remove deprecated call once Betwixt is updated on ibiblio + setWriteIDs(false); + //getBindingConfiguration().setMapIDs(false); } } 1.4 +17 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DDLExecutor.java Index: DDLExecutor.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DDLExecutor.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- DDLExecutor.java 13 Oct 2003 08:30:24 -0000 1.3 +++ DDLExecutor.java 16 Dec 2003 15:09:51 -0000 1.4 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -97,6 +97,10 @@ public DDLExecutor() { } + public DDLExecutor(DataSource dataSource) { + super(dataSource); + } + public DDLExecutor(DataSource dataSource, SqlBuilder sqlBuilder) { super(dataSource); this.sqlBuilder = sqlBuilder; @@ -177,7 +181,18 @@ // Implementation methods //------------------------------------------------------------------------- - protected void evaluateBatch(String sql) throws SQLException { + /** + * Executes a series of sql statements. It is assumed that the parameter + * string contains sql statements separated by a semicolon. + * + * @todo consider outputting a collection of String or some kind of statement + * object from the SqlBuilder instead of having to parse strings here + * + * @param sql A list of sql statements + * + * @throws SQLException if an error occurs and isContinueOnError == false + */ + public void evaluateBatch(String sql) throws SQLException { Connection connection = borrowConnection(); Statement statement = null; int errors = 0; 1.4 +26 -3 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DataSourceWrapper.java Index: DataSourceWrapper.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/util/DataSourceWrapper.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- DataSourceWrapper.java 13 Oct 2003 08:30:24 -0000 1.3 +++ DataSourceWrapper.java 16 Dec 2003 15:09:51 -0000 1.4 @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 1999 The Apache Software Foundation. All rights + * Copyright (c) 1999,2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -81,15 +81,38 @@ private String userName; private String password; + /** + * Default constructor + */ + public DataSourceWrapper() { + } + + public DataSourceWrapper(String driverClassName, String jdbcURL) + throws ClassNotFoundException { + + this(driverClassName, jdbcURL, null, null); + } + + public DataSourceWrapper(String driverClassName, String jdbcURL, String userName, String password) + throws ClassNotFoundException { + + setDriverClassName(driverClassName); + setJdbcURL(jdbcURL); + setUserName(userName); + setPassword(password); + } + public void setDriverClassName(String driverClassName) - throws ClassNotFoundException, InstantiationException, IllegalAccessException { + throws ClassNotFoundException { if (log.isDebugEnabled()) { log.debug("Loading JDBC driver: [" + driverClassName + "]"); } this.driverClassName = driverClassName; - getClass().getClassLoader().loadClass(driverClassName).newInstance(); +//changed to get rid of instantiation and access exceptions +// getClass().getClassLoader().loadClass(driverClassName).newInstance(); + Class c = Class.forName(driverClassName, true, getClass().getClassLoader()); } public void setJdbcURL(String jdbcURL) { --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org