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) {
}
}
}
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());
}
}
Aggregations