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++;
}
}
}
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);
}
}
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();
}
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();
}
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;
}
Aggregations