[jOOQ/jOOQ#9380] Interpret DROP TABLE .. CASCADE

This commit is contained in:
Lukas Eder 2019-10-16 15:07:18 +02:00
parent 4b2c044cdc
commit 31bcc97ea5
2 changed files with 33 additions and 20 deletions

View File

@ -270,6 +270,24 @@ final class DDLInterpreter {
));
}
private final void drop(List<MutableTable> tables, MutableTable table, Cascade cascade) {
for (boolean check : cascade == CASCADE ? new boolean [] { false } : new boolean [] { true, false }) {
if (table.primaryKey != null)
cascade(table.primaryKey, null, check ? RESTRICT : CASCADE);
cascade(table.uniqueKeys, null, check);
}
Iterator<MutableTable> it = tables.iterator();
while (it.hasNext()) {
if (it.next().name.equals(table.name)) {
it.remove();
break;
}
}
}
private final void dropColumns(MutableTable table, List<MutableField> fields, Cascade cascade) {
Iterator<MutableIndex> it1 = table.indexes.iterator();
@ -515,7 +533,7 @@ final class DDLInterpreter {
else if (existing.view)
throw objectNotTable(table);
schema.drop(table);
drop(schema.tables, existing, query.$cascade());
}
private final void accept0(CreateViewImpl<?> query) {
@ -527,7 +545,7 @@ final class DDLInterpreter {
if (!existing.view)
throw objectNotView(table);
else if (query.$orReplace())
schema.drop(table);
drop(schema.tables, existing, RESTRICT);
else if (!query.$ifNotExists())
throw viewAlreadyExists(table);
else
@ -576,7 +594,7 @@ final class DDLInterpreter {
else if (!existing.view)
throw objectNotView(table);
schema.drop(table);
drop(schema.tables, existing, RESTRICT);
}
private final void accept0(CreateSequenceImpl query) {
@ -1041,17 +1059,6 @@ final class DDLInterpreter {
return null;
}
final void drop(Table<?> t) {
UnqualifiedName n = normalize(t);
for (int i = 0; i < tables.size(); i++) {
if (tables.get(i).name.equals(n)) {
tables.remove(i);
break;
}
}
}
private final class InterpretedSchema extends SchemaImpl {
InterpretedSchema(MutableCatalog.InterpretedCatalog catalog) {
super(MutableSchema.this.name, catalog, MutableSchema.this.comment);

View File

@ -49,10 +49,13 @@ import static org.jooq.SQLDialect.MYSQL;
// ...
// ...
// ...
import static org.jooq.impl.Cascade.CASCADE;
import static org.jooq.impl.Cascade.RESTRICT;
import static org.jooq.impl.Keywords.K_CASCADE;
import static org.jooq.impl.Keywords.K_DROP;
import static org.jooq.impl.Keywords.K_DROP_TABLE;
import static org.jooq.impl.Keywords.K_IF_EXISTS;
import static org.jooq.impl.Keywords.K_RESTRICT;
import static org.jooq.impl.Keywords.K_TABLE;
import static org.jooq.impl.Keywords.K_TEMPORARY;
@ -85,7 +88,7 @@ final class DropTableImpl extends AbstractRowCountQuery implements
private final Table<?> table;
private final boolean temporary;
private final boolean ifExists;
private boolean cascade;
private Cascade cascade;
DropTableImpl(Configuration configuration, Table<?> table) {
this(configuration, table, false, false);
@ -103,8 +106,9 @@ final class DropTableImpl extends AbstractRowCountQuery implements
this.temporary = temporary;
}
final Table<?> $table() { return table; }
final boolean $ifExists() { return ifExists; }
final Table<?> $table() { return table; }
final boolean $ifExists() { return ifExists; }
final Cascade $cascade() { return cascade; }
// ------------------------------------------------------------------------
// XXX: DSL API
@ -112,13 +116,13 @@ final class DropTableImpl extends AbstractRowCountQuery implements
@Override
public final DropTableImpl cascade() {
cascade = true;
cascade = CASCADE;
return this;
}
@Override
public final DropTableImpl restrict() {
cascade = false;
cascade = RESTRICT;
return this;
}
@ -159,8 +163,10 @@ final class DropTableImpl extends AbstractRowCountQuery implements
ctx.visit(table);
if (cascade)
if (cascade == CASCADE)
ctx.sql(' ').visit(K_CASCADE);
else if (cascade == RESTRICT)
ctx.sql(' ').visit(K_RESTRICT);
ctx.end(DROP_TABLE_TABLE);
}