use of org.hibernate.boot.model.relational.Database 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);
}
}
}
}
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class Ejb3Column method buildColumnFromAnnotation.
public static Ejb3Column[] buildColumnFromAnnotation(javax.persistence.Column[] anns, org.hibernate.annotations.Formula formulaAnn, Nullability nullability, PropertyHolder propertyHolder, PropertyData inferredData, String suffixForDefaultColumnName, Map<String, Join> secondaryTables, MetadataBuildingContext context) {
Ejb3Column[] columns;
if (formulaAnn != null) {
Ejb3Column formulaColumn = new Ejb3Column();
formulaColumn.setFormula(formulaAnn.value());
formulaColumn.setImplicit(false);
formulaColumn.setBuildingContext(context);
formulaColumn.setPropertyHolder(propertyHolder);
formulaColumn.bind();
columns = new Ejb3Column[] { formulaColumn };
} else {
javax.persistence.Column[] actualCols = anns;
javax.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn(StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
if (overriddenCols != null) {
//check for overridden first
if (anns != null && overriddenCols.length != anns.length) {
throw new AnnotationException("AttributeOverride.column() should override all columns for now");
}
actualCols = overriddenCols.length == 0 ? null : overriddenCols;
LOG.debugf("Column(s) overridden for property %s", inferredData.getPropertyName());
}
if (actualCols == null) {
columns = buildImplicitColumn(inferredData, suffixForDefaultColumnName, secondaryTables, propertyHolder, nullability, context);
} else {
final int length = actualCols.length;
columns = new Ejb3Column[length];
for (int index = 0; index < length; index++) {
final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
final Database database = context.getMetadataCollector().getDatabase();
final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
javax.persistence.Column col = actualCols[index];
final String sqlType;
if (col.columnDefinition().equals("")) {
sqlType = null;
} else {
sqlType = normalizer.applyGlobalQuoting(col.columnDefinition());
}
final String tableName;
if (StringHelper.isEmpty(col.table())) {
tableName = "";
} else {
tableName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.table()).render();
// final Identifier logicalName = database.getJdbcEnvironment()
// .getIdentifierHelper()
// .toIdentifier( col.table() );
// final Identifier physicalName = physicalNamingStrategy.toPhysicalTableName( logicalName );
// tableName = physicalName.render( database.getDialect() );
}
final String columnName;
if ("".equals(col.name())) {
columnName = null;
} else {
// NOTE : this is the logical column name, not the physical!
columnName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.name()).render();
}
Ejb3Column column = new Ejb3Column();
if (length == 1) {
applyColumnDefault(column, inferredData);
}
column.setImplicit(false);
column.setSqlType(sqlType);
column.setLength(col.length());
column.setPrecision(col.precision());
column.setScale(col.scale());
if (StringHelper.isEmpty(columnName) && !StringHelper.isEmpty(suffixForDefaultColumnName)) {
column.setLogicalColumnName(inferredData.getPropertyName() + suffixForDefaultColumnName);
} else {
column.setLogicalColumnName(columnName);
}
column.setPropertyName(BinderHelper.getRelativePath(propertyHolder, inferredData.getPropertyName()));
column.setNullable(col.nullable());
//TODO force to not null if available? This is a (bad) user choice.
column.setUnique(col.unique());
column.setInsertable(col.insertable());
column.setUpdatable(col.updatable());
column.setExplicitTableName(tableName);
column.setPropertyHolder(propertyHolder);
column.setJoins(secondaryTables);
column.setBuildingContext(context);
column.extractDataFromPropertyData(inferredData);
column.bind();
columns[index] = column;
}
}
}
return columns;
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class TestExtraPhysicalTableTypes method buildInformationExtractorJdbcDatabaseMetaDataImplTest.
private InformationExtractorJdbcDatabaseMetaDataImplTest buildInformationExtractorJdbcDatabaseMetaDataImplTest() throws SQLException {
Database database = metadata.getDatabase();
final ConnectionProvider connectionProvider = ssr.getService(ConnectionProvider.class);
DatabaseInformation dbInfo = new DatabaseInformationImpl(ssr, database.getJdbcEnvironment(), new DdlTransactionIsolatorTestingImpl(ssr, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider)), database.getDefaultNamespace().getName());
ExtractionContextImpl extractionContext = new ExtractionContextImpl(ssr, database.getJdbcEnvironment(), ssr.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess(), (ExtractionContext.DatabaseObjectAccess) dbInfo, database.getDefaultNamespace().getPhysicalName().getCatalog(), database.getDefaultNamespace().getPhysicalName().getSchema());
return new InformationExtractorJdbcDatabaseMetaDataImplTest(extractionContext);
}
Aggregations