Search in sources :

Example 1 with SequenceInformation

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

the class AbstractSchemaValidator method performValidation.

public void performValidation(Metadata metadata, DatabaseInformation databaseInformation, ExecutionOptions options, Dialect dialect) {
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        if (schemaFilter.includeNamespace(namespace)) {
            validateTables(metadata, databaseInformation, options, dialect, namespace);
        }
    }
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        if (schemaFilter.includeNamespace(namespace)) {
            for (Sequence sequence : namespace.getSequences()) {
                if (schemaFilter.includeSequence(sequence)) {
                    final SequenceInformation sequenceInformation = databaseInformation.getSequenceInformation(sequence.getName());
                    validateSequence(sequence, sequenceInformation);
                }
            }
        }
    }
}
Also used : Sequence(org.hibernate.boot.model.relational.Sequence) SequenceInformation(org.hibernate.tool.schema.extract.spi.SequenceInformation) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 2 with SequenceInformation

use of org.hibernate.tool.schema.extract.spi.SequenceInformation 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 SequenceInformation

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

the class SequenceInformationExtractorH2DatabaseImpl method extractMetadata.

@Override
public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException {
    final IdentifierHelper identifierHelper = extractionContext.getJdbcEnvironment().getIdentifierHelper();
    final Statement statement = extractionContext.getJdbcConnection().createStatement();
    try {
        ResultSet resultSet = statement.executeQuery("select SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME, INCREMENT " + "from information_schema.sequences");
        try {
            final List<SequenceInformation> sequenceInformationList = new ArrayList<SequenceInformation>();
            while (resultSet.next()) {
                sequenceInformationList.add(new SequenceInformationImpl(new QualifiedSequenceName(identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_CATALOG")), identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_SCHEMA")), identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_NAME"))), resultSet.getInt("INCREMENT")));
            }
            return sequenceInformationList;
        } finally {
            try {
                resultSet.close();
            } catch (SQLException ignore) {
            }
        }
    } finally {
        try {
            statement.close();
        } catch (SQLException ignore) {
        }
    }
}
Also used : QualifiedSequenceName(org.hibernate.boot.model.relational.QualifiedSequenceName) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) SequenceInformation(org.hibernate.tool.schema.extract.spi.SequenceInformation) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper)

Example 4 with SequenceInformation

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

the class SequenceInformationExtractorLegacyImpl method extractMetadata.

@Override
public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException {
    final String lookupSql = extractionContext.getJdbcEnvironment().getDialect().getQuerySequencesString();
    // *should* never happen, but to be safe in the interest of performance...
    if (lookupSql == null) {
        return SequenceInformationExtractorNoOpImpl.INSTANCE.extractMetadata(extractionContext);
    }
    final IdentifierHelper identifierHelper = extractionContext.getJdbcEnvironment().getIdentifierHelper();
    final Statement statement = extractionContext.getJdbcConnection().createStatement();
    try {
        final ResultSet resultSet = statement.executeQuery(lookupSql);
        try {
            final List<SequenceInformation> sequenceInformationList = new ArrayList<SequenceInformation>();
            while (resultSet.next()) {
                sequenceInformationList.add(new SequenceInformationImpl(new QualifiedSequenceName(null, null, identifierHelper.toIdentifier(resultSet.getString(1))), -1));
            }
            return sequenceInformationList;
        } finally {
            try {
                resultSet.close();
            } catch (SQLException ignore) {
            }
        }
    } finally {
        try {
            statement.close();
        } catch (SQLException ignore) {
        }
    }
}
Also used : QualifiedSequenceName(org.hibernate.boot.model.relational.QualifiedSequenceName) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) SequenceInformation(org.hibernate.tool.schema.extract.spi.SequenceInformation) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper)

Aggregations

SequenceInformation (org.hibernate.tool.schema.extract.spi.SequenceInformation)4 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 Namespace (org.hibernate.boot.model.relational.Namespace)2 QualifiedSequenceName (org.hibernate.boot.model.relational.QualifiedSequenceName)2 Sequence (org.hibernate.boot.model.relational.Sequence)2 IdentifierHelper (org.hibernate.engine.jdbc.env.spi.IdentifierHelper)2 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 Formatter (org.hibernate.engine.jdbc.internal.Formatter)1 Table (org.hibernate.mapping.Table)1 NameSpaceTablesInformation (org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation)1 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)1