use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method getIndexes.
@Override
public Iterable<IndexInformation> getIndexes(TableInformation tableInformation) {
final Map<Identifier, IndexInformationImpl.Builder> builders = 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().getIndexInfo(catalogFilter, schemaFilter, tableName.getTableName().getText(), // DO NOT limit to just unique
false, // DO require up-to-date results
true);
try {
while (resultSet.next()) {
if (resultSet.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic) {
continue;
}
final Identifier indexIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("INDEX_NAME"));
IndexInformationImpl.Builder builder = builders.get(indexIdentifier);
if (builder == null) {
builder = IndexInformationImpl.builder(indexIdentifier);
builders.put(indexIdentifier, builder);
}
final Identifier columnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME"));
final ColumnInformation columnInformation = tableInformation.getColumn(columnIdentifier);
if (columnInformation == null) {
// See HHH-10191: this may happen when dealing with Oracle/PostgreSQL function indexes
log.logCannotLocateIndexColumnInformation(columnIdentifier.getText(), indexIdentifier.getText());
}
builder.addColumn(columnInformation);
}
} finally {
resultSet.close();
}
} catch (SQLException e) {
throw convertSQLException(e, "Error accessing index information: " + tableInformation.getName().toString());
}
final List<IndexInformation> indexes = new ArrayList<IndexInformation>();
for (IndexInformationImpl.Builder builder : builders.values()) {
IndexInformationImpl index = builder.build();
indexes.add(index);
}
return indexes;
}
use of org.hibernate.boot.model.naming.Identifier 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.naming.Identifier 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());
}
}
use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.
the class TableInformationImpl method foreignKeys.
protected Map<Identifier, ForeignKeyInformation> foreignKeys() {
if (foreignKeys == null) {
final Map<Identifier, ForeignKeyInformation> fkMap = new HashMap<>();
final Iterable<ForeignKeyInformation> fks = extractor.getForeignKeys(this);
for (ForeignKeyInformation fk : fks) {
fkMap.put(fk.getForeignKeyIdentifier(), fk);
}
this.foreignKeys = fkMap;
}
return foreignKeys;
}
use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.
the class SchemaDropperImpl method dropFromMetadata.
private void dropFromMetadata(Metadata metadata, ExecutionOptions options, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
final Database database = metadata.getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
boolean tryToDropCatalogs = false;
boolean tryToDropSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToDropSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToDropCatalogs = true;
}
}
final Set<String> exportIdentifiers = new HashSet<String>(50);
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
continue;
}
if (!auxiliaryDatabaseObject.appliesToDialect(dialect)) {
continue;
}
applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
// we need to drop all constraints/indexes prior to dropping the tables
applyConstraintDropping(namespace, metadata, formatter, options, targets);
// now it's safe to drop the tables
for (Table table : namespace.getTables()) {
if (!table.isPhysicalTable()) {
continue;
}
if (!schemaFilter.includeTable(table)) {
continue;
}
checkExportIdentifier(table, exportIdentifiers);
applySqlStrings(dialect.getTableExporter().getSqlDropStrings(table, metadata), formatter, options, targets);
}
for (Sequence sequence : namespace.getSequences()) {
if (!schemaFilter.includeSequence(sequence)) {
continue;
}
checkExportIdentifier(sequence, exportIdentifiers);
applySqlStrings(dialect.getSequenceExporter().getSqlDropStrings(sequence, metadata), formatter, options, targets);
}
}
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.beforeTablesOnCreation()) {
continue;
}
if (!auxiliaryDatabaseObject.appliesToDialect(dialect)) {
continue;
}
applySqlStrings(auxiliaryDatabaseObject.sqlDropStrings(jdbcEnvironment.getDialect()), formatter, options, targets);
}
if (tryToDropCatalogs || tryToDropSchemas) {
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
if (tryToDropSchemas && namespace.getPhysicalName().getSchema() != null) {
applySqlStrings(dialect.getDropSchemaCommand(namespace.getPhysicalName().getSchema().render(dialect)), formatter, options, targets);
}
if (tryToDropCatalogs) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
applySqlStrings(dialect.getDropCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
exportedCatalogs.add(catalogLogicalName);
}
}
}
}
}
Aggregations