[jOOQ/jOOQ#9434] Add DDLExportConfiguration.respectOrder flags

This commit is contained in:
Lukas Eder 2019-10-23 14:20:19 +02:00
parent 995e80ee4c
commit e1efb1f082
3 changed files with 352 additions and 30 deletions

View File

@ -56,6 +56,13 @@ public final class DDLExportConfiguration {
private final boolean createIndexIfNotExists;
private final boolean createSequenceIfNotExists;
private final EnumSet<DDLFlag> flags;
private final boolean respectCatalogOrder;
private final boolean respectSchemaOrder;
private final boolean respectTableOrder;
private final boolean respectColumnOrder;
private final boolean respectConstraintOrder;
private final boolean respectIndexOrder;
private final boolean respectSequenceOrder;
/**
* Create a new default export configuration instance.
@ -63,9 +70,18 @@ public final class DDLExportConfiguration {
public DDLExportConfiguration() {
this(
EnumSet.allOf(DDLFlag.class),
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false
);
}
@ -75,13 +91,29 @@ public final class DDLExportConfiguration {
boolean createSchemaIfNotExists,
boolean createTableIfNotExists,
boolean createIndexIfNotExists,
boolean createSequenceIfNotExists
boolean createSequenceIfNotExists,
boolean respectCatalogOrder,
boolean respectSchemaOrder,
boolean respectTableOrder,
boolean respectColumnOrder,
boolean respectConstraintOrder,
boolean respectIndexOrder,
boolean respectSequenceOrder
) {
this.flags = EnumSet.copyOf(flags);
this.createSchemaIfNotExists = createSchemaIfNotExists;
this.createTableIfNotExists = createTableIfNotExists;
this.createIndexIfNotExists = createIndexIfNotExists;
this.createSequenceIfNotExists = createSequenceIfNotExists;
this.respectCatalogOrder = respectCatalogOrder;
this.respectSchemaOrder = respectSchemaOrder;
this.respectTableOrder = respectTableOrder;
this.respectColumnOrder = respectColumnOrder;
this.respectConstraintOrder = respectConstraintOrder;
this.respectIndexOrder = respectIndexOrder;
this.respectSequenceOrder = respectSequenceOrder;
}
/**
@ -102,7 +134,20 @@ 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, createIndexIfNotExists, createSequenceIfNotExists);
return new DDLExportConfiguration(
newFlags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
@ -120,7 +165,20 @@ 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, createIndexIfNotExists, createSequenceIfNotExists);
return new DDLExportConfiguration(
flags,
newCreateSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
@ -138,7 +196,20 @@ 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, createIndexIfNotExists, createSequenceIfNotExists);
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
newCreateTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
@ -156,7 +227,20 @@ public final class DDLExportConfiguration {
* Whether to generate <code>CREATE INDEX IF NOT EXISTS</code> statements.
*/
public final DDLExportConfiguration createIndexIfNotExists(boolean newCreateIndexIfNotExists) {
return new DDLExportConfiguration(flags, createSchemaIfNotExists, createTableIfNotExists, newCreateIndexIfNotExists, createSequenceIfNotExists);
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
newCreateIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
@ -174,6 +258,222 @@ public final class DDLExportConfiguration {
* Whether to generate <code>CREATE SEQUENCE IF NOT EXISTS</code> statements.
*/
public final DDLExportConfiguration createSequenceIfNotExists(boolean newCreateSequenceIfNotExists) {
return new DDLExportConfiguration(flags, createSchemaIfNotExists, createTableIfNotExists, createIndexIfNotExists, newCreateSequenceIfNotExists);
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
newCreateSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the catalog order produced by the {@link Meta} source
* when generated catalog DDL.
*/
public final boolean respectCatalogOrder() {
return respectCatalogOrder;
}
/**
* Whether to respect the catalog order produced by the {@link Meta} source
* when generated catalog DDL.
*/
public final DDLExportConfiguration respectCatalogOrder(boolean newRespectCatalogOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
newRespectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the schema order produced by the {@link Meta} source
* when generated schema DDL.
*/
public final boolean respectSchemaOrder() {
return respectSchemaOrder;
}
/**
* Whether to respect the schema order produced by the {@link Meta} source
* when generated schema DDL.
*/
public final DDLExportConfiguration respectSchemaOrder(boolean newRespectSchemaOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
newRespectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the table order produced by the {@link Meta} source
* when generated table DDL.
*/
public final boolean respectTableOrder() {
return respectTableOrder;
}
/**
* Whether to respect the table order produced by the {@link Meta} source
* when generated table DDL.
*/
public final DDLExportConfiguration respectTableOrder(boolean newRespectTableOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
newRespectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the column order produced by the {@link Meta} source
* when generated column DDL.
*/
public final boolean respectColumnOrder() {
return respectColumnOrder;
}
/**
* Whether to respect the column order produced by the {@link Meta} source
* when generated column DDL.
*/
public final DDLExportConfiguration respectColumnOrder(boolean newRespectColumnOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
newRespectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the constraint order produced by the {@link Meta} source
* when generated constraint DDL.
*/
public final boolean respectConstraintOrder() {
return respectConstraintOrder;
}
/**
* Whether to respect the constraint order produced by the {@link Meta} source
* when generated constraint DDL.
*/
public final DDLExportConfiguration respectConstraintOrder(boolean newRespectConstraintOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
newRespectConstraintOrder,
respectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the index order produced by the {@link Meta} source
* when generated index DDL.
*/
public final boolean respectIndexOrder() {
return respectIndexOrder;
}
/**
* Whether to respect the index order produced by the {@link Meta} source
* when generated index DDL.
*/
public final DDLExportConfiguration respectIndexOrder(boolean newRespectIndexOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
newRespectIndexOrder,
respectSequenceOrder
);
}
/**
* Whether to respect the sequence order produced by the {@link Meta} source
* when generated sequence DDL.
*/
public final boolean respectSequenceOrder() {
return respectSequenceOrder;
}
/**
* Whether to respect the sequence order produced by the {@link Meta} source
* when generated sequence DDL.
*/
public final DDLExportConfiguration respectSequenceOrder(boolean newRespectSequenceOrder) {
return new DDLExportConfiguration(
flags,
createSchemaIfNotExists,
createTableIfNotExists,
createIndexIfNotExists,
createSequenceIfNotExists,
respectCatalogOrder,
respectSchemaOrder,
respectTableOrder,
respectColumnOrder,
respectConstraintOrder,
respectIndexOrder,
newRespectSequenceOrder
);
}
}

View File

@ -48,7 +48,10 @@ import static org.jooq.DDLFlag.UNIQUE;
import static org.jooq.impl.DSL.constraint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jooq.Constraint;
@ -58,7 +61,9 @@ import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Index;
import org.jooq.Key;
import org.jooq.Meta;
import org.jooq.Named;
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.Schema;
@ -86,7 +91,7 @@ final class DDL {
return (configuration.createTableIfNotExists()
? ctx.createTableIfNotExists(table)
: ctx.createTable(table))
.columns(table.fields())
.columns(sortIf(Arrays.asList(table.fields()), !configuration.respectColumnOrder()))
.constraints(constraints);
}
@ -104,7 +109,7 @@ final class DDL {
List<Query> result = new ArrayList<>();
if (configuration.flags().contains(DDLFlag.INDEX))
for (Index i : table.getIndexes())
for (Index i : sortIf(table.getIndexes(), !configuration.respectIndexOrder()))
result.add(
(configuration.createIndexIfNotExists()
? i.getUnique()
@ -154,7 +159,7 @@ final class DDL {
List<Constraint> result = new ArrayList<>();
if (configuration.flags().contains(UNIQUE))
for (UniqueKey<?> key : table.getKeys())
for (UniqueKey<?> key : sortKeysIf(table.getKeys(), !configuration.respectConstraintOrder()))
if (!key.isPrimary())
result.add(constraint(key.getName()).unique(key.getFieldsArray()));
@ -165,7 +170,7 @@ final class DDL {
List<Constraint> result = new ArrayList<>();
if (configuration.flags().contains(FOREIGN_KEY))
for (ForeignKey<?, ?> key : table.getReferences())
for (ForeignKey<?, ?> key : sortKeysIf(table.getReferences(), !configuration.respectConstraintOrder()))
result.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
return result;
@ -196,7 +201,7 @@ final class DDL {
if (!StringUtils.isEmpty(tComment))
result.add(ctx.commentOnTable(table).is(tComment));
for (Field<?> field : table.fields()) {
for (Field<?> field : sortIf(Arrays.asList(table.fields()), !configuration.respectColumnOrder())) {
String fComment = field.getComment();
if (!StringUtils.isEmpty(fComment))
@ -209,7 +214,7 @@ final class DDL {
final Queries queries() {
List<Query> queries = new ArrayList<>();
List<Schema> schemas = meta.getSchemas();
List<Schema> schemas = sortIf(meta.getSchemas(), !configuration.respectSchemaOrder());
for (Schema schema : schemas)
if (configuration.flags().contains(SCHEMA) && !StringUtils.isBlank(schema.getName()))
@ -220,7 +225,7 @@ final class DDL {
if (configuration.flags().contains(TABLE)) {
for (Schema schema : schemas) {
for (Table<?> table : schema.getTables()) {
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) {
List<Constraint> constraints = new ArrayList<>();
constraints.addAll(primaryKeys(table));
@ -233,38 +238,64 @@ final class DDL {
else {
for (Schema schema : schemas) {
if (configuration.flags().contains(PRIMARY_KEY))
for (Table<?> table : schema.getTables())
for (Constraint constraint : primaryKeys(table))
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder()))
for (Constraint constraint : sortIf(primaryKeys(table), !configuration.respectConstraintOrder()))
queries.add(ctx.alterTable(table).add(constraint));
if (configuration.flags().contains(UNIQUE))
for (Table<?> table : schema.getTables())
for (Constraint constraint : uniqueKeys(table))
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder()))
for (Constraint constraint : sortIf(uniqueKeys(table), !configuration.respectConstraintOrder()))
queries.add(ctx.alterTable(table).add(constraint));
}
}
if (configuration.flags().contains(FOREIGN_KEY))
for (Schema schema : schemas)
for (Table<?> table : schema.getTables())
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder()))
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())
for (Sequence<?> sequence : sortIf(schema.getSequences(), !configuration.respectSequenceOrder()))
queries.add(createSequence(sequence));
if (configuration.flags().contains(COMMENT))
for (Schema schema : schemas)
for (Table<?> table : schema.getTables())
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder()))
queries.addAll(commentOn(table));
if (configuration.flags().contains(INDEX))
for (Schema schema : schemas)
for (Table<?> table : schema.getTables())
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder()))
queries.addAll(createIndex(table));
return ctx.queries(queries);
}
// [#9435] TODO: Remove this again when Key extends Named
private final <N extends Key<?>> List<N> sortKeysIf(List<N> input, boolean sort) {
if (sort) {
List<N> result = new ArrayList<>(input);
Collections.sort(result, new Comparator<Key<?>>() {
@Override
public int compare(Key<?> o1, Key<?> o2) {
return o1.getName().compareTo(o2.getName());
}
});
return result;
}
return input;
}
private final <N extends Named> List<N> sortIf(List<N> input, boolean sort) {
if (sort) {
List<N> result = new ArrayList<>(input);
Collections.sort(result, NamedComparator.INSTANCE);
return result;
}
return input;
}
}

View File

@ -221,15 +221,6 @@ package org.jooq.impl;