Search in sources :

Example 6 with TableInformation

use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.

the class GroupedSchemaMigratorImpl method performTablesMigration.

@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
    if (schemaFilter.includeNamespace(namespace)) {
        createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
        final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation(namespace);
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                checkExportIdentifier(table, exportIdentifiers);
                final TableInformation tableInformation = tables.getTableInformation(table);
                if (tableInformation == null) {
                    createTable(table, dialect, metadata, formatter, options, targets);
                } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
                    tablesInformation.addTableInformation(tableInformation);
                    migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                final TableInformation tableInformation = tablesInformation.getTableInformation(table);
                if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
                    applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
                    applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
    }
    return tablesInformation;
}
Also used : Table(org.hibernate.mapping.Table) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 7 with TableInformation

use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method getTable.

@Override
public TableInformation getTable(Identifier catalog, Identifier schema, Identifier tableName) {
    if (catalog != null || schema != null) {
        return locateTableInNamespace(catalog, schema, tableName);
    } else {
        // The table did not define an explicit namespace:
        // 		1) look in current namespace
        //		2) look in default namespace
        //		3) look in all namespaces - multiple hits is considered an error
        TableInformation tableInfo = null;
        // 1) look in current namespace
        if (extractionContext.getJdbcEnvironment().getCurrentCatalog() != null || extractionContext.getJdbcEnvironment().getCurrentSchema() != null) {
            tableInfo = locateTableInNamespace(extractionContext.getJdbcEnvironment().getCurrentCatalog(), extractionContext.getJdbcEnvironment().getCurrentSchema(), tableName);
            if (tableInfo != null) {
                return tableInfo;
            }
        }
        // 2) look in default namespace
        if (extractionContext.getDefaultCatalog() != null || extractionContext.getDefaultSchema() != null) {
            tableInfo = locateTableInNamespace(extractionContext.getDefaultCatalog(), extractionContext.getDefaultSchema(), tableName);
            if (tableInfo != null) {
                return tableInfo;
            }
        }
        // 3) look in all namespaces
        try {
            final String tableNameFilter = toMetaDataObjectName(tableName);
            final ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getTables(null, null, tableNameFilter, tableTypes);
            try {
                return processTableResults(null, null, tableName, resultSet);
            } finally {
                try {
                    resultSet.close();
                } catch (SQLException ignore) {
                }
            }
        } catch (SQLException sqlException) {
            throw convertSQLException(sqlException, "Error accessing table metadata");
        }
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 8 with TableInformation

use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method processTableResults.

private NameSpaceTablesInformation processTableResults(ResultSet resultSet) throws SQLException {
    try {
        NameSpaceTablesInformation tables = new NameSpaceTablesInformation(identifierHelper());
        while (resultSet.next()) {
            final TableInformation tableInformation = extractTableInformation(resultSet);
            tables.addTableInformation(tableInformation);
        }
        return tables;
    } finally {
        try {
            resultSet.close();
        } catch (SQLException ignore) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 9 with TableInformation

use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.

the class IndividuallySchemaMigratorImpl method performTablesMigration.

@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
    if (schemaFilter.includeNamespace(namespace)) {
        createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                checkExportIdentifier(table, exportIdentifiers);
                final TableInformation tableInformation = existingDatabase.getTableInformation(table.getQualifiedTableName());
                if (tableInformation == null) {
                    createTable(table, dialect, metadata, formatter, options, targets);
                } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
                    tablesInformation.addTableInformation(tableInformation);
                    migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
        for (Table table : namespace.getTables()) {
            if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
                final TableInformation tableInformation = tablesInformation.getTableInformation(table);
                if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
                    applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
                    applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
                }
            }
        }
    }
    return tablesInformation;
}
Also used : Table(org.hibernate.mapping.Table) NameSpaceTablesInformation(org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Aggregations

TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)9 SQLException (java.sql.SQLException)5 Table (org.hibernate.mapping.Table)4 NameSpaceTablesInformation (org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)1 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 Database (org.hibernate.boot.model.relational.Database)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 Sequence (org.hibernate.boot.model.relational.Sequence)1 Formatter (org.hibernate.engine.jdbc.internal.Formatter)1 ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)1 SchemaExtractionException (org.hibernate.tool.schema.extract.spi.SchemaExtractionException)1 SequenceInformation (org.hibernate.tool.schema.extract.spi.SequenceInformation)1