diff --git a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/DDLTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/DDLTests.java index e88a701f02..c9a0fff35d 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/DDLTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/DDLTests.java @@ -111,7 +111,14 @@ extends BaseTest { create().fetch("select * from {0}", name("v1")); @@ -157,6 +164,23 @@ extends BaseTest create().dropTable("t").execute()); + } + } + public void testCreateSequence() throws Exception { assumeNotNull(cSequences()); @@ -181,6 +205,21 @@ extends BaseTest create().dropTable("t").execute()); + } + } + private String varchar() { return SQLDataType.VARCHAR.length(10).getCastTypeName(create().configuration()); } diff --git a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java index 987cd34117..30bedd76e6 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java @@ -1349,6 +1349,11 @@ public abstract class jOOQAbstractTest< new DDLTests(this).testDropIndex(); } + @Test + public void testDropIndexIfExists() throws Exception { + new DDLTests(this).testDropIndexIfExists(); + } + @Test public void testCreateSequence() throws Exception { new DDLTests(this).testCreateSequence(); @@ -1364,6 +1369,11 @@ public abstract class jOOQAbstractTest< new DDLTests(this).testDropSequence(); } + @Test + public void testDropSequenceIfExists() throws Exception { + new DDLTests(this).testDropSequenceIfExists(); + } + @Test public void testAlterTableAdd() throws Exception { new DDLTests(this).testAlterTableAdd(); @@ -1389,6 +1399,11 @@ public abstract class jOOQAbstractTest< new DDLTests(this).testDropTable(); } + @Test + public void testDropTableIfExists() throws Exception { + new DDLTests(this).testDropTableIfExists(); + } + @Test public void testCreateTableAsSelect() throws Exception { new DDLTests(this).testCreateTableAsSelect(); diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 3e7bd46493..5a2b3cb8c9 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -62,6 +62,7 @@ import java.math.BigInteger; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.Collection; import java.util.List; @@ -4917,6 +4918,28 @@ public interface DSLContext { @Support DropViewFinalStep dropView(String table); + /** + * Create a new DSL DROP VIEW IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropViewIfExists(Table) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + DropViewFinalStep dropViewIfExists(Table table); + + /** + * Create a new DSL DROP VIEW IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropViewIfExists(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + DropViewFinalStep dropViewIfExists(String table); + /** * Create a new DSL DROP TABLE statement. * @@ -4933,6 +4956,28 @@ public interface DSLContext { @Support DropTableStep dropTable(String table); + /** + * Create a new DSL DROP TABLE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropTableIfExists(Table) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + DropTableStep dropTableIfExists(Table table); + + /** + * Create a new DSL ALTER TABLE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropTableIfExists(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + DropTableStep dropTableIfExists(String table); + /** * Create a new DSL DROP INDEX statement. * @@ -4941,10 +4986,21 @@ public interface DSLContext { @Support DropIndexFinalStep dropIndex(String index); + /** + * Create a new DSL DROP INDEX IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropIndexIfExists(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + DropIndexFinalStep dropIndexIfExists(String index); + /** * Create a new DSL DROP SEQUENCE statement. * - * @see DSLContext#dropSequence(Sequence) + * @see DSL#dropSequence(Sequence) */ @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) DropSequenceFinalStep dropSequence(Sequence sequence); @@ -4952,11 +5008,33 @@ public interface DSLContext { /** * Create a new DSL DROP SEQUENCE statement. * - * @see DSLContext#dropSequence(Sequence) + * @see DSL#dropSequence(String) */ @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) DropSequenceFinalStep dropSequence(String sequence); + /** + * Create a new DSL DROP SEQUENCE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropSequenceIfExists(Sequence) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + DropSequenceFinalStep dropSequenceIfExists(Sequence sequence); + + /** + * Create a new DSL DROP SEQUENCE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSL#dropSequenceIfExists(String) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + DropSequenceFinalStep dropSequenceIfExists(String sequence); + /** * Create a new DSL truncate statement. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 8cbde96edb..4245ede6ed 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -4341,6 +4341,32 @@ public class DSL { return using(new DefaultConfiguration()).dropView(table); } + /** + * Create a new DSL DROP VIEW IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropViewIfExists(Table) + */ + @Support + public static DropViewFinalStep dropViewIfExists(Table table) { + return using(new DefaultConfiguration()).dropViewIfExists(table); + } + + /** + * Create a new DSL DROP VIEW IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropViewIfExists(String) + */ + @Support + public static DropViewFinalStep dropViewIfExists(String table) { + return using(new DefaultConfiguration()).dropViewIfExists(table); + } + /** * Create a new DSL DROP TABLE statement. * @@ -4357,6 +4383,32 @@ public class DSL { * @see DSLContext#dropTable(String) */ @Support + public static DropTableStep dropTableIfExists(String table) { + return using(new DefaultConfiguration()).dropTableIfExists(table); + } + + /** + * Create a new DSL DROP TABLE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropTableIfExists(Table) + */ + @Support + public static DropTableStep dropTableIfExists(Table table) { + return using(new DefaultConfiguration()).dropTableIfExists(table); + } + + /** + * Create a new DSL DROP TABLE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropTableIfExists(String) + */ + @Support public static DropTableStep dropTable(String table) { return using(new DefaultConfiguration()).dropTable(table); } @@ -4371,6 +4423,19 @@ public class DSL { return using(new DefaultConfiguration()).dropIndex(index); } + /** + * Create a new DSL DROP INDEX IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropIndexIfExists(String) + */ + @Support + public static DropIndexFinalStep dropIndexIfExists(String index) { + return using(new DefaultConfiguration()).dropIndexIfExists(index); + } + /** * Create a new DSL DROP SEQUENCE statement. * @@ -4384,13 +4449,39 @@ public class DSL { /** * Create a new DSL DROP SEQUENCE statement. * - * @see DSLContext#dropSequence(Sequence) + * @see DSLContext#dropSequence(String) */ @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) public static DropSequenceFinalStep dropSequence(String sequence) { return using(new DefaultConfiguration()).dropSequence(sequence); } + /** + * Create a new DSL DROP SEQUENCE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropSequenceIfExists(Sequence) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + public static DropSequenceFinalStep dropSequenceIfExists(Sequence sequence) { + return using(new DefaultConfiguration()).dropSequenceIfExists(sequence); + } + + /** + * Create a new DSL DROP SEQUENCE IF EXISTS statement. + *

+ * If your database doesn't natively support IF EXISTS, this is + * emulated by catching (and ignoring) the relevant {@link SQLException}. + * + * @see DSLContext#dropSequenceIfExists(String) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + public static DropSequenceFinalStep dropSequenceIfExists(String sequence) { + return using(new DefaultConfiguration()).dropSequenceIfExists(sequence); + } + /** * Create a new DSL truncate statement. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index e258c206b0..28cd5c4475 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -1693,6 +1693,16 @@ public class DefaultDSLContext implements DSLContext, Serializable { return dropView(tableByName(table)); } + @Override + public DropViewFinalStep dropViewIfExists(Table table) { + return new DropViewImpl(configuration, table, true); + } + + @Override + public DropViewFinalStep dropViewIfExists(String table) { + return dropViewIfExists(tableByName(table)); + } + @Override public DropTableStep dropTable(Table table) { return new DropTableImpl(configuration, table); @@ -1703,11 +1713,26 @@ public class DefaultDSLContext implements DSLContext, Serializable { return dropTable(tableByName(table)); } + @Override + public DropTableStep dropTableIfExists(Table table) { + return new DropTableImpl(configuration, table, true); + } + + @Override + public DropTableStep dropTableIfExists(String table) { + return dropTableIfExists(tableByName(table)); + } + @Override public DropIndexFinalStep dropIndex(String index) { return new DropIndexImpl(configuration, index); } + @Override + public DropIndexFinalStep dropIndexIfExists(String index) { + return new DropIndexImpl(configuration, index, true); + } + @Override public DropSequenceFinalStep dropSequence(Sequence sequence) { return new DropSequenceImpl(configuration, sequence); @@ -1718,6 +1743,16 @@ public class DefaultDSLContext implements DSLContext, Serializable { return dropSequence(sequenceByName(sequence)); } + @Override + public DropSequenceFinalStep dropSequenceIfExists(Sequence sequence) { + return new DropSequenceImpl(configuration, sequence, true); + } + + @Override + public DropSequenceFinalStep dropSequenceIfExists(String sequence) { + return dropSequenceIfExists(sequenceByName(sequence)); + } + @Override public TruncateIdentityStep truncate(Table table) { return new TruncateImpl(configuration, table); diff --git a/jOOQ/src/main/java/org/jooq/impl/DropIndexImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropIndexImpl.java index 934c1f3530..3cba8cbb84 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropIndexImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropIndexImpl.java @@ -40,8 +40,19 @@ */ package org.jooq.impl; +import static java.util.Arrays.asList; import static org.jooq.Clause.DROP_INDEX; +// ... +// ... +import static org.jooq.SQLDialect.CUBRID; +// ... +import static org.jooq.SQLDialect.DERBY; +import static org.jooq.SQLDialect.FIREBIRD; +// ... +// ... +// ... import static org.jooq.impl.DSL.name; +import static org.jooq.impl.DropStatementType.INDEX; import org.jooq.Clause; import org.jooq.Configuration; @@ -62,23 +73,47 @@ class DropIndexImpl extends AbstractQuery implements private static final long serialVersionUID = 8904572826501186329L; private static final Clause[] CLAUSES = { DROP_INDEX }; - private final String index; + private final String index; + private final boolean ifExists; DropIndexImpl(Configuration configuration, String index) { + this(configuration, index, false); + } + + DropIndexImpl(Configuration configuration, String index, boolean ifExists) { super(configuration); this.index = index; + this.ifExists = ifExists; } // ------------------------------------------------------------------------ // XXX: QueryPart API // ------------------------------------------------------------------------ + private final boolean supportsIfExists(Context ctx) { + return !asList(CUBRID, DERBY, FIREBIRD).contains(ctx.family()); + } + @Override public final void accept(Context ctx) { - ctx.keyword("drop index") - .sql(" ") - .visit(name(index)); + if (ifExists && !supportsIfExists(ctx)) { + Utils.executeImmediateBegin(ctx, INDEX); + accept0(ctx); + Utils.executeImmediateEnd(ctx, INDEX); + } + else { + accept0(ctx); + } + } + + private void accept0(Context ctx) { + ctx.keyword("drop index").sql(" "); + + if (ifExists && supportsIfExists(ctx)) + ctx.keyword("if exists").sql(" "); + + ctx.visit(name(index)); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java index 829df57321..6955915d3d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java @@ -40,8 +40,19 @@ */ package org.jooq.impl; +import static java.util.Arrays.asList; import static org.jooq.Clause.DROP_SEQUENCE; import static org.jooq.Clause.DROP_SEQUENCE_SEQUENCE; +// ... +// ... +import static org.jooq.SQLDialect.CUBRID; +// ... +import static org.jooq.SQLDialect.DERBY; +import static org.jooq.SQLDialect.FIREBIRD; +// ... +// ... +// ... +import static org.jooq.impl.DropStatementType.SEQUENCE; import org.jooq.Clause; import org.jooq.Configuration; @@ -64,23 +75,47 @@ class DropSequenceImpl extends AbstractQuery implements private static final Clause[] CLAUSES = { DROP_SEQUENCE }; private final Sequence sequence; + private final boolean ifExists; DropSequenceImpl(Configuration configuration, Sequence sequence) { + this(configuration, sequence, false); + } + + DropSequenceImpl(Configuration configuration, Sequence sequence, boolean ifExists) { super(configuration); this.sequence = sequence; + this.ifExists = ifExists; } // ------------------------------------------------------------------------ // XXX: QueryPart API // ------------------------------------------------------------------------ + private final boolean supportsIfExists(Context ctx) { + return !asList(CUBRID, DERBY, FIREBIRD).contains(ctx.family()); + } + @Override public final void accept(Context ctx) { + if (ifExists && !supportsIfExists(ctx)) { + Utils.executeImmediateBegin(ctx, SEQUENCE); + accept0(ctx); + Utils.executeImmediateEnd(ctx, SEQUENCE); + } + else { + accept0(ctx); + } + } + + private void accept0(Context ctx) { ctx.start(DROP_SEQUENCE_SEQUENCE) - .keyword("drop sequence") - .sql(" ") - .visit(sequence) + .keyword("drop sequence").sql(" "); + + if (ifExists && supportsIfExists(ctx)) + ctx.keyword("if exists").sql(" "); + + ctx.visit(sequence) .end(DROP_SEQUENCE_SEQUENCE); } diff --git a/jOOQ/src/main/java/org/jooq/impl/DropStatementType.java b/jOOQ/src/main/java/org/jooq/impl/DropStatementType.java new file mode 100644 index 0000000000..22dc2e9f54 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/DropStatementType.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.impl; + +/** + * @author Lukas Eder + */ +enum DropStatementType { + INDEX, SEQUENCE, TABLE, VIEW +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java index 9b032eef07..7308c99543 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java @@ -40,8 +40,19 @@ */ package org.jooq.impl; +import static java.util.Arrays.asList; import static org.jooq.Clause.DROP_TABLE; import static org.jooq.Clause.DROP_TABLE_TABLE; +// ... +// ... +import static org.jooq.SQLDialect.CUBRID; +// ... +import static org.jooq.SQLDialect.DERBY; +import static org.jooq.SQLDialect.FIREBIRD; +// ... +// ... +// ... +import static org.jooq.impl.DropStatementType.TABLE; import org.jooq.Clause; import org.jooq.Configuration; @@ -65,12 +76,18 @@ class DropTableImpl extends AbstractQuery implements private static final Clause[] CLAUSES = { DROP_TABLE }; private final Table table; + private final boolean ifExists; private boolean cascade; DropTableImpl(Configuration configuration, Table table) { + this(configuration, table, false); + } + + DropTableImpl(Configuration configuration, Table table, boolean ifExists) { super(configuration); this.table = table; + this.ifExists = ifExists; } // ------------------------------------------------------------------------ @@ -93,11 +110,30 @@ class DropTableImpl extends AbstractQuery implements // XXX: QueryPart API // ------------------------------------------------------------------------ + private final boolean supportsIfExists(Context ctx) { + return !asList(CUBRID, DERBY, FIREBIRD).contains(ctx.family()); + } + @Override public final void accept(Context ctx) { + if (ifExists && !supportsIfExists(ctx)) { + Utils.executeImmediateBegin(ctx, TABLE); + accept0(ctx); + Utils.executeImmediateEnd(ctx, TABLE); + } + else { + accept0(ctx); + } + } + + private void accept0(Context ctx) { ctx.start(DROP_TABLE_TABLE) - .keyword("drop table").sql(" ") - .visit(table); + .keyword("drop table").sql(" "); + + if (ifExists && supportsIfExists(ctx)) + ctx.keyword("if exists").sql(" "); + + ctx.visit(table); if (cascade) { ctx.sql(" ").keyword("cascade"); diff --git a/jOOQ/src/main/java/org/jooq/impl/DropViewImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropViewImpl.java index 59ca69fdae..bd9cd461b5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropViewImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropViewImpl.java @@ -40,8 +40,19 @@ */ package org.jooq.impl; +import static java.util.Arrays.asList; import static org.jooq.Clause.DROP_VIEW; import static org.jooq.Clause.DROP_VIEW_TABLE; +// ... +// ... +import static org.jooq.SQLDialect.CUBRID; +// ... +import static org.jooq.SQLDialect.DERBY; +import static org.jooq.SQLDialect.FIREBIRD; +// ... +// ... +// ... +import static org.jooq.impl.DropStatementType.VIEW; import org.jooq.Clause; import org.jooq.Configuration; @@ -65,22 +76,47 @@ class DropViewImpl extends AbstractQuery implements private static final Clause[] CLAUSES = { DROP_VIEW }; private final Table table; + private final boolean ifExists; DropViewImpl(Configuration configuration, Table table) { + this(configuration, table, false); + } + + DropViewImpl(Configuration configuration, Table table, boolean ifExists) { super(configuration); this.table = table; + this.ifExists = ifExists; } // ------------------------------------------------------------------------ // XXX: QueryPart API // ------------------------------------------------------------------------ + private final boolean supportsIfExists(Context ctx) { + return !asList(CUBRID, DERBY, FIREBIRD).contains(ctx.family()); + } + @Override public final void accept(Context ctx) { + if (ifExists && !supportsIfExists(ctx)) { + Utils.executeImmediateBegin(ctx, VIEW); + accept0(ctx); + Utils.executeImmediateEnd(ctx, VIEW); + } + else { + accept0(ctx); + } + } + + private void accept0(Context ctx) { ctx.start(DROP_VIEW_TABLE) - .keyword("drop view").sql(" ") - .visit(table); + .keyword("drop view").sql(" "); + + if (ifExists && supportsIfExists(ctx)) + ctx.keyword("if exists").sql(" "); + + ctx.visit(table); ctx.end(DROP_VIEW_TABLE); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Utils.java b/jOOQ/src/main/java/org/jooq/impl/Utils.java index 93c35f8873..beff0cb574 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Utils.java +++ b/jOOQ/src/main/java/org/jooq/impl/Utils.java @@ -55,6 +55,10 @@ import static org.jooq.impl.DSL.getDataType; import static org.jooq.impl.DSL.nullSafe; import static org.jooq.impl.DSL.val; import static org.jooq.impl.DefaultExecuteContext.localConnection; +import static org.jooq.impl.DropStatementType.INDEX; +import static org.jooq.impl.DropStatementType.SEQUENCE; +import static org.jooq.impl.DropStatementType.TABLE; +import static org.jooq.impl.DropStatementType.VIEW; import static org.jooq.impl.Identifiers.QUOTES; import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER; import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER_ESCAPED; @@ -3328,4 +3332,121 @@ final class Utils { } } } + + /** + * Wrap a DROP .. IF EXISTS statement with + * BEGIN EXECUTE IMMEDIATE '...' EXCEPTION WHEN ... END;, if + * IF EXISTS is not supported. + */ + @SuppressWarnings("unused") + static void executeImmediateBegin(Context ctx, DropStatementType type) { + switch (ctx.family()) { + /* [pro] xx + xxxx xxxx x + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxx xxxxxxxx xxxxxxx xxx xxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx xxxx + + xxxxxx + x + + xxxx xxxxxxx x + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx xxxx + + xxxxxx + x + + xxxx xxxxxxxxxx x + xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxx + x + + xx + xxxxxxxx xxxxxxx xxxxx xx xxxxxxxxxxx xx xxxxx + + xxxxxx xxxxxxxxx xxxxxxxx + xx xxxxxxxxx xx xxxxx xx xxxxx xxxxxx xxxxxx xxxxx xxxxx xxxxxx xxxxx xxxxxxxxx xxxxx + xxx xxxxxxxxx xxxx xxxxxx + xxxx xxxx + xxx xxxxxxxxxx + xxxxxxx xxxxxxxxx xxxxxxxxx + xxxx xxxxxxxxx xxxxxxx + xx + + xx [/pro] */ + + case FIREBIRD: { + ctx.keyword("execute block").formatSeparator() + .keyword("as").formatSeparator() + .keyword("begin").formatIndentStart().formatSeparator() + .keyword("execute statement").sql(" '"); + + break; + } + + default: + break; + } + } + + /** + * Wrap a DROP .. IF EXISTS statement with + * BEGIN EXECUTE IMMEDIATE '...' EXCEPTION WHEN ... END;, if + * IF EXISTS is not supported. + */ + static void executeImmediateEnd(Context ctx, DropStatementType type) { + switch (ctx.family()) { + /* [pro] xx + xxxx xxxx x + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxx + + xxxxxx + x + + xxxx xxxxxxx x + xxxxxx xxx x + xxxx xx xxxxx x xxxxxxxxxxx + x xxxx xx xxxxxxxx x xxxxxxxxxxx + x xxxx xx xxxxx x xxxxxxxxxxx + x xxxx xx xxxx x xxxxxxxxxxx + x xxxxxxxxxxxx + + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxx xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx xx x xxx x xxx xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxxx + + xxxxxx + x + + xxxx xxxxxxxxxx x + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxx xx xxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxx xxxxxxxx + + xxxxxx + x + + xx [/pro] */ + + case FIREBIRD: { + ctx.sql("';").formatSeparator() + .keyword("when").sql(" sqlcode -607 ").keyword("do").formatIndentStart().formatSeparator() + .keyword("begin end").formatIndentEnd().formatIndentEnd().formatSeparator() + .keyword("end"); + + break; + } + + default: + break; + } + } }