Search in sources :

Example 11 with IIndex

use of org.jumpmind.db.model.IIndex in project symmetric-ds by JumpMind.

the class DatabaseXmlUtil method write.

public static void write(Table table, Writer output) {
    try {
        output.write("\t<table name=\"" + StringEscapeUtils.escapeXml(table.getName()) + "\">\n");
        for (Column column : table.getColumns()) {
            output.write("\t\t<column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
            if (column.isPrimaryKey()) {
                output.write(" primaryKey=\"" + column.isPrimaryKey() + "\"");
            }
            if (column.isRequired()) {
                output.write(" required=\"" + column.isRequired() + "\"");
            }
            if (column.getMappedType() != null) {
                output.write(" type=\"" + column.getMappedType() + "\"");
            }
            if (column.getSize() != null) {
                output.write(" size=\"" + column.getSize() + "\"");
            }
            if (column.getDefaultValue() != null) {
                output.write(" default=\"" + StringEscapeUtils.escapeXml(column.getDefaultValue()) + "\"");
            }
            if (column.isAutoIncrement()) {
                output.write(" autoIncrement=\"" + column.isAutoIncrement() + "\"");
            }
            if (column.getJavaName() != null) {
                output.write(" javaName=\"" + column.getJavaName() + "\"");
            }
            if (column.isUnique()) {
                output.write(" unique=\"" + column.isUnique() + "\"");
            }
            if (column.getPlatformColumns() != null && column.getPlatformColumns().size() > 0) {
                Collection<PlatformColumn> platformColumns = column.getPlatformColumns().values();
                output.write(">\n");
                for (PlatformColumn platformColumn : platformColumns) {
                    output.write("\t\t\t<platform-column name=\"" + platformColumn.getName() + "\"");
                    output.write(" type=\"" + platformColumn.getType() + "\"");
                    if (platformColumn.getSize() > 0) {
                        output.write(" size=\"" + platformColumn.getSize() + "\"");
                    }
                    if (platformColumn.getDecimalDigits() > 0) {
                        output.write(" decimalDigits=\"" + platformColumn.getDecimalDigits() + "\"");
                    }
                    if (platformColumn.getDefaultValue() != null) {
                        output.write(" default=\"" + StringEscapeUtils.escapeXml(platformColumn.getDefaultValue()) + "\"");
                    }
                    output.write("/>\n");
                }
                output.write("\t\t</column>\n");
            } else {
                output.write("/>\n");
            }
        }
        for (ForeignKey fk : table.getForeignKeys()) {
            output.write("\t\t<foreign-key name=\"" + StringEscapeUtils.escapeXml(fk.getName()) + "\" foreignTable=\"" + StringEscapeUtils.escapeXml(fk.getForeignTableName()) + "\">\n");
            for (Reference ref : fk.getReferences()) {
                output.write("\t\t\t<reference local=\"" + StringEscapeUtils.escapeXml(ref.getLocalColumnName()) + "\" foreign=\"" + StringEscapeUtils.escapeXml(ref.getForeignColumnName()) + "\"/>\n");
            }
            output.write("\t\t</foreign-key>\n");
        }
        for (IIndex index : table.getIndices()) {
            if (index.isUnique()) {
                output.write("\t\t<unique name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
                for (IndexColumn column : index.getColumns()) {
                    output.write("\t\t\t<unique-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"/>\n");
                }
                output.write("\t\t</unique>\n");
            } else {
                output.write("\t\t<index name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
                for (IndexColumn column : index.getColumns()) {
                    output.write("\t\t\t<index-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
                    if (column.getSize() != null) {
                        output.write(" size=\"" + column.getSize() + "\"");
                    }
                    output.write("/>\n");
                }
                output.write("\t\t</index>\n");
            }
        }
        output.write("\t</table>\n");
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) Reference(org.jumpmind.db.model.Reference) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) ForeignKey(org.jumpmind.db.model.ForeignKey) PlatformColumn(org.jumpmind.db.model.PlatformColumn) IndexColumn(org.jumpmind.db.model.IndexColumn)

Example 12 with IIndex

use of org.jumpmind.db.model.IIndex in project symmetric-ds by JumpMind.

the class AbstractDatabasePlatform method alterCaseToMatchDatabaseDefaultCase.

public void alterCaseToMatchDatabaseDefaultCase(Table table) {
    table.setName(alterCaseToMatchDatabaseDefaultCase(table.getName()));
    Column[] columns = table.getColumns();
    for (Column column : columns) {
        column.setName(alterCaseToMatchDatabaseDefaultCase(column.getName()));
    }
    IIndex[] indexes = table.getIndices();
    for (IIndex index : indexes) {
        index.setName(alterCaseToMatchDatabaseDefaultCase(index.getName()));
        IndexColumn[] indexColumns = index.getColumns();
        for (IndexColumn indexColumn : indexColumns) {
            indexColumn.setName(alterCaseToMatchDatabaseDefaultCase(indexColumn.getName()));
        }
    }
    ForeignKey[] fks = table.getForeignKeys();
    for (ForeignKey foreignKey : fks) {
        foreignKey.setName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getName()));
        foreignKey.setForeignTableName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getForeignTableName()));
        Reference[] references = foreignKey.getReferences();
        for (Reference reference : references) {
            reference.setForeignColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getForeignColumnName()));
            reference.setLocalColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getLocalColumnName()));
        }
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey) IndexColumn(org.jumpmind.db.model.IndexColumn)

