Search in sources :

Example 6 with ForeignKey

use of org.alfresco.util.schemacomp.model.ForeignKey in project alfresco-repository by Alfresco.

the class XMLToSchema method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    lastText.setLength(0);
    if (qName.equals(XML.EL_SCHEMA)) {
        String name = atts.getValue(XML.ATTR_NAME);
        String dbPrefix = atts.getValue(XML.ATTR_DB_PREFIX);
        int version = Integer.parseInt(atts.getValue(XML.ATTR_VERSION));
        String attrTableColumnOrder = atts.getValue(XML.ATTR_TABLE_COLUMN_ORDER);
        // Should column order be checked for tables?
        boolean compareTableColOrder = attrTableColumnOrder != null ? Boolean.parseBoolean(attrTableColumnOrder) : true;
        schema = new Schema(name, dbPrefix, version, compareTableColOrder);
        stack.push(schema);
    } else if (qName.equals(XML.EL_TABLE)) {
        stack.push(new Table(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_COLUMN)) {
        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);
        }
        column.setCompareOrder(schema.isCheckTableColumnOrder());
        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)) {
        stack.push(new PrimaryKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_FOREIGN_KEY)) {
        stack.push(new ForeignKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_INDEX)) {
        Index index = new Index(atts.getValue(XML.ATTR_NAME));
        boolean unique = Boolean.parseBoolean(atts.getValue(XML.ATTR_UNIQUE));
        index.setUnique(unique);
        stack.push(index);
    } else if (qName.equals(XML.EL_SEQUENCE)) {
        stack.push(new Sequence(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_VALIDATOR)) {
        String className = atts.getValue(XML.ATTR_CLASS);
        DbValidator validator = null;
        try {
            validator = (DbValidator) Class.forName(className).newInstance();
        } catch (Throwable e) {
            throw new RuntimeException("Couldn't create validator, class: " + className, e);
        }
        stack.push(validator);
    } else if (qName.equals(XML.EL_PROPERTY)) {
        String name = atts.getValue(XML.ATTR_NAME);
        stack.push(name);
    }
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 7 with ForeignKey

use of org.alfresco.util.schemacomp.model.ForeignKey in project alfresco-repository by Alfresco.

the class PostgreSQLDialectExportTester method checkOtherTable.

private void checkOtherTable(Schema schema, Table otherTable) {
    assertNotNull("Couldn't find table export_test_other", otherTable);
    assertSame("Incorrect parent or no parent set", schema, otherTable.getParent());
    Iterator<Column> colIt = otherTable.getColumns().iterator();
    Column col = colIt.next();
    assertSame("Incorrect parent or no parent set", otherTable, 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", 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());
    assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
    assertEquals("local_name", col.getName());
    assertEquals("varchar(200)", col.getType());
    assertEquals(false, col.isNullable());
    assertEquals(4, col.getOrder());
    assertEquals(2, otherTable.getIndexes().size());
    Iterator<Index> indexIt = otherTable.getIndexes().iterator();
    Index index = indexIt.next();
    assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
    assertEquals("export_test_idx_other_1", index.getName());
    assertEquals(true, index.isUnique());
    assertEquals(2, index.getColumnNames().size());
    assertEquals("ex_id", index.getColumnNames().get(0));
    assertEquals("local_name", index.getColumnNames().get(1));
    index = indexIt.next();
    assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
    assertEquals("export_test_idx_other_2", index.getName());
    assertEquals(1, index.getColumnNames().size());
    assertEquals("ex_id", index.getColumnNames().get(0));
    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);
    assertSame("Incorrect parent or no parent set", otherTable, fk.getParent());
    assertEquals("export_test_fk_example", fk.getName());
    assertEquals("ex_id", fk.getLocalColumn());
    assertEquals("export_test_example", fk.getTargetTable());
    assertEquals("id", fk.getTargetColumn());
}
Also used : Column(org.alfresco.util.schemacomp.model.Column) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey)

Example 8 with ForeignKey

use of org.alfresco.util.schemacomp.model.ForeignKey in project alfresco-repository by Alfresco.

the class DbObjectXMLTransformerTest method transformObjectWithValidators.

