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);
}
}
}
}
}
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);
}
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);
}
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);
}
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());
}
Aggregations