Search in sources :

Example 11 with Database

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

the class SequenceStyleConfigUnitTest method testDefaultedTableBackedConfiguration.

/**
	 * Test all params defaulted with a dialect which does not support sequences
	 */
@Test
public void testDefaultedTableBackedConfiguration() {
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, TableDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 12 with Database

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

the class SequenceStyleConfigUnitTest method testForceTableUse.

/**
	 * Test forcing of table as backing structure with dialect supporting sequences
	 */
@Test
public void testForceTableUse() {
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, SequenceDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.FORCE_TBL_PARAM, "true");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 13 with Database

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

the class SequenceStyleConfigUnitTest method testExplicitOptimizerWithExplicitIncrementSize.

/**
	 * Test explicitly specifying both optimizer and increment
	 */
@Test
public void testExplicitOptimizerWithExplicitIncrementSize() {
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, SequenceDialect.class.getName()).build();
    // optimizer=none w/ increment > 1 => should honor optimizer
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.NONE.getExternalName());
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(1, generator.getOptimizer().getIncrementSize());
        assertEquals(1, generator.getDatabaseStructure().getIncrementSize());
        // optimizer=hilo w/ increment > 1 => hilo
        props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.HILO.getExternalName());
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
        generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(HiLoOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(20, generator.getOptimizer().getIncrementSize());
        assertEquals(20, generator.getDatabaseStructure().getIncrementSize());
        // optimizer=pooled w/ increment > 1 => hilo
        props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
        generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        // because the dialect reports to not support pooled seqyences, the expectation is that we will
        // use a table for the backing structure...
        assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(20, generator.getOptimizer().getIncrementSize());
        assertEquals(20, generator.getDatabaseStructure().getIncrementSize());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 14 with Database

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

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

the class AbstractSchemaMigrator method migrateTable.

protected void migrateTable(Table table, TableInformation tableInformation, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
    final Database database = metadata.getDatabase();
    //noinspection unchecked
    applySqlStrings(false, table.sqlAlterStrings(dialect, metadata, tableInformation, getDefaultCatalogName(database, dialect), getDefaultSchemaName(database, dialect)), formatter, options, targets);
}
Also used : Database(org.hibernate.boot.model.relational.Database)

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