Search in sources :

Example 1 with IIndex

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

the class Db2As400DdlReader method readIndices.

@Override
protected Collection<IIndex> readIndices(Connection connection, DatabaseMetaDataWrapper metaData, String tableName) throws SQLException {
    Map<String, IIndex> indices = new LinkedHashMap<String, IIndex>();
    if (getPlatformInfo().isIndicesSupported()) {
        ResultSet indexData = null;
        try {
            indexData = metaData.getIndices(getTableNamePatternForConstraints(tableName), false, false);
            Collection<Column> columns = readColumns(metaData, tableName);
            while (indexData.next()) {
                Map<String, Object> values = readMetaData(indexData, getColumnsForIndex());
                String columnName = (String) values.get("COLUMN_NAME");
                if (hasColumn(columns, columnName)) {
                    readIndex(metaData, values, indices);
                }
            }
        } finally {
            close(indexData);
        }
    }
    return indices.values();
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with IIndex

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

the class AbstractJdbcDdlReader method removeInternalForeignKeyIndex.

/*
     * Tries to remove the internal index for the given foreign key.
     * 
     * @param metaData The database meta data
     * 
     * @param table The table where the table is defined
     * 
     * @param fk The foreign key
     */
protected void removeInternalForeignKeyIndex(Connection connection, DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk) throws SQLException {
    List<String> columnNames = new ArrayList<String>();
    for (int columnIdx = 0; columnIdx < fk.getReferenceCount(); columnIdx++) {
        columnNames.add(fk.getReference(columnIdx).getLocalColumnName());
    }
    for (int indexIdx = 0; indexIdx < table.getIndexCount(); ) {
        IIndex index = table.getIndex(indexIdx);
        if (matches(index, columnNames) && isInternalForeignKeyIndex(connection, metaData, table, fk, index)) {
            fk.setAutoIndexPresent(true);
            table.removeIndex(indexIdx);
        } else {
            indexIdx++;
        }
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ArrayList(java.util.ArrayList)

Example 3 with IIndex

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

the class AbstractDatabasePlatform method prefixIndexes.

protected void prefixIndexes(Table table, String tablePrefix, boolean storesUpperCaseIdentifiers) throws CloneNotSupportedException {
    IIndex[] indexes = table.getIndices();
    if (indexes != null) {
        for (IIndex index : indexes) {
            String prefixedName = tablePrefix + index.getName();
            prefixedName = storesUpperCaseIdentifiers ? prefixedName.toUpperCase() : prefixedName.toLowerCase();
            index.setName(prefixedName);
        }
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex)

Example 4 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 5 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)

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