Search in sources :

Example 1 with TableInformation

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method populateTablesWithColumns.

private void populateTablesWithColumns(String catalogFilter, String schemaFilter, NameSpaceTablesInformation tables) {
    try {
        ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getColumns(catalogFilter, schemaFilter, null, "%");
        try {
            String currentTableName = "";
            TableInformation currentTable = null;
            while (resultSet.next()) {
                if (!currentTableName.equals(resultSet.getString("TABLE_NAME"))) {
                    currentTableName = resultSet.getString("TABLE_NAME");
                    currentTable = tables.getTableInformation(currentTableName);
                }
                if (currentTable != null) {
                    final ColumnInformationImpl columnInformation = new ColumnInformationImpl(currentTable, DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME")), resultSet.getInt("DATA_TYPE"), new StringTokenizer(resultSet.getString("TYPE_NAME"), "() ").nextToken(), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), interpretTruthValue(resultSet.getString("IS_NULLABLE")));
                    currentTable.addColumn(columnInformation);
                }
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing tables metadata");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 2 with TableInformation

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method processTableResults.

private TableInformation processTableResults(Identifier catalog, Identifier schema, Identifier tableName, ResultSet resultSet) throws SQLException {
    try {
        boolean found = false;
        TableInformation tableInformation = null;
        while (resultSet.next()) {
            if (tableName.equals(Identifier.toIdentifier(resultSet.getString("TABLE_NAME"), tableName.isQuoted()))) {
                if (found) {
                    log.multipleTablesFound(tableName.render());
                    final String catalogName = catalog == null ? "" : catalog.render();
                    final String schemaName = schema == null ? "" : schema.render();
                    throw new SchemaExtractionException(String.format(Locale.ENGLISH, "More than one table found in namespace (%s, %s) : %s", catalogName, schemaName, tableName.render()));
                } else {
                    found = true;
                    tableInformation = extractTableInformation(resultSet);
                    addColumns(tableInformation);
                }
            }
        }
        if (!found) {
            log.tableNotFound(tableName.render());
        }
        return tableInformation;
    } finally {
        try {
            resultSet.close();
        } catch (SQLException ignore) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) SchemaExtractionException(org.hibernate.tool.schema.extract.spi.SchemaExtractionException)

Example 3 with TableInformation

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method getForeignKeys.

@Override
public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInformation) {
    final Map<Identifier, ForeignKeyBuilder> fkBuilders = new HashMap<>();
    final QualifiedTableName tableName = tableInformation.getName();
    final Identifier catalog = tableName.getCatalogName();
    final Identifier schema = tableName.getSchemaName();
    final String catalogFilter;
    final String schemaFilter;
    if (catalog == null) {
        catalogFilter = "";
    } else {
        catalogFilter = catalog.getText();
    }
    if (schema == null) {
        schemaFilter = "";
    } else {
        schemaFilter = schema.getText();
    }
    try {
        ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getImportedKeys(catalogFilter, schemaFilter, tableInformation.getName().getTableName().getText());
        try {
            while (resultSet.next()) {
                // IMPL NOTE : The builder is mainly used to collect the column reference mappings
                final Identifier fkIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FK_NAME"));
                ForeignKeyBuilder fkBuilder = fkBuilders.get(fkIdentifier);
                if (fkBuilder == null) {
                    fkBuilder = generateForeignKeyBuilder(fkIdentifier);
                    fkBuilders.put(fkIdentifier, fkBuilder);
                }
                final QualifiedTableName incomingPkTableName = extractKeyTableName(resultSet, "PK");
                final TableInformation pkTableInformation = extractionContext.getDatabaseObjectAccess().locateTableInformation(incomingPkTableName);
                if (pkTableInformation == null) {
                    // should match.
                    continue;
                }
                final Identifier fkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FKCOLUMN_NAME"));
                final Identifier pkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("PKCOLUMN_NAME"));
                fkBuilder.addColumnMapping(tableInformation.getColumn(fkColumnIdentifier), pkTableInformation.getColumn(pkColumnIdentifier));
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing column metadata: " + tableInformation.getName().toString());
    }
    final List<ForeignKeyInformation> fks = new ArrayList<ForeignKeyInformation>();
    for (ForeignKeyBuilder fkBuilder : fkBuilders.values()) {
        ForeignKeyInformation fk = fkBuilder.build();
        fks.add(fk);
    }
    return fks;
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 4 with TableInformation

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

the class IndividuallySchemaValidatorImpl method validateTables.

@Override
protected void validateTables(Metadata metadata, DatabaseInformation databaseInformation, ExecutionOptions options, Dialect dialect, Namespace namespace) {
    for (Table table : namespace.getTables()) {
        if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
            final TableInformation tableInformation = databaseInformation.getTableInformation(table.getQualifiedTableName());
            validateTable(table, tableInformation, metadata, options, dialect);
        }
    }
}
Also used : Table(org.hibernate.mapping.Table) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 5 with TableInformation

use of org.hibernate.tool.schema.extract.spi.TableInformation 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)

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