Search in sources :

Example 1 with Formatter

use of org.hibernate.engine.jdbc.internal.Formatter in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method performCreation.

private void performCreation(Metadata metadata, Dialect dialect, ExecutionOptions options, SourceDescriptor sourceDescriptor, GenerationTarget... targets) {
    final ImportSqlCommandExtractor commandExtractor = tool.getServiceRegistry().getService(ImportSqlCommandExtractor.class);
    final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
    final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
    switch(sourceDescriptor.getSourceType()) {
        case SCRIPT:
            {
                createFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
                break;
            }
        case METADATA:
            {
                createFromMetadata(metadata, options, dialect, formatter, targets);
                break;
            }
        case METADATA_THEN_SCRIPT:
            {
                createFromMetadata(metadata, options, dialect, formatter, targets);
                createFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
                break;
            }
        case SCRIPT_THEN_METADATA:
            {
                createFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
                createFromMetadata(metadata, options, dialect, formatter, targets);
            }
    }
    applyImportSources(options, commandExtractor, format, targets);
}
Also used : Formatter(org.hibernate.engine.jdbc.internal.Formatter) ImportSqlCommandExtractor(org.hibernate.tool.hbm2ddl.ImportSqlCommandExtractor)

Example 2 with Formatter

use of org.hibernate.engine.jdbc.internal.Formatter 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 3 with Formatter

use of org.hibernate.engine.jdbc.internal.Formatter in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method applyImportSources.

private void applyImportSources(ExecutionOptions options, ImportSqlCommandExtractor commandExtractor, boolean format, GenerationTarget... targets) {
    final ServiceRegistry serviceRegistry = tool.getServiceRegistry();
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    // I have had problems applying the formatter to these imported statements.
    // and legacy SchemaExport did not format them, so doing same here
    //final Formatter formatter = format ? DDLFormatterImpl.INSTANCE : FormatStyle.NONE.getFormatter();
    final Formatter formatter = FormatStyle.NONE.getFormatter();
    final Object importScriptSetting = options.getConfigurationValues().get(HBM2DDL_LOAD_SCRIPT_SOURCE);
    String charsetName = (String) options.getConfigurationValues().get(HBM2DDL_CHARSET_NAME);
    if (importScriptSetting != null) {
        final ScriptSourceInput importScriptInput = interpretScriptSourceSetting(importScriptSetting, classLoaderService, charsetName);
        log.executingImportScript(importScriptInput.toString());
        importScriptInput.prepare();
        try {
            for (String command : importScriptInput.read(commandExtractor)) {
                applySqlString(command, formatter, options, targets);
            }
        } finally {
            importScriptInput.release();
        }
    }
    final String importFiles = ConfigurationHelper.getString(AvailableSettings.HBM2DDL_IMPORT_FILES, options.getConfigurationValues(), DEFAULT_IMPORT_FILE);
    for (String currentFile : importFiles.split(",")) {
        final String resourceName = currentFile.trim();
        final ScriptSourceInput importScriptInput = interpretLegacyImportScriptSetting(resourceName, classLoaderService, charsetName);
        importScriptInput.prepare();
        try {
            log.executingImportScript(importScriptInput.toString());
            for (String command : importScriptInput.read(commandExtractor)) {
                applySqlString(command, formatter, options, targets);
            }
        } finally {
            importScriptInput.release();
        }
    }
}
Also used : ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) Formatter(org.hibernate.engine.jdbc.internal.Formatter) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) ServiceRegistry(org.hibernate.service.ServiceRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 4 with Formatter

use of org.hibernate.engine.jdbc.internal.Formatter in project hibernate-orm by hibernate.

the class SchemaDropperImpl method performDrop.

private void performDrop(Metadata metadata, ExecutionOptions options, Dialect dialect, SourceDescriptor sourceDescriptor, GenerationTarget... targets) {
    final ImportSqlCommandExtractor commandExtractor = tool.getServiceRegistry().getService(ImportSqlCommandExtractor.class);
    final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
    final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
    if (sourceDescriptor.getSourceType() == SourceType.SCRIPT) {
        dropFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
    } else if (sourceDescriptor.getSourceType() == SourceType.METADATA) {
        dropFromMetadata(metadata, options, dialect, formatter, targets);
    } else if (sourceDescriptor.getSourceType() == SourceType.METADATA_THEN_SCRIPT) {
        dropFromMetadata(metadata, options, dialect, formatter, targets);
        dropFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
    } else {
        dropFromScript(sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets);
        dropFromMetadata(metadata, options, dialect, formatter, targets);
    }
}
Also used : Formatter(org.hibernate.engine.jdbc.internal.Formatter) ImportSqlCommandExtractor(org.hibernate.tool.hbm2ddl.ImportSqlCommandExtractor)

Aggregations

Formatter (org.hibernate.engine.jdbc.internal.Formatter)4 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)2 ImportSqlCommandExtractor (org.hibernate.tool.hbm2ddl.ImportSqlCommandExtractor)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 Database (org.hibernate.boot.model.relational.Database)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 Sequence (org.hibernate.boot.model.relational.Sequence)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 Table (org.hibernate.mapping.Table)1 ServiceRegistry (org.hibernate.service.ServiceRegistry)1 NameSpaceTablesInformation (org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation)1 SequenceInformation (org.hibernate.tool.schema.extract.spi.SequenceInformation)1 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)1 ScriptSourceInput (org.hibernate.tool.schema.spi.ScriptSourceInput)1