Search in sources :

Example 16 with IIndex

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

the class AbstractJdbcDdlReader method removeInternalPrimaryKeyIndex.

/*
     * Tries to remove the internal index for the table's primary key.
     * 
     * @param metaData The database meta data
     * 
     * @param table The table
     */
protected void removeInternalPrimaryKeyIndex(Connection connection, DatabaseMetaDataWrapper metaData, Table table) throws SQLException {
    Column[] pks = table.getPrimaryKeyColumns();
    List<String> columnNames = new ArrayList<String>();
    for (int columnIdx = 0; columnIdx < pks.length; columnIdx++) {
        columnNames.add(pks[columnIdx].getName());
    }
    for (int indexIdx = 0; indexIdx < table.getIndexCount(); ) {
        IIndex index = table.getIndex(indexIdx);
        if (index.isUnique() && matches(index, columnNames) && isInternalPrimaryKeyIndex(connection, metaData, table, index)) {
            table.removeIndex(indexIdx);
        } else {
            indexIdx++;
        }
    }
}
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) ArrayList(java.util.ArrayList)

Example 17 with IIndex

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

the class AbstractJdbcDdlReader method readIndex.

/*
     * Reads the next index spec from the result set.
     * 
     * @param metaData The database meta data
     * 
     * @param values The index meta data as defined by {@link
     * #getColumnsForIndex()}
     * 
     * @param knownIndices The already read indices for the current table
     */
protected void readIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> values, Map<String, IIndex> knownIndices) throws SQLException {
    Short indexType = (Short) values.get("TYPE");
    // we're ignoring statistic indices
    if ((indexType != null) && (indexType.shortValue() == DatabaseMetaData.tableIndexStatistic)) {
        return;
    }
    String indexName = (String) values.get("INDEX_NAME");
    if (indexName != null) {
        IIndex index = (IIndex) knownIndices.get(indexName);
        if (index == null) {
            if (((Boolean) values.get("NON_UNIQUE")).booleanValue()) {
                index = new NonUniqueIndex();
            } else {
                index = new UniqueIndex();
            }
            index.setName(indexName);
            knownIndices.put(indexName, index);
        }
        IndexColumn indexColumn = new IndexColumn();
        String columnName = (String) values.get("COLUMN_NAME");
        if (columnName.startsWith("\"") && columnName.endsWith("\"")) {
            columnName = columnName.substring(1, columnName.length() - 1);
        }
        indexColumn.setName(columnName);
        if (values.containsKey("ORDINAL_POSITION")) {
            indexColumn.setOrdinalPosition(((Short) values.get("ORDINAL_POSITION")).intValue());
        }
        index.addColumn(indexColumn);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) UniqueIndex(org.jumpmind.db.model.UniqueIndex) IndexColumn(org.jumpmind.db.model.IndexColumn)

Example 18 with IIndex

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

the class AbstractJdbcDdlReader method readIndices.

/*
     * Determines the indices for the indicated table.
     * 
     * @param metaData The database meta data
     * 
     * @param tableName The name of the table
     * 
     * @return The list of indices
     */
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);
            while (indexData.next()) {
                Map<String, Object> values = readMetaData(indexData, getColumnsForIndex());
                readIndex(metaData, values, indices);
            }
        } finally {
            close(indexData);
        }
    }
    return indices.values();
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with IIndex

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

the class InterbaseDdlReader 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, b.RDB$RELATION_NAME, b.RDB$UNIQUE_FLAG,    ");
    query.append(" a.RDB$FIELD_POSITION, a.RDB$FIELD_NAME                             ");
    query.append(" FROM RDB$INDEX_SEGMENTS a, RDB$INDICES b                           ");
    query.append(" 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 = new HashMap<String, Object>();
            values.put("INDEX_NAME", indexData.getString(1).trim());
            values.put("TABLE_NAME", indexData.getString(2).trim());
            values.put("NON_UNIQUE", !indexData.getBoolean(3));
            values.put("ORDINAL_POSITION", indexData.getShort(4));
            values.put("COLUMN_NAME", indexData.getString(5).trim());
            values.put("INDEX_TYPE", 3);
            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) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 20 with IIndex

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

the class MsSqlDdlReader method readTable.

@Override
protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    String tableName = (String) values.get("TABLE_NAME");
    for (int idx = 0; idx < KNOWN_SYSTEM_TABLES.length; idx++) {
        if (KNOWN_SYSTEM_TABLES[idx].equals(tableName)) {
            return null;
        }
    }
    Table table = super.readTable(connection, metaData, values);
    if (table != null) {
        // Sql Server does not return the auto-increment status via the
        // database metadata
        determineAutoIncrementFromResultSetMetaData(connection, table, table.getColumns());
        // This is then probably of interest to every platform
        for (int idx = 0; idx < table.getIndexCount(); ) {
            IIndex index = table.getIndex(idx);
            if (index.isUnique() && existsPKWithName(metaData, table, index.getName())) {
                table.removeIndex(idx);
            } else {
                idx++;
            }
        }
    }
    return table;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table)

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