Search in sources :

Example 41 with Identifier

use of org.hibernate.boot.model.naming.Identifier 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)

Example 42 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method addColumns.

private void addColumns(TableInformation tableInformation) {
    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().getColumns(catalogFilter, schemaFilter, tableName.getTableName().getText(), "%");
        try {
            while (resultSet.next()) {
                final String columnName = resultSet.getString("COLUMN_NAME");
                final ColumnInformationImpl columnInformation = new ColumnInformationImpl(tableInformation, DatabaseIdentifier.toIdentifier(columnName), resultSet.getInt("DATA_TYPE"), new StringTokenizer(resultSet.getString("TYPE_NAME"), "() ").nextToken(), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), interpretTruthValue(resultSet.getString("IS_NULLABLE")));
                tableInformation.addColumn(columnInformation);
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing column metadata: " + tableName.toString());
    }
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) StringTokenizer(java.util.StringTokenizer) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 43 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method getPrimaryKey.

@Override
public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation) {
    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().getPrimaryKeys(catalogFilter, schemaFilter, tableInformation.getName().getTableName().getText());
        final List<ColumnInformation> pkColumns = new ArrayList<ColumnInformation>();
        boolean firstPass = true;
        Identifier pkIdentifier = null;
        try {
            while (resultSet.next()) {
                final String currentPkName = resultSet.getString("PK_NAME");
                final Identifier currentPkIdentifier = currentPkName == null ? null : DatabaseIdentifier.toIdentifier(currentPkName);
                if (firstPass) {
                    pkIdentifier = currentPkIdentifier;
                    firstPass = false;
                } else {
                    if (!EqualsHelper.equals(pkIdentifier, currentPkIdentifier)) {
                        throw new SchemaExtractionException(String.format("Encountered primary keys differing name on table %s", tableInformation.getName().toString()));
                    }
                }
                final int columnPosition = resultSet.getInt("KEY_SEQ");
                final Identifier columnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME"));
                final ColumnInformation column = tableInformation.getColumn(columnIdentifier);
                pkColumns.add(columnPosition - 1, column);
            }
        } finally {
            resultSet.close();
        }
        if (firstPass) {
            // we did not find any results (no pk)
            return null;
        } else {
            // validate column list is properly contiguous
            for (int i = 0; i < pkColumns.size(); i++) {
                if (pkColumns.get(i) == null) {
                    throw new SchemaExtractionException("Primary Key information was missing for KEY_SEQ = " + (i + 1));
                }
            }
            // build the return
            return new PrimaryKeyInformationImpl(pkIdentifier, pkColumns);
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error while reading primary key meta data for " + tableInformation.getName().toString());
    }
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) SQLException(java.sql.SQLException) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) SchemaExtractionException(org.hibernate.tool.schema.extract.spi.SchemaExtractionException)

Example 44 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class TableInformationImpl method foreignKeys.

protected Map<Identifier, ForeignKeyInformation> foreignKeys() {
    if (foreignKeys == null) {
        final Map<Identifier, ForeignKeyInformation> fkMap = new HashMap<>();
        final Iterable<ForeignKeyInformation> fks = extractor.getForeignKeys(this);
        for (ForeignKeyInformation fk : fks) {
            fkMap.put(fk.getForeignKeyIdentifier(), fk);
        }
        this.foreignKeys = fkMap;
    }
    return foreignKeys;
}
Also used : ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) HashMap(java.util.HashMap)

Example 45 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class SchemaDropperImpl method dropFromMetadata.

private void dropFromMetadata(Metadata metadata, ExecutionOptions options, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
    final Database database = metadata.getDatabase();
    final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
    boolean tryToDropCatalogs = false;
    boolean tryToDropSchemas = false;
    if (options.shouldManageNamespaces()) {
        if (dialect.canCreateSchema()) {
            tryToDropSchemas = true;
        }
        if (dialect.canCreateCatalog()) {
            tryToDropCatalogs = true;
        }
    }
    final Set<String> exportIdentifiers = new HashSet<String>(50);
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            continue;
        }
        if (!auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            continue;
        }
        applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
    }
    for (Namespace namespace : database.getNamespaces()) {
        if (!schemaFilter.includeNamespace(namespace)) {
            continue;
        }
        // we need to drop all constraints/indexes prior to dropping the tables
        applyConstraintDropping(namespace, metadata, formatter, options, targets);
        // now it's safe to drop the tables
        for (Table table : namespace.getTables()) {
            if (!table.isPhysicalTable()) {
                continue;
            }
            if (!schemaFilter.includeTable(table)) {
                continue;
            }
            checkExportIdentifier(table, exportIdentifiers);
            applySqlStrings(dialect.getTableExporter().getSqlDropStrings(table, metadata), formatter, options, targets);
        }
        for (Sequence sequence : namespace.getSequences()) {
            if (!schemaFilter.includeSequence(sequence)) {
                continue;
            }
            checkExportIdentifier(sequence, exportIdentifiers);
            applySqlStrings(dialect.getSequenceExporter().getSqlDropStrings(sequence, metadata), formatter, options, targets);
        }
    }
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            continue;
        }
        if (!auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            continue;
        }
        applySqlStrings(auxiliaryDatabaseObject.sqlDropStrings(jdbcEnvironment.getDialect()), formatter, options, targets);
    }
    if (tryToDropCatalogs || tryToDropSchemas) {
        Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
        for (Namespace namespace : database.getNamespaces()) {
            if (!schemaFilter.includeNamespace(namespace)) {
                continue;
            }
            if (tryToDropSchemas && namespace.getPhysicalName().getSchema() != null) {
                applySqlStrings(dialect.getDropSchemaCommand(namespace.getPhysicalName().getSchema().render(dialect)), formatter, options, targets);
            }
            if (tryToDropCatalogs) {
                final Identifier catalogLogicalName = namespace.getName().getCatalog();
                final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
                if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
                    applySqlStrings(dialect.getDropCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
                    exportedCatalogs.add(catalogLogicalName);
                }
            }
        }
    }
}
Also used : Table(org.hibernate.mapping.Table) Identifier(org.hibernate.boot.model.naming.Identifier) Database(org.hibernate.boot.model.relational.Database) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Sequence(org.hibernate.boot.model.relational.Sequence) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Namespace(org.hibernate.boot.model.relational.Namespace) HashSet(java.util.HashSet)

Aggregations

Identifier (org.hibernate.boot.model.naming.Identifier)46 Table (org.hibernate.mapping.Table)15 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)10 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)9 SimpleValue (org.hibernate.mapping.SimpleValue)9 HashMap (java.util.HashMap)7 MappingException (org.hibernate.boot.MappingException)7 Namespace (org.hibernate.boot.model.relational.Namespace)7 Database (org.hibernate.boot.model.relational.Database)6 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)5 Column (org.hibernate.mapping.Column)5 Property (org.hibernate.mapping.Property)5 HashSet (java.util.HashSet)4 DuplicateMappingException (org.hibernate.DuplicateMappingException)4 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)4 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)4 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)4