Example 13 with IIndex

use of org.jumpmind.db.model.IIndex in project symmetric-ds by JumpMind.

the class AbstractDdlBuilder method getRealTargetTableFor.

/**
     * Creates the target table object that differs from the given target table
     * only in the indices. More specifically, only those indices are used that
     * have not changed.
     *
     * @param targetModel
     *            The target database
     * @param sourceTable
     *            The source table
     * @param targetTable
     *            The target table
     * @return The table
     */
protected Table getRealTargetTableFor(Database targetModel, Table sourceTable, Table targetTable) {
    Table table = new Table();
    table.setCatalog(targetTable.getCatalog());
    table.setSchema(targetTable.getSchema());
    table.setName(targetTable.getName());
    table.setType(targetTable.getType());
    for (int idx = 0; idx < targetTable.getColumnCount(); idx++) {
        try {
            table.addColumn((Column) targetTable.getColumn(idx).clone());
        } catch (CloneNotSupportedException ex) {
            throw new DdlException(ex);
        }
    }
    boolean caseSensitive = delimitedIdentifierModeOn;
    for (int idx = 0; idx < targetTable.getIndexCount(); idx++) {
        IIndex targetIndex = targetTable.getIndex(idx);
        IIndex sourceIndex = sourceTable.findIndex(targetIndex.getName(), caseSensitive);
        if (sourceIndex != null) {
            if ((caseSensitive && sourceIndex.equals(targetIndex)) || (!caseSensitive && sourceIndex.equalsIgnoreCase(targetIndex))) {
                table.addIndex(targetIndex);
            }
        }
    }
    return table;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table)

Example 14 with IIndex

use of org.jumpmind.db.model.IIndex in project symmetric-ds by JumpMind.

the class MsSql2000DdlBuilder method processChanges.

@Override
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) {
    if (!changes.isEmpty()) {
        writeQuotationOnStatement(ddl);
    }
    /*
         * For column data type and size changes, we need to drop and then
         * re-create indexes and foreign keys using the column, as well as any
         * primary keys containg these columns However, if the index/foreign
         * key/primary key is already slated for removal or change, then we
         * don't want to generate change duplication
         */
    HashSet<IIndex> removedIndexes = new HashSet<IIndex>();
    HashSet<ForeignKey> removedForeignKeys = new HashSet<ForeignKey>();
    HashSet<Table> removedPKs = new HashSet<Table>();
    for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        IModelChange change = changeIt.next();
        if (change instanceof RemoveIndexChange) {
            removedIndexes.add(((RemoveIndexChange) change).getIndex());
        } else if (change instanceof RemoveForeignKeyChange) {
            removedForeignKeys.add(((RemoveForeignKeyChange) change).getForeignKey());
        } else if (change instanceof RemovePrimaryKeyChange) {
            removedPKs.add(((RemovePrimaryKeyChange) change).getChangedTable());
        }
    }
    ArrayList<TableChange> additionalChanges = new ArrayList<TableChange>();
    for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        IModelChange change = changeIt.next();
        if ((change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange)) {
            Column column = ((ColumnChange) change).getChangedColumn();
            Table table = ((ColumnChange) change).getChangedTable();
            if (column.isPrimaryKey() && !removedPKs.contains(table)) {
                Column[] pk = table.getPrimaryKeyColumns();
                additionalChanges.add(new RemovePrimaryKeyChange(table, pk));
                additionalChanges.add(new AddPrimaryKeyChange(table, pk));
                removedPKs.add(table);
            }
            for (int idx = 0; idx < table.getIndexCount(); idx++) {
                IIndex index = table.getIndex(idx);
                if (index.hasColumn(column) && !removedIndexes.contains(index)) {
                    additionalChanges.add(new RemoveIndexChange(table, index));
                    additionalChanges.add(new AddIndexChange(table, index));
                    removedIndexes.add(index);
                }
            }
            for (int tableIdx = 0; tableIdx < currentModel.getTableCount(); tableIdx++) {
                Table curTable = currentModel.getTable(tableIdx);
                for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++) {
                    ForeignKey curFk = curTable.getForeignKey(fkIdx);
                    if ((curFk.hasLocalColumn(column) || curFk.hasForeignColumn(column)) && !removedForeignKeys.contains(curFk)) {
                        additionalChanges.add(new RemoveForeignKeyChange(curTable, curFk));
                        additionalChanges.add(new AddForeignKeyChange(curTable, curFk));
                        removedForeignKeys.add(curFk);
                    }
                }
            }
        }
    }
    changes.addAll(additionalChanges);
    super.processChanges(currentModel, desiredModel, changes, ddl);
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) Table(org.jumpmind.db.model.Table) AddIndexChange(org.jumpmind.db.alter.AddIndexChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ArrayList(java.util.ArrayList) IModelChange(org.jumpmind.db.alter.IModelChange) AddForeignKeyChange(org.jumpmind.db.alter.AddForeignKeyChange) RemoveForeignKeyChange(org.jumpmind.db.alter.RemoveForeignKeyChange) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) ForeignKey(org.jumpmind.db.model.ForeignKey) Column(org.jumpmind.db.model.Column) ColumnChange(org.jumpmind.db.alter.ColumnChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) RemoveIndexChange(org.jumpmind.db.alter.RemoveIndexChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) HashSet(java.util.HashSet) TableChange(org.jumpmind.db.alter.TableChange)

