[jOOQ/jOOQ#9364] Add DDLFlag.INDEX to allow for exporting CREATE INDEX statements

This commit is contained in:
Lukas Eder 2019-10-15 11:20:14 +02:00
parent 9a01e94521
commit d9f720d505
4 changed files with 59 additions and 5 deletions

View File

@ -53,6 +53,7 @@ public final class DDLExportConfiguration {
private final boolean createSchemaIfNotExists;
private final boolean createTableIfNotExists;
private final boolean createIndexIfNotExists;
private final boolean createSequenceIfNotExists;
private final EnumSet<DDLFlag> flags;
@ -64,6 +65,7 @@ public final class DDLExportConfiguration {
EnumSet.allOf(DDLFlag.class),
false,
false,
false,
false
);
}
@ -72,11 +74,13 @@ public final class DDLExportConfiguration {
Collection<DDLFlag> flags,
boolean createSchemaIfNotExists,
boolean createTableIfNotExists,
boolean createIndexIfNotExists,
boolean createSequenceIfNotExists
) {
this.flags = EnumSet.copyOf(flags);
this.createSchemaIfNotExists = createSchemaIfNotExists;
this.createTableIfNotExists = createTableIfNotExists;
this.createIndexIfNotExists = createIndexIfNotExists;
this.createSequenceIfNotExists = createSequenceIfNotExists;
}
@ -98,7 +102,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, createSequenceIfNotExists);
return new DDLExportConfiguration(newFlags, createSchemaIfNotExists, createTableIfNotExists, createIndexIfNotExists, createSequenceIfNotExists);
}
/**
@ -116,7 +120,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, createSequenceIfNotExists);
return new DDLExportConfiguration(flags, newCreateSchemaIfNotExists, createTableIfNotExists, createIndexIfNotExists, createSequenceIfNotExists);
}
/**
@ -134,7 +138,25 @@ 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, createSequenceIfNotExists);
return new DDLExportConfiguration(flags, createSchemaIfNotExists, newCreateTableIfNotExists, createIndexIfNotExists, createSequenceIfNotExists);
}
/**
* Whether to generate <code>CREATE INDEX IF NOT EXISTS</code> statements.
* <p>
* Not all RDBMS support this flag. Check
* {@link DSLContext#createIndexIfNotExists(Index)} to see if your
* {@link SQLDialect} supports the clause.
*/
public final boolean createIndexIfNotExists() {
return createTableIfNotExists;
}
/**
* 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);
}
/**
@ -152,6 +174,6 @@ 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, newCreateSequenceIfNotExists);
return new DDLExportConfiguration(flags, createSchemaIfNotExists, createTableIfNotExists, createIndexIfNotExists, newCreateSequenceIfNotExists);
}
}

View File

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

View File

@ -39,6 +39,7 @@ package org.jooq.impl;
import static org.jooq.DDLFlag.COMMENT;
import static org.jooq.DDLFlag.FOREIGN_KEY;
import static org.jooq.DDLFlag.INDEX;
import static org.jooq.DDLFlag.PRIMARY_KEY;
import static org.jooq.DDLFlag.SCHEMA;
import static org.jooq.DDLFlag.SEQUENCE;
@ -53,9 +54,11 @@ import java.util.List;
import org.jooq.Catalog;
import org.jooq.Constraint;
import org.jooq.DDLExportConfiguration;
import org.jooq.DDLFlag;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Index;
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.Schema;
@ -95,6 +98,25 @@ final class DDL {
return createTable(table, constraints(table));
}
private final List<Query> createIndex(Table<?> table) {
List<Query> result = new ArrayList<>();
if (configuration.flags().contains(DDLFlag.INDEX))
for (Index i : table.getIndexes())
result.add(
(configuration.createIndexIfNotExists()
? i.getUnique()
? ctx.createUniqueIndexIfNotExists(i)
: ctx.createIndexIfNotExists(i)
: i.getUnique()
? ctx.createUniqueIndex(i)
: ctx.createIndex(i))
.on(i.getTable(), i.getFields())
);
return result;
}
private final List<Query> alterTableAddConstraints(Table<?> table) {
List<Constraint> constraints = constraints(table);
List<Query> result = new ArrayList<>(constraints.size());
@ -156,6 +178,7 @@ final class DDL {
else
queries.addAll(alterTableAddConstraints(table));
queries.addAll(createIndex(table));
queries.addAll(commentOn(table));
}
@ -234,6 +257,11 @@ final class DDL {
for (Table<?> table : schema.getTables())
queries.addAll(commentOn(table));
if (configuration.flags().contains(INDEX))
for (Schema schema : schemas)
for (Table<?> table : schema.getTables())
queries.addAll(createIndex(table));
return ctx.queries(queries);
}

View File

@ -217,7 +217,6 @@ final class DDLInterpreter {
for (Constraint constraint : query.$constraints()) {
ConstraintImpl impl = (ConstraintImpl) constraint;
// XXX handle case that primary key already exists?
if (impl.$primaryKey() != null)
t.primaryKey = new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), t, t.fields(impl.$primaryKey(), true));