Search in sources :

Example 11 with SqlStringGenerationContext

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

the class HibernateSequenceTest method testHibernateSequenceSchema.

@Test
public void testHibernateSequenceSchema(SessionFactoryScope scope) {
    EntityPersister persister = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(HibernateSequenceEntity.class.getName());
    IdentifierGenerator generator = persister.getIdentifierGenerator();
    assertTrue(SequenceStyleGenerator.class.isInstance(generator));
    SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
    SqlStringGenerationContext sqlStringGenerationContext = scope.getSessionFactory().getSqlStringGenerationContext();
    assertEquals(Table.qualify(null, SCHEMA_NAME, "HibernateSequenceEntity_SEQ"), sqlStringGenerationContext.format(seqGenerator.getDatabaseStructure().getPhysicalName()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) HibernateSequenceEntity(org.hibernate.orm.test.annotations.id.sequences.entities.HibernateSequenceEntity) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.jupiter.api.Test)

Example 12 with SqlStringGenerationContext

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

the class TestExtraPhysicalTableTypes method buildInformationExtractorJdbcDatabaseMetaDataImplTest.

private InformationExtractorJdbcDatabaseMetaDataImplTest buildInformationExtractorJdbcDatabaseMetaDataImplTest(DdlTransactionIsolator ddlTransactionIsolator) throws SQLException {
    Database database = metadata.getDatabase();
    SqlStringGenerationContext sqlStringGenerationContext = SqlStringGenerationContextImpl.forTests(database.getJdbcEnvironment());
    DatabaseInformation dbInfo = new DatabaseInformationImpl(ssr, database.getJdbcEnvironment(), sqlStringGenerationContext, ddlTransactionIsolator, database.getServiceRegistry().getService(SchemaManagementTool.class));
    ExtractionContextImpl extractionContext = new ExtractionContextImpl(ssr, database.getJdbcEnvironment(), sqlStringGenerationContext, ssr.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess(), (ExtractionContext.DatabaseObjectAccess) dbInfo);
    return new InformationExtractorJdbcDatabaseMetaDataImplTest(extractionContext);
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) DatabaseInformation(org.hibernate.tool.schema.extract.spi.DatabaseInformation) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) 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)

Example 13 with SqlStringGenerationContext

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

the class AbstractSchemaMigratorTest method testForeignKeyPreExistenceDetectionIgnoresCaseForTableAndColumnName.

@Test
@TestForIssue(jiraKey = "HHH-13779")
public void testForeignKeyPreExistenceDetectionIgnoresCaseForTableAndColumnName() {
    final AbstractSchemaMigrator schemaMigrator = new AbstractSchemaMigrator(null, null) {

        @Override
        protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, ContributableMatcher contributableInclusionFilter, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, SqlStringGenerationContext sqlStringGenerationContext, GenerationTarget[] targets) {
            return null;
        }
    };
    final TableInformation existingTableInformation = mock(TableInformation.class);
    final ArrayList<ForeignKeyInformation.ColumnReferenceMapping> columnReferenceMappings = new ArrayList<>();
    final TableInformation destinationTableInformation = mock(TableInformation.class);
    doReturn(new QualifiedTableName(toIdentifier("catalog"), toIdentifier("schema"), // Table name is lower case
    toIdentifier("referenced_table"))).when(destinationTableInformation).getName();
    columnReferenceMappings.add(new ForeignKeyInformationImpl.ColumnReferenceMappingImpl(new // column name is lower case
    ColumnInformationImpl(// column name is lower case
    null, // column name is lower case
    toIdentifier("referencing_column"), 0, "typeName", 255, 0, TruthValue.TRUE), new ColumnInformationImpl(destinationTableInformation, null, 1, "typeName", 0, 0, TruthValue.TRUE)));
    doReturn(singletonList(new ForeignKeyInformationImpl(toIdentifier("FKp8mpamfw2inhj88hwhty1eipm"), columnReferenceMappings))).when(existingTableInformation).getForeignKeys();
    final boolean existInDatabase = schemaMigrator.equivalentForeignKeyExistsInDatabase(existingTableInformation, "REFERENCING_COLUMN", // Table and column names are UPPER-case here, to prove the test
    "REFERENCED_TABLE");
    assertThat("Expected ForeignKey pre-existence check to be case-insensitive", existInDatabase, is(true));
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Set(java.util.Set) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) Formatter(org.hibernate.engine.jdbc.internal.Formatter) Metadata(org.hibernate.boot.Metadata) ArrayList(java.util.ArrayList) Namespace(org.hibernate.boot.model.relational.Namespace) ColumnInformationImpl(org.hibernate.tool.schema.extract.internal.ColumnInformationImpl) ContributableMatcher(org.hibernate.tool.schema.spi.ContributableMatcher) DatabaseInformation(org.hibernate.tool.schema.extract.spi.DatabaseInformation) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) Dialect(org.hibernate.dialect.Dialect) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with SqlStringGenerationContext

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

the class SchemaUpdateTableBackedSequenceTest method testCreateTableOnUpdate.

@Test
public void testCreateTableOnUpdate() throws SQLException {
    Metadata metadata = new MetadataSources(ssr).buildMetadata();
    Database database = metadata.getDatabase();
    TableStructure tableStructure = new TableStructure(database.getJdbcEnvironment(), "orm", new QualifiedTableName(null, null, Identifier.toIdentifier("test_seq")), Identifier.toIdentifier("nextval"), 20, 30, Long.class);
    tableStructure.registerExportables(database);
    // lets make sure the InitCommand is there
    assertEquals(1, database.getDefaultNamespace().getTables().size());
    Table table = database.getDefaultNamespace().getTables().iterator().next();
    SqlStringGenerationContext context = SqlStringGenerationContextImpl.forTests(database.getJdbcEnvironment(), null, null);
    assertEquals(1, table.getInitCommands(context).size());
    final TargetImpl target = new TargetImpl();
    ssr.getService(SchemaManagementTool.class).getSchemaMigrator(Collections.emptyMap()).doMigration(metadata, new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return ssr.getService(ConfigurationService.class).getSettings();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }

        @Override
        public SchemaFilter getSchemaFilter() {
            return SchemaFilter.ALL;
        }
    }, ContributableMatcher.ALL, new TargetDescriptor() {

        @Override
        public EnumSet<TargetType> getTargetTypes() {
            return EnumSet.of(TargetType.SCRIPT, TargetType.DATABASE);
        }

        @Override
        public ScriptTargetOutput getScriptTargetOutput() {
            return target;
        }
    });
    assertTrue(target.found);
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) EnumSet(java.util.EnumSet) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) SchemaFilter(org.hibernate.tool.schema.spi.SchemaFilter) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) TableStructure(org.hibernate.id.enhanced.TableStructure) Database(org.hibernate.boot.model.relational.Database) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) Map(java.util.Map) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Aggregations

SqlStringGenerationContext (org.hibernate.boot.model.relational.SqlStringGenerationContext)14 Database (org.hibernate.boot.model.relational.Database)7 Table (org.hibernate.mapping.Table)5 Metadata (org.hibernate.boot.Metadata)3 MetadataSources (org.hibernate.boot.MetadataSources)3 Identifier (org.hibernate.boot.model.naming.Identifier)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 Dialect (org.hibernate.dialect.Dialect)3 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)3 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 DatabaseInformation (org.hibernate.tool.schema.extract.spi.DatabaseInformation)3 HashSet (java.util.HashSet)2 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)2 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)2 TestForIssue (org.hibernate.testing.TestForIssue)2