Return-Path: Delivered-To: apmail-db-ddlutils-dev-archive@www.apache.org Received: (qmail 31471 invoked from network); 27 Aug 2005 21:51:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Aug 2005 21:51:07 -0000 Received: (qmail 77502 invoked by uid 500); 27 Aug 2005 21:51:06 -0000 Delivered-To: apmail-db-ddlutils-dev-archive@db.apache.org Received: (qmail 77438 invoked by uid 500); 27 Aug 2005 21:51:06 -0000 Mailing-List: contact ddlutils-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ddlutils-dev@db.apache.org Delivered-To: mailing list ddlutils-dev@db.apache.org Received: (qmail 77411 invoked by uid 500); 27 Aug 2005 21:51:05 -0000 Delivered-To: apmail-db-ddlutils-commits@db.apache.org Received: (qmail 77404 invoked by uid 99); 27 Aug 2005 21:51:05 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sat, 27 Aug 2005 14:51:03 -0700 Received: (qmail 31453 invoked by uid 65534); 27 Aug 2005 21:51:02 -0000 Message-ID: <20050827215102.31452.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r240460 [1/2] - in /db/ddlutils/trunk: ./ lib/ src/java/ src/java/org/apache/ddlutils/ src/java/org/apache/ddlutils/builder/ src/java/org/apache/ddlutils/dynabean/ src/java/org/apache/ddlutils/io/ src/java/org/apache/ddlutils/model/ src/jav... Date: Sat, 27 Aug 2005 21:50:57 -0000 To: ddlutils-commits@db.apache.org From: tomdz@apache.org X-Mailer: svnmailer-1.0.4 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: tomdz Date: Sat Aug 27 14:44:57 2005 New Revision: 240460 URL: http://svn.apache.org/viewcvs?rev=240460&view=rev Log: (empty) Added: db/ddlutils/trunk/lib/commons-betwixt-0.7.jar (with props) db/ddlutils/trunk/src/java/mapping.xml db/ddlutils/trunk/src/java/org/apache/ddlutils/DdlUtilsException.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseIO.java - copied, changed from r226549, db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseReader.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelException.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java - copied, changed from r226549, db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Unique.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java Removed: db/ddlutils/trunk/lib/commons-betwixt-0.5.jar db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseReader.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseWriter.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.betwixt db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.betwixt db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Unique.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueColumn.java Modified: db/ddlutils/trunk/.classpath db/ddlutils/trunk/src/java/org/apache/ddlutils/DynaSqlException.java db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/InterbaseBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/PostgreSqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/SqlDynaClass.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataDtdWriter.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataReader.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataWriter.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/JdbcModelReader.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToFileCommand.java db/ddlutils/trunk/src/test/org/apache/ddlutils/TestPlatformBase.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDataReader.java Modified: db/ddlutils/trunk/.classpath URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/.classpath?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/.classpath (original) +++ db/ddlutils/trunk/.classpath Sat Aug 27 14:44:57 2005 @@ -5,7 +5,6 @@ - @@ -13,5 +12,6 @@ + Added: db/ddlutils/trunk/lib/commons-betwixt-0.7.jar URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/lib/commons-betwixt-0.7.jar?rev=240460&view=auto ============================================================================== Binary file - no diff available. Propchange: db/ddlutils/trunk/lib/commons-betwixt-0.7.jar ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: db/ddlutils/trunk/src/java/mapping.xml URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/mapping.xml?rev=240460&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/mapping.xml (added) +++ db/ddlutils/trunk/src/java/mapping.xml Sat Aug 27 14:44:57 2005 @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/DdlUtilsException.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/DdlUtilsException.java?rev=240460&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/DdlUtilsException.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/DdlUtilsException.java Sat Aug 27 14:44:57 2005 @@ -0,0 +1,68 @@ +package org.apache.ddlutils; + +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.commons.lang.exception.NestableRuntimeException; + +/** + * Base class for DdlUtils exceptions + */ +public class DdlUtilsException extends NestableRuntimeException +{ + /** Constant for serializing instances of this class */ + private static final long serialVersionUID = 5624776387174310551L; + + /** + * Creates a new empty exception object. + */ + public DdlUtilsException() + { + super(); + } + + /** + * Creates a new exception object. + * + * @param msg The exception message + */ + public DdlUtilsException(String msg) + { + super(msg); + } + + /** + * Creates a new exception object. + * + * @param baseEx The base exception + */ + public DdlUtilsException(Throwable baseEx) + { + super(baseEx); + } + + /** + * Creates a new exception object. + * + * @param msg The exception message + * @param baseEx The base exception + */ + public DdlUtilsException(String msg, Throwable baseEx) + { + super(msg, baseEx); + } + +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/DynaSqlException.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/DynaSqlException.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/DynaSqlException.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/DynaSqlException.java Sat Aug 27 14:44:57 2005 @@ -1,13 +1,28 @@ package org.apache.ddlutils; -import org.apache.commons.lang.exception.NestableRuntimeException; +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /** * Is thrown when by the {@link org.apache.ddlutils.dynabean.DynaSql} and * related classes when a database operation fails, but a normal * {@link java.sql.SQLException} cannot be generated. */ -public class DynaSqlException extends NestableRuntimeException +public class DynaSqlException extends DdlUtilsException { /** Constant for serializing instances of this class */ private static final long serialVersionUID = 7524362294381844776L; Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/InterbaseBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/InterbaseBuilder.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/InterbaseBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/InterbaseBuilder.java Sat Aug 27 14:44:57 2005 @@ -69,7 +69,7 @@ protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException { super.writeExternalForeignKeyCreateStmt(database, table, key); - if (key.getForeignTable() != null) + if (key.getForeignTableName() != null) { print("COMMIT"); printEndOfStatement(); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/PostgreSqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/PostgreSqlBuilder.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/PostgreSqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/PostgreSqlBuilder.java Sat Aug 27 14:44:57 2005 @@ -18,7 +18,6 @@ import java.io.IOException; import java.sql.Types; -import java.util.Iterator; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -60,9 +59,9 @@ */ public void createTable(Database database, Table table) throws IOException { - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); if (column.isAutoIncrement()) { Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java Sat Aug 27 14:44:57 2005 @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Locale; import java.util.Map; @@ -252,9 +251,9 @@ dropTables(database); } - for (Iterator it = database.getTables().iterator(); it.hasNext(); ) + for (int idx = 0; idx < database.getTableCount(); idx++) { - Table table = (Table)it.next(); + Table table = database.getTable(idx); writeTableComment(table); createTable(database, table); @@ -292,9 +291,9 @@ { ArrayList newTables = new ArrayList(); - for (Iterator tableIt = desiredModel.getTables().iterator(); tableIt.hasNext();) + for (int tableIdx = 0; tableIdx < desiredModel.getTableCount(); tableIdx++) { - Table desiredTable = (Table)tableIt.next(); + Table desiredTable = desiredModel.getTable(tableIdx); Table currentTable = currentModel.findTable(desiredTable.getName()); if (currentTable == null) @@ -309,9 +308,9 @@ } else { - for (Iterator columnIt = desiredTable.getColumns().iterator(); columnIt.hasNext();) + for (int columnIdx = 0; columnIdx < desiredTable.getColumnCount(); columnIdx++) { - Column desiredColumn = (Column)columnIt.next(); + Column desiredColumn = desiredTable.getColumn(columnIdx); Column currentColumn = currentTable.findColumn(desiredColumn.getName()); if (null == currentColumn) @@ -348,8 +347,9 @@ } // add fk constraints - for (Iterator fkIt = desiredTable.getForeignKeys().iterator(); fkIt.hasNext();) { - ForeignKey desiredFk = (ForeignKey) fkIt.next(); + for (int fkIdx = 0; fkIdx < desiredTable.getForeignKeyCount(); fkIdx++) + { + ForeignKey desiredFk = desiredTable.getForeignKey(fkIdx); ForeignKey currentFk = currentTable.findForeignKey(desiredFk); if ( currentFk == null ) { if (_log.isInfoEnabled()) @@ -362,9 +362,9 @@ // TODO: should we check the index fields for differences? //create new indexes - for (Iterator indexIt = desiredTable.getIndexes().iterator(); indexIt.hasNext();) + for (int indexIdx = 0; indexIdx < desiredTable.getIndexCount(); indexIdx++) { - Index desiredIndex = (Index)indexIt.next(); + Index desiredIndex = desiredTable.getIndex(indexIdx); Index currentIndex = currentTable.findIndex(desiredIndex.getName()); if (null == currentIndex) @@ -378,8 +378,9 @@ } // drop fk constraints - for (Iterator fkIt = currentTable.getForeignKeys().iterator(); fkIt.hasNext();) { - ForeignKey currentFk = (ForeignKey) fkIt.next(); + for (int fkIdx = 0; fkIdx < currentTable.getForeignKeyCount(); fkIdx++) + { + ForeignKey currentFk = currentTable.getForeignKey(fkIdx); ForeignKey desiredFk = desiredTable.findForeignKey(currentFk); if ( desiredFk == null ) { @@ -395,9 +396,9 @@ //Drop columns - for (Iterator columnIt = currentTable.getColumns().iterator(); columnIt.hasNext();) + for (int columnIdx = 0; columnIdx < currentTable.getColumnCount(); columnIdx++) { - Column currentColumn = (Column)columnIt.next(); + Column currentColumn = currentTable.getColumn(columnIdx); Column desiredColumn = desiredTable.findColumn(currentColumn.getName()); if (null == desiredColumn) @@ -424,9 +425,9 @@ } //Drop indexes - for (Iterator indexIt = currentTable.getIndexes().iterator(); indexIt.hasNext();) + for (int indexIdx = 0; indexIdx < currentTable.getIndexCount(); indexIdx++) { - Index currentIndex = (Index)indexIt.next(); + Index currentIndex = currentTable.getIndex(indexIdx); Index desiredIndex = desiredTable.findIndex(currentIndex.getName()); if (null == desiredIndex) @@ -434,7 +435,7 @@ // make sure this isn't the primary key index boolean isPk = true; - for (Iterator columnIt = currentIndex.getIndexColumns().iterator(); columnIt.hasNext();) + for (Iterator columnIt = currentIndex.getColumns().iterator(); columnIt.hasNext();) { IndexColumn indexColumn = (IndexColumn)columnIt.next(); Column column = currentTable.findColumn(indexColumn.getName()); @@ -470,9 +471,9 @@ } // check for table drops - for (Iterator tableIt = currentModel.getTables().iterator(); tableIt.hasNext();) + for (int idx = 0; idx < currentModel.getTableCount(); idx++) { - Table currentTable = (Table)tableIt.next(); + Table currentTable = currentModel.getTable(idx); Table desiredTable = desiredModel.findTable(currentTable.getName()); if ((desiredTable == null) && (currentTable.getName() != null) && (currentTable.getName().length() > 0)) @@ -547,9 +548,9 @@ */ public void createExternalForeignKeys(Database database) throws IOException { - for (Iterator it = database.getTables().iterator(); it.hasNext(); ) + for (int idx = 0; idx < database.getTableCount(); idx++) { - createExternalForeignKeys(database, (Table)it.next()); + createExternalForeignKeys(database, database.getTable(idx)); } } @@ -563,11 +564,9 @@ { if (!getPlatformInfo().isForeignKeysEmbedded()) { - int numKey = 1; - - for (Iterator it = table.getForeignKeys().iterator(); it.hasNext(); numKey++) + for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { - writeExternalForeignKeyCreateStmt(database, table, (ForeignKey)it.next()); + writeExternalForeignKeyCreateStmt(database, table, table.getForeignKey(idx)); } } } @@ -579,12 +578,10 @@ */ public void dropTables(Database database) throws IOException { - List tables = database.getTables(); - // we're dropping the external foreignkeys first - for (int idx = tables.size() - 1; idx >= 0; idx--) + for (int idx = database.getTableCount() - 1; idx >= 0; idx--) { - Table table = (Table)tables.get(idx); + Table table = database.getTable(idx); if ((table.getName() != null) && (table.getName().length() > 0)) @@ -597,9 +594,9 @@ // TODO: It might be more useful to either (or both) // * determine an order in which the tables can be dropped safely (via the foreignkeys) // * alter the tables first to drop the internal foreignkeys - for (int idx = tables.size() - 1; idx >= 0; idx--) + for (int idx = database.getTableCount() - 1; idx >= 0; idx--) { - Table table = (Table)tables.get(idx); + Table table = database.getTable(idx); if ((table.getName() != null) && (table.getName().length() > 0)) @@ -631,11 +628,9 @@ { if (!getPlatformInfo().isForeignKeysEmbedded()) { - int numKey = 1; - - for (Iterator it = table.getForeignKeys().iterator(); it.hasNext(); numKey++) + for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { - writeExternalForeignKeyDropStmt(table, (ForeignKey)it.next()); + writeExternalForeignKeyDropStmt(table, table.getForeignKey(idx)); } } } @@ -659,9 +654,9 @@ buffer.append(getTableName(table)); buffer.append(" ("); - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); if (columnValues.containsKey(column.getName())) { @@ -690,9 +685,9 @@ else { addComma = false; - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); if (columnValues.containsKey(column.getName())) { @@ -729,9 +724,9 @@ buffer.append(getTableName(table)); buffer.append(" SET "); - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); if (!column.isPrimaryKey() && columnValues.containsKey(column.getName())) { @@ -754,9 +749,9 @@ } buffer.append(" WHERE "); addSep = false; - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); if (column.isPrimaryKey() && columnValues.containsKey(column.getName())) { @@ -922,11 +917,11 @@ */ protected void writeColumns(Table table) throws IOException { - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { printIndent(); - writeColumn(table, (Column)it.next()); - if (it.hasNext()) + writeColumn(table, table.getColumn(idx)); + if (idx < table.getColumnCount() - 1) { println(","); } @@ -1195,7 +1190,7 @@ protected String getForeignKeyName(ForeignKey fk) { //table and local column should be sufficient - is it possible for one //column to reference multiple tables - return fk.firstReference().getLocal() + "_" + fk.getForeignTable(); + return fk.getFirstReference().getLocalColumnName() + "_" + fk.getForeignTableName(); } /** @@ -1234,9 +1229,9 @@ */ protected void writeEmbeddedPrimaryKeysStmt(Table table) throws IOException { - List primaryKeyColumns = table.getPrimaryKeyColumns(); + Column[] primaryKeyColumns = table.getPrimaryKeyColumns(); - if (!primaryKeyColumns.isEmpty() && shouldGeneratePrimaryKeys(primaryKeyColumns)) + if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns)) { println(","); printIndent(); @@ -1251,9 +1246,9 @@ */ protected void writeExternalPrimaryKeysCreateStmt(Table table) throws IOException { - List primaryKeyColumns = table.getPrimaryKeyColumns(); + Column[] primaryKeyColumns = table.getPrimaryKeyColumns(); - if (!primaryKeyColumns.isEmpty() && shouldGeneratePrimaryKeys(primaryKeyColumns)) + if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns)) { print("ALTER TABLE "); println(getTableName(table)); @@ -1275,11 +1270,11 @@ * @param primaryKeyColumns The pk columns * @return true if a pk statement should be generated for the columns */ - protected boolean shouldGeneratePrimaryKeys(List primaryKeyColumns) + protected boolean shouldGeneratePrimaryKeys(Column[] primaryKeyColumns) { - for (Iterator it = primaryKeyColumns.iterator(); it.hasNext();) + for (int idx = 0; idx < primaryKeyColumns.length; idx++) { - if (!((Column)it.next()).isAutoIncrement()) + if (!primaryKeyColumns[idx].isAutoIncrement()) { return true; } @@ -1293,13 +1288,13 @@ * @param table The table * @param primaryKeyColumns The primary columns */ - protected void writePrimaryKeyStmt(Table table, List primaryKeyColumns) throws IOException + protected void writePrimaryKeyStmt(Table table, Column[] primaryKeyColumns) throws IOException { print("PRIMARY KEY ("); - for (Iterator it = primaryKeyColumns.iterator(); it.hasNext();) + for (int idx = 0; idx < primaryKeyColumns.length; idx++) { - print(getColumnName((Column)it.next())); - if (it.hasNext()) + print(getColumnName(primaryKeyColumns[idx])); + if (idx < primaryKeyColumns.length - 1) { print(", "); } @@ -1326,9 +1321,9 @@ */ protected void writeExternalIndicesCreateStmt(Table table) throws IOException { - for (Iterator it = table.getIndexes().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getIndexCount(); idx++) { - writeExternalIndexCreateStmt(table, (Index)it.next()); + writeExternalIndexCreateStmt(table, table.getIndex(idx)); } } @@ -1367,7 +1362,7 @@ print(getTableName(table)); print(" ("); - for (Iterator it = index.getIndexColumns().iterator(); it.hasNext();) + for (Iterator it = index.getColumns().iterator(); it.hasNext();) { IndexColumn idxColumn = (IndexColumn)it.next(); @@ -1419,13 +1414,11 @@ */ protected void writeEmbeddedForeignKeysStmt(Database database, Table table) throws IOException { - int numKey = 1; - - for (Iterator it = table.getForeignKeys().iterator(); it.hasNext(); numKey++) + for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { - ForeignKey key = (ForeignKey)it.next(); + ForeignKey key = table.getForeignKey(idx); - if (key.getForeignTable() == null) + if (key.getForeignTableName() == null) { _log.warn("Foreign key table is null for key " + key); } @@ -1437,13 +1430,13 @@ if (getPlatformInfo().isEmbeddedForeignKeysNamed()) { print("CONSTRAINT "); - print(getConstraintName(null, table, "FK", Integer.toString(numKey))); + print(getConstraintName(null, table, "FK", Integer.toString(idx))); print(" "); } print("FOREIGN KEY ("); writeLocalReferences(key); print(") REFERENCES "); - print(getTableName(database.findTable(key.getForeignTable()))); + print(getTableName(database.findTable(key.getForeignTableName()))); print(" ("); writeForeignReferences(key); print(")"); @@ -1461,7 +1454,7 @@ */ protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException { - if (key.getForeignTable() == null) + if (key.getForeignTableName() == null) { _log.warn("Foreign key table is null for key " + key); } @@ -1474,7 +1467,7 @@ print(" FOREIGN KEY ("); writeLocalReferences(key); print(") REFERENCES "); - print(getTableName(database.findTable(key.getForeignTable()))); + print(getTableName(database.findTable(key.getForeignTableName()))); print(" ("); writeForeignReferences(key); print(")"); @@ -1491,7 +1484,7 @@ { for (Iterator it = key.getReferences().iterator(); it.hasNext();) { - print(((Reference)it.next()).getLocal()); + print(((Reference)it.next()).getLocalColumnName()); if (it.hasNext()) { print(", "); @@ -1508,7 +1501,7 @@ { for (Iterator it = key.getReferences().iterator(); it.hasNext();) { - print(((Reference)it.next()).getForeign()); + print(((Reference)it.next()).getForeignColumnName()); if (it.hasNext()) { print(", "); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/SqlDynaClass.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/SqlDynaClass.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/SqlDynaClass.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/SqlDynaClass.java Sat Aug 27 14:44:57 2005 @@ -17,14 +17,12 @@ */ import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.apache.commons.beanutils.BasicDynaClass; import org.apache.commons.beanutils.DynaProperty; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.Table; /** @@ -46,13 +44,17 @@ /** * Creates a new SqlDynaClass instance from a Table model. */ - public static SqlDynaClass newInstance(Table table) { + public static SqlDynaClass newInstance(Table table) + { List properties = new ArrayList(); - for (Iterator iter = table.getColumns().iterator(); iter.hasNext(); ) { - Column column = (Column) iter.next(); - properties.add( new SqlDynaProperty(column)); + + for (int idx = 0; idx < table.getColumnCount(); idx++) + { + properties.add(new SqlDynaProperty(table.getColumn(idx))); } + SqlDynaProperty[] array = new SqlDynaProperty[properties.size()]; + properties.toArray(array); return new SqlDynaClass(table, array); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataDtdWriter.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataDtdWriter.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataDtdWriter.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataDtdWriter.java Sat Aug 27 14:44:57 2005 @@ -19,7 +19,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; -import java.util.Iterator; import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.Database; @@ -47,12 +46,12 @@ writer.println("\n"); writer.println(""); - for (Iterator tableIt = model.getTables().iterator(); tableIt.hasNext();) + for (int idx = 0; idx < model.getTableCount(); idx++) { - writeTableElement((Table)tableIt.next(), writer); + writeTableElement(model.getTable(idx), writer); } } @@ -78,11 +77,10 @@ { writer.println("\n"); writer.println(""); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataReader.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataReader.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataReader.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataReader.java Sat Aug 27 14:44:57 2005 @@ -18,7 +18,6 @@ import java.sql.Types; import java.util.HashMap; -import java.util.Iterator; import org.apache.commons.digester.Digester; import org.apache.ddlutils.io.converters.DateConverter; @@ -200,16 +199,16 @@ rules.setCaseSensitive(isCaseSensitive()); setRules(rules); - for (Iterator tableIt = _model.getTables().iterator(); tableIt.hasNext();) + for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) { // TODO: For now we hardcode the root as 'data' but ultimately we should wildcard it ('?') - Table table = (Table)tableIt.next(); + Table table = _model.getTable(tableIdx); String path = "data/"+table.getName(); addRule(path, new DynaSqlCreateRule(_model, table, _sink)); - for (Iterator columnIt = table.getColumns().iterator(); columnIt.hasNext();) + for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) { - Column column = (Column)columnIt.next(); + Column column = (Column)table.getColumn(columnIdx); SqlTypeConverter converter = getRegisteredConverter(table, column); addRule(path, new SetColumnPropertyRule(column, converter, isCaseSensitive())); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java Sat Aug 27 14:44:57 2005 @@ -147,17 +147,17 @@ // lists of already-processed identities for these tables _processedIdentities.clear(); _waitingObjects.clear(); - for (Iterator tableIt = _model.getTables().iterator(); tableIt.hasNext();) + for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) { - Table curTable = (Table)tableIt.next(); + Table table = _model.getTable(tableIdx); - for (Iterator fkIt = curTable.getForeignKeys().iterator(); fkIt.hasNext();) + for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { - ForeignKey curFk = (ForeignKey)fkIt.next(); + ForeignKey curFk = table.getForeignKey(fkIdx); - if (!_processedIdentities.containsKey(curFk.getForeignTable())) + if (!_processedIdentities.containsKey(curFk.getForeignTableName())) { - _processedIdentities.put(curFk.getForeignTable(), new HashSet()); + _processedIdentities.put(curFk.getForeignTableName(), new HashSet()); } } } @@ -178,18 +178,18 @@ { Table table = _model.getDynaClassFor(bean).getTable(); - if (!table.getForeignKeys().isEmpty()) + if (table.getForeignKeyCount() > 0) { WaitingObject waitingObj = new WaitingObject(bean); - for (Iterator fkIt = table.getForeignKeys().iterator(); fkIt.hasNext();) + for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { - ForeignKey fk = (ForeignKey)fkIt.next(); + ForeignKey fk = table.getForeignKey(idx); Identity fkIdentity = buildIdentityFromFK(table, fk, bean); if (fkIdentity != null) { - HashSet identitiesForTable = (HashSet)_processedIdentities.get(fk.getForeignTable()); + HashSet identitiesForTable = (HashSet)_processedIdentities.get(fk.getForeignTableName()); if (!identitiesForTable.contains(fkIdentity)) { @@ -303,20 +303,20 @@ { Reference ref = (Reference)it.next(); - result.append(ref.getLocal()); + result.append(ref.getLocalColumnName()); if (it.hasNext()) { result.append(","); } } result.append("]->"); - result.append(fk.getForeignTable()); + result.append(fk.getForeignTableName()); result.append("["); for (Iterator it = fk.getReferences().iterator(); it.hasNext();) { Reference ref = (Reference)it.next(); - result.append(ref.getForeign()); + result.append(ref.getForeignColumnName()); if (it.hasNext()) { result.append(","); @@ -339,9 +339,9 @@ { Identity identity = new Identity(table.getName()); - for (Iterator it = table.getPrimaryKeyColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); identity.setIdentityColumn(column.getName(), bean.get(column.getName())); } @@ -359,18 +359,18 @@ */ private Identity buildIdentityFromFK(Table owningTable, ForeignKey fk, DynaBean bean) { - Identity identity = new Identity(fk.getForeignTable(), getFKName(owningTable, fk)); + Identity identity = new Identity(fk.getForeignTableName(), getFKName(owningTable, fk)); for (Iterator refIt = fk.getReferences().iterator(); refIt.hasNext();) { Reference reference = (Reference)refIt.next(); - Object value = bean.get(reference.getLocal()); + Object value = bean.get(reference.getLocalColumnName()); if (value == null) { return null; } - identity.setIdentityColumn(reference.getForeign(), value); + identity.setIdentityColumn(reference.getForeignColumnName(), value); } return identity; } @@ -389,11 +389,11 @@ Table targetTable = ((SqlDynaClass)referencedBean.getDynaClass()).getTable(); ForeignKey fk = null; - for (Iterator it = sourceTable.getForeignKeys().iterator(); it.hasNext();) + for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++) { - ForeignKey curFk = (ForeignKey)it.next(); + ForeignKey curFk = sourceTable.getForeignKey(idx); - if (curFk.getForeignTable().equalsIgnoreCase(targetTable.getName())) + if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getName())) { if (fkName.equals(getFKName(sourceTable, curFk))) { @@ -407,8 +407,8 @@ for (Iterator it = fk.getReferences().iterator(); it.hasNext();) { Reference curRef = (Reference)it.next(); - Column sourceColumn = sourceTable.findColumn(curRef.getLocal()); - Column targetColumn = targetTable.findColumn(curRef.getForeign()); + Column sourceColumn = sourceTable.findColumn(curRef.getLocalColumnName()); + Column targetColumn = targetTable.findColumn(curRef.getForeignColumnName()); referencingBean.set(sourceColumn.getName(), referencedBean.get(targetColumn.getName())); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataWriter.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataWriter.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataWriter.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataWriter.java Sat Aug 27 14:44:57 2005 @@ -110,9 +110,9 @@ Table table = dynaClass.getTable(); _output.println(" <" + table.getName()); - for (Iterator it = table.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < table.getColumnCount(); idx++) { - Column column = (Column)it.next(); + Column column = table.getColumn(idx); Object value = bean.get(column.getName()); if (value != null) Copied: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseIO.java (from r226549, db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseReader.java) URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseIO.java?p2=db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseIO.java&p1=db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseReader.java&r1=226549&r2=240460&rev=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseReader.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DatabaseIO.java Sat Aug 27 14:44:57 2005 @@ -17,33 +17,38 @@ */ import java.beans.IntrospectionException; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; -import org.apache.commons.betwixt.XMLIntrospector; import org.apache.commons.betwixt.io.BeanReader; +import org.apache.commons.betwixt.io.BeanWriter; import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; +import org.apache.ddlutils.DdlUtilsException; import org.apache.ddlutils.model.Database; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; /** - * This class parsers XML and creates a fully populated Database bean. - * This class is-a Digester and so can support configuration via custom rules. + * This class provides functions to read and write database models from/to XML. * * @author James Strachan + * @author Matthew Hawthorne + * @author Thomas Dudziak * @version $Revision$ */ -public class DatabaseReader extends BeanReader +public class DatabaseIO { /** Whether to use the internal dtd that comes with commons-sql */ - private boolean _useInternalDtd = false; - - public DatabaseReader() throws IntrospectionException - { - setXMLIntrospector(newXMLIntrospector()); - registerBeanClass(Database.class); - setValidating(false); - } + private boolean _useInternalDtd = true; /** - * Returns whether the internal dtd that comes with commons-sql is used. + * Returns whether the internal dtd that comes with DdlUtils is used. * * @return true if parsing uses the internal dtd */ @@ -60,32 +65,166 @@ public void setUseInternalDtd(boolean useInternalDtd) { _useInternalDtd = useInternalDtd; - if (_useInternalDtd) + } + + /** + * Returns a new bean reader configured to read database models. + * + * @return The reader + */ + protected BeanReader getReader() throws IntrospectionException, SAXException, IOException + { + BeanReader reader = new BeanReader(); + + reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); + reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); + reader.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper()); + if (isUseInternalDtd()) { - setEntityResolver(new LocalEntityResolver()); + reader.setEntityResolver(new LocalEntityResolver()); } - else + reader.registerMultiMapping(new InputSource(getClass().getResourceAsStream("/mapping.xml"))); + + return reader; + } + + /** + * Returns a new bean writer configured to writer database models. + * + * @param output The target output writer + * @return The writer + */ + protected BeanWriter getWriter(Writer output) throws IntrospectionException, SAXException, IOException + { + BeanWriter writer = new BeanWriter(output); + + writer.getXMLIntrospector().register(new InputSource(getClass().getResourceAsStream("/mapping.xml"))); + writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); + writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); + writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper()); + writer.getBindingConfiguration().setMapIDs(false); + writer.enablePrettyPrint(); + + return writer; + } + + /** + * Reads the database model contained in the specified file. + * + * @param filename The model file name + * @return The database model + */ + public Database read(String filename) throws DdlUtilsException + { + Database model = null; + + try + { + model = (Database)getReader().parse(filename); + } + catch (Exception ex) { - setEntityResolver(this); + throw new DdlUtilsException(ex); } + model.initialize(); + return model; } /** - * A factory method to create the default introspector used to turn - * the Database object model into XML - */ - protected static XMLIntrospector newXMLIntrospector() + * Reads the database model contained in the specified file. + * + * @param file The model file + * @return The database model + */ + public Database read(File file) throws DdlUtilsException { - XMLIntrospector introspector = new XMLIntrospector(); + Database model = null; - // configure the style of the XML, to brief and attribute based - introspector.setAttributesForPrimitives(true); - introspector.setWrapCollectionsInElement(false); + try + { + model = (Database)getReader().parse(file); + } + catch (Exception ex) + { + throw new DdlUtilsException(ex); + } + model.initialize(); + return model; + } - // set the mixed case name mapper - introspector.setElementNameMapper(new HyphenatedNameMapper()); - //introspector.setElementNameMapper(new DecapitalizeNameMapper()); + /** + * Reads the database model given by the reader. + * + * @param reader The reader that returns the model XML + * @return The database model + */ + public Database read(Reader reader) throws DdlUtilsException + { + Database model = null; + + try + { + model = (Database)getReader().parse(reader); + } + catch (Exception ex) + { + throw new DdlUtilsException(ex); + } + model.initialize(); + return model; + } + + /** + * Writes the database model to the specified file. + * + * @param model The database model + * @param filename The model file name + */ + public void write(Database model, String filename) throws DdlUtilsException + { + try + { + getWriter(new BufferedWriter(new FileWriter(filename))).write(model); + } + catch (Exception ex) + { + throw new DdlUtilsException(ex); + } + } - return introspector; + /** + * Writes the database model to the given output stream. + * + * @param model The database model + * @param output The output stream + */ + public void write(Database model, OutputStream output) throws DdlUtilsException + { + try + { + getWriter(new OutputStreamWriter(output)).write(model); + } + catch (Exception ex) + { + throw new DdlUtilsException(ex); + } + } + + /** + * Writes the database model to the given output writer. + * + * @param model The database model + * @param output The output writer + */ + public void write(Database model, Writer output) throws DdlUtilsException + { + try + { + getWriter(output).write(model); + } + catch (Exception ex) + { + throw new DdlUtilsException(ex); + } } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/JdbcModelReader.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/JdbcModelReader.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/JdbcModelReader.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/JdbcModelReader.java Sat Aug 27 14:44:57 2005 @@ -38,6 +38,8 @@ import org.apache.ddlutils.model.Database; import org.apache.ddlutils.model.ForeignKey; import org.apache.ddlutils.model.Index; +import org.apache.ddlutils.model.UniqueIndex; +import org.apache.ddlutils.model.NonUniqueIndex; import org.apache.ddlutils.model.IndexColumn; import org.apache.ddlutils.model.Reference; import org.apache.ddlutils.model.Table; @@ -165,7 +167,7 @@ table.setType(getValueAsString(tableData, "TABLE_TYPE", availableColumns, "UNKNOWN")); table.setCatalog(getValueAsString(tableData, "TABLE_CAT", availableColumns, null)); table.setSchema(getValueAsString(tableData, "TABLE_SCHEM", availableColumns, null)); - table.setRemarks(getValueAsString(tableData, "REMARKS", availableColumns, "")); + table.setDescription(getValueAsString(tableData, "REMARKS", availableColumns, "")); tables.add(table); } } @@ -329,13 +331,13 @@ fks.add(currFk); } currFk = new ForeignKey(getValueAsString(fkData, "FK_NAME", availableColumns, null)); - currFk.setForeignTable(pkTable); + currFk.setForeignTableName(pkTable); prevPkTable = pkTable; } Reference ref = new Reference(); - ref.setForeign(getValueAsString(fkData, "PKCOLUMN_NAME", availableColumns, null)); - ref.setLocal(getValueAsString(fkData, "FKCOLUMN_NAME", availableColumns, null)); + ref.setForeignColumnName(getValueAsString(fkData, "PKCOLUMN_NAME", availableColumns, null)); + ref.setLocalColumnName(getValueAsString(fkData, "FKCOLUMN_NAME", availableColumns, null)); currFk.addReference(ref); } if (currFk != null) @@ -378,15 +380,22 @@ while (indexData.next()) { - String indexName = getValueAsString(indexData, "INDEX_NAME", availableColumns, null); - Index index = (Index)indicesByName.get(indexName); + String indexName = getValueAsString(indexData, "INDEX_NAME", availableColumns, null); + boolean isUnique = !getValueAsBoolean(indexData, "NON_UNIQUE", availableColumns, true); + Index index = (Index)indicesByName.get(indexName); if ((index == null) && (indexName != null)) { - index = new Index(); + if (isUnique) + { + index = new UniqueIndex(); + } + else + { + index = new NonUniqueIndex(); + } index.setName(indexName); - index.setUnique(!getValueAsBoolean(indexData, "NON_UNIQUE", availableColumns, true)); indicesByName.put(indexName, index); } if (index != null) @@ -394,7 +403,7 @@ IndexColumn ic = new IndexColumn(); ic.setName(getValueAsString(indexData, "COLUMN_NAME", availableColumns, null)); - index.addIndexColumn(ic); + index.addColumn(ic); } } indices.addAll(indicesByName.values()); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java Sat Aug 27 14:44:57 2005 @@ -18,8 +18,10 @@ public class Column implements Cloneable { + // TODO: Implement equals and hashcode private String name; private String javaName; + private String description; private boolean primaryKey = false; private boolean required = false; private boolean autoIncrement = false; @@ -133,6 +135,26 @@ public void setJavaName(String javaName) { this.javaName = javaName; + } + + /** + * Returns the description. + * + * @return The description + */ + public String getDescription() + { + return description; + } + + /** + * Sets the description. + * + * @param description The description + */ + public void setDescription(String description) + { + this.description = description; } public boolean isPrimaryKey() Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java Sat Aug 27 14:44:57 2005 @@ -16,11 +16,14 @@ * limitations under the License. */ +import java.io.Serializable; +import java.sql.Types; import java.util.ArrayList; +import java.util.HashSet; import java.util.Iterator; -import java.util.List; import org.apache.commons.beanutils.DynaBean; +import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.ddlutils.DynaSqlException; import org.apache.ddlutils.dynabean.DynaClassCache; import org.apache.ddlutils.dynabean.SqlDynaClass; @@ -35,8 +38,11 @@ * @author Thomas Dudziak * @version $Revision$ */ -public class Database +public class Database implements Serializable, Cloneable { + /** Unique ID for serialization purposes */ + private static final long serialVersionUID = -3160443396757573868L; + /** The name of the database model */ private String _name; /** The method for generating primary keys (currently ignored) */ @@ -44,7 +50,7 @@ /** The version of the model */ private String _version; /** The tables */ - private List _tables = new ArrayList(); + private ArrayList _tables = new ArrayList(); /** The dyna class cache for this model */ private DynaClassCache _dynaClassCache = new DynaClassCache(); @@ -139,24 +145,34 @@ } /** - * Returns the list of tables in this model. + * Returns the number of tables in this model. + * + * @return The number of tables + */ + public int getTableCount() + { + return _tables.size(); + } + + /** + * Returns the tables in this model. * * @return The tables */ - public List getTables() + public Table[] getTables() { - return _tables; + return (Table[])_tables.toArray(new Table[_tables.size()]); } /** * Returns the table at the specified position. * - * @param index The index of the table + * @param idx The index of the table * @return The table */ - public Table getTable(int index) + public Table getTable(int idx) { - return (Table)_tables.get(index); + return (Table)_tables.get(idx); } /** @@ -166,25 +182,211 @@ */ public void addTable(Table table) { - _tables.add(table); + if (table != null) + { + _tables.add(table); + } + } + + /** + * Adds a table at the specified position. + * + * @param idx The index where to insert the table + * @param table The table to add + */ + public void addTable(int idx, Table table) + { + if (table != null) + { + _tables.add(idx, table); + } + } + + /** + * Removes the given table. + * + * @param table The table to remove + */ + public void removeTable(Table table) + { + if (table != null) + { + _tables.remove(table); + } } /** - * Replaces the table at the specified position. + * Removes the indicated table. * - * @param index The index of the table - * @param table The new table + * @param idx The index of the table to remove */ - public void setTable(int index, Table table) + public void removeTable(int idx) { - _tables.set(index, table); + _tables.remove(idx); } // Helper methods /** + * Initializes the model by establishing the relationships between elements in this model encoded + * eg. in foreign keys etc. Also checks that the model elements are valid (table and columns have + * a name, foreign keys rference existing tables etc.) + */ + public void initialize() throws ModelException + { + // we have to setup + // * target tables in foreign keys + // * columns in foreign key references + // * columns in indices + // * columns in uniques + HashSet namesOfProcessedTables = new HashSet(); + HashSet namesOfProcessedColumns = new HashSet(); + HashSet namesOfProcessedFks = new HashSet(); + HashSet namesOfProcessedIndices = new HashSet(); + int tableIdx = 0; + + if ((getName() == null) || (getName().length() == 0)) + { + throw new ModelException("The database model has no name"); + } + + for (Iterator tableIt = _tables.iterator(); tableIt.hasNext(); tableIdx++) + { + Table curTable = (Table)tableIt.next(); + + if ((curTable.getName() == null) || (curTable.getName().length() == 0)) + { + throw new ModelException("The table nr. "+tableIdx+" has no name"); + } + if (namesOfProcessedTables.contains(curTable.getName())) + { + throw new ModelException("There are multiple tables with the name "+curTable.getName()); + } + namesOfProcessedTables.add(curTable.getName()); + + namesOfProcessedColumns.clear(); + namesOfProcessedFks.clear(); + namesOfProcessedIndices.clear(); + + for (int idx = 0; idx < curTable.getColumnCount(); idx++) + { + Column column = curTable.getColumn(idx); + + if ((column.getName() == null) || (column.getName().length() == 0)) + { + throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has no name"); + } + if (namesOfProcessedColumns.contains(column.getName())) + { + throw new ModelException("There are multiple column with the name "+column.getName()+" in the table "+curTable.getName()); + } + namesOfProcessedColumns.add(column.getName()); + + if ((column.getType() == null) || (column.getType().length() == 0)) + { + throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has no type"); + } + if ((column.getTypeCode() == Types.OTHER) && !"OTHER".equalsIgnoreCase(column.getType())) + { + throw new ModelException("The column nr. "+idx+" in table "+curTable.getName()+" has an unknown type "+column.getType()); + } + namesOfProcessedColumns.add(column.getName()); + } + + for (int idx = 0; idx < curTable.getForeignKeyCount(); idx++) + { + ForeignKey fk = curTable.getForeignKey(idx); + String fkName = (fk.getName() == null ? "" : fk.getName()); + String fkDesc = (fkName.length() == 0 ? "nr. " + idx : fkName); + + if (fkName.length() > 0) + { + if (namesOfProcessedFks.contains(fkName)) + { + throw new ModelException("There are multiple foreign keys in table "+curTable.getName()+" with the name "+fkName); + } + namesOfProcessedFks.add(fkName); + } + + if (fk.getForeignTable() == null) + { + Table targetTable = findTable(fk.getForeignTableName(), true); + + if (targetTable == null) + { + throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined table "+fk.getForeignTableName()); + } + else + { + fk.setForeignTable(targetTable); + } + } + for (Iterator refIt = fk.getReferences().iterator(); refIt.hasNext();) + { + Reference ref = (Reference)refIt.next(); + + if (ref.getLocalColumn() == null) + { + Column localColumn = curTable.findColumn(ref.getLocalColumnName(), true); + + if (localColumn == null) + { + throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined local column "+ref.getLocalColumnName()); + } + else + { + ref.setLocalColumn(localColumn); + } + } + if (ref.getForeignColumn() == null) + { + Column foreignColumn = fk.getForeignTable().findColumn(ref.getForeignColumnName(), true); + + if (foreignColumn == null) + { + throw new ModelException("The foreignkey "+fkDesc+" in table "+curTable.getName()+" references the undefined local column "+ref.getForeignColumnName()+" in table "+fk.getForeignTable().getName()); + } + else + { + ref.setForeignColumn(foreignColumn); + } + } + } + } + + for (int idx = 0; idx < curTable.getIndexCount(); idx++) + { + Index index = curTable.getIndex(idx); + String indexName = (index.getName() == null ? "" : index.getName()); + String indexDesc = (indexName.length() == 0 ? "nr. " + idx : indexName); + + if (indexName.length() > 0) + { + if (namesOfProcessedIndices.contains(indexName)) + { + throw new ModelException("There are multiple indices in table "+curTable.getName()+" with the name "+indexName); + } + namesOfProcessedIndices.add(indexName); + } + + int indexColumnIdx = 0; + + for (Iterator indexColumnIt = index.getColumns().iterator(); indexColumnIt.hasNext(); indexColumnIdx++) + { + IndexColumn indexColumn = (IndexColumn)indexColumnIt.next(); + + if (curTable.findColumn(indexColumn.getName(), true) == null) + { + throw new ModelException("The index "+indexDesc+" in table "+curTable.getName()+" references the undefined column "+indexColumn.getName()); + } + } + } + } + } + + /** * 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 to avoid introspection * problems. * * @param name The name of the table to find @@ -197,7 +399,7 @@ /** * 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) to avoid introspection * problems. * * @param name The name of the table to find @@ -262,12 +464,55 @@ return _dynaClassCache.createNewInstance(table); } + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + protected Object clone() throws CloneNotSupportedException + { + Database result = new Database(); + + result._name = _name; + result._idMethod = _idMethod; + result._version = _version; + result._tables = (ArrayList)_tables.clone(); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) + { + if (obj instanceof Database) + { + Database other = (Database)obj; + + return _name.equals(other._name) && + _tables.equals(other._tables); + } + else + { + return false; + } + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37) + .append(_name) + .append(_tables) + .toHashCode(); + } + /* * (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { - return super.toString() + "[name=" + _name + ";tableCount=" + _tables.size() + "]"; + return "Database " + _name + " [" + _tables.size() + " tables]"; } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java Sat Aug 27 14:44:57 2005 @@ -20,17 +20,24 @@ import java.util.Collections; import java.util.List; -// TODO: Add a name property to the foreignkey that is respected by -// create/alter/drop +/** + * Represents a database foreign key. + */ public class ForeignKey implements Cloneable { - private String name; - private String foreignTable; - - private ArrayList references = new ArrayList(); + // TODO: Make the create/alter/drop functionality respect the name property + + /** The name of the foreign key, may be null */ + private String _name; + /** The target table */ + private Table _foreignTable; + /** The name of the foreign table */ + private String _foreignTableName; + /** The references between local and remote columns */ + private ArrayList _references = new ArrayList(); /** - * Creates a new foreign key object. + * Creates a new foreign key object that has no name. */ public ForeignKey() { @@ -40,68 +47,142 @@ /** * Creates a new foreign key object. * - * @param name The name of the foreignkey + * @param name The name of the foreign key */ public ForeignKey(String name) { - this.name = name; + _name = name; } - /* (non-Javadoc) - * @see java.lang.Object#clone() + /** + * Returns the name of this foreign key. + * + * @return The name */ - public Object clone() throws CloneNotSupportedException + public String getName() { - ForeignKey result = new ForeignKey(); - - result.foreignTable = foreignTable; - result.references = (ArrayList)references.clone(); - return result; + return _name; } /** - * Returns the name of this foreign key. + * Sets the name of this foreign key. * - * @return The name + * @param name The name */ - public String getName() + public void setName(String name) { - return name; + _name = name; } - - public String getForeignTable() + + /** + * Returns the foreign table. + * + * @return The foreign table + */ + public Table getForeignTable() { - return foreignTable; + return _foreignTable; } - - public void setForeignTable(String foreignTable) + + /** + * Sets the foreign table. + * + * @param foreignTable The foreign table + */ + public void setForeignTable(Table foreignTable) + { + _foreignTable = foreignTable; + _foreignTableName = (foreignTable == null ? null : foreignTable.getName()); + } + + /** + * Returns the name of the foreign table. + * + * @return The table name + */ + public String getForeignTableName() { - this.foreignTable= foreignTable; + return _foreignTableName; } + /** + * Sets the name of the foreign table. Please note that you should not use this method + * when manually constructing or manipulating the database model. Rather utilize the + * {@link #setForeignTable(Table)} method. + * + * @param foreignTableName The table name + */ + public void setForeignTableName(String foreignTableName) + { + if ((_foreignTable != null) && !_foreignTable.getName().equals(foreignTableName)) + { + _foreignTable = null; + } + _foreignTableName = foreignTableName; + } + + /** + * Adds a reference, ie. a mapping between a local column (in the table that owns this foreign key) + * and a remote column. + * + * @param reference The reference to add + */ public void addReference(Reference reference) { - references.add(reference); + _references.add(reference); } - + + /** + * Returns the references. + * + * @return The references + */ public List getReferences() { - return references; + return _references; } - public Reference firstReference() { - return (Reference) (references.size() == 0 ? null : references.get(0)); + /** + * Returns the indicated reference. + * + * @param idx The index + * @return The reference + */ + public Reference getReference(int idx) + { + return (Reference)_references.get(idx); } - public boolean equals(Object o) { - boolean result = o != null && getClass().equals(o.getClass()); - if ( result ) { - ForeignKey fk = (ForeignKey) o; - result = this.foreignTable.equalsIgnoreCase(fk.foreignTable) && this.references.size() == fk.references.size(); - if ( result ) { + /** + * Returns the first reference if it exists. + * + * @return The first reference + */ + public Reference getFirstReference() + { + return (Reference)(_references.isEmpty() ? null : _references.get(0)); + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object other) + { + boolean result = (other != null) && getClass().equals(other.getClass()); + + if (result) + { + ForeignKey fk = (ForeignKey) other; + + result = _foreignTableName.equals(fk._foreignTableName) && + (_references.size() == fk._references.size()); + + if (result) + { //check all references - need to ensure order is same for valid comparison - List copyThis = (List) this.references.clone(); - List copyThat = (List) fk.references.clone(); + List copyThis = (List)_references.clone(); + List copyThat = (List)fk._references.clone(); + Collections.sort(copyThis); Collections.sort(copyThat); result = copyThis.equals(copyThat); @@ -109,9 +190,25 @@ } return result; } - - public String toString() { - return "ForeignKey[" + this.foreignTable + "]"; - //TODO show references + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() + { + //TODO show name and references + return "ForeignKey[" + _foreignTableName + "]"; + } + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + public Object clone() throws CloneNotSupportedException + { + ForeignKey result = new ForeignKey(_name); + + result._foreignTableName = _foreignTableName; + result._references = (ArrayList)_references.clone(); + return result; } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java?rev=240460&r1=240459&r2=240460&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java Sat Aug 27 14:44:57 2005 @@ -16,58 +16,53 @@ * limitations under the License. */ - -import java.util.ArrayList; +import java.io.Serializable; import java.util.List; -public class Index implements Cloneable +/** + * Represents an index definition for a table which may be either unique or non-unique. + */ +public interface Index extends Cloneable, Serializable { - protected String name; - protected ArrayList indexColumns = new ArrayList(); - protected boolean unique = false; - - public Index() {} - - /* (non-Javadoc) - * @see java.lang.Object#clone() + /** + * Determines whether this index is unique or not. + * + * @return true if the index is an unique one */ - public Object clone() throws CloneNotSupportedException - { - Index result = new Index(); + public boolean isUnique(); - result.name = name; - result.indexColumns = (ArrayList)indexColumns.clone(); - result.unique = unique; - return result; - } - - public String getName() - { - return name; - } - - public void setName(String name) - { - if ( name == null ) throw new IllegalArgumentException("Null index name"); - this.name = name; - } - - public void addIndexColumn(IndexColumn indexColumn) - { - indexColumns.add(indexColumn); - } + /** + * Returns the name of the index. + * + * @return The name + */ + public String getName(); - public List getIndexColumns() - { - return indexColumns; - } + /** + * Sets the name of the index. + * + * @param name The name + */ + public void setName(String name); - public boolean isUnique() { - return unique; - } + /** + * Adds a column that makes up this index. + * @param column + */ + public void addColumn(IndexColumn column); - public void setUnique(boolean unique) { - this.unique = unique; - } + /** + * Returns the indicated column making up this index. + * + * @param idx The index of the column + * @return The column + */ + public IndexColumn getColumn(int idx); + /** + * Returns the columns that make up this index. + * + * @return The columns + */ + public List getColumns(); } Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelException.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelException.java?rev=240460&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelException.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelException.java Sat Aug 27 14:44:57 2005 @@ -0,0 +1,68 @@ +package org.apache.ddlutils.model; + +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.ddlutils.DdlUtilsException; + +/** + * Indicates a model error. + */ +public class ModelException extends DdlUtilsException +{ + /** Constant for serializing instances of this class */ + private static final long serialVersionUID = -694578915559780711L; + + /** + * Creates a new empty exception object. + */ + public ModelException() + { + super(); + } + + /** + * Creates a new exception object. + * + * @param msg The exception message + */ + public ModelException(String msg) + { + super(msg); + } + + /** + * Creates a new exception object. + * + * @param baseEx The base exception + */ + public ModelException(Throwable baseEx) + { + super(baseEx); + } + + /** + * Creates a new exception object. + * + * @param msg The exception message + * @param baseEx The base exception + */ + public ModelException(String msg, Throwable baseEx) + { + super(msg, baseEx); + } + +} Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java?rev=240460&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java Sat Aug 27 14:44:57 2005 @@ -0,0 +1,95 @@ +package org.apache.ddlutils.model; + +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents an index definition for a table. + */ +public class NonUniqueIndex implements Index +{ + /** Unique ID for serialization purposes */ + private static final long serialVersionUID = -3591499395114850301L; + + /** The name of the index */ + protected String _name; + /** The columns making up the index */ + protected ArrayList _columns = new ArrayList(); + + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#isUnique() + */ + public boolean isUnique() + { + return false; + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#getName() + */ + public String getName() + { + return _name; + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#setName(java.lang.String) + */ + public void setName(String name) + { + _name = name; + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#addColumn(org.apache.ddlutils.model.IndexColumn) + */ + public void addColumn(IndexColumn column) + { + _columns.add(column); + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#getColumn(int) + */ + public IndexColumn getColumn(int idx) + { + return (IndexColumn)_columns.get(idx); + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#getColumns() + */ + public List getColumns() + { + return _columns; + } + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + public Object clone() throws CloneNotSupportedException + { + NonUniqueIndex result = new NonUniqueIndex(); + + result._name = _name; + result._columns = (ArrayList)_columns.clone(); + return result; + } +}