From 462994f3ebd531e0defd429e6d8509a57cb68378 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Thu, 24 Nov 2011 18:17:41 +0000 Subject: [PATCH] ALF-11668: Add position identifiers (same as seq elements in current schema dump tool) As for current schemadump tool, table columns and primary key column names have order associated with them. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@32296 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../Schema-Reference.xml | 572 +++++++++--------- .../schemacomp/DbObjectXMLTransformer.java | 44 +- .../DbObjectXMLTransformerTest.java | 20 +- .../alfresco/util/schemacomp/ExportDb.java | 6 + ...e.java => SchemaCompPackageTestSuite.java} | 2 +- .../schemacomp/SchemaCompTestingUtils.java | 11 +- .../org/alfresco/util/schemacomp/XML.java | 1 + .../alfresco/util/schemacomp/XMLToSchema.java | 17 +- .../util/schemacomp/XMLToSchemaTest.java | 6 +- .../util/schemacomp/model/Column.java | 25 +- .../util/schemacomp/model/ColumnTest.java | 5 +- .../util/schemacomp/model/PrimaryKey.java | 47 +- .../util/schemacomp/model/PrimaryKeyTest.java | 17 +- .../exportdb/MySQLDialectExportTester.java | 17 + .../PostgreSQLDialectExportTester.java | 18 + .../schemacomp/xml_to_schema_test.xml | 8 +- 16 files changed, 503 insertions(+), 313 deletions(-) rename source/java/org/alfresco/util/schemacomp/{SchemaCompPackagetTestSuite.java => SchemaCompPackageTestSuite.java} (94%) diff --git a/config/alfresco/dbscripts/create/org.hibernate.dialect.PostgreSQLDialect/Schema-Reference.xml b/config/alfresco/dbscripts/create/org.hibernate.dialect.PostgreSQLDialect/Schema-Reference.xml index 2e89c8b85f..618ef9cfd2 100644 --- a/config/alfresco/dbscripts/create/org.hibernate.dialect.PostgreSQLDialect/Schema-Reference.xml +++ b/config/alfresco/dbscripts/create/org.hibernate.dialect.PostgreSQLDialect/Schema-Reference.xml @@ -40,38 +40,38 @@ - + int8 false - + int8 false - + int8 false - + int8 false - + bool false - + int4 false - + int8 true - id + id @@ -119,58 +119,58 @@
- + int8 false - + int8 false - + varchar(36) false - + bool false - + int8 false - + bool false - + int8 true - + int4 false - + int8 true - + bool false - + bool false - + int8 true - id + id @@ -203,30 +203,30 @@
- + int8 false - + int8 false - + varchar(1024) true - + varchar(1024) true - + varchar(1024) true - id + id @@ -234,18 +234,18 @@
- + int8 false - + int8 true - id + id @@ -259,30 +259,30 @@
- + int8 false - + int8 false - + int8 false - + int8 false - + int4 false - id + id @@ -319,54 +319,54 @@
- + int8 false - + int8 true - + timestamp false - + varchar(1024) true - + varchar(255) true - + varchar(255) false - + varchar(10) true - + varchar(255) true - + varchar(36) true - + varchar(255) false - + timestamp false - id + id @@ -400,30 +400,30 @@
- + int8 false - + varchar(255) false - + varchar(255) true - + varchar(36) true - + timestamp false - id + id @@ -437,50 +437,50 @@
- + int8 false - + timestamp false - + varchar(10) false - + varchar(1024) false - + varchar(255) false - + int4 false - + varchar(255) true - + varchar(36) true - + varchar(255) false - + timestamp false - sequence_id + sequence_id @@ -499,54 +499,54 @@
- + varchar(64) false - + varchar(1024) true - + int4 true - + int4 true - + int4 true - + int4 true - + timestamp true - + varchar(64) true - + bool true - + bool true - + varchar(1024) true - id + id @@ -554,30 +554,30 @@
- + int8 false - + int4 false - + int8 false - + int8 false - + int8 false - id + id @@ -617,30 +617,30 @@
- + int8 false - + int8 false - + int8 false - + int8 true - + int8 true - id + id @@ -685,22 +685,22 @@
- + int8 false - + int8 false - + int8 false - id + id @@ -725,26 +725,26 @@
- + int8 false - + int8 false - + varchar(100) true - + int8 true - id + id @@ -764,26 +764,26 @@
- + int8 false - + int8 false - + int8 false - + int8 false - id + id @@ -819,58 +819,58 @@
- + int8 false - + int8 false - + int8 false - + int8 false - + int8 false - + varchar(50) false - + int8 false - + int8 false - + varchar(255) false - + int8 false - + bool true - + int4 true - id + id @@ -942,34 +942,34 @@
- + int8 false - + int8 false - + int8 true - + int8 true - + int8 true - + int8 true - id + id @@ -1019,34 +1019,34 @@
- + int8 false - + varchar(255) false - + varchar(12) false - + int8 false - + int8 false - + int8 true - id + id @@ -1066,22 +1066,22 @@
- + int8 false - + int8 false - + varchar(100) false - id + id @@ -1095,22 +1095,22 @@
- + int8 false - + int8 false - + varchar(20) false - id + id @@ -1124,38 +1124,38 @@
- + int8 false - + int8 false - + int8 false - + int8 false - + varchar(36) false - + int8 false - + int8 false - id + id @@ -1186,26 +1186,26 @@
- + int8 false - + int8 false - + int8 false - + varchar(255) false - id + id @@ -1226,22 +1226,22 @@
- + int8 false - + int8 false - + varchar(100) false - id + id @@ -1255,22 +1255,22 @@
- + int8 false - + int8 false - + varchar(100) false - id + id @@ -1284,66 +1284,66 @@
- + int8 false - + int8 false - + int8 false - + varchar(36) false - + int8 false - + bool false - + int8 false - + int8 false - + int8 true - + varchar(255) true - + varchar(30) true - + varchar(255) true - + varchar(30) true - + varchar(30) true - id + id @@ -1414,19 +1414,19 @@
- + int8 false - + int8 false - node_id - qname_id + node_id + qname_id @@ -1456,34 +1456,34 @@
- + int8 false - + int8 false - + int8 false - + int8 false - + int8 false - + int8 false - id + id @@ -1533,61 +1533,61 @@
- + int8 false - + int4 false - + int4 false - + bool true - + int8 true - + float4 true - + float8 true - + varchar(1024) true - + bytea true - + int8 false - + int4 false - + int8 false - node_id - qname_id - list_index - locale_id + node_id + qname_id + list_index + locale_id @@ -1627,26 +1627,26 @@
- + int8 false - + int8 false - + int8 false - + varchar(100) false - id + id @@ -1672,26 +1672,26 @@
- + int8 false - + varchar(255) false - + varchar(32) false - + int8 false - id + id @@ -1711,50 +1711,50 @@
- + int8 false - + int4 false - + int2 false - + int2 false - + int2 false - + int2 false - + int2 false - + int4 false - + int2 false - + int2 false - date_value + date_value @@ -1770,18 +1770,18 @@
- + int8 false - + float8 false - id + id @@ -1795,32 +1795,32 @@
- + int8 false - + int8 false - + int8 false - + int8 false - + int8 false - root_prop_id - contained_in - prop_index + root_prop_id + contained_in + prop_index @@ -1862,18 +1862,18 @@
- + int8 false - + int4 false - id + id @@ -1881,18 +1881,18 @@
- + int8 false - + bytea false - id + id @@ -1900,26 +1900,26 @@
- + int8 false - + varchar(1024) false - + varchar(16) false - + int8 false - id + id @@ -1939,34 +1939,34 @@
- + int8 false - + int4 false - + int8 false - + int8 false - + int8 false - + int8 true - id + id @@ -2018,26 +2018,26 @@
- + int8 false - + int8 false - + int2 false - + int8 false - id + id @@ -2058,26 +2058,26 @@
- + int8 false - + int8 false - + int8 false - + varchar(200) false - id + id @@ -2103,22 +2103,22 @@
- + int8 false - + int8 false - + varchar(39) false - id + id @@ -2132,30 +2132,30 @@
- + int8 false - + int8 false - + varchar(50) false - + varchar(100) false - + int8 true - id + id @@ -2181,19 +2181,19 @@
- + int8 false - + int8 false - user_node_id - node_id + user_node_id + node_id @@ -2218,30 +2218,30 @@
- + int8 false - + int8 false - + int8 true - + varchar(56) false - + int8 true - id + id @@ -2266,26 +2266,26 @@
- + int8 false - + int8 false - + int8 false - + int8 false - id + id diff --git a/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformer.java b/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformer.java index 0cd9113ff7..4fd84e2953 100644 --- a/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformer.java +++ b/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformer.java @@ -18,7 +18,6 @@ */ package org.alfresco.util.schemacomp; -import java.util.Collection; import java.util.List; import org.alfresco.util.schemacomp.model.Column; @@ -136,6 +135,11 @@ public class DbObjectXMLTransformer Index index = (Index) dbObject; attribs.addAttribute("", "", XML.ATTR_UNIQUE, "CDATA", Boolean.toString(index.isUnique())); } + else if (dbObject instanceof Column) + { + Column column = (Column) dbObject; + attribs.addAttribute("", "", XML.ATTR_ORDER, "CDATA", Integer.toString(column.getOrder())); + } } private void transformDbObject(DbObject dbObject) throws SAXException @@ -223,12 +227,12 @@ public class DbObjectXMLTransformer private void transformIndex(Index index) throws SAXException { - columnNameList(index.getColumnNames()); + columnNameList(index.getColumnNames(), null); } private void transformPrimaryKey(PrimaryKey pk) throws SAXException { - columnNameList(pk.getColumnNames()); + columnNameList(pk.getColumnNames(), pk.getColumnOrders()); } @@ -260,12 +264,40 @@ public class DbObjectXMLTransformer xmlOut.endElement("", "", tag); } - private void columnNameList(Collection columnNames) throws SAXException + /** + * Outputs a list of columnname elements sandwiched within a columnnames element. + *

+ * The columnOrders parameter will provide a corresponding list of integers that will be + * provided in each columnname element's order attribute. This parameter may be null + * in which case order attributes will be ommitted. + * + * @param columnNames + * @param columnOrders + * @throws SAXException + */ + private void columnNameList(List columnNames, + List columnOrders) throws SAXException { simpleStartTag(XML.EL_COLUMN_NAMES); - for (String columnName : columnNames) + for (int i = 0; i < columnNames.size(); i++) { - simpleElement(XML.EL_COLUMN_NAME, columnName); + String columnName = columnNames.get(i); + + final AttributesImpl attribs = new AttributesImpl(); + if (columnOrders != null) + { + int columnOrder = columnOrders.get(i); + attribs.addAttribute("", "", XML.ATTR_ORDER, "CDATA", Integer.toString(columnOrder)); + } + // Create a or start tag + xmlOut.startElement("", "", XML.EL_COLUMN_NAME, attribs); + + // Provide the elements content + char[] chars = columnName.toCharArray(); + xmlOut.characters(chars, 0, chars.length); + + // Provide the closing tag + simpleEndTag(XML.EL_COLUMN_NAME); } simpleEndTag(XML.EL_COLUMN_NAMES); } diff --git a/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformerTest.java b/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformerTest.java index 09340f3509..126585e5f8 100644 --- a/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformerTest.java +++ b/source/java/org/alfresco/util/schemacomp/DbObjectXMLTransformerTest.java @@ -105,13 +105,13 @@ public class DbObjectXMLTransformerTest public void transformColumn() throws IOException { Column column = new Column(null, "last_name", "VARCHAR2(100)", true); - + column.setOrder(2); transformer.output(column); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); dumpOutput(); assertHasPreamble(reader); - assertEquals("", reader.readLine()); + assertEquals("", reader.readLine()); assertEquals(" VARCHAR2(100)", reader.readLine()); assertEquals(" true", reader.readLine()); assertEquals("", reader.readLine()); @@ -159,7 +159,11 @@ public class DbObjectXMLTransformerTest @Test public void transformPrimaryKey() throws IOException { - PrimaryKey pk = new PrimaryKey(null, "pk_name", Arrays.asList("first", "second")); + PrimaryKey pk = new PrimaryKey( + null, + "pk_name", + Arrays.asList("a_column", "b_column"), + Arrays.asList(2, 1)); transformer.output(pk); @@ -168,8 +172,8 @@ public class DbObjectXMLTransformerTest assertHasPreamble(reader); assertEquals("", reader.readLine()); assertEquals(" ", reader.readLine()); - assertEquals(" first", reader.readLine()); - assertEquals(" second", reader.readLine()); + assertEquals(" a_column", reader.readLine()); + assertEquals(" b_column", reader.readLine()); assertEquals(" ", reader.readLine()); assertEquals("", reader.readLine()); } @@ -178,7 +182,7 @@ public class DbObjectXMLTransformerTest public void transformSchema() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); - PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); + PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1)); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); @@ -225,7 +229,7 @@ public class DbObjectXMLTransformerTest public void transformTable() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); - PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); + PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1)); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); @@ -258,7 +262,7 @@ public class DbObjectXMLTransformerTest public void transformObjectWithValidators() throws IOException { Collection columns = columns("one VARCHAR2(100)", "two NUMBER(10)"); - PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id")); + PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1)); Collection fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc")); Collection indexes = indexes("index_one col1 col2", "index_two col3 col4"); diff --git a/source/java/org/alfresco/util/schemacomp/ExportDb.java b/source/java/org/alfresco/util/schemacomp/ExportDb.java index 1eec1b5641..746d816176 100644 --- a/source/java/org/alfresco/util/schemacomp/ExportDb.java +++ b/source/java/org/alfresco/util/schemacomp/ExportDb.java @@ -230,6 +230,9 @@ public class ExportDb String nullableString = columns.getString("IS_NULLABLE"); column.setNullable(parseBoolean(nullableString)); + + column.setOrder(columns.getInt("ORDINAL_POSITION")); + column.setParent(table); table.getColumns().add(column); } @@ -250,6 +253,9 @@ public class ExportDb } String columnName = primarykeycols.getString("COLUMN_NAME"); pk.getColumnNames().add(columnName); + + int columnOrder = primarykeycols.getInt("KEY_SEQ"); + pk.getColumnOrders().add(columnOrder); } primarykeycols.close(); diff --git a/source/java/org/alfresco/util/schemacomp/SchemaCompPackagetTestSuite.java b/source/java/org/alfresco/util/schemacomp/SchemaCompPackageTestSuite.java similarity index 94% rename from source/java/org/alfresco/util/schemacomp/SchemaCompPackagetTestSuite.java rename to source/java/org/alfresco/util/schemacomp/SchemaCompPackageTestSuite.java index ef97b95001..37642040a0 100644 --- a/source/java/org/alfresco/util/schemacomp/SchemaCompPackagetTestSuite.java +++ b/source/java/org/alfresco/util/schemacomp/SchemaCompPackageTestSuite.java @@ -34,6 +34,6 @@ import org.junit.runners.Suite; ValidatorTestSuite.class, SchemaCompTestSuite.class }) -public class SchemaCompPackagetTestSuite +public class SchemaCompPackageTestSuite { } diff --git a/source/java/org/alfresco/util/schemacomp/SchemaCompTestingUtils.java b/source/java/org/alfresco/util/schemacomp/SchemaCompTestingUtils.java index a6a79eec7a..1706d19a4c 100644 --- a/source/java/org/alfresco/util/schemacomp/SchemaCompTestingUtils.java +++ b/source/java/org/alfresco/util/schemacomp/SchemaCompTestingUtils.java @@ -20,6 +20,7 @@ package org.alfresco.util.schemacomp; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -84,6 +85,7 @@ public class SchemaCompTestingUtils { String[] parts = colDefs[i].split(" "); columns[i] = new Column(null, parts[0], parts[1], false); + columns[i].setOrder(i+1); } return Arrays.asList(columns); } @@ -91,7 +93,14 @@ public class SchemaCompTestingUtils public static PrimaryKey pk(String name, String... columnNames) { assertTrue("No columns specified", columnNames.length > 0); - PrimaryKey pk = new PrimaryKey(null, name, Arrays.asList(columnNames)); + // Create a list of column orders, ordered the same as the supplied column names + // i.e. 1, 2, 3... N + List columnOrders = new ArrayList(columnNames.length); + for (int i = 1; i <= columnNames.length; i++) + { + columnOrders.add(i); + } + PrimaryKey pk = new PrimaryKey(null, name, Arrays.asList(columnNames), columnOrders); return pk; } diff --git a/source/java/org/alfresco/util/schemacomp/XML.java b/source/java/org/alfresco/util/schemacomp/XML.java index d53075c43b..01560dc1c2 100644 --- a/source/java/org/alfresco/util/schemacomp/XML.java +++ b/source/java/org/alfresco/util/schemacomp/XML.java @@ -51,6 +51,7 @@ public abstract class XML public static final String EL_PROPERTY = "property"; public static final String ATTR_NAME = "name"; + public static final String ATTR_ORDER = "order"; public static final String ATTR_UNIQUE = "unique"; public static final String ATTR_CLASS = "class"; } diff --git a/source/java/org/alfresco/util/schemacomp/XMLToSchema.java b/source/java/org/alfresco/util/schemacomp/XMLToSchema.java index 3a43dd077a..4912665bb3 100644 --- a/source/java/org/alfresco/util/schemacomp/XMLToSchema.java +++ b/source/java/org/alfresco/util/schemacomp/XMLToSchema.java @@ -212,7 +212,22 @@ public class XMLToSchema extends DefaultHandler } else if (qName.equals(XML.EL_COLUMN)) { - stack.push(new Column(atts.getValue(XML.ATTR_NAME))); + Column column = new Column(atts.getValue(XML.ATTR_NAME)); + if (atts.getValue(XML.ATTR_ORDER) != null) + { + int order = Integer.parseInt(atts.getValue(XML.ATTR_ORDER)); + column.setOrder(order); + } + stack.push(column); + } + else if (qName.equals(XML.EL_COLUMN_NAME)) + { + if (stack.peek() instanceof PrimaryKey && atts.getValue(XML.ATTR_ORDER) != null) + { + PrimaryKey pk = (PrimaryKey) stack.peek(); + Integer columnOrder = Integer.parseInt(atts.getValue(XML.ATTR_ORDER)); + pk.getColumnOrders().add(columnOrder); + } } else if (qName.equals(XML.EL_PRIMARY_KEY)) { diff --git a/source/java/org/alfresco/util/schemacomp/XMLToSchemaTest.java b/source/java/org/alfresco/util/schemacomp/XMLToSchemaTest.java index 4f20b35ff3..1d2e0f73b0 100644 --- a/source/java/org/alfresco/util/schemacomp/XMLToSchemaTest.java +++ b/source/java/org/alfresco/util/schemacomp/XMLToSchemaTest.java @@ -80,21 +80,25 @@ public class XMLToSchemaTest assertEquals("id", table.getColumns().get(0).getName()); assertEquals("NUMBER(10)", table.getColumns().get(0).getType()); assertEquals(false, table.getColumns().get(0).isNullable()); + assertEquals(1, table.getColumns().get(0).getOrder()); assertSame("Wrong or no parent set", table, table.getColumns().get(1).getParent()); assertEquals("nodeRef", table.getColumns().get(1).getName()); assertEquals("VARCHAR2(200)", table.getColumns().get(1).getType()); assertEquals(false, table.getColumns().get(1).isNullable()); - + assertEquals(3, table.getColumns().get(1).getOrder()); + assertSame("Wrong or no parent set", table, table.getColumns().get(2).getParent()); assertEquals("name", table.getColumns().get(2).getName()); assertEquals("VARCHAR2(150)", table.getColumns().get(2).getType()); assertEquals(true, table.getColumns().get(2).isNullable()); + assertEquals(2, table.getColumns().get(2).getOrder()); assertSame("Wrong or no parent set", table, table.getPrimaryKey().getParent()); assertEquals("pk_node", table.getPrimaryKey().getName()); assertEquals(1, table.getPrimaryKey().getColumnNames().size()); assertEquals("id", table.getPrimaryKey().getColumnNames().get(0)); + assertEquals(1, table.getPrimaryKey().getColumnOrders().get(0).intValue()); assertEquals(1, table.getForeignKeys().size()); assertSame("Wrong or no parent set", table, table.getForeignKeys().get(0).getParent()); diff --git a/source/java/org/alfresco/util/schemacomp/model/Column.java b/source/java/org/alfresco/util/schemacomp/model/Column.java index 1b33308a7b..28e41fbcf1 100644 --- a/source/java/org/alfresco/util/schemacomp/model/Column.java +++ b/source/java/org/alfresco/util/schemacomp/model/Column.java @@ -32,6 +32,7 @@ public class Column extends AbstractDbObject { private String type; private boolean nullable; + private int order; public Column(String name) @@ -86,16 +87,34 @@ public class Column extends AbstractDbObject this.nullable = nullable; } + + /** + * @return the order + */ + public int getOrder() + { + return this.order; + } + + /** + * @param order the order to set + */ + public void setOrder(int order) + { + this.order = order; + } + @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + (this.nullable ? 1231 : 1237); + result = prime * result + this.order; result = prime * result + ((this.type == null) ? 0 : this.type.hashCode()); return result; } - + @Override public boolean equals(Object obj) { @@ -104,6 +123,7 @@ public class Column extends AbstractDbObject if (getClass() != obj.getClass()) return false; Column other = (Column) obj; if (this.nullable != other.nullable) return false; + if (this.order != other.order) return false; if (this.type == null) { if (other.type != null) return false; @@ -117,13 +137,16 @@ public class Column extends AbstractDbObject { DbProperty thisTypeProp = new DbProperty(this, "type"); DbProperty thisNullableProp = new DbProperty(this, "nullable"); + DbProperty thisOrderProp = new DbProperty(this, "order"); Column thatColumn = (Column) right; DbProperty thatTypeProp = new DbProperty(thatColumn, "type"); DbProperty thatNullableProp = new DbProperty(thatColumn, "nullable"); + DbProperty thatOrderProp = new DbProperty(thatColumn, "order"); comparisonUtils.compareSimple(thisTypeProp, thatTypeProp, ctx); comparisonUtils.compareSimple(thisNullableProp, thatNullableProp, ctx); + comparisonUtils.compareSimple(thisOrderProp, thatOrderProp, ctx); } @Override diff --git a/source/java/org/alfresco/util/schemacomp/model/ColumnTest.java b/source/java/org/alfresco/util/schemacomp/model/ColumnTest.java index 2048a9cd19..f564bf75cd 100644 --- a/source/java/org/alfresco/util/schemacomp/model/ColumnTest.java +++ b/source/java/org/alfresco/util/schemacomp/model/ColumnTest.java @@ -63,8 +63,11 @@ public class ColumnTest extends DbObjectTestBase DbProperty thisNullableProp = new DbProperty(thisColumn, "nullable"); DbProperty thatNullableProp = new DbProperty(thatColumn, "nullable"); - inOrder.verify(comparisonUtils).compareSimple(thisNullableProp, thatNullableProp, ctx); + + DbProperty thisOrderProp = new DbProperty(thisColumn, "order"); + DbProperty thatOrderProp = new DbProperty(thatColumn, "order"); + inOrder.verify(comparisonUtils).compareSimple(thisOrderProp, thatOrderProp, ctx); } @Test diff --git a/source/java/org/alfresco/util/schemacomp/model/PrimaryKey.java b/source/java/org/alfresco/util/schemacomp/model/PrimaryKey.java index 9175d24cf6..3a1d5ea8cf 100644 --- a/source/java/org/alfresco/util/schemacomp/model/PrimaryKey.java +++ b/source/java/org/alfresco/util/schemacomp/model/PrimaryKey.java @@ -34,6 +34,7 @@ import org.alfresco.util.schemacomp.Result.Strength; public class PrimaryKey extends AbstractDbObject { private final List columnNames = new ArrayList(); + private final List columnOrders = new ArrayList(); public PrimaryKey(String name) @@ -47,10 +48,24 @@ public class PrimaryKey extends AbstractDbObject * @param name * @param columnNames */ - public PrimaryKey(Table table, String name, List columnNames) + public PrimaryKey(Table table, String name, List columnNames, List columnOrders) { super(table, name); this.columnNames.addAll(columnNames); + this.columnOrders.addAll(columnOrders); + checkColumnOrders(); + } + + /** + * Ensure that if columnOrders are being used, there are + * as many of them as there are column names. + */ + private void checkColumnOrders() + { + if (columnOrders.size() > 0 && columnOrders.size() != columnNames.size()) + { + throw new IllegalArgumentException("columnOrders size does not match columnNames"); + } } /** @@ -69,6 +84,23 @@ public class PrimaryKey extends AbstractDbObject this.columnNames.clear(); this.columnNames.addAll(columnNames); } + + /** + * @return the columnOrders + */ + public List getColumnOrders() + { + return this.columnOrders; + } + + /** + * @param columnOrders the columnOrders to set + */ + public void setColumnOrders(List columnOrders) + { + this.columnOrders.clear(); + this.columnOrders.addAll(columnOrders); + } @Override public int hashCode() @@ -76,6 +108,7 @@ public class PrimaryKey extends AbstractDbObject final int prime = 31; int result = super.hashCode(); result = prime * result + ((this.columnNames == null) ? 0 : this.columnNames.hashCode()); + result = prime * result + ((this.columnOrders == null) ? 0 : this.columnOrders.hashCode()); return result; } @@ -91,23 +124,35 @@ public class PrimaryKey extends AbstractDbObject if (other.columnNames != null) return false; } else if (!this.columnNames.equals(other.columnNames)) return false; + if (this.columnOrders == null) + { + if (other.columnOrders != null) return false; + } + else if (!this.columnOrders.equals(other.columnOrders)) return false; return true; } @Override protected void doDiff(DbObject right, DiffContext ctx, Strength strength) { + checkColumnOrders(); PrimaryKey rightPK = (PrimaryKey) right; comparisonUtils.compareSimpleCollections( new DbProperty(this, "columnNames"), new DbProperty(rightPK, "columnNames"), ctx, strength); + comparisonUtils.compareSimpleCollections( + new DbProperty(this, "columnOrders"), + new DbProperty(rightPK, "columnOrders"), + ctx, + strength); } @Override public void accept(DbObjectVisitor visitor) { + checkColumnOrders(); visitor.visit(this); } } diff --git a/source/java/org/alfresco/util/schemacomp/model/PrimaryKeyTest.java b/source/java/org/alfresco/util/schemacomp/model/PrimaryKeyTest.java index 8665fe6d78..4b121461e2 100644 --- a/source/java/org/alfresco/util/schemacomp/model/PrimaryKeyTest.java +++ b/source/java/org/alfresco/util/schemacomp/model/PrimaryKeyTest.java @@ -41,8 +41,16 @@ public class PrimaryKeyTest extends DbObjectTestBase @Before public void setUp() { - thisPK = new PrimaryKey(null, "this_pk", Arrays.asList("id", "name", "age")); - thatPK = new PrimaryKey(null, "that_pk", Arrays.asList("a", "b")); + thisPK = new PrimaryKey( + null, + "this_pk", + Arrays.asList("id", "name", "age"), + Arrays.asList(2, 1, 3)); + thatPK = new PrimaryKey( + null, + "that_pk", + Arrays.asList("a", "b"), + Arrays.asList(1, 2)); } @Override @@ -65,6 +73,11 @@ public class PrimaryKeyTest extends DbObjectTestBase new DbProperty(thatPK, "columnNames"), ctx, Strength.ERROR); + inOrder.verify(comparisonUtils).compareSimpleCollections( + new DbProperty(thisPK, "columnOrders"), + new DbProperty(thatPK, "columnOrders"), + ctx, + Strength.ERROR); } @Test diff --git a/source/java/org/alfresco/util/schemacomp/test/exportdb/MySQLDialectExportTester.java b/source/java/org/alfresco/util/schemacomp/test/exportdb/MySQLDialectExportTester.java index 158a177847..8427662aff 100644 --- a/source/java/org/alfresco/util/schemacomp/test/exportdb/MySQLDialectExportTester.java +++ b/source/java/org/alfresco/util/schemacomp/test/exportdb/MySQLDialectExportTester.java @@ -95,18 +95,21 @@ public class MySQLDialectExportTester extends AbstractExportTester assertEquals("id", col.getName()); assertEquals("bigint", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(1, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); assertEquals("version", col.getName()); assertEquals("bigint", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(2, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); assertEquals("ex_id", col.getName()); assertEquals("bigint", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(3, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); @@ -114,6 +117,7 @@ public class MySQLDialectExportTester extends AbstractExportTester assertEquals("local_name", col.getName()); assertEquals("varchar(200)", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(4, col.getOrder()); assertEquals(2, otherTable.getIndexes().size()); Iterator indexIt = otherTable.getIndexes().iterator(); @@ -135,6 +139,7 @@ public class MySQLDialectExportTester extends AbstractExportTester PrimaryKey pk = otherTable.getPrimaryKey(); assertSame("Incorrect parent or no parent set", otherTable, pk.getParent()); assertEquals("id", pk.getColumnNames().get(0)); + assertEquals(1, pk.getColumnOrders().get(0).intValue()); assertEquals(1, otherTable.getForeignKeys().size()); ForeignKey fk = otherTable.getForeignKeys().get(0); @@ -158,70 +163,82 @@ public class MySQLDialectExportTester extends AbstractExportTester assertEquals("id", col.getName()); assertEquals("bigint", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(1, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("description", col.getName()); assertEquals("text", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(2, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("fixes_from_schema", col.getName()); assertEquals("int", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(3, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("fixes_to_schema", col.getName()); assertEquals("int", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(4, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_to_schema", col.getName()); assertEquals("int", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(5, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("target_schema", col.getName()); assertEquals("int", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(6, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_on_date", col.getName()); assertEquals("datetime", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(7, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_to_server", col.getName()); assertEquals("varchar(64)", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(8, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("was_executed", col.getName()); assertEquals("bit", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(9, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("succeeded", col.getName()); assertEquals("bit", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(10, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("report", col.getName()); assertEquals("text", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(11, col.getOrder()); PrimaryKey pk = exampleTable.getPrimaryKey(); assertSame("Incorrect parent or no parent set", exampleTable, pk.getParent()); assertEquals("id", pk.getColumnNames().get(0)); + assertEquals(1, pk.getColumnOrders().get(0).intValue()); } diff --git a/source/java/org/alfresco/util/schemacomp/test/exportdb/PostgreSQLDialectExportTester.java b/source/java/org/alfresco/util/schemacomp/test/exportdb/PostgreSQLDialectExportTester.java index d072167d17..06007e583f 100644 --- a/source/java/org/alfresco/util/schemacomp/test/exportdb/PostgreSQLDialectExportTester.java +++ b/source/java/org/alfresco/util/schemacomp/test/exportdb/PostgreSQLDialectExportTester.java @@ -100,18 +100,21 @@ public class PostgreSQLDialectExportTester extends AbstractExportTester assertEquals("id", col.getName()); assertEquals("int8", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(1, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); assertEquals("version", col.getName()); assertEquals("int8", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(2, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); assertEquals("ex_id", col.getName()); assertEquals("int8", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(3, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", otherTable, col.getParent()); @@ -119,6 +122,7 @@ public class PostgreSQLDialectExportTester extends AbstractExportTester assertEquals("local_name", col.getName()); assertEquals("varchar(200)", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(4, col.getOrder()); assertEquals(2, otherTable.getIndexes().size()); Iterator indexIt = otherTable.getIndexes().iterator(); @@ -140,6 +144,7 @@ public class PostgreSQLDialectExportTester extends AbstractExportTester PrimaryKey pk = otherTable.getPrimaryKey(); assertSame("Incorrect parent or no parent set", otherTable, pk.getParent()); assertEquals("id", pk.getColumnNames().get(0)); + assertEquals(1, pk.getColumnOrders().get(0).intValue()); assertEquals(1, otherTable.getForeignKeys().size()); ForeignKey fk = otherTable.getForeignKeys().get(0); @@ -159,74 +164,87 @@ public class PostgreSQLDialectExportTester extends AbstractExportTester assertEquals("export_test_example", exampleTable.getName()); Iterator colIt = exampleTable.getColumns().iterator(); Column col = colIt.next(); + assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("id", col.getName()); assertEquals("int8", col.getType()); assertEquals(false, col.isNullable()); + assertEquals(1, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("description", col.getName()); assertEquals("varchar(1024)", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(2, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("fixes_from_schema", col.getName()); assertEquals("int4", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(3, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("fixes_to_schema", col.getName()); assertEquals("int4", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(4, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_to_schema", col.getName()); assertEquals("int4", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(5, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("target_schema", col.getName()); assertEquals("int4", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(6, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_on_date", col.getName()); assertEquals("timestamp", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(7, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("applied_to_server", col.getName()); assertEquals("varchar(64)", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(8, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("was_executed", col.getName()); assertEquals("bool", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(9, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("succeeded", col.getName()); assertEquals("bool", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(10, col.getOrder()); col = colIt.next(); assertSame("Incorrect parent or no parent set", exampleTable, col.getParent()); assertEquals("report", col.getName()); assertEquals("varchar(1024)", col.getType()); assertEquals(true, col.isNullable()); + assertEquals(11, col.getOrder()); PrimaryKey pk = exampleTable.getPrimaryKey(); assertSame("Incorrect parent or no parent set", exampleTable, pk.getParent()); assertEquals("id", pk.getColumnNames().get(0)); + assertEquals(1, pk.getColumnOrders().get(0).intValue()); } diff --git a/source/test-resources/schemacomp/xml_to_schema_test.xml b/source/test-resources/schemacomp/xml_to_schema_test.xml index 1c044e9b7b..2ba5e81869 100644 --- a/source/test-resources/schemacomp/xml_to_schema_test.xml +++ b/source/test-resources/schemacomp/xml_to_schema_test.xml @@ -6,22 +6,22 @@

- + NUMBER(10) false - + VARCHAR2(200) false - + VARCHAR2(150) true - id + id