From 31bcc97ea5ed0ec83e3347b94e15fe13865169cf Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 16 Oct 2019 15:07:18 +0200 Subject: [PATCH] [jOOQ/jOOQ#9380] Interpret DROP TABLE .. CASCADE --- .../java/org/jooq/impl/DDLInterpreter.java | 35 +++++++++++-------- .../java/org/jooq/impl/DropTableImpl.java | 18 ++++++---- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java index 286c32191d..d0e7a0f612 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java @@ -270,6 +270,24 @@ final class DDLInterpreter { )); } + private final void drop(List 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 it = tables.iterator(); + + while (it.hasNext()) { + if (it.next().name.equals(table.name)) { + it.remove(); + break; + } + } + } + private final void dropColumns(MutableTable table, List fields, Cascade cascade) { Iterator 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); diff --git a/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java index a3992c2f54..e42a59accc 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java @@ -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); }