use of org.hibernate.boot.model.relational.AuxiliaryDatabaseObject in project hibernate-orm by hibernate.
the class StoredProcedureTest method configure.
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.addAuxiliaryDatabaseObject(new AuxiliaryDatabaseObject() {
@Override
public String getExportIdentifier() {
return "function:findOneUser";
}
@Override
public boolean appliesToDialect(Dialect dialect) {
return H2Dialect.class.isInstance(dialect);
}
@Override
public boolean beforeTablesOnCreation() {
return false;
}
@Override
public String[] sqlCreateStrings(Dialect dialect) {
return new String[] { "CREATE ALIAS findOneUser AS $$\n" + "import org.h2.tools.SimpleResultSet;\n" + "import java.sql.*;\n" + "@CODE\n" + "ResultSet findOneUser() {\n" + " SimpleResultSet rs = new SimpleResultSet();\n" + " rs.addColumn(\"ID\", Types.INTEGER, 10, 0);\n" + " rs.addColumn(\"NAME\", Types.VARCHAR, 255, 0);\n" + " rs.addRow(1, \"Steve\");\n" + " return rs;\n" + "}\n" + "$$" };
}
@Override
public String[] sqlDropStrings(Dialect dialect) {
return new String[] { "DROP ALIAS findUser IF EXISTS" };
}
});
configuration.addAuxiliaryDatabaseObject(new AuxiliaryDatabaseObject() {
@Override
public String getExportIdentifier() {
return "function:findUsers";
}
@Override
public boolean appliesToDialect(Dialect dialect) {
return H2Dialect.class.isInstance(dialect);
}
@Override
public boolean beforeTablesOnCreation() {
return false;
}
@Override
public String[] sqlCreateStrings(Dialect dialect) {
return new String[] { "CREATE ALIAS findUsers AS $$\n" + "import org.h2.tools.SimpleResultSet;\n" + "import java.sql.*;\n" + "@CODE\n" + "ResultSet findUsers() {\n" + " SimpleResultSet rs = new SimpleResultSet();\n" + " rs.addColumn(\"ID\", Types.INTEGER, 10, 0);\n" + " rs.addColumn(\"NAME\", Types.VARCHAR, 255, 0);\n" + " rs.addRow(1, \"Steve\");\n" + " rs.addRow(2, \"John\");\n" + " rs.addRow(3, \"Jane\");\n" + " return rs;\n" + "}\n" + "$$" };
}
@Override
public String[] sqlDropStrings(Dialect dialect) {
return new String[] { "DROP ALIAS findUser IF EXISTS" };
}
});
configuration.addAuxiliaryDatabaseObject(new AuxiliaryDatabaseObject() {
@Override
public String getExportIdentifier() {
return "function:findUserRange";
}
@Override
public boolean appliesToDialect(Dialect dialect) {
return H2Dialect.class.isInstance(dialect);
}
@Override
public boolean beforeTablesOnCreation() {
return false;
}
@Override
public String[] sqlCreateStrings(Dialect dialect) {
return new String[] { "CREATE ALIAS findUserRange AS $$\n" + "import org.h2.tools.SimpleResultSet;\n" + "import java.sql.*;\n" + "@CODE\n" + "ResultSet findUserRange(int start, int end) {\n" + " SimpleResultSet rs = new SimpleResultSet();\n" + " rs.addColumn(\"ID\", Types.INTEGER, 10, 0);\n" + " rs.addColumn(\"NAME\", Types.VARCHAR, 255, 0);\n" + " for ( int i = start; i < end; i++ ) {\n" + " rs.addRow(1, \"User \" + i );\n" + " }\n" + " return rs;\n" + "}\n" + "$$" };
}
@Override
public String[] sqlDropStrings(Dialect dialect) {
return new String[] { "DROP ALIAS findUserRange IF EXISTS" };
}
});
}
use of org.hibernate.boot.model.relational.AuxiliaryDatabaseObject in project hibernate-orm by hibernate.
the class AuxiliaryDatabaseObjectBinder method processAuxiliaryDatabaseObject.
/**
* Handling for a {@code <database-object/>} declaration.
*
* @param context Access to information relative to the mapping document containing this binding
* @param auxDbObjectMapping The {@code <database-object/>} binding
*/
public static void processAuxiliaryDatabaseObject(HbmLocalMetadataBuildingContext context, JaxbHbmAuxiliaryDatabaseObjectType auxDbObjectMapping) {
final AuxiliaryDatabaseObject auxDbObject;
if (auxDbObjectMapping.getDefinition() != null) {
final String auxDbObjectImplClass = auxDbObjectMapping.getDefinition().getClazz();
try {
auxDbObject = (AuxiliaryDatabaseObject) context.getBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class).classForName(auxDbObjectImplClass).newInstance();
} catch (ClassLoadingException cle) {
throw cle;
} catch (Exception e) {
throw new org.hibernate.boot.MappingException(String.format("Unable to instantiate custom AuxiliaryDatabaseObject class [%s]", auxDbObjectImplClass), context.getOrigin());
}
} else {
auxDbObject = new SimpleAuxiliaryDatabaseObject(context.getMetadataCollector().getDatabase().getDefaultNamespace(), auxDbObjectMapping.getCreate(), auxDbObjectMapping.getDrop(), null);
}
if (!auxDbObjectMapping.getDialectScope().isEmpty()) {
if (AuxiliaryDatabaseObject.Expandable.class.isInstance(auxDbObject)) {
final AuxiliaryDatabaseObject.Expandable expandable = (AuxiliaryDatabaseObject.Expandable) auxDbObject;
for (JaxbHbmDialectScopeType dialectScopeBinding : auxDbObjectMapping.getDialectScope()) {
expandable.addDialectScope(dialectScopeBinding.getName());
}
} else {
// error? warn?
}
}
context.getMetadataCollector().getDatabase().addAuxiliaryDatabaseObject(auxDbObject);
}
use of org.hibernate.boot.model.relational.AuxiliaryDatabaseObject in project hibernate-orm by hibernate.
the class SchemaCreatorImpl method createFromMetadata.
public void createFromMetadata(Metadata metadata, ExecutionOptions options, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
boolean tryToCreateCatalogs = false;
boolean tryToCreateSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToCreateSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToCreateCatalogs = true;
}
}
final Database database = metadata.getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Set<String> exportIdentifiers = new HashSet<String>(50);
// first, create each catalog/schema
if (tryToCreateCatalogs || tryToCreateSchemas) {
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
if (tryToCreateCatalogs) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
applySqlStrings(dialect.getCreateCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
exportedCatalogs.add(catalogLogicalName);
}
}
if (tryToCreateSchemas && namespace.getPhysicalName().getSchema() != null) {
applySqlStrings(dialect.getCreateSchemaCommand(namespace.getPhysicalName().getSchema().render(dialect)), formatter, options, targets);
}
}
}
// next, create all "beforeQuery table" auxiliary objects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
continue;
}
if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// then, create all schema objects (tables, sequences, constraints, etc) in each schema
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
// sequences
for (Sequence sequence : namespace.getSequences()) {
if (!schemaFilter.includeSequence(sequence)) {
continue;
}
checkExportIdentifier(sequence, exportIdentifiers);
applySqlStrings(dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), // ),
formatter, options, targets);
}
// tables
for (Table table : namespace.getTables()) {
if (!table.isPhysicalTable()) {
continue;
}
if (!schemaFilter.includeTable(table)) {
continue;
}
checkExportIdentifier(table, exportIdentifiers);
applySqlStrings(dialect.getTableExporter().getSqlCreateStrings(table, metadata), formatter, options, targets);
}
for (Table table : namespace.getTables()) {
if (!table.isPhysicalTable()) {
continue;
}
if (!schemaFilter.includeTable(table)) {
continue;
}
// indexes
final Iterator indexItr = table.getIndexIterator();
while (indexItr.hasNext()) {
final Index index = (Index) indexItr.next();
checkExportIdentifier(index, exportIdentifiers);
applySqlStrings(dialect.getIndexExporter().getSqlCreateStrings(index, metadata), formatter, options, targets);
}
// unique keys
final Iterator ukItr = table.getUniqueKeyIterator();
while (ukItr.hasNext()) {
final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
checkExportIdentifier(uniqueKey, exportIdentifiers);
applySqlStrings(dialect.getUniqueKeyExporter().getSqlCreateStrings(uniqueKey, metadata), formatter, options, targets);
}
}
}
//NOTE : Foreign keys must be created *afterQuery* all tables of all namespaces for cross namespace fks. see HHH-10420
for (Namespace namespace : database.getNamespaces()) {
if (!schemaFilter.includeNamespace(namespace)) {
continue;
}
for (Table table : namespace.getTables()) {
if (!schemaFilter.includeTable(table)) {
continue;
}
// foreign keys
final Iterator fkItr = table.getForeignKeyIterator();
while (fkItr.hasNext()) {
final ForeignKey foreignKey = (ForeignKey) fkItr.next();
applySqlStrings(dialect.getForeignKeyExporter().getSqlCreateStrings(foreignKey, metadata), formatter, options, targets);
}
}
}
// next, create all "afterQuery table" auxiliary objects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.appliesToDialect(dialect) && !auxiliaryDatabaseObject.beforeTablesOnCreation()) {
checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// and finally add all init commands
for (InitCommand initCommand : database.getInitCommands()) {
// todo: this should alo probably use the DML formatter...
applySqlStrings(initCommand.getInitCommands(), formatter, options, targets);
}
}
use of org.hibernate.boot.model.relational.AuxiliaryDatabaseObject in project hibernate-orm by hibernate.
the class AbstractSchemaMigrator method performMigration.
private void performMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, GenerationTarget... targets) {
final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
final Set<String> exportIdentifiers = new HashSet<String>(50);
final Database database = metadata.getDatabase();
// Drop all AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// Create beforeQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
boolean tryToCreateCatalogs = false;
boolean tryToCreateSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToCreateSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToCreateCatalogs = true;
}
}
final Map<Namespace, NameSpaceTablesInformation> tablesInformation = new HashMap<>();
Set<Identifier> exportedCatalogs = new HashSet<>();
for (Namespace namespace : database.getNamespaces()) {
final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration(metadata, existingDatabase, options, dialect, formatter, exportIdentifiers, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
tablesInformation.put(namespace, nameSpaceTablesInformation);
if (schemaFilter.includeNamespace(namespace)) {
for (Sequence sequence : namespace.getSequences()) {
checkExportIdentifier(sequence, exportIdentifiers);
final SequenceInformation sequenceInformation = existingDatabase.getSequenceInformation(sequence.getName());
if (sequenceInformation == null) {
applySqlStrings(false, dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), formatter, options, targets);
}
}
}
}
//NOTE : Foreign keys must be created *afterQuery* all tables of all namespaces for cross namespace fks. see HHH-10420
for (Namespace namespace : database.getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation.get(namespace);
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table)) {
final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyForeignKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}
}
// Create afterQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
}
use of org.hibernate.boot.model.relational.AuxiliaryDatabaseObject 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