[jOOQ/jOOQ#9327] Add DDLFlag.SEQUENCE to allow for exporting CREATE

SEQUENCE statements from DSLContext.ddl()
This commit is contained in:
Lukas Eder 2019-10-03 16:39:05 +02:00
parent 77d4d00479
commit 733050cbcf
3 changed files with 44 additions and 4 deletions

View File

@ -53,6 +53,7 @@ public final class DDLExportConfiguration {
private final boolean createSchemaIfNotExists;
private final boolean createTableIfNotExists;
private final boolean createSequenceIfNotExists;
private final EnumSet<DDLFlag> flags;
/**
@ -62,6 +63,7 @@ public final class DDLExportConfiguration {
this(
EnumSet.allOf(DDLFlag.class),
false,
false,
false
);
}
@ -69,11 +71,13 @@ public final class DDLExportConfiguration {
private DDLExportConfiguration(
Collection<DDLFlag> flags,
boolean createSchemaIfNotExists,
boolean createTableIfNotExists
boolean createTableIfNotExists,
boolean createSequenceIfNotExists
) {
this.flags = EnumSet.copyOf(flags);
this.createSchemaIfNotExists = createSchemaIfNotExists;
this.createTableIfNotExists = createTableIfNotExists;
this.createSequenceIfNotExists = createSequenceIfNotExists;
}
/**
@ -94,7 +98,7 @@ public final class DDLExportConfiguration {
* The {@link DDLFlag} that are enabled on this configuration.
*/
public final DDLExportConfiguration flags(Collection<DDLFlag> newFlags) {
return new DDLExportConfiguration(newFlags, createSchemaIfNotExists, createTableIfNotExists);
return new DDLExportConfiguration(newFlags, createSchemaIfNotExists, createTableIfNotExists, createSequenceIfNotExists);
}
/**
@ -112,7 +116,7 @@ public final class DDLExportConfiguration {
* Whether to generate <code>CREATE SCHEMA IF NOT EXISTS</code> statements.
*/
public final DDLExportConfiguration createSchemaIfNotExists(boolean newCreateSchemaIfNotExists) {
return new DDLExportConfiguration(flags, newCreateSchemaIfNotExists, createTableIfNotExists);
return new DDLExportConfiguration(flags, newCreateSchemaIfNotExists, createTableIfNotExists, createSequenceIfNotExists);
}
/**
@ -130,6 +134,24 @@ public final class DDLExportConfiguration {
* Whether to generate <code>CREATE TABLE IF NOT EXISTS</code> statements.
*/
public final DDLExportConfiguration createTableIfNotExists(boolean newCreateTableIfNotExists) {
return new DDLExportConfiguration(flags, createSchemaIfNotExists, newCreateTableIfNotExists);
return new DDLExportConfiguration(flags, createSchemaIfNotExists, newCreateTableIfNotExists, createSequenceIfNotExists);
}
/**
* Whether to generate <code>CREATE SEQUENCE IF NOT EXISTS</code> statements.
* <p>
* Not all RDBMS support this flag. Check
* {@link DSLContext#createSequenceIfNotExists(Sequence)} to see if your
* {@link SQLDialect} supports the clause.
*/
public final boolean createSequenceIfNotExists() {
return createSequenceIfNotExists;
}
/**
* Whether to generate <code>CREATE SEQUENCE IF NOT EXISTS</code> statements.
*/
public final DDLExportConfiguration createSequenceIfNotExists(boolean newCreateSequenceIfNotExists) {
return new DDLExportConfiguration(flags, createSchemaIfNotExists, createTableIfNotExists, newCreateSequenceIfNotExists);
}
}

View File

@ -67,6 +67,11 @@ public enum DDLFlag {
*/
FOREIGN_KEY,
/**
* Whether <code>SEQUENCE</code> statements should be generated.
*/
SEQUENCE,
/**
* Whether <code>COMMENT</code> statements should be generated.
*/

View File

@ -41,6 +41,7 @@ import static org.jooq.DDLFlag.COMMENT;
import static org.jooq.DDLFlag.FOREIGN_KEY;
import static org.jooq.DDLFlag.PRIMARY_KEY;
import static org.jooq.DDLFlag.SCHEMA;
import static org.jooq.DDLFlag.SEQUENCE;
import static org.jooq.DDLFlag.TABLE;
import static org.jooq.DDLFlag.UNIQUE;
import static org.jooq.impl.DSL.constraint;
@ -58,6 +59,7 @@ import org.jooq.ForeignKey;
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.tools.StringUtils;
@ -83,6 +85,12 @@ final class DDL {
.constraints(constraints);
}
private final Query createSequence(Sequence<?> sequence) {
return configuration.createSequenceIfNotExists()
? ctx.createSequenceIfNotExists(sequence)
: ctx.createSequence(sequence);
}
private final Query createTable(Table<?> table) {
return createTable(table, constraints(table));
}
@ -216,6 +224,11 @@ final class DDL {
for (Constraint constraint : foreignKeys(table))
queries.add(ctx.alterTable(table).add(constraint));
if (configuration.flags().contains(SEQUENCE))
for (Schema schema : schemas)
for (Sequence<?> sequence : schema.getSequences())
queries.add(createSequence(sequence));
if (configuration.flags().contains(COMMENT))
for (Schema schema : schemas)
for (Table<?> table : schema.getTables())