String schemaName = (table.getSchema() == null) ? null : table.getSchema().getName();
// get table name
String tableName = table.getName();
// create table's index
Index index = factory.createIndex();
// process all columns included into index
while (resultSet.next()) {
// table name, schema and catalog are already known, so it is useless to fetch
// Can index values be non-unique
Boolean nonUnique = getBoolean(resultSet, "NON_UNIQUE", false);
// index catalog
String indexQualifier = getString(resultSet, "INDEX_QUALIFIER", false);
// index name ; null when TYPE is tableIndexStatistic
String indexName = getString(resultSet, "INDEX_NAME", false);
// index type
Integer indexType = getInteger(resultSet, "TYPE", false);
// sequence number within index
Integer ordinalPosition = getInteger(resultSet, "ORDINAL_POSITION", false);
// index column name
String indexColumnName = getString(resultSet, "COLUMN_NAME", false);
// column sort sequence, "A" => ascending, "D" => descending, may be null if sort sequence is not supported;
// null when TYPE is tableIndexStatistic
String ascOrDesc = getString(resultSet, "ASC_OR_DESC", false);
// cardinality; When TYPE is tableIndexStatistic, then this is the number of rows in the table;
// otherwise, it is the number of unique values in the index.
Integer cardinality = getInteger(resultSet, "CARDINALITY", false);
// pages; When TYPE is tableIndexStatisic then this is the number of pages used for the table,
// otherwise it is the number of pages used for the current index.
Integer pages = getInteger(resultSet, "PAGES", false);
// filter condition if any (may be null)
String filterCondition = getString(resultSet, "FILTER_CONDITION", false);
//
// check if index name has been set earlier and current record shows different index -
// so we need to add index (previous) to the table
//
if ((index.getName() != null) && (!index.getName().equals(indexName))) {
// add previous FK to the table
table.addIndex(index);
// log
if (traceLog.isDebugEnabled()) {
traceLog.debug(String.format("[Database %s] The table '%s' (schema %s, catalog %s) index %s has been added.",
database.getName(),
tableName,
schemaName,
catalogName,
indexName));
}
// create new index if a record is not last
index = factory.createIndex();
} else {
// ***************************
// *** DatabaseNamedObject ***
// ***************************
// set name
index.setName(indexName);
// set remarks
// index.setRemarks (remarks); // N/A
// TODO set extra properties
// index.addExtraProperty (String key, Object value);
// ********************
// *** SchemaObject ***
// ********************
// set catalog; index catalog me be defined by indexQualifier
index.setCatalog((indexQualifier == null) ? table.getCatalog() : database.findCatalogByName(indexQualifier));
// set schema
index.setSchema(table.getSchema());
// ***************************
// *** Index ***
// ***************************
// set unique as inversion of non unique
index.setUnique(nonUnique == null ? null : (nonUnique.booleanValue() == true) ? Boolean.FALSE : Boolean.TRUE);
// set Index Type
index.setIndexType(getIndexType(indexType));
// set Cardinality
index.setCardinality(cardinality);
// set Pages
index.setPages(pages);
// set filter condition
index.setFilterCondition(filterCondition);
// trying to find table column with specified name
TableColumn tableColumn = table.findColumnByName(indexColumnName);
String errMessage = null;
// warn if null
if (tableColumn == null) {
errMessage = String.format("[Database %s] Unable to find table column '%5$s' for the table %s (schema %s, catalog %s)",
database.getName(),
tableName,
schemaName,
catalogName,
indexColumnName);
traceLog.debug(errMessage);
}
// if fail is enabled
if (failOnError && (tableColumn == null)) {
throw new DatabaseMetaDataMethodException(errMessage, "populateIndexes");
}
// create index column
IndexColumn indexColumn = factory.createIndexColumn();
// check if we found the original table column
if (tableColumn != null) {
// mark original table column as part of index
tableColumn.setIndexColumn(Boolean.TRUE);
// clone properties from original table column to the index column
PropertyUtils.copyProperties(indexColumn, tableColumn);
} else { // recovery if table column is not found but we still want to create index column
// set name at least
indexColumn.setName(indexColumnName);
}
// modify ordinal position that correspond to the position in index
indexColumn.setOrdinalPosition(ordinalPosition);
// sort sequence type
indexColumn.setSortSequenceType(getSortSequenceType(ascOrDesc));
// add index column to the index
index.addColumn(indexColumn);
}
}
// add index to the table
table.addIndex(index);
// log
if (traceLog.isDebugEnabled()) {
traceLog.debug(String.format("[Database %s] The table '%s' (schema %s, catalog %s) index %s has been added.",
database.getName(),
tableName,
schemaName,
catalogName,
index.getName()));
}
// return set of created indexes
return table.getIndexes();
}