Search in sources :

Example 11 with Column

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

the class ExportDb method processTables.

private void processTables(DatabaseMetaData dbmd, ResultSet tables) throws SQLException, IllegalArgumentException, IllegalAccessException {
    if (tables == null) {
        return;
    }
    while (tables.next()) {
        final String tableName = tables.getString("TABLE_NAME");
        if (log.isDebugEnabled()) {
            log.debug("Examining table tableName=[" + tableName + "]");
        }
        // ALF-14129 fix, check whether schema already contains object with provided name
        if (tableName.startsWith("BIN$") || schema.containsByName(tableName)) {
            continue;
        }
        if (tables.getString("TABLE_TYPE").equals("SEQUENCE")) {
            Sequence sequence = new Sequence(tableName);
            schema.add(sequence);
            continue;
        }
        Table table = new Table(tableName);
        schema.add(table);
        // Table columns
        final ResultSet columns = dbmd.getColumns(null, tables.getString("TABLE_SCHEM"), tableName, "%");
        while (columns.next()) {
            String columnName = columns.getString("COLUMN_NAME");
            Column column = new Column(columnName);
            String dbType = columns.getString("TYPE_NAME");
            int colSize = columns.getInt("COLUMN_SIZE");
            int scale = columns.getInt("DECIMAL_DIGITS");
            int jdbcType = columns.getInt("DATA_TYPE");
            String type = generateType(dbType, colSize, scale, jdbcType);
            column.setType(type);
            String nullableString = columns.getString("IS_NULLABLE");
            column.setNullable(parseBoolean(nullableString));
            column.setOrder(columns.getInt("ORDINAL_POSITION"));
            try {
                String autoIncString = columns.getString("IS_AUTOINCREMENT");
                column.setAutoIncrement(parseBoolean(autoIncString));
            } catch (SQLException jtdsDoesNOtHAveIsUatoincrement) {
                column.setAutoIncrement((dbType.endsWith("identity")));
            }
            column.setParent(table);
            table.getColumns().add(column);
        }
        columns.close();
        // Primary key
        final ResultSet primarykeycols = dbmd.getPrimaryKeys(null, tables.getString("TABLE_SCHEM"), tableName);
        PrimaryKey pk = null;
        while (primarykeycols.next()) {
            if (pk == null) {
                String pkName = primarykeycols.getString("PK_NAME");
                pk = new PrimaryKey(pkName);
            }
            String columnName = primarykeycols.getString("COLUMN_NAME");
            pk.getColumnNames().add(columnName);
            int columnOrder = primarykeycols.getInt("KEY_SEQ");
            pk.getColumnOrders().add(columnOrder);
        }
        primarykeycols.close();
        // If this table has a primary key, add it.
        if (pk != null) {
            pk.setParent(table);
            table.setPrimaryKey(pk);
        }
        // Indexes
        final ResultSet indexes = dbmd.getIndexInfo(null, tables.getString("TABLE_SCHEM"), tableName, false, true);
        String lastIndexName = "";
        Index index = null;
        while (indexes.next()) {
            final String indexName = indexes.getString("INDEX_NAME");
            if (indexName == null) {
                // Oracle seems to have some dummy index entries
                continue;
            } else // Skip the index corresponding to the PK if it is mentioned
            if (indexName.equals(table.getPrimaryKey().getName())) {
                continue;
            }
            if (!indexName.equals(lastIndexName)) {
                index = new Index(indexName);
                index.setUnique(!indexes.getBoolean("NON_UNIQUE"));
                index.setParent(table);
                table.getIndexes().add(index);
                lastIndexName = indexName;
            }
            if (index != null) {
                String columnName = indexes.getString("COLUMN_NAME");
                index.getColumnNames().add(columnName);
            }
        }
        indexes.close();
        final ResultSet foreignkeys = dbmd.getImportedKeys(null, tables.getString("TABLE_SCHEM"), tableName);
        String lastKeyName = "";
        ForeignKey fk = null;
        while (foreignkeys.next()) {
            final String keyName = foreignkeys.getString("FK_NAME");
            if (!keyName.equals(lastKeyName)) {
                fk = new ForeignKey(keyName);
                fk.setParent(table);
                table.getForeignKeys().add(fk);
                lastKeyName = keyName;
            }
            if (fk != null) {
                fk.setLocalColumn(foreignkeys.getString("FKCOLUMN_NAME"));
                fk.setTargetTable(foreignkeys.getString("PKTABLE_NAME"));
                fk.setTargetColumn(foreignkeys.getString("PKCOLUMN_NAME"));
            }
        }
        foreignkeys.close();
    }
    tables.close();
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Column(org.alfresco.util.schemacomp.model.Column) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) 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)

Example 12 with Column

use of org.alfresco.util.schemacomp.model.Column 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 13 with Column

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

the class TypeNameOnlyValidator method validate.

@Override
public void validate(DbObject reference, DbObject target, DiffContext ctx) {
    if (!(target instanceof Column)) {
        throw new AlfrescoRuntimeException("TypeNameOnlyValidator could be used only in context of column object but was: " + target.toString());
    }
    String referenceTypeName = ((Column) reference).getType();
    String targetTypeName = ((Column) target).getType();
    if (referenceTypeName.contains(TYPE_SIZE_SPLITTER)) {
        referenceTypeName = referenceTypeName.substring(0, referenceTypeName.indexOf(TYPE_SIZE_SPLITTER));
    }
    if (targetTypeName.contains(TYPE_SIZE_SPLITTER)) {
        targetTypeName = targetTypeName.substring(0, targetTypeName.indexOf(TYPE_SIZE_SPLITTER));
    }
    if (!referenceTypeName.equals(targetTypeName)) {
        String message = I18NUtil.getMessage("system.schema_comp.column_names_validator", targetTypeName, referenceTypeName);
        ValidationResult result = new ValidationResult(new DbProperty(target, "type"), message);
        ctx.getComparisonResults().add(result);
    }
}
Also used : Column(org.alfresco.util.schemacomp.model.Column) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ValidationResult(org.alfresco.util.schemacomp.ValidationResult) DbProperty(org.alfresco.util.schemacomp.DbProperty)

Example 14 with Column

use of org.alfresco.util.schemacomp.model.Column 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 15 with Column

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

the class PostgreSQLDialectExportTester method checkExampleTable.

private void checkExampleTable(Schema schema, Table exampleTable) {
    assertNotNull("Couldn't find export_test_example", exampleTable);
    assertSame("Incorrect parent or no parent set", schema, exampleTable.getParent());
    assertEquals("export_test_example", exampleTable.getName());
    Iterator<Column> 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());
}
Also used : Column(org.alfresco.util.schemacomp.model.Column) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey)

Aggregations

Column (org.alfresco.util.schemacomp.model.Column)19 Index (org.alfresco.util.schemacomp.model.Index)11 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)11 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)10 Test (org.junit.Test)8 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 Schema (org.alfresco.util.schemacomp.model.Schema)4 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 DbProperty (org.alfresco.util.schemacomp.DbProperty)1 ValidationResult (org.alfresco.util.schemacomp.ValidationResult)1 DbObject (org.alfresco.util.schemacomp.model.DbObject)1 NameValidator (org.alfresco.util.schemacomp.validator.NameValidator)1