@Test
public void transformObjectWithValidators() throws IOException {
    Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
    PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
    Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
    Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
    Table table = new Table(null, "my_table", columns, pk, fks, indexes);
    NameValidator nameValidator = new NameValidator();
    nameValidator.setPattern(Pattern.compile("match_me_if_you_can"));
    List<DbValidator> validators = new ArrayList<DbValidator>();
    validators.add(nameValidator);
    table.setValidators(validators);
    transformer.output(table);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<table name=\"my_table\">", reader.readLine());
    assertEquals("  <validators>", reader.readLine());
    assertEquals("    <validator class=\"org.alfresco.util.schemacomp.validator.NameValidator\">", reader.readLine());
    assertEquals("      <properties>", reader.readLine());
    assertEquals("        <property name=\"pattern\">match_me_if_you_can</property>", reader.readLine());
    assertEquals("      </properties>", reader.readLine());
    assertEquals("    </validator>", reader.readLine());
    assertEquals("  </validators>", reader.readLine());
    assertEquals("  <columns>", reader.readLine());
    skipUntilEnd("       {column}", reader);
    skipUntilEnd("       {column}", reader);
    assertEquals("  </columns>", reader.readLine());
    skipUntilEnd("  {primarykey}", reader);
    assertEquals("  <foreignkeys>", reader.readLine());
    skipUntilEnd("       {foreignkey}", reader);
    skipUntilEnd("       {foreignkey}", reader);
    assertEquals("  </foreignkeys>", reader.readLine());
    assertEquals("  <indexes>", reader.readLine());
    skipUntilEnd("       {index}", reader);
    skipUntilEnd("       {index}", reader);
    assertEquals("  </indexes>", reader.readLine());
    assertEquals("</table>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) NameValidator(org.alfresco.util.schemacomp.validator.NameValidator) ArrayList(java.util.ArrayList) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Test(org.junit.Test)

Example 9 with ForeignKey

use of org.alfresco.util.schemacomp.model.ForeignKey in project alfresco-repository by Alfresco.

the class DbObjectXMLTransformerTest method transformSchemaNoColumnOrderCheck.

@Test
public void transformSchemaNoColumnOrderCheck() throws IOException {
    Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
    PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
    Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
    Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
    Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes);
    Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes);
    Schema schema = new Schema("my_schema", "alf_", 132, false);
    schema.add(tableOne);
    schema.add(tableTwo);
    schema.add(new Sequence(null, "sequence_one"));
    schema.add(new Sequence(null, "sequence_two"));
    schema.add(new Sequence(null, "sequence_three"));
    schema.setValidators(new ArrayList<DbValidator>());
    transformer.output(schema);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<schema " + "xmlns=\"http://www.alfresco.org/repo/db-schema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\" " + "name=\"my_schema\" dbprefix=\"alf_\" version=\"132\" tablecolumnorder=\"false\">", reader.readLine());
    assertEquals("  <objects>", reader.readLine());
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    assertEquals("  </objects>", reader.readLine());
    assertEquals("</schema>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Test(org.junit.Test)

Example 10 with ForeignKey

use of org.alfresco.util.schemacomp.model.ForeignKey in project alfresco-repository by Alfresco.

the class DbObjectXMLTransformerTest method transformTable.

@Test
public void transformTable() throws IOException {
    Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
    PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
    Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
    Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
    Table table = new Table(null, "my_table", columns, pk, fks, indexes);
    transformer.output(table);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<table name=\"my_table\">", reader.readLine());
    assertEquals("  <columns>", reader.readLine());
    skipUntilEnd("       {column}", reader);
    skipUntilEnd("       {column}", reader);
    assertEquals("  </columns>", reader.readLine());
    skipUntilEnd("  {primarykey}", reader);
    assertEquals("  <foreignkeys>", reader.readLine());
    skipUntilEnd("       {foreignkey}", reader);
    skipUntilEnd("       {foreignkey}", reader);
    assertEquals("  </foreignkeys>", reader.readLine());
    assertEquals("  <indexes>", reader.readLine());
    skipUntilEnd("       {index}", reader);
    skipUntilEnd("       {index}", reader);
    assertEquals("  </indexes>", reader.readLine());
    assertEquals("</table>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Test(org.junit.Test)

Aggregations

ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)11 Column (org.alfresco.util.schemacomp.model.Column)10 Index (org.alfresco.util.schemacomp.model.Index)10 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)9 Table (org.alfresco.util.schemacomp.model.Table)7 BufferedReader (java.io.BufferedReader)5 StringReader (java.io.StringReader)5 Sequence (org.alfresco.util.schemacomp.model.Sequence)5 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)5 Test (org.junit.Test)5 Schema (org.alfresco.util.schemacomp.model.Schema)3 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DbObject (org.alfresco.util.schemacomp.model.DbObject)1 NameValidator (org.alfresco.util.schemacomp.validator.NameValidator)1