[#7356] Emulate MySQL's CREATE TABLE (... INDEX) clause using anonymous blocks

This commit is contained in:
lukaseder 2018-03-29 15:33:04 +02:00
parent e6018573c2
commit d6740ee153
2 changed files with 40 additions and 9 deletions

View File

@ -37,8 +37,13 @@
*/
package org.jooq;
// ...
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
import java.util.Collection;
@ -52,18 +57,18 @@ public interface CreateTableIndexStep extends CreateTableOnCommitStep {
/**
* Add an index to the table.
*/
@Support({ MARIADB, MYSQL })
@Support({ FIREBIRD, MARIADB, MYSQL, POSTGRES })
CreateTableIndexStep index(Index index);
/**
* Add indexes to the table.
*/
@Support({ MARIADB, MYSQL })
@Support({ FIREBIRD, MARIADB, MYSQL, POSTGRES })
CreateTableIndexStep indexes(Index... indexes);
/**
* Add indexes to the table.
*/
@Support({ MARIADB, MYSQL })
@Support({ FIREBIRD, MARIADB, MYSQL, POSTGRES })
CreateTableIndexStep indexes(Collection<? extends Index> indexes);
}

View File

@ -60,6 +60,7 @@ import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.SQLDialect.SQLITE;
// ...
import static org.jooq.impl.DSL.commentOnTable;
import static org.jooq.impl.DSL.createIndex;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.insertInto;
import static org.jooq.impl.DSL.name;
@ -125,7 +126,7 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
private static final long serialVersionUID = 8904572826501186329L;
private static final EnumSet<SQLDialect> NO_SUPPORT_IF_NOT_EXISTS = EnumSet.of(DERBY, FIREBIRD);
private static final EnumSet<SQLDialect> NO_SUPPORT_WITH_DATA = EnumSet.of(H2, MARIADB, MYSQL, SQLITE);
private static final EnumSet<SQLDialect> NO_SUPPORT_INDEXES = EnumSet.of(HSQLDB);
private static final EnumSet<SQLDialect> EMULATE_INDEXES_IN_BLOCK = EnumSet.of(POSTGRES);
private static final EnumSet<SQLDialect> REQUIRES_WITH_DATA = EnumSet.of(HSQLDB);
private static final EnumSet<SQLDialect> WRAP_SELECT_IN_PARENS = EnumSet.of(HSQLDB);
private static final EnumSet<SQLDialect> SUPPORT_TEMPORARY = EnumSet.of(MARIADB, MYSQL, POSTGRES);
@ -323,7 +324,10 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
}
private final void accept0(Context<?> ctx) {
if (comment != null && EMULATE_COMMENT_IN_BLOCK.contains(ctx.family())) {
boolean c = comment != null && EMULATE_COMMENT_IN_BLOCK.contains(ctx.family());
boolean i = !indexes.isEmpty() && EMULATE_INDEXES_IN_BLOCK.contains(ctx.family());
if (c || i) {
begin(ctx);
@ -340,21 +344,43 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
ctx.sql(';');
ctx.formatSeparator();
if (c) {
ctx.formatSeparator();
ctx.visit(commentOnTable(table).is(comment));
ctx.visit(commentOnTable(table).is(comment));
ctx.sql(';');
ctx.sql(';');
}
if (i) {
for (Index index : indexes) {
ctx.formatSeparator();
ctx.visit(createIndex(index.getUnqualifiedName()).on(index.getTable(), index.getFields()));
ctx.sql(';');
}
}
end(ctx);
return;
@ -417,7 +443,7 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
ctx.end(CREATE_TABLE_CONSTRAINTS);
if (!indexes.isEmpty() && !NO_SUPPORT_INDEXES.contains(ctx.family())) {
if (!indexes.isEmpty() && !EMULATE_INDEXES_IN_BLOCK.contains(ctx.family())) {
ctx.qualify(false);
for (Index index : indexes) {