[jOOQ/jOOQ#8657] Add DDLExportConfiguration.createSchemaIfNotExists and createTableIfNotExists flags
This commit is contained in:
parent
4921fdf6bc
commit
cccc232aae
@ -51,29 +51,85 @@ import java.util.Set;
|
||||
*/
|
||||
public final class DDLExportConfiguration {
|
||||
|
||||
final EnumSet<DDLFlag> flags;
|
||||
private final boolean createSchemaIfNotExists;
|
||||
private final boolean createTableIfNotExists;
|
||||
private final EnumSet<DDLFlag> flags;
|
||||
|
||||
/**
|
||||
* Create a new default export configuration instance.
|
||||
*/
|
||||
public DDLExportConfiguration() {
|
||||
this(
|
||||
EnumSet.allOf(DDLFlag.class)
|
||||
EnumSet.allOf(DDLFlag.class),
|
||||
false,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private DDLExportConfiguration(
|
||||
Set<DDLFlag> flags
|
||||
Collection<DDLFlag> flags,
|
||||
boolean createSchemaIfNotExists,
|
||||
boolean createTableIfNotExists
|
||||
) {
|
||||
this.flags = EnumSet.copyOf(flags);
|
||||
this.createSchemaIfNotExists = createSchemaIfNotExists;
|
||||
this.createTableIfNotExists = createTableIfNotExists;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link DDLFlag} that are enabled on this configuration.
|
||||
*/
|
||||
public final Set<DDLFlag> flags() {
|
||||
return Collections.unmodifiableSet(flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link DDLFlag} that are enabled on this configuration.
|
||||
*/
|
||||
public final DDLExportConfiguration flags(DDLFlag... newFlags) {
|
||||
return flags(Arrays.asList(newFlags));
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link DDLFlag} that are enabled on this configuration.
|
||||
*/
|
||||
public final DDLExportConfiguration flags(Collection<DDLFlag> newFlags) {
|
||||
return new DDLExportConfiguration(EnumSet.copyOf(newFlags));
|
||||
return new DDLExportConfiguration(newFlags, createSchemaIfNotExists, createTableIfNotExists);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to generate <code>CREATE SCHEMA IF NOT EXISTS</code> statements.
|
||||
* <p>
|
||||
* Not all RDBMS support this flag. Check
|
||||
* {@link DSLContext#createSchemaIfNotExists(Schema)} to see if your
|
||||
* {@link SQLDialect} supports the clause.
|
||||
*/
|
||||
public final boolean createSchemaIfNotExists() {
|
||||
return createSchemaIfNotExists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to generate <code>CREATE SCHEMA IF NOT EXISTS</code> statements.
|
||||
*/
|
||||
public final DDLExportConfiguration createSchemaIfNotExists(boolean newCreateSchemaIfNotExists) {
|
||||
return new DDLExportConfiguration(flags, newCreateSchemaIfNotExists, createTableIfNotExists);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to generate <code>CREATE TABLE IF NOT EXISTS</code> statements.
|
||||
* <p>
|
||||
* Not all RDBMS support this flag. Check
|
||||
* {@link DSLContext#createTableIfNotExists(Table)} to see if your
|
||||
* {@link SQLDialect} supports the clause.
|
||||
*/
|
||||
public final boolean createTableIfNotExists() {
|
||||
return createTableIfNotExists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to generate <code>CREATE TABLE IF NOT EXISTS</code> statements.
|
||||
*/
|
||||
public final DDLExportConfiguration createTableIfNotExists(boolean newCreateTableIfNotExists) {
|
||||
return new DDLExportConfiguration(flags, createSchemaIfNotExists, newCreateTableIfNotExists);
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +47,7 @@ import static org.jooq.impl.DSL.constraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.Catalog;
|
||||
@ -75,10 +76,16 @@ final class DDL {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
private final Query createTable(Table<?> table) {
|
||||
return ctx.createTable(table)
|
||||
private final Query createTable(Table<?> table, Collection<? extends Constraint> constraints) {
|
||||
return (configuration.createTableIfNotExists()
|
||||
? ctx.createTableIfNotExists(table)
|
||||
: ctx.createTable(table))
|
||||
.columns(table.fields())
|
||||
.constraints(constraints(table));
|
||||
.constraints(constraints);
|
||||
}
|
||||
|
||||
private final Query createTable(Table<?> table) {
|
||||
return createTable(table, constraints(table));
|
||||
}
|
||||
|
||||
private final List<Query> alterTableAddConstraints(Table<?> table) {
|
||||
@ -172,7 +179,10 @@ final class DDL {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
if (configuration.flags().contains(SCHEMA) && !StringUtils.isBlank(schema.getName()))
|
||||
queries.add(ctx.createSchema(schema.getName()));
|
||||
if (configuration.createSchemaIfNotExists())
|
||||
queries.add(ctx.createSchemaIfNotExists(schema.getName()));
|
||||
else
|
||||
queries.add(ctx.createSchema(schema.getName()));
|
||||
|
||||
if (configuration.flags().contains(TABLE)) {
|
||||
for (Table<?> table : schema.getTables()) {
|
||||
@ -181,11 +191,7 @@ final class DDL {
|
||||
constraints.addAll(primaryKeys(table));
|
||||
constraints.addAll(uniqueKeys(table));
|
||||
|
||||
queries.add(
|
||||
ctx.createTable(table)
|
||||
.columns(table.fields())
|
||||
.constraints(constraints)
|
||||
);
|
||||
queries.add(createTable(table, constraints));
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user