Search in sources :

Example 6 with IIndex

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

the class AbstractDdlBuilder method writeExternalIndicesCreateStmt.

/**
     * Writes the indexes of the given table.
     *
     * @param table
     *            The table
     */
protected void writeExternalIndicesCreateStmt(Table table, StringBuilder ddl) {
    for (int idx = 0; idx < table.getIndexCount(); idx++) {
        IIndex index = table.getIndex(idx);
        if (!index.isUnique() && !databaseInfo.isIndicesSupported()) {
            return;
        }
        writeExternalIndexCreateStmt(table, index, ddl);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex)

Example 7 with IIndex

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

the class FirebirdDdlReader method readIndices.

@Override
protected Collection<IIndex> readIndices(Connection connection, DatabaseMetaDataWrapper metaData, String tableName) throws SQLException {
    // Jaybird is not able to read indices when delimited identifiers are
    // turned on, so we gather the data manually using Firebird's system tables
    @SuppressWarnings("unchecked") Map<String, IIndex> indices = new ListOrderedMap();
    StringBuilder query = new StringBuilder();
    query.append("SELECT a.RDB$INDEX_NAME INDEX_NAME, b.RDB$RELATION_NAME TABLE_NAME, b.RDB$UNIQUE_FLAG NON_UNIQUE,");
    query.append(" a.RDB$FIELD_POSITION ORDINAL_POSITION, a.RDB$FIELD_NAME COLUMN_NAME, 3 INDEX_TYPE");
    query.append(" FROM RDB$INDEX_SEGMENTS a, RDB$INDICES b WHERE a.RDB$INDEX_NAME=b.RDB$INDEX_NAME AND b.RDB$RELATION_NAME = ?");
    PreparedStatement stmt = connection.prepareStatement(query.toString());
    ResultSet indexData = null;
    stmt.setString(1, getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn() ? tableName : tableName.toUpperCase());
    try {
        indexData = stmt.executeQuery();
        while (indexData.next()) {
            Map<String, Object> values = readMetaData(indexData, getColumnsForIndex());
            // we have to reverse the meaning of the unique flag
            values.put("NON_UNIQUE", Boolean.FALSE.equals(values.get("NON_UNIQUE")) ? Boolean.TRUE : Boolean.FALSE);
            // and trim the names
            values.put("INDEX_NAME", ((String) values.get("INDEX_NAME")).trim());
            values.put("TABLE_NAME", ((String) values.get("TABLE_NAME")).trim());
            values.put("COLUMN_NAME", ((String) values.get("COLUMN_NAME")).trim());
            readIndex(metaData, values, indices);
        }
    } finally {
        if (indexData != null) {
            indexData.close();
        }
    }
    return indices.values();
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ListOrderedMap(org.apache.commons.collections.map.ListOrderedMap) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 8 with IIndex

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

the class ModelComparator method compareTables.

/**
     * Compares the two tables and returns the changes necessary to create the
     * second table from the first one.
     * 
     * @param sourceModel
     *            The source model which contains the source table
     * @param sourceTable
     *            The source table
     * @param targetModel
     *            The target model which contains the target table
     * @param targetTable
     *            The target table
     * @return The changes
     */
public List<IModelChange> compareTables(Database sourceModel, Table sourceTable, Database targetModel, Table targetTable) {
    ArrayList<IModelChange> changes = new ArrayList<IModelChange>();
    if (platformInfo.isForeignKeysSupported()) {
        for (int fkIdx = 0; fkIdx < sourceTable.getForeignKeyCount(); fkIdx++) {
            ForeignKey sourceFk = sourceTable.getForeignKey(fkIdx);
            ForeignKey targetFk = findCorrespondingForeignKey(targetTable, sourceFk);
            if (targetFk == null) {
                if (log.isDebugEnabled()) {
                    log.debug(sourceFk + " needs to be removed from table " + sourceTable.getName());
                }
                changes.add(new RemoveForeignKeyChange(sourceTable, sourceFk));
            }
        }
        for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) {
            ForeignKey targetFk = targetTable.getForeignKey(fkIdx);
            ForeignKey sourceFk = findCorrespondingForeignKey(sourceTable, targetFk);
            if (sourceFk == null) {
                if (log.isDebugEnabled()) {
                    log.debug(targetFk + " needs to be created for table " + sourceTable.getName());
                }
                /*
                     * we have to use the target table here because the foreign
                     * key might reference a new column
                     */
                changes.add(new AddForeignKeyChange(targetTable, targetFk));
            }
        }
    }
    if (platformInfo.isIndicesSupported()) {
        for (int indexIdx = 0; indexIdx < sourceTable.getIndexCount(); indexIdx++) {
            IIndex sourceIndex = sourceTable.getIndex(indexIdx);
            IIndex targetIndex = findCorrespondingIndex(targetTable, sourceIndex);
            if (targetIndex == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Index " + sourceIndex.getName() + " needs to be removed from table " + sourceTable.getName());
                }
                changes.add(new RemoveIndexChange(sourceTable, sourceIndex));
            }
        }
        for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++) {
            IIndex targetIndex = targetTable.getIndex(indexIdx);
            IIndex sourceIndex = findCorrespondingIndex(sourceTable, targetIndex);
            if (sourceIndex == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Index " + targetIndex.getName() + " needs to be created for table " + sourceTable.getName());
                }
                // we have to use the target table here because the index might
                // reference a new column
                changes.add(new AddIndexChange(targetTable, targetIndex));
            }
        }
    }
    HashMap<Column, TableChange> addColumnChanges = new HashMap<Column, TableChange>();
    for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++) {
        Column targetColumn = targetTable.getColumn(columnIdx);
        Column sourceColumn = sourceTable.findColumn(targetColumn.getName(), caseSensitive);
        if (sourceColumn == null) {
            log.debug("Column {} needs to be created for table {}", new Object[] { targetColumn.getName(), sourceTable.getName() });
            AddColumnChange change = new AddColumnChange(sourceTable, targetColumn, columnIdx > 0 ? targetTable.getColumn(columnIdx - 1) : null, columnIdx < targetTable.getColumnCount() - 1 ? targetTable.getColumn(columnIdx + 1) : null);
            changes.add(change);
            addColumnChanges.put(targetColumn, change);
        } else {
            changes.addAll(compareColumns(sourceTable, sourceColumn, targetTable, targetColumn));
        }
    }
    // at the changes
    for (int columnIdx = targetTable.getColumnCount() - 1; columnIdx >= 0; columnIdx--) {
        Column targetColumn = targetTable.getColumn(columnIdx);
        AddColumnChange change = (AddColumnChange) addColumnChanges.get(targetColumn);
        if (change == null) {
            // that were added
            break;
        } else {
            change.setAtEnd(true);
        }
    }
    Column[] sourcePK = sourceTable.getPrimaryKeyColumns();
    Column[] targetPK = targetTable.getPrimaryKeyColumns();
    if ((sourcePK.length == 0) && (targetPK.length > 0)) {
        if (log.isDebugEnabled()) {
            log.debug("A primary key needs to be added to the table " + sourceTable.getName());
        }
        // we have to use the target table here because the primary key
        // might
        // reference a new column
        changes.add(new AddPrimaryKeyChange(targetTable, targetPK));
    } else if ((targetPK.length == 0) && (sourcePK.length > 0)) {
        if (log.isDebugEnabled()) {
            log.debug("The primary key needs to be removed from the table " + sourceTable.getName());
        }
        changes.add(new RemovePrimaryKeyChange(sourceTable, sourcePK));
    } else if ((sourcePK.length > 0) && (targetPK.length > 0)) {
        boolean changePK = false;
        if (sourcePK.length != targetPK.length) {
            changePK = true;
        } else {
            for (int pkColumnIdx = 0; (pkColumnIdx < sourcePK.length) && !changePK; pkColumnIdx++) {
                if ((caseSensitive && !sourcePK[pkColumnIdx].getName().equals(targetPK[pkColumnIdx].getName())) || (!caseSensitive && !sourcePK[pkColumnIdx].getName().equalsIgnoreCase(targetPK[pkColumnIdx].getName()))) {
                    changePK = true;
                }
            }
        }
        if (changePK) {
            if (log.isDebugEnabled()) {
                log.debug("The primary key of table " + sourceTable.getName() + " needs to be changed");
            }
            changes.add(new PrimaryKeyChange(sourceTable, sourcePK, targetPK));
        }
    }
    for (int columnIdx = 0; columnIdx < sourceTable.getColumnCount(); columnIdx++) {
        Column sourceColumn = sourceTable.getColumn(columnIdx);
        Column targetColumn = targetTable.findColumn(sourceColumn.getName(), caseSensitive);
        if (targetColumn == null) {
            if (log.isDebugEnabled()) {
                log.debug("Column " + sourceColumn.getName() + " needs to be removed from table " + sourceTable.getName());
            }
            changes.add(new RemoveColumnChange(sourceTable, sourceColumn));
        }
    }
    return changes;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ForeignKey(org.jumpmind.db.model.ForeignKey) Column(org.jumpmind.db.model.Column)

Example 9 with IIndex

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

the class RemoveIndexChange method apply.

/**
     * {@inheritDoc}
     */
public void apply(Database database, boolean caseSensitive) {
    Table table = database.findTable(getChangedTable().getName(), caseSensitive);
    IIndex index = table.findIndex(_index.getName(), caseSensitive);
    table.removeIndex(index);
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table)

Example 10 with IIndex

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

the class AddIndexChange method apply.

/**
     * {@inheritDoc}
     */
public void apply(Database database, boolean caseSensitive) {
    IIndex newIndex = null;
    try {
        newIndex = (IIndex) _newIndex.clone();
    } catch (CloneNotSupportedException ex) {
        throw new DdlException(ex);
    }
    database.findTable(getChangedTable().getName(), caseSensitive).addIndex(newIndex);
}
Also used : IIndex(org.jumpmind.db.model.IIndex) DdlException(org.jumpmind.db.platform.DdlException)

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