Search in sources :

Example 16 with Database

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

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

the class Ejb3Column method buildColumnFromAnnotation.

public static Ejb3Column[] buildColumnFromAnnotation(javax.persistence.Column[] anns, org.hibernate.annotations.Formula formulaAnn, Nullability nullability, PropertyHolder propertyHolder, PropertyData inferredData, String suffixForDefaultColumnName, Map<String, Join> secondaryTables, MetadataBuildingContext context) {
    Ejb3Column[] columns;
    if (formulaAnn != null) {
        Ejb3Column formulaColumn = new Ejb3Column();
        formulaColumn.setFormula(formulaAnn.value());
        formulaColumn.setImplicit(false);
        formulaColumn.setBuildingContext(context);
        formulaColumn.setPropertyHolder(propertyHolder);
        formulaColumn.bind();
        columns = new Ejb3Column[] { formulaColumn };
    } else {
        javax.persistence.Column[] actualCols = anns;
        javax.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn(StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
        if (overriddenCols != null) {
            //check for overridden first
            if (anns != null && overriddenCols.length != anns.length) {
                throw new AnnotationException("AttributeOverride.column() should override all columns for now");
            }
            actualCols = overriddenCols.length == 0 ? null : overriddenCols;
            LOG.debugf("Column(s) overridden for property %s", inferredData.getPropertyName());
        }
        if (actualCols == null) {
            columns = buildImplicitColumn(inferredData, suffixForDefaultColumnName, secondaryTables, propertyHolder, nullability, context);
        } else {
            final int length = actualCols.length;
            columns = new Ejb3Column[length];
            for (int index = 0; index < length; index++) {
                final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
                final Database database = context.getMetadataCollector().getDatabase();
                final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
                final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
                javax.persistence.Column col = actualCols[index];
                final String sqlType;
                if (col.columnDefinition().equals("")) {
                    sqlType = null;
                } else {
                    sqlType = normalizer.applyGlobalQuoting(col.columnDefinition());
                }
                final String tableName;
                if (StringHelper.isEmpty(col.table())) {
                    tableName = "";
                } else {
                    tableName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.table()).render();
                //						final Identifier logicalName = database.getJdbcEnvironment()
                //								.getIdentifierHelper()
                //								.toIdentifier( col.table() );
                //						final Identifier physicalName = physicalNamingStrategy.toPhysicalTableName( logicalName );
                //						tableName = physicalName.render( database.getDialect() );
                }
                final String columnName;
                if ("".equals(col.name())) {
                    columnName = null;
                } else {
                    // NOTE : this is the logical column name, not the physical!
                    columnName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.name()).render();
                }
                Ejb3Column column = new Ejb3Column();
                if (length == 1) {
                    applyColumnDefault(column, inferredData);
                }
                column.setImplicit(false);
                column.setSqlType(sqlType);
                column.setLength(col.length());
                column.setPrecision(col.precision());
                column.setScale(col.scale());
                if (StringHelper.isEmpty(columnName) && !StringHelper.isEmpty(suffixForDefaultColumnName)) {
                    column.setLogicalColumnName(inferredData.getPropertyName() + suffixForDefaultColumnName);
                } else {
                    column.setLogicalColumnName(columnName);
                }
                column.setPropertyName(BinderHelper.getRelativePath(propertyHolder, inferredData.getPropertyName()));
                column.setNullable(col.nullable());
                //TODO force to not null if available? This is a (bad) user choice.
                column.setUnique(col.unique());
                column.setInsertable(col.insertable());
                column.setUpdatable(col.updatable());
                column.setExplicitTableName(tableName);
                column.setPropertyHolder(propertyHolder);
                column.setJoins(secondaryTables);
                column.setBuildingContext(context);
                column.extractDataFromPropertyData(inferredData);
                column.bind();
                columns[index] = column;
            }
        }
    }
    return columns;
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) Column(org.hibernate.mapping.Column) Database(org.hibernate.boot.model.relational.Database) AnnotationException(org.hibernate.AnnotationException)

Example 18 with Database

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

the class TestExtraPhysicalTableTypes method buildInformationExtractorJdbcDatabaseMetaDataImplTest.

private InformationExtractorJdbcDatabaseMetaDataImplTest buildInformationExtractorJdbcDatabaseMetaDataImplTest() throws SQLException {
    Database database = metadata.getDatabase();
    final ConnectionProvider connectionProvider = ssr.getService(ConnectionProvider.class);
    DatabaseInformation dbInfo = new DatabaseInformationImpl(ssr, database.getJdbcEnvironment(), new DdlTransactionIsolatorTestingImpl(ssr, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider)), database.getDefaultNamespace().getName());
    ExtractionContextImpl extractionContext = new ExtractionContextImpl(ssr, database.getJdbcEnvironment(), ssr.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess(), (ExtractionContext.DatabaseObjectAccess) dbInfo, database.getDefaultNamespace().getPhysicalName().getCatalog(), database.getDefaultNamespace().getPhysicalName().getSchema());
    return new InformationExtractorJdbcDatabaseMetaDataImplTest(extractionContext);
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) DatabaseInformation(org.hibernate.tool.schema.extract.spi.DatabaseInformation) ExtractionContext(org.hibernate.tool.schema.extract.spi.ExtractionContext) Database(org.hibernate.boot.model.relational.Database) DatabaseInformationImpl(org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl) ExtractionContextImpl(org.hibernate.tool.schema.extract.internal.ExtractionContextImpl) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

Aggregations

Database (org.hibernate.boot.model.relational.Database)18 Test (org.junit.Test)9 Properties (java.util.Properties)7 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)7 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)7 Identifier (org.hibernate.boot.model.naming.Identifier)6 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)4 Table (org.hibernate.mapping.Table)4 HashSet (java.util.HashSet)3 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)3 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)3 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)3 Namespace (org.hibernate.boot.model.relational.Namespace)3 Sequence (org.hibernate.boot.model.relational.Sequence)3 Map (java.util.Map)2 AnnotationException (org.hibernate.AnnotationException)2 MetadataSources (org.hibernate.boot.MetadataSources)2 ImplicitBasicColumnNameSource (org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource)2 AttributePath (org.hibernate.boot.model.source.spi.AttributePath)2 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)2