Search in sources :

Example 6 with Namespace

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

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

the class SchemaCreatorImpl method createFromMetadata.

public void createFromMetadata(Metadata metadata, ExecutionOptions options, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
    boolean tryToCreateCatalogs = false;
    boolean tryToCreateSchemas = false;
    if (options.shouldManageNamespaces()) {
        if (dialect.canCreateSchema()) {
            tryToCreateSchemas = true;
        }
        if (dialect.canCreateCatalog()) {
            tryToCreateCatalogs = true;
        }
    }
    final Database database = metadata.getDatabase();
    final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
    final Set<String> exportIdentifiers = new HashSet<String>(50);
    // first, create each catalog/schema
    if (tryToCreateCatalogs || tryToCreateSchemas) {
        Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
        for (Namespace namespace : database.getNamespaces()) {
            if (!schemaFilter.includeNamespace(namespace)) {
                continue;
            }
            if (tryToCreateCatalogs) {
                final Identifier catalogLogicalName = namespace.getName().getCatalog();
                final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
                if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
                    applySqlStrings(dialect.getCreateCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
                    exportedCatalogs.add(catalogLogicalName);
                }
            }
            if (tryToCreateSchemas && namespace.getPhysicalName().getSchema() != null) {
                applySqlStrings(dialect.getCreateSchemaCommand(namespace.getPhysicalName().getSchema().render(dialect)), formatter, options, targets);
            }
        }
    }
    // next, create all "beforeQuery table" auxiliary objects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            continue;
        }
        if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
            applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
        }
    }
    // then, create all schema objects (tables, sequences, constraints, etc) in each schema
    for (Namespace namespace : database.getNamespaces()) {
        if (!schemaFilter.includeNamespace(namespace)) {
            continue;
        }
        // sequences
        for (Sequence sequence : namespace.getSequences()) {
            if (!schemaFilter.includeSequence(sequence)) {
                continue;
            }
            checkExportIdentifier(sequence, exportIdentifiers);
            applySqlStrings(dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), //						),
            formatter, options, targets);
        }
        // tables
        for (Table table : namespace.getTables()) {
            if (!table.isPhysicalTable()) {
                continue;
            }
            if (!schemaFilter.includeTable(table)) {
                continue;
            }
            checkExportIdentifier(table, exportIdentifiers);
            applySqlStrings(dialect.getTableExporter().getSqlCreateStrings(table, metadata), formatter, options, targets);
        }
        for (Table table : namespace.getTables()) {
            if (!table.isPhysicalTable()) {
                continue;
            }
            if (!schemaFilter.includeTable(table)) {
                continue;
            }
            // indexes
            final Iterator indexItr = table.getIndexIterator();
            while (indexItr.hasNext()) {
                final Index index = (Index) indexItr.next();
                checkExportIdentifier(index, exportIdentifiers);
                applySqlStrings(dialect.getIndexExporter().getSqlCreateStrings(index, metadata), formatter, options, targets);
            }
            // unique keys
            final Iterator ukItr = table.getUniqueKeyIterator();
            while (ukItr.hasNext()) {
                final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
                checkExportIdentifier(uniqueKey, exportIdentifiers);
                applySqlStrings(dialect.getUniqueKeyExporter().getSqlCreateStrings(uniqueKey, 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)) {
            continue;
        }
        for (Table table : namespace.getTables()) {
            if (!schemaFilter.includeTable(table)) {
                continue;
            }
            // foreign keys
            final Iterator fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                final ForeignKey foreignKey = (ForeignKey) fkItr.next();
                applySqlStrings(dialect.getForeignKeyExporter().getSqlCreateStrings(foreignKey, metadata), formatter, options, targets);
            }
        }
    }
    // next, create all "afterQuery table" auxiliary objects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (auxiliaryDatabaseObject.appliesToDialect(dialect) && !auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
            applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
        }
    }
    // and finally add all init commands
    for (InitCommand initCommand : database.getInitCommands()) {
        // todo: this should alo probably use the DML formatter...
        applySqlStrings(initCommand.getInitCommands(), formatter, options, targets);
    }
}
Also used : Table(org.hibernate.mapping.Table) InitCommand(org.hibernate.boot.model.relational.InitCommand) Index(org.hibernate.mapping.Index) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Sequence(org.hibernate.boot.model.relational.Sequence) ForeignKey(org.hibernate.mapping.ForeignKey) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Namespace(org.hibernate.boot.model.relational.Namespace) Identifier(org.hibernate.boot.model.naming.Identifier) UniqueKey(org.hibernate.mapping.UniqueKey) Database(org.hibernate.boot.model.relational.Database) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 8 with Namespace

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

