Search in sources :

Example 6 with Sequence

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

Example 7 with Sequence

use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.

the class LegacySequenceExportTest method testMultipleUsesOfDefaultSequenceName.

@Test
@TestForIssue(jiraKey = "HHH-9936")
public void testMultipleUsesOfDefaultSequenceName() {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Entity1.class).addAnnotatedClass(Entity2.class).buildMetadata();
    metadata.validate();
    assertEquals(0, metadata.getDatabase().getAuxiliaryDatabaseObjects().size());
    int count = 0;
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        for (Sequence sequence : namespace.getSequences()) {
            count++;
        }
    }
    assertEquals(1, count);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 with Sequence

use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.

the class LegacySequenceExportTest method testMultipleUsesOfExplicitSequenceName.

@Test
@TestForIssue(jiraKey = "HHH-9936")
public void testMultipleUsesOfExplicitSequenceName() {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Entity3.class).addAnnotatedClass(Entity4.class).buildMetadata();
    metadata.validate();
    assertEquals(0, metadata.getDatabase().getAuxiliaryDatabaseObjects().size());
    int count = 0;
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        for (Sequence sequence : namespace.getSequences()) {
            count++;
        }
    }
    assertEquals(1, count);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with Sequence

use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.

the class SequenceExportTest method testMultipleUsesOfDefaultSequenceName.

@Test
@TestForIssue(jiraKey = "HHH-9936")
public void testMultipleUsesOfDefaultSequenceName() {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Entity1.class).addAnnotatedClass(Entity2.class).buildMetadata();
    metadata.validate();
    int namespaceCount = 0;
    int sequenceCount = 0;
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        namespaceCount++;
        for (Sequence sequence : namespace.getSequences()) {
            sequenceCount++;
        }
    }
    assertEquals(1, namespaceCount);
    assertEquals(1, sequenceCount);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with Sequence

use of org.hibernate.boot.model.relational.Sequence in project hibernate-orm by hibernate.

the class SequenceStructure method buildSequence.

protected void buildSequence(Database database) {
    final int sourceIncrementSize = getSourceIncrementSize();
    final Namespace namespace = database.locateNamespace(logicalQualifiedSequenceName.getCatalogName(), logicalQualifiedSequenceName.getSchemaName());
    Sequence sequence = namespace.locateSequence(logicalQualifiedSequenceName.getObjectName());
    if (sequence != null) {
        sequence.validate(initialValue, sourceIncrementSize);
    } else {
        sequence = namespace.createSequence(logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize);
    }
    this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(sequence.getName(), database.getJdbcEnvironment().getDialect());
}
Also used : Sequence(org.hibernate.boot.model.relational.Sequence) Namespace(org.hibernate.boot.model.relational.Namespace)

Aggregations

Namespace (org.hibernate.boot.model.relational.Namespace)10 Sequence (org.hibernate.boot.model.relational.Sequence)10 MetadataSources (org.hibernate.boot.MetadataSources)4 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 TestForIssue (org.hibernate.testing.TestForIssue)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Identifier (org.hibernate.boot.model.naming.Identifier)3 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)3 Database (org.hibernate.boot.model.relational.Database)3 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)3 Table (org.hibernate.mapping.Table)3 SequenceInformation (org.hibernate.tool.schema.extract.spi.SequenceInformation)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 InitCommand (org.hibernate.boot.model.relational.InitCommand)1 Dialect (org.hibernate.dialect.Dialect)1 Formatter (org.hibernate.engine.jdbc.internal.Formatter)1 ForeignKey (org.hibernate.mapping.ForeignKey)1 Index (org.hibernate.mapping.Index)1