[#3532] Add support for DROP ... IF EXISTS clauses in DDL
This commit is contained in:
parent
bb2181030e
commit
e89f310cfa
@ -111,7 +111,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
}
|
||||
finally {
|
||||
create().dropView(tableByName("v1")).execute();
|
||||
create().dropView(tableByName("v2")).execute();
|
||||
|
||||
if (asList().contains(dialect().family())) {
|
||||
create().dropView(tableByName("v2")).execute();
|
||||
}
|
||||
else {
|
||||
create().dropViewIfExists(tableByName("v2")).execute();
|
||||
create().dropViewIfExists(tableByName("v2")).execute();
|
||||
}
|
||||
|
||||
assertThrows(DataAccessException.class, () -> {
|
||||
create().fetch("select * from {0}", name("v1"));
|
||||
@ -157,6 +164,23 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
}
|
||||
}
|
||||
|
||||
public void testDropIndexIfExists() throws Exception {
|
||||
assumeFamilyNotIn();
|
||||
|
||||
try {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("create table {0} ({1} int, {2} int)", name("t"), name("a"), name("b"));
|
||||
create().createIndex("idx1").on("t", "a").execute();
|
||||
create().dropIndexIfExists("idx1").execute();
|
||||
create().dropIndexIfExists("idx1").execute();
|
||||
create().createIndex("idx1").on("t", "a").execute();
|
||||
create().dropIndexIfExists("idx2").execute();
|
||||
}
|
||||
finally {
|
||||
ignoreThrows(() -> create().dropTable("t").execute());
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateSequence() throws Exception {
|
||||
assumeNotNull(cSequences());
|
||||
|
||||
@ -181,6 +205,21 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
catch (DataAccessException expected) {}
|
||||
}
|
||||
|
||||
public void testDropSequenceIfExists() throws Exception {
|
||||
assumeFamilyNotIn();
|
||||
|
||||
assumeNotNull(SAuthorID());
|
||||
|
||||
create().dropSequenceIfExists(SAuthorID()).execute();
|
||||
create().dropSequenceIfExists(SAuthorID()).execute();
|
||||
|
||||
try {
|
||||
create().nextval(SAuthorID());
|
||||
fail();
|
||||
}
|
||||
catch (DataAccessException expected) {}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testAlterSequence() throws Exception {
|
||||
assumeNotNull(cSequences());
|
||||
@ -315,6 +354,22 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
catch (DataAccessException expected) {}
|
||||
}
|
||||
|
||||
public void testDropTableIfExists() throws Exception {
|
||||
assumeFamilyNotIn();
|
||||
|
||||
try {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("create table {0} ({1} int, {2} int)", name("t"), name("a"), name("b"));
|
||||
create().dropTableIfExists("t").execute();
|
||||
create().dropTableIfExists("t").execute();
|
||||
create().execute("create table {0} ({1} int, {2} int)", name("t"), name("a"), name("b"));
|
||||
create().dropTableIfExists("t2").execute();
|
||||
}
|
||||
finally {
|
||||
ignoreThrows(() -> create().dropTable("t").execute());
|
||||
}
|
||||
}
|
||||
|
||||
private String varchar() {
|
||||
return SQLDataType.VARCHAR.length(10).getCastTypeName(create().configuration());
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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 <code>DROP VIEW IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP VIEW IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP TABLE</code> statement.
|
||||
*
|
||||
@ -4933,6 +4956,28 @@ public interface DSLContext {
|
||||
@Support
|
||||
DropTableStep dropTable(String table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP TABLE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>ALTER TABLE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP INDEX</code> statement.
|
||||
*
|
||||
@ -4941,10 +4986,21 @@ public interface DSLContext {
|
||||
@Support
|
||||
DropIndexFinalStep dropIndex(String index);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP INDEX IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP SEQUENCE</code> 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 <code>DROP SEQUENCE</code> statement.
|
||||
*
|
||||
* @see DSLContext#dropSequence(Sequence)
|
||||
* @see DSL#dropSequence(String)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
DropSequenceFinalStep dropSequence(String sequence);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP SEQUENCE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP SEQUENCE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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.
|
||||
* <p>
|
||||
|
||||
@ -4341,6 +4341,32 @@ public class DSL {
|
||||
return using(new DefaultConfiguration()).dropView(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP VIEW IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP VIEW IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP TABLE</code> 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 <code>DROP TABLE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP TABLE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP INDEX IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, 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 <code>DROP SEQUENCE</code> statement.
|
||||
*
|
||||
@ -4384,13 +4449,39 @@ public class DSL {
|
||||
/**
|
||||
* Create a new DSL <code>DROP SEQUENCE</code> statement.
|
||||
*
|
||||
* @see DSLContext#dropSequence(Sequence)
|
||||
* @see DSLContext#dropSequence(String)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
public static <T extends Number> DropSequenceFinalStep dropSequence(String sequence) {
|
||||
return using(new DefaultConfiguration()).dropSequence(sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP SEQUENCE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, this is
|
||||
* emulated by catching (and ignoring) the relevant {@link SQLException}.
|
||||
*
|
||||
* @see DSLContext#dropSequenceIfExists(Sequence)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
public static <T extends Number> DropSequenceFinalStep dropSequenceIfExists(Sequence<?> sequence) {
|
||||
return using(new DefaultConfiguration()).dropSequenceIfExists(sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP SEQUENCE IF EXISTS</code> statement.
|
||||
* <p>
|
||||
* If your database doesn't natively support <code>IF EXISTS</code>, this is
|
||||
* emulated by catching (and ignoring) the relevant {@link SQLException}.
|
||||
*
|
||||
* @see DSLContext#dropSequenceIfExists(String)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
public static <T extends Number> DropSequenceFinalStep dropSequenceIfExists(String sequence) {
|
||||
return using(new DefaultConfiguration()).dropSequenceIfExists(sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL truncate statement.
|
||||
* <p>
|
||||
|
||||
@ -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 <R extends Record> TruncateIdentityStep<R> truncate(Table<R> table) {
|
||||
return new TruncateImpl<R>(configuration, table);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
48
jOOQ/src/main/java/org/jooq/impl/DropStatementType.java
Normal file
48
jOOQ/src/main/java/org/jooq/impl/DropStatementType.java
Normal file
@ -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
|
||||
}
|
||||
@ -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");
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 <code>DROP .. IF EXISTS</code> statement with
|
||||
* <code>BEGIN EXECUTE IMMEDIATE '...' EXCEPTION WHEN ... END;</code>, if
|
||||
* <code>IF EXISTS</code> 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 <code>DROP .. IF EXISTS</code> statement with
|
||||
* <code>BEGIN EXECUTE IMMEDIATE '...' EXCEPTION WHEN ... END;</code>, if
|
||||
* <code>IF EXISTS</code> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user