the class QualifiedTableNamingTest method testQualifiedNameSeparator.

@Test
public void testQualifiedNameSeparator() throws Exception {
    Namespace.Name namespaceName = new Namespace.Name(Identifier.toIdentifier("DB1"), Identifier.toIdentifier("PUBLIC"));
    String expectedName = null;
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        if (!namespace.getName().equals(namespaceName)) {
            continue;
        }
        assertEquals(1, namespace.getTables().size());
        expectedName = metadata().getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(namespace.getTables().iterator().next().getQualifiedTableName(), getDialect());
    }
    assertNotNull(expectedName);
    SingleTableEntityPersister persister = (SingleTableEntityPersister) sessionFactory().getEntityPersister(Box.class.getName());
    assertEquals(expectedName, persister.getTableName());
}
Also used : SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) Namespace(org.hibernate.boot.model.relational.Namespace) Test(org.junit.Test)

Example 9 with Namespace

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

the class ForeignKeyConstraintTest method assertForeignKey.

private void assertForeignKey(String foreignKeyName, String... columns) {
    Set<String> columnSet = new LinkedHashSet<>(Arrays.asList(columns));
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        for (org.hibernate.mapping.Table table : namespace.getTables()) {
            Iterator<org.hibernate.mapping.ForeignKey> fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                org.hibernate.mapping.ForeignKey fk = fkItr.next();
                if (foreignKeyName.equals(fk.getName())) {
                    assertEquals("ForeignKey column count not like expected", columnSet.size(), fk.getColumnSpan());
                    List<String> columnNames = fk.getColumns().stream().map(Column::getName).collect(Collectors.toList());
                    assertTrue("ForeignKey columns [" + columnNames + "] do not match expected columns [" + columnSet + "]", columnSet.containsAll(columnNames));
                    return;
                }
            }
        }
    }
    fail("ForeignKey '" + foreignKeyName + "' could not be found!");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ForeignKey(javax.persistence.ForeignKey) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 10 with Namespace

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

the class ForeignKeyConstraintTest method assertNoForeignKey.

private void assertNoForeignKey(String foreignKeyName, String... columns) {
    Set<String> columnSet = new LinkedHashSet<>(Arrays.asList(columns));
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        for (org.hibernate.mapping.Table table : namespace.getTables()) {
            Iterator<org.hibernate.mapping.ForeignKey> fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                org.hibernate.mapping.ForeignKey fk = fkItr.next();
                assertFalse("ForeignKey [" + foreignKeyName + "] defined and shouldn't have been.", foreignKeyName.equals(fk.getName()));
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ForeignKey(javax.persistence.ForeignKey) Namespace(org.hibernate.boot.model.relational.Namespace)

Aggregations

Namespace (org.hibernate.boot.model.relational.Namespace)22 Sequence (org.hibernate.boot.model.relational.Sequence)10 Table (org.hibernate.mapping.Table)10 Identifier (org.hibernate.boot.model.naming.Identifier)7 Test (org.junit.Test)7 MetadataSources (org.hibernate.boot.MetadataSources)5 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)5 TestForIssue (org.hibernate.testing.TestForIssue)5 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 Column (org.hibernate.mapping.Column)4 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)4 HashSet (java.util.HashSet)3 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)3 Database (org.hibernate.boot.model.relational.Database)3 Dialect (org.hibernate.dialect.Dialect)3 Iterator (java.util.Iterator)2 LinkedHashSet (java.util.LinkedHashSet)2 ForeignKey (javax.persistence.ForeignKey)2 InitCommand (org.hibernate.boot.model.relational.InitCommand)2 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)2