Search in sources :

Example 1 with IndexInformation

use of org.hibernate.tool.schema.extract.spi.IndexInformation in project hibernate-orm by hibernate.

the class TableInformationImpl method indexes.

protected Map<Identifier, IndexInformation> indexes() {
    if (indexes == null) {
        final Map<Identifier, IndexInformation> indexMap = new HashMap<>();
        final Iterable<IndexInformation> indexes = extractor.getIndexes(this);
        for (IndexInformation index : indexes) {
            indexMap.put(index.getIndexIdentifier(), index);
        }
        this.indexes = indexMap;
    }
    return indexes;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) IndexInformation(org.hibernate.tool.schema.extract.spi.IndexInformation) HashMap(java.util.HashMap)

Example 2 with IndexInformation

use of org.hibernate.tool.schema.extract.spi.IndexInformation in project hibernate-orm by hibernate.

the class AbstractSchemaMigrator method applyIndexes.

protected void applyIndexes(Table table, TableInformation tableInformation, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
    final Exporter<Index> exporter = dialect.getIndexExporter();
    final Iterator<Index> indexItr = table.getIndexIterator();
    while (indexItr.hasNext()) {
        final Index index = indexItr.next();
        if (!StringHelper.isEmpty(index.getName())) {
            IndexInformation existingIndex = null;
            if (tableInformation != null) {
                existingIndex = findMatchingIndex(index, tableInformation);
            }
            if (existingIndex == null) {
                applySqlStrings(false, exporter.getSqlCreateStrings(index, metadata), formatter, options, targets);
            }
        }
    }
}
Also used : IndexInformation(org.hibernate.tool.schema.extract.spi.IndexInformation) Index(org.hibernate.mapping.Index)

Example 3 with IndexInformation

use of org.hibernate.tool.schema.extract.spi.IndexInformation in project hibernate-orm by hibernate.

the class AbstractSchemaMigrator method applyUniqueKeys.

protected void applyUniqueKeys(Table table, TableInformation tableInfo, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
    if (uniqueConstraintStrategy == null) {
        uniqueConstraintStrategy = determineUniqueConstraintSchemaUpdateStrategy(metadata);
    }
    if (uniqueConstraintStrategy != UniqueConstraintSchemaUpdateStrategy.SKIP) {
        final Exporter<Constraint> exporter = dialect.getUniqueKeyExporter();
        final Iterator ukItr = table.getUniqueKeyIterator();
        while (ukItr.hasNext()) {
            final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
            // Skip if index already exists. Most of the time, this
            // won't work since most Dialects use Constraints. However,
            // keep it for the few that do use Indexes.
            IndexInformation indexInfo = null;
            if (tableInfo != null && StringHelper.isNotEmpty(uniqueKey.getName())) {
                indexInfo = tableInfo.getIndex(Identifier.toIdentifier(uniqueKey.getName()));
            }
            if (indexInfo == null) {
                if (uniqueConstraintStrategy == UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY) {
                    applySqlStrings(true, exporter.getSqlDropStrings(uniqueKey, metadata), formatter, options, targets);
                }
                applySqlStrings(true, exporter.getSqlCreateStrings(uniqueKey, metadata), formatter, options, targets);
            }
        }
    }
}
Also used : IndexInformation(org.hibernate.tool.schema.extract.spi.IndexInformation) Constraint(org.hibernate.mapping.Constraint) UniqueKey(org.hibernate.mapping.UniqueKey) Iterator(java.util.Iterator)

Example 4 with IndexInformation

use of org.hibernate.tool.schema.extract.spi.IndexInformation in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method getIndexes.

@Override
public Iterable<IndexInformation> getIndexes(TableInformation tableInformation) {
    final Map<Identifier, IndexInformationImpl.Builder> builders = new HashMap<>();
    final QualifiedTableName tableName = tableInformation.getName();
    final Identifier catalog = tableName.getCatalogName();
    final Identifier schema = tableName.getSchemaName();
    final String catalogFilter;
    final String schemaFilter;
    if (catalog == null) {
        catalogFilter = "";
    } else {
        catalogFilter = catalog.getText();
    }
    if (schema == null) {
        schemaFilter = "";
    } else {
        schemaFilter = schema.getText();
    }
    try {
        ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getIndexInfo(catalogFilter, schemaFilter, tableName.getTableName().getText(), // DO NOT limit to just unique
        false, // DO require up-to-date results
        true);
        try {
            while (resultSet.next()) {
                if (resultSet.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic) {
                    continue;
                }
                final Identifier indexIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("INDEX_NAME"));
                IndexInformationImpl.Builder builder = builders.get(indexIdentifier);
                if (builder == null) {
                    builder = IndexInformationImpl.builder(indexIdentifier);
                    builders.put(indexIdentifier, builder);
                }
                final Identifier columnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME"));
                final ColumnInformation columnInformation = tableInformation.getColumn(columnIdentifier);
                if (columnInformation == null) {
                    // See HHH-10191: this may happen when dealing with Oracle/PostgreSQL function indexes
                    log.logCannotLocateIndexColumnInformation(columnIdentifier.getText(), indexIdentifier.getText());
                }
                builder.addColumn(columnInformation);
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing index information: " + tableInformation.getName().toString());
    }
    final List<IndexInformation> indexes = new ArrayList<IndexInformation>();
    for (IndexInformationImpl.Builder builder : builders.values()) {
        IndexInformationImpl index = builder.build();
        indexes.add(index);
    }
    return indexes;
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) IndexInformation(org.hibernate.tool.schema.extract.spi.IndexInformation) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) ResultSet(java.sql.ResultSet)

Aggregations

IndexInformation (org.hibernate.tool.schema.extract.spi.IndexInformation)4 HashMap (java.util.HashMap)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 Constraint (org.hibernate.mapping.Constraint)1 Index (org.hibernate.mapping.Index)1 UniqueKey (org.hibernate.mapping.UniqueKey)1 ColumnInformation (org.hibernate.tool.schema.extract.spi.ColumnInformation)1