use of org.hibernate.tool.schema.extract.spi.SequenceInformation in project hibernate-orm by hibernate.
the class AbstractSchemaValidator method performValidation.
public void performValidation(Metadata metadata, DatabaseInformation databaseInformation, ExecutionOptions options, Dialect dialect) {
for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
validateTables(metadata, databaseInformation, options, dialect, namespace);
}
}
for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
for (Sequence sequence : namespace.getSequences()) {
if (schemaFilter.includeSequence(sequence)) {
final SequenceInformation sequenceInformation = databaseInformation.getSequenceInformation(sequence.getName());
validateSequence(sequence, sequenceInformation);
}
}
}
}
}
use of org.hibernate.tool.schema.extract.spi.SequenceInformation 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.tool.schema.extract.spi.SequenceInformation in project hibernate-orm by hibernate.
the class SequenceInformationExtractorH2DatabaseImpl method extractMetadata.
@Override
public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException {
final IdentifierHelper identifierHelper = extractionContext.getJdbcEnvironment().getIdentifierHelper();
final Statement statement = extractionContext.getJdbcConnection().createStatement();
try {
ResultSet resultSet = statement.executeQuery("select SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME, INCREMENT " + "from information_schema.sequences");
try {
final List<SequenceInformation> sequenceInformationList = new ArrayList<SequenceInformation>();
while (resultSet.next()) {
sequenceInformationList.add(new SequenceInformationImpl(new QualifiedSequenceName(identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_CATALOG")), identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_SCHEMA")), identifierHelper.toIdentifier(resultSet.getString("SEQUENCE_NAME"))), resultSet.getInt("INCREMENT")));
}
return sequenceInformationList;
} finally {
try {
resultSet.close();
} catch (SQLException ignore) {
}
}
} finally {
try {
statement.close();
} catch (SQLException ignore) {
}
}
}
use of org.hibernate.tool.schema.extract.spi.SequenceInformation in project hibernate-orm by hibernate.
the class SequenceInformationExtractorLegacyImpl method extractMetadata.
@Override
public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException {
final String lookupSql = extractionContext.getJdbcEnvironment().getDialect().getQuerySequencesString();
// *should* never happen, but to be safe in the interest of performance...
if (lookupSql == null) {
return SequenceInformationExtractorNoOpImpl.INSTANCE.extractMetadata(extractionContext);
}
final IdentifierHelper identifierHelper = extractionContext.getJdbcEnvironment().getIdentifierHelper();
final Statement statement = extractionContext.getJdbcConnection().createStatement();
try {
final ResultSet resultSet = statement.executeQuery(lookupSql);
try {
final List<SequenceInformation> sequenceInformationList = new ArrayList<SequenceInformation>();
while (resultSet.next()) {
sequenceInformationList.add(new SequenceInformationImpl(new QualifiedSequenceName(null, null, identifierHelper.toIdentifier(resultSet.getString(1))), -1));
}
return sequenceInformationList;
} finally {
try {
resultSet.close();
} catch (SQLException ignore) {
}
}
} finally {
try {
statement.close();
} catch (SQLException ignore) {
}
}
}
Aggregations