Search in sources :

Example 1 with NameSpaceTablesInformation

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method getTables.

public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {
    String catalogFilter = null;
    String schemaFilter = null;
    if (extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs()) {
        if (catalog == null) {
            if (extractionContext.getJdbcEnvironment().getCurrentCatalog() != null) {
                // 1) look in current namespace
                catalogFilter = toMetaDataObjectName(extractionContext.getJdbcEnvironment().getCurrentCatalog());
            } else if (extractionContext.getDefaultCatalog() != null) {
                // 2) look in default namespace
                catalogFilter = toMetaDataObjectName(extractionContext.getDefaultCatalog());
            } else {
                catalogFilter = "";
            }
        } else {
            catalogFilter = toMetaDataObjectName(catalog);
        }
    }
    if (extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas()) {
        if (schema == null) {
            if (extractionContext.getJdbcEnvironment().getCurrentSchema() != null) {
                // 1) look in current namespace
                schemaFilter = toMetaDataObjectName(extractionContext.getJdbcEnvironment().getCurrentSchema());
            } else if (extractionContext.getDefaultSchema() != null) {
                // 2) look in default namespace
                schemaFilter = toMetaDataObjectName(extractionContext.getDefaultSchema());
            } else {
                schemaFilter = "";
            }
        } else {
            schemaFilter = toMetaDataObjectName(schema);
        }
    }
    try {
        ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getTables(catalogFilter, schemaFilter, "%", tableTypes);
        final NameSpaceTablesInformation tablesInformation = processTableResults(resultSet);
        populateTablesWithColumns(catalogFilter, schemaFilter, tablesInformation);
        return tablesInformation;
    } catch (SQLException sqlException) {
        throw convertSQLException(sqlException, "Error accessing table metadata");
    }
}
Also used : SQLException(java.sql.SQLException) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) ResultSet(java.sql.ResultSet)

Example 2 with NameSpaceTablesInformation

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

the class AbstractSchemaMigrator method performMigration.

private void performMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, GenerationTarget... targets) {
    final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
    final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
    final Set<String> exportIdentifiers = new HashSet<String>(50);
    final Database database = metadata.getDatabase();
    // Drop all AuxiliaryDatabaseObjects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            applySqlStrings(true, dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
        }
    }
    // Create beforeQuery-table AuxiliaryDatabaseObjects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (!auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
        }
    }
    boolean tryToCreateCatalogs = false;
    boolean tryToCreateSchemas = false;
    if (options.shouldManageNamespaces()) {
        if (dialect.canCreateSchema()) {
            tryToCreateSchemas = true;
        }
        if (dialect.canCreateCatalog()) {
            tryToCreateCatalogs = true;
        }
    }
    final Map<Namespace, NameSpaceTablesInformation> tablesInformation = new HashMap<>();
    Set<Identifier> exportedCatalogs = new HashSet<>();
    for (Namespace namespace : database.getNamespaces()) {
        final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration(metadata, existingDatabase, options, dialect, formatter, exportIdentifiers, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
        tablesInformation.put(namespace, nameSpaceTablesInformation);
        if (schemaFilter.includeNamespace(namespace)) {
            for (Sequence sequence : namespace.getSequences()) {
                checkExportIdentifier(sequence, exportIdentifiers);
                final SequenceInformation sequenceInformation = existingDatabase.getSequenceInformation(sequence.getName());
                if (sequenceInformation == null) {
                    applySqlStrings(false, dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), formatter, options, targets);
                }
            }
        }
    }
    //NOTE : Foreign keys must be created *afterQuery* all tables of all namespaces for cross namespace fks. see HHH-10420
    for (Namespace namespace : database.getNamespaces()) {
        if (schemaFilter.includeNamespace(namespace)) {
            final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation.get(namespace);
            for (Table table : namespace.getTables()) {
                if (schemaFilter.includeTable(table)) {
                    final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation(table);
                    if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
                        applyForeignKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
                    }
                }
            }
        }
    }
    // Create afterQuery-table AuxiliaryDatabaseObjects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
        }
    }
}
Also used : Table(org.hibernate.mapping.Table) HashMap(java.util.HashMap) Formatter(org.hibernate.engine.jdbc.internal.Formatter) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace) Identifier(org.hibernate.boot.model.naming.Identifier) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) Database(org.hibernate.boot.model.relational.Database) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) SequenceInformation(org.hibernate.tool.schema.extract.spi.SequenceInformation) HashSet(java.util.HashSet)

Example 3 with NameSpaceTablesInformation

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

the class GroupedSchemaMigratorImpl method performTablesMigration.

@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
    if (schemaFilter.includeNamespace(namespace)) {
        createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
        final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation(namespace);
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                checkExportIdentifier(table, exportIdentifiers);
                final TableInformation tableInformation = tables.getTableInformation(table);
                if (tableInformation == null) {
                    createTable(table, dialect, metadata, formatter, options, targets);
                } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
                    tablesInformation.addTableInformation(tableInformation);
                    migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                final TableInformation tableInformation = tablesInformation.getTableInformation(table);
                if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
                    applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
                    applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
    }
    return tablesInformation;
}
Also used : Table(org.hibernate.mapping.Table) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 4 with NameSpaceTablesInformation

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method processTableResults.

private NameSpaceTablesInformation processTableResults(ResultSet resultSet) throws SQLException {
    try {
        NameSpaceTablesInformation tables = new NameSpaceTablesInformation(identifierHelper());
        while (resultSet.next()) {
            final TableInformation tableInformation = extractTableInformation(resultSet);
            tables.addTableInformation(tableInformation);
        }
        return tables;
    } finally {
        try {
            resultSet.close();
        } catch (SQLException ignore) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 5 with NameSpaceTablesInformation

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

the class IndividuallySchemaMigratorImpl method performTablesMigration.

@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
    if (schemaFilter.includeNamespace(namespace)) {
        createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                checkExportIdentifier(table, exportIdentifiers);
                final TableInformation tableInformation = existingDatabase.getTableInformation(table.getQualifiedTableName());
                if (tableInformation == null) {
                    createTable(table, dialect, metadata, formatter, options, targets);
                } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
                    tablesInformation.addTableInformation(tableInformation);
                    migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                final TableInformation tableInformation = tablesInformation.getTableInformation(table);
                if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
                    applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
                    applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
    }
    return tablesInformation;
}
Also used : Table(org.hibernate.mapping.Table) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Aggregations

NameSpaceTablesInformation (org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation)5 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)4 Table (org.hibernate.mapping.Table)3 SQLException (java.sql.SQLException)2 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 Database (org.hibernate.boot.model.relational.Database)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 Sequence (org.hibernate.boot.model.relational.Sequence)1 Formatter (org.hibernate.engine.jdbc.internal.Formatter)1 SequenceInformation (org.hibernate.tool.schema.extract.spi.SequenceInformation)1