Example 15 with IIndex

use of org.jumpmind.db.model.IIndex in project symmetric-ds by JumpMind.

the class SqliteDdlReader method readTable.

public Table readTable(String catalog, String schema, String tableName) {
    Table table = null;
    List<Column> columns = platform.getSqlTemplate().query("pragma table_info(" + tableName + ")", COLUMN_MAPPER);
    checkForAutoIncrementColumn(columns, tableName);
    if (columns != null && columns.size() > 0) {
        table = new Table(tableName);
        for (Column column : columns) {
            table.addColumn(column);
        }
        List<IIndex> indexes = platform.getSqlTemplate().query("pragma index_list(" + tableName + ")", INDEX_MAPPER);
        for (IIndex index : indexes) {
            List<IndexColumn> indexColumns = platform.getSqlTemplate().query("pragma index_info(" + index.getName() + ")", INDEX_COLUMN_MAPPER);
            for (IndexColumn indexColumn : indexColumns) {
                if (!indexColumn.getName().startsWith("sqlite_autoindex_")) {
                    index.addColumn(indexColumn);
                    indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
                }
            }
            if (index.isUnique() && index.getName().toLowerCase().contains("autoindex") && !index.hasAllPrimaryKeys()) {
                for (IndexColumn indexColumn : indexColumns) {
                    table.getColumnWithName(indexColumn.getName()).setUnique(true);
                }
            } else if (!((index.hasAllPrimaryKeys() || index.isUnique()) && index.getName().toLowerCase().contains("autoindex"))) {
                table.addIndex(index);
            }
        }
        Map<Integer, ForeignKey> keys = new HashMap<Integer, ForeignKey>();
        List<Row> rows = platform.getSqlTemplate().query("pragma foreign_key_list(" + tableName + ")", new RowMapper());
        for (Row row : rows) {
            Integer id = row.getInt("id");
            ForeignKey fk = keys.get(id);
            if (fk == null) {
                fk = new ForeignKey();
                fk.setForeignTable(new Table(row.getString("table")));
                keys.put(id, fk);
                table.addForeignKey(fk);
            }
            fk.addReference(new Reference(new Column(row.getString("from")), new Column(row.getString("to"))));
        }
    }
    return table;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table) HashMap(java.util.HashMap) Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey) IndexColumn(org.jumpmind.db.model.IndexColumn) IndexColumn(org.jumpmind.db.model.IndexColumn) Column(org.jumpmind.db.model.Column) Row(org.jumpmind.db.sql.Row) RowMapper(org.jumpmind.db.sql.mapper.RowMapper) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Aggregations

IIndex (org.jumpmind.db.model.IIndex)21 Column (org.jumpmind.db.model.Column)9 Table (org.jumpmind.db.model.Table)7 ForeignKey (org.jumpmind.db.model.ForeignKey)6 IndexColumn (org.jumpmind.db.model.IndexColumn)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 PlatformColumn (org.jumpmind.db.model.PlatformColumn)4 Reference (org.jumpmind.db.model.Reference)4 PreparedStatement (java.sql.PreparedStatement)3 LinkedHashMap (java.util.LinkedHashMap)3 IOException (java.io.IOException)2 ListOrderedMap (org.apache.commons.collections.map.ListOrderedMap)2 NonUniqueIndex (org.jumpmind.db.model.NonUniqueIndex)2 UniqueIndex (org.jumpmind.db.model.UniqueIndex)2 IoException (org.jumpmind.exception.IoException)2 HashSet (java.util.HashSet)1 AddColumnChange (org.jumpmind.db.alter.AddColumnChange)1 AddForeignKeyChange (org.jumpmind.db.alter.AddForeignKeyChange)1