Author: tomdz Date: Mon Sep 5 15:27:13 2005 New Revision: 278870 URL: http://svn.apache.org/viewcvs?rev=278870&view=rev Log: Updated the model so everywhere where there are multiple sub elements (eg. a table has multiple columns), the sub elements are exposed as arrays rather than a list
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/builder/SqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.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/NonUniqueIndex.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java 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=278870&r1=278869&r2=278870&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 Mon Sep 5 15:27:13 2005 @@ -35,7 +35,6 @@ import org.apache.ddlutils.model.ForeignKey; import org.apache.ddlutils.model.Index; import org.apache.ddlutils.model.IndexColumn; -import org.apache.ddlutils.model.Reference; import org.apache.ddlutils.model.Table; import org.apache.ddlutils.model.TypeMap; @@ -435,9 +434,9 @@ // make sure this isn't the primary key index boolean isPk = true; - for (Iterator columnIt = currentIndex.getColumns().iterator(); columnIt.hasNext();) + for (int columnIdx = 0; columnIdx < currentIndex.getColumnCount(); columnIdx++) { - IndexColumn indexColumn = (IndexColumn)columnIt.next(); + IndexColumn indexColumn = currentIndex.getColumn(columnIdx); Column column = currentTable.findColumn(indexColumn.getName()); if (column != null && !column.isPrimaryKey()) @@ -1362,20 +1361,21 @@ print(getTableName(table)); print(" ("); - for (Iterator it = index.getColumns().iterator(); it.hasNext();) + for (int idx = 0; idx < index.getColumnCount(); idx++) { - IndexColumn idxColumn = (IndexColumn)it.next(); - + IndexColumn idxColumn = index.getColumn(idx); Column col = table.findColumn(idxColumn.getName()); - if ( col == null ) { + + if (col == null) + { //would get null pointer on next line anyway, so throw exception throw new RuntimeException("Invalid column '" + idxColumn.getName() + "' on index " + index.getName() + " for table " + table.getName()); } - print(getColumnName(col)); - if (it.hasNext()) + if (idx > 0) { print(", "); } + print(getColumnName(col)); } print(")"); @@ -1482,13 +1482,13 @@ */ protected void writeLocalReferences(ForeignKey key) throws IOException { - for (Iterator it = key.getReferences().iterator(); it.hasNext();) + for (int idx = 0; idx < key.getReferenceCount(); idx++) { - print(((Reference)it.next()).getLocalColumnName()); - if (it.hasNext()) + if (idx > 0) { print(", "); } + print(key.getReference(idx).getLocalColumnName()); } } @@ -1499,13 +1499,13 @@ */ protected void writeForeignReferences(ForeignKey key) throws IOException { - for (Iterator it = key.getReferences().iterator(); it.hasNext();) + for (int idx = 0; idx < key.getReferenceCount(); idx++) { - print(((Reference)it.next()).getForeignColumnName()); - if (it.hasNext()) + if (idx > 0) { print(", "); } + print(key.getReference(idx).getForeignColumnName()); } } 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=278870&r1=278869&r2=278870&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 Mon Sep 5 15:27:13 2005 @@ -299,28 +299,24 @@ result.append(owningTable.getName()); result.append("["); - for (Iterator it = fk.getReferences().iterator(); it.hasNext();) + for (int idx = 0; idx < fk.getReferenceCount(); idx++) { - Reference ref = (Reference)it.next(); - - result.append(ref.getLocalColumnName()); - if (it.hasNext()) + if (idx > 0) { result.append(","); } + result.append(fk.getReference(idx).getLocalColumnName()); } result.append("]->"); result.append(fk.getForeignTableName()); result.append("["); - for (Iterator it = fk.getReferences().iterator(); it.hasNext();) + for (int idx = 0; idx < fk.getReferenceCount(); idx++) { - Reference ref = (Reference)it.next(); - - result.append(ref.getForeignColumnName()); - if (it.hasNext()) + if (idx > 0) { result.append(","); } + result.append(fk.getReference(idx).getForeignColumnName()); } result.append("]"); return result.toString(); @@ -361,9 +357,9 @@ { Identity identity = new Identity(fk.getForeignTableName(), getFKName(owningTable, fk)); - for (Iterator refIt = fk.getReferences().iterator(); refIt.hasNext();) + for (int idx = 0; idx < fk.getReferenceCount(); idx++) { - Reference reference = (Reference)refIt.next(); + Reference reference = (Reference)fk.getReference(idx); Object value = bean.get(reference.getLocalColumnName()); if (value == null) @@ -404,9 +400,9 @@ } if (fk != null) { - for (Iterator it = fk.getReferences().iterator(); it.hasNext();) + for (int idx = 0; idx < fk.getReferenceCount(); idx++) { - Reference curRef = (Reference)it.next(); + Reference curRef = fk.getReference(idx); Column sourceColumn = sourceTable.findColumn(curRef.getLocalColumnName()); Column targetColumn = targetTable.findColumn(curRef.getForeignColumnName()); 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=278870&r1=278869&r2=278870&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 Mon Sep 5 15:27:13 2005 @@ -321,9 +321,9 @@ fk.setForeignTable(targetTable); } } - for (Iterator refIt = fk.getReferences().iterator(); refIt.hasNext();) + for (int refIdx = 0; refIdx < fk.getReferenceCount(); refIdx++) { - Reference ref = (Reference)refIt.next(); + Reference ref = fk.getReference(refIdx); if (ref.getLocalColumn() == null) { @@ -369,11 +369,9 @@ namesOfProcessedIndices.add(indexName); } - int indexColumnIdx = 0; - - for (Iterator indexColumnIt = index.getColumns().iterator(); indexColumnIt.hasNext(); indexColumnIdx++) + for (int indexColumnIdx = 0; indexColumnIdx < index.getColumnCount(); indexColumnIdx++) { - IndexColumn indexColumn = (IndexColumn)indexColumnIt.next(); + IndexColumn indexColumn = index.getColumn(indexColumnIdx); if (curTable.findColumn(indexColumn.getName(), true) == null) { 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=278870&r1=278869&r2=278870&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 Mon Sep 5 15:27:13 2005 @@ -122,35 +122,34 @@ } /** - * Adds a reference, ie. a mapping between a local column (in the table that owns this foreign key) - * and a remote column. + * Returns the number of references. * - * @param reference The reference to add + * @return The number of references */ - public void addReference(Reference reference) + public int getReferenceCount() { - _references.add(reference); + return _references.size(); } /** - * Returns the references. + * Returns the indicated reference. * - * @return The references + * @param idx The index + * @return The reference */ - public List getReferences() + public Reference getReference(int idx) { - return _references; + return (Reference)_references.get(idx); } /** - * Returns the indicated reference. + * Returns the references. * - * @param idx The index - * @return The reference + * @return The references */ - public Reference getReference(int idx) + public Reference[] getReferences() { - return (Reference)_references.get(idx); + return (Reference[])_references.toArray(new Reference[_references.size()]); } /** @@ -161,6 +160,58 @@ public Reference getFirstReference() { return (Reference)(_references.isEmpty() ? null : _references.get(0)); + } + + /** + * 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) + { + if (reference != null) + { + _references.add(reference); + } + } + + /** + * Adds a reference, ie. a mapping between a local column (in the table that owns this foreign key) + * and a remote column, at the specified place. + * + * @param idx The index to add the reference at + * @param reference The reference to add + */ + public void addReference(int idx, Reference reference) + { + if (reference != null) + { + _references.add(idx, reference); + } + } + + /** + * Removes the given reference. + * + * @param reference The reference to remove + */ + public void removeReference(Reference reference) + { + if (reference != null) + { + _references.remove(reference); + } + } + + /** + * Removes the indicated reference. + * + * @param idx The index of the reference to remove + */ + public void removeReference(int idx) + { + _references.remove(idx); } /* (non-Javadoc) 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=278870&r1=278869&r2=278870&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 Mon Sep 5 15:27:13 2005 @@ -17,7 +17,6 @@ */ import java.io.Serializable; -import java.util.List; /** * Represents an index definition for a table which may be either unique or non-unique. @@ -46,10 +45,11 @@ public void setName(String name); /** - * Adds a column that makes up this index. - * @param column + * Returns the number of columns that make up this index. + * + * @return The number of index columns */ - public void addColumn(IndexColumn column); + public int getColumnCount(); /** * Returns the indicated column making up this index. @@ -64,5 +64,34 @@ * * @return The columns */ - public List getColumns(); + public IndexColumn[] getColumns(); + + /** + * Adds a column that makes up this index. + * + * @param column The column to add + */ + public void addColumn(IndexColumn column); + + /** + * Adds a column that makes up this index at the specified position. + * + * @param idx The position to add the index colum at + * @param column The column to add + */ + public void addColumn(int idx, IndexColumn column); + + /** + * Removes the given index column from this index. + * + * @param column The column to remove + */ + public void removeColumn(IndexColumn column); + + /** + * Removes the column at the specified position in this index. + * + * @param idx The position of the index column to remove + */ + public void removeColumn(int idx); } Modified: 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=278870&r1=278869&r2=278870&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java Mon Sep 5 15:27:13 2005 @@ -17,7 +17,6 @@ */ import java.util.ArrayList; -import java.util.List; /** * Represents an index definition for a table. @@ -58,11 +57,11 @@ } /* (non-Javadoc) - * @see org.apache.ddlutils.model.Index#addColumn(org.apache.ddlutils.model.IndexColumn) + * @see org.apache.ddlutils.model.Index#getColumnCount() */ - public void addColumn(IndexColumn column) + public int getColumnCount() { - _columns.add(column); + return _columns.size(); } /* (non-Javadoc) @@ -76,9 +75,47 @@ /* (non-Javadoc) * @see org.apache.ddlutils.model.Index#getColumns() */ - public List getColumns() + public IndexColumn[] getColumns() + { + return (IndexColumn[])_columns.toArray(new IndexColumn[_columns.size()]); + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#addColumn(org.apache.ddlutils.model.IndexColumn) + */ + public void addColumn(IndexColumn column) + { + if (column != null) + { + _columns.add(column); + } + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#addColumn(int, org.apache.ddlutils.model.IndexColumn) + */ + public void addColumn(int idx, IndexColumn column) + { + if (column != null) + { + _columns.add(idx, column); + } + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#removeColumn(org.apache.ddlutils.model.IndexColumn) + */ + public void removeColumn(IndexColumn column) + { + _columns.remove(column); + } + + /* (non-Javadoc) + * @see org.apache.ddlutils.model.Index#removeColumn(int) + */ + public void removeColumn(int idx) { - return _columns; + _columns.remove(idx); } /* (non-Javadoc) Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java?rev=278870&r1=278869&r2=278870&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java Mon Sep 5 15:27:13 2005 @@ -17,7 +17,6 @@ */ import java.util.ArrayList; -import java.util.List; /** * Provides compatibility with Torque-style xml with separate <index> and @@ -27,62 +26,17 @@ * @author John Marshall/Connectria * @version $Revision$ */ -public class UniqueIndex implements Index +public class UniqueIndex extends NonUniqueIndex { /** Unique ID for serialization purposes */ private static final long serialVersionUID = -4097003126550294993L; - /** The name of the index */ - protected String _name; - /** The columns making up the unique index */ - protected ArrayList _columns = new ArrayList(); - /* (non-Javadoc) * @see org.apache.ddlutils.model.Index#isUnique() */ public boolean isUnique() { return true; - } - - /* (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) Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java?rev=278870&r1=278869&r2=278870&view=diff ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java (original) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDatabaseIO.java Mon Sep 5 15:27:13 2005 @@ -223,7 +223,7 @@ assertEquals(someTable.getName(), fk.getForeignTableName()); assertEquals(1, - fk.getReferences().size()); + fk.getReferenceCount()); Reference ref = fk.getFirstReference(); @@ -347,7 +347,7 @@ index.getName()); assertFalse(index.isUnique()); assertEquals(1, - index.getColumns().size()); + index.getColumnCount()); IndexColumn indexColumn = index.getColumn(0); @@ -360,7 +360,7 @@ assertNull(index.getName()); assertFalse(index.isUnique()); assertEquals(2, - index.getColumns().size()); + index.getColumnCount()); indexColumn = index.getColumn(0); @@ -471,7 +471,7 @@ index.getName()); assertTrue(index.isUnique()); assertEquals(1, - index.getColumns().size()); + index.getColumnCount()); IndexColumn indexColumn = index.getColumn(0); @@ -484,7 +484,7 @@ assertNull(index.getName()); assertFalse(index.isUnique()); assertEquals(1, - index.getColumns().size()); + index.getColumnCount()); indexColumn = index.getColumn(0); @@ -670,7 +670,7 @@ assertEquals("A", fk.getForeignTableName()); assertEquals(1, - fk.getReferences().size()); + fk.getReferenceCount()); Reference ref = fk.getFirstReference(); @@ -688,7 +688,7 @@ assertNull(index.getName()); assertTrue(index.isUnique()); assertEquals(1, - index.getColumns().size()); + index.getColumnCount()); IndexColumn indexColumn = index.getColumn(0); @@ -775,7 +775,7 @@ assertEquals("A", fk.getForeignTableName()); assertEquals(1, - fk.getReferences().size()); + fk.getReferenceCount()); ref = fk.getFirstReference(); @@ -796,7 +796,7 @@ assertEquals("C", fk.getForeignTableName()); assertEquals(1, - fk.getReferences().size()); + fk.getReferenceCount()); ref = fk.getFirstReference(); @@ -814,7 +814,7 @@ assertNull(index.getName()); assertFalse(index.isUnique()); assertEquals(2, - index.getColumns().size()); + index.getColumnCount()); indexColumn = index.getColumn(0); @@ -887,7 +887,7 @@ index.getName()); assertFalse(index.isUnique()); assertEquals(1, - index.getColumns().size()); + index.getColumnCount()); indexColumn = index.getColumn(0);