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