Search in sources :

Example 1 with SchemaExtractionException

use of org.hibernate.tool.schema.extract.spi.SchemaExtractionException 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 2 with SchemaExtractionException

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

SQLException (java.sql.SQLException)2 SchemaExtractionException (org.hibernate.tool.schema.extract.spi.SchemaExtractionException)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 ColumnInformation (org.hibernate.tool.schema.extract.spi.ColumnInformation)1 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)1