Search in sources :

Example 6 with QualifiedTableName

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method extractKeyTableName.

private QualifiedTableName extractKeyTableName(ResultSet resultSet, String prefix) throws SQLException {
    final String incomingCatalogName = resultSet.getString(prefix + "TABLE_CAT");
    final String incomingSchemaName = resultSet.getString(prefix + "TABLE_SCHEM");
    final String incomingTableName = resultSet.getString(prefix + "TABLE_NAME");
    final DatabaseIdentifier catalog = DatabaseIdentifier.toIdentifier(incomingCatalogName);
    final DatabaseIdentifier schema = DatabaseIdentifier.toIdentifier(incomingSchemaName);
    final DatabaseIdentifier table = DatabaseIdentifier.toIdentifier(incomingTableName);
    return new QualifiedTableName(catalog, schema, table);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier)

Example 7 with QualifiedTableName

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method addColumns.

private void addColumns(TableInformation tableInformation) {
    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().getColumns(catalogFilter, schemaFilter, tableName.getTableName().getText(), "%");
        try {
            while (resultSet.next()) {
                final String columnName = resultSet.getString("COLUMN_NAME");
                final ColumnInformationImpl columnInformation = new ColumnInformationImpl(tableInformation, DatabaseIdentifier.toIdentifier(columnName), resultSet.getInt("DATA_TYPE"), new StringTokenizer(resultSet.getString("TYPE_NAME"), "() ").nextToken(), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), interpretTruthValue(resultSet.getString("IS_NULLABLE")));
                tableInformation.addColumn(columnInformation);
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing column metadata: " + tableName.toString());
    }
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) StringTokenizer(java.util.StringTokenizer) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 8 with QualifiedTableName

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

the class InformationExtractorJdbcDatabaseMetaDataImpl method getPrimaryKey.

@Override
public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation) {
    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().getPrimaryKeys(catalogFilter, schemaFilter, tableInformation.getName().getTableName().getText());
        final List<ColumnInformation> pkColumns = new ArrayList<ColumnInformation>();
        boolean firstPass = true;
        Identifier pkIdentifier = null;
        try {
            while (resultSet.next()) {
                final String currentPkName = resultSet.getString("PK_NAME");
                final Identifier currentPkIdentifier = currentPkName == null ? null : DatabaseIdentifier.toIdentifier(currentPkName);
                if (firstPass) {
                    pkIdentifier = currentPkIdentifier;
                    firstPass = false;
                } else {
                    if (!EqualsHelper.equals(pkIdentifier, currentPkIdentifier)) {
                        throw new SchemaExtractionException(String.format("Encountered primary keys differing name on table %s", tableInformation.getName().toString()));
                    }
                }
                final int columnPosition = resultSet.getInt("KEY_SEQ");
                final Identifier columnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME"));
                final ColumnInformation column = tableInformation.getColumn(columnIdentifier);
                pkColumns.add(columnPosition - 1, column);
            }
        } finally {
            resultSet.close();
        }
        if (firstPass) {
            // we did not find any results (no pk)
            return null;
        } else {
            // validate column list is properly contiguous
            for (int i = 0; i < pkColumns.size(); i++) {
                if (pkColumns.get(i) == null) {
                    throw new SchemaExtractionException("Primary Key information was missing for KEY_SEQ = " + (i + 1));
                }
            }
            // build the return
            return new PrimaryKeyInformationImpl(pkIdentifier, pkColumns);
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error while reading primary key meta data for " + tableInformation.getName().toString());
    }
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) SQLException(java.sql.SQLException) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) SchemaExtractionException(org.hibernate.tool.schema.extract.spi.SchemaExtractionException)

Aggregations

QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)8 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)6 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Identifier (org.hibernate.boot.model.naming.Identifier)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 ColumnInformation (org.hibernate.tool.schema.extract.spi.ColumnInformation)2 EnumSet (java.util.EnumSet)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 Database (org.hibernate.boot.model.relational.Database)1 TableStructure (org.hibernate.id.enhanced.TableStructure)1 Table (org.hibernate.mapping.Table)1 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)1 ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)1 IndexInformation (org.hibernate.tool.schema.extract.spi.IndexInformation)1 SchemaExtractionException (org.hibernate.tool.schema.extract.spi.SchemaExtractionException)1