[#883] Add support for DROP TABLE
This commit is contained in:
parent
c5e4e72a08
commit
2161b80782
@ -166,8 +166,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
create().alterTable("t").add("h", SQLDataType.INTEGER.nullable(false)).execute();
|
||||
}
|
||||
finally {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("drop table t");
|
||||
create().dropTable("t").execute();
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,8 +186,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
catch (DataAccessException expected) {}
|
||||
}
|
||||
finally {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("drop table t");
|
||||
create().dropTable("t").execute();
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,8 +200,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertEquals("empty", create().fetchOne("select b from t").getValue(0));
|
||||
}
|
||||
finally {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("drop table t");
|
||||
create().dropTable("t").execute();
|
||||
}
|
||||
}
|
||||
|
||||
@ -221,8 +218,22 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertEquals(asList(1), asList(create().fetchOne(table("t")).intoArray()));
|
||||
}
|
||||
finally {
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("drop table t");
|
||||
create().dropTable("t").execute();
|
||||
}
|
||||
}
|
||||
|
||||
public void testDropTable() throws Exception {
|
||||
|
||||
// TODO: Re-use jOOQ API for this
|
||||
create().execute("create table t (a int, b int, c int)");
|
||||
create().execute("insert into t values (1, 2, 3)");
|
||||
assertEquals(asList(1, 2, 3), asList(create().fetchOne(table("t")).intoArray()));
|
||||
|
||||
create().dropTable("t").execute();
|
||||
try {
|
||||
create().fetch(table("t"));
|
||||
fail();
|
||||
}
|
||||
catch (DataAccessException expected) {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1163,6 +1163,11 @@ public abstract class jOOQAbstractTest<
|
||||
new DDLTests(this).testAlterTableDrop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDropTable() throws Exception {
|
||||
new DDLTests(this).testDropTable();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTruncate() throws Exception {
|
||||
new TruncateTests(this).testTruncate();
|
||||
|
||||
@ -800,7 +800,7 @@ public enum Clause {
|
||||
ALTER_SEQUENCE_RESTART,
|
||||
|
||||
/**
|
||||
* A complete <code>ALTER ALTER_TABLE</code> statement.
|
||||
* A complete <code>ALTER TABLE</code> statement.
|
||||
*/
|
||||
ALTER_TABLE,
|
||||
|
||||
@ -848,6 +848,22 @@ public enum Clause {
|
||||
*/
|
||||
ALTER_TABLE_DROP,
|
||||
|
||||
/**
|
||||
* A complete <code>DROP TABLE</code> statement.
|
||||
*/
|
||||
DROP_TABLE,
|
||||
|
||||
/**
|
||||
* A <code>TABLE</code> clause within an {@link #DROP_TABLE} statement.
|
||||
* <p>
|
||||
* This clause surrounds
|
||||
* <ul>
|
||||
* <li>the <code>DROP TABLE</code> keywords</li>
|
||||
* <li>the table that is being dropped</li>
|
||||
* </ul>
|
||||
*/
|
||||
DROP_TABLE_TABLE,
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Other clauses
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -4770,7 +4770,7 @@ public interface DSLContext {
|
||||
/**
|
||||
* Create a new DSL <code>ALTER SEQUENCE</code> statement.
|
||||
*
|
||||
* @see DSL#alterSequence(Sequence)
|
||||
* @see DSL#alterSequence(String)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
@Transition(
|
||||
@ -4794,7 +4794,7 @@ public interface DSLContext {
|
||||
/**
|
||||
* Create a new DSL <code>ALTER TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#alterTable(Table)
|
||||
* @see DSL#alterTable(String)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
@ -4803,6 +4803,30 @@ public interface DSLContext {
|
||||
)
|
||||
AlterTableStep alterTable(String table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#dropTable(Table)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
name = "DROP TABLE",
|
||||
args = "Table"
|
||||
)
|
||||
DropTableStep dropTable(Table<?> table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>ALTER TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#dropTable(String)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
name = "DROP TABLE",
|
||||
args = "Table"
|
||||
)
|
||||
DropTableStep dropTable(String table);
|
||||
|
||||
/**
|
||||
* Create a new DSL truncate statement.
|
||||
* <p>
|
||||
|
||||
50
jOOQ/src/main/java/org/jooq/DropTableFinalStep.java
Normal file
50
jOOQ/src/main/java/org/jooq/DropTableFinalStep.java
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* The final step in the <code>DROP TABLE</code> DSL.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface DropTableFinalStep extends Query {
|
||||
|
||||
}
|
||||
65
jOOQ/src/main/java/org/jooq/DropTableStep.java
Normal file
65
jOOQ/src/main/java/org/jooq/DropTableStep.java
Normal file
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
import org.jooq.api.annotation.State;
|
||||
|
||||
/**
|
||||
* The step in the <code>DROP TABLE</code> DSL used to specify <code>DROP</code>
|
||||
* behaviour.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
@State
|
||||
public interface DropTableStep extends DropTableFinalStep {
|
||||
|
||||
/**
|
||||
* Add a <code>CASCADE</code> clause to the <code>DROP TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
DropTableFinalStep cascade();
|
||||
|
||||
/**
|
||||
* Add a <code>RESTRICT</code> clause to the <code>DROP TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
DropTableFinalStep restrict();
|
||||
}
|
||||
@ -65,7 +65,7 @@ import org.jooq.Table;
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
// Cascading interface implementations for AlterSequence behaviour
|
||||
// Cascading interface implementations for ALTER TABLE behaviour
|
||||
AlterTableStep,
|
||||
AlterTableDropStep,
|
||||
AlterTableAlterStep {
|
||||
|
||||
@ -92,6 +92,7 @@ import org.jooq.DatePart;
|
||||
import org.jooq.Delete;
|
||||
import org.jooq.DeleteWhereStep;
|
||||
import org.jooq.DerivedColumnList;
|
||||
import org.jooq.DropTableStep;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.GroupConcatOrderByStep;
|
||||
import org.jooq.GroupField;
|
||||
@ -4383,7 +4384,7 @@ public class DSL {
|
||||
/**
|
||||
* Create a new DSL <code>ALTER SEQUENCE</code> statement.
|
||||
*
|
||||
* @see DSLContext#alterSequence(Sequence)
|
||||
* @see DSLContext#alterSequence(String)
|
||||
*/
|
||||
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
|
||||
@Transition(
|
||||
@ -4397,7 +4398,7 @@ public class DSL {
|
||||
/**
|
||||
* Create a new DSL <code>ALTER TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#alterTable(Table)
|
||||
* @see DSLContext#alterTable(Table)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
@ -4411,7 +4412,7 @@ public class DSL {
|
||||
/**
|
||||
* Create a new DSL <code>ALTER TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#alterTable(Table)
|
||||
* @see DSLContext#alterTable(String)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
@ -4422,6 +4423,34 @@ public class DSL {
|
||||
return using(new DefaultConfiguration()).alterTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#dropTable(Table)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
name = "ALTER TABLE",
|
||||
args = "Table"
|
||||
)
|
||||
public static DropTableStep dropTable(Table<?> table) {
|
||||
return using(new DefaultConfiguration()).dropTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>DROP TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#dropTable(String)
|
||||
*/
|
||||
@Support
|
||||
@Transition(
|
||||
name = "ALTER TABLE",
|
||||
args = "Table"
|
||||
)
|
||||
public static DropTableStep dropTable(String table) {
|
||||
return using(new DefaultConfiguration()).dropTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL truncate statement.
|
||||
* <p>
|
||||
|
||||
@ -86,6 +86,7 @@ import org.jooq.DSLContext;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.DeleteQuery;
|
||||
import org.jooq.DeleteWhereStep;
|
||||
import org.jooq.DropTableStep;
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.ExecuteListener;
|
||||
import org.jooq.Field;
|
||||
@ -1576,6 +1577,16 @@ public class DefaultDSLContext implements DSLContext, Serializable {
|
||||
return alterTable(table(table));
|
||||
}
|
||||
|
||||
@Override
|
||||
public DropTableStep dropTable(Table<?> table) {
|
||||
return new DropTableImpl(configuration, table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DropTableStep dropTable(String table) {
|
||||
return dropTable(table(table));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R extends Record> TruncateIdentityStep<R> truncate(Table<R> table) {
|
||||
return new TruncateImpl<R>(configuration, table);
|
||||
|
||||
114
jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java
Normal file
114
jOOQ/src/main/java/org/jooq/impl/DropTableImpl.java
Normal file
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
import static org.jooq.Clause.DROP_TABLE;
|
||||
import static org.jooq.Clause.DROP_TABLE_TABLE;
|
||||
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.DropTableStep;
|
||||
import org.jooq.Table;
|
||||
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class DropTableImpl extends AbstractQuery implements
|
||||
|
||||
// Cascading interface implementations for DROP TABLE behaviour
|
||||
DropTableStep {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 8904572826501186329L;
|
||||
private static final Clause[] CLAUSES = { DROP_TABLE };
|
||||
|
||||
private final Table<?> table;
|
||||
private boolean cascade;
|
||||
|
||||
DropTableImpl(Configuration configuration, Table<?> table) {
|
||||
super(configuration);
|
||||
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// XXX: DSL API
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public final DropTableImpl cascade() {
|
||||
cascade = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DropTableImpl restrict() {
|
||||
cascade = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// XXX: QueryPart API
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.start(DROP_TABLE_TABLE)
|
||||
.keyword("drop table").sql(" ")
|
||||
.visit(table);
|
||||
|
||||
if (cascade) {
|
||||
ctx.sql(" ").keyword("cascade");
|
||||
}
|
||||
|
||||
ctx.end(DROP_TABLE_TABLE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Clause[] clauses(Context<?> ctx) {
|
||||
return CLAUSES;
|
||||
}
|
||||
}
|
||||
@ -67,6 +67,8 @@ import static org.jooq.Clause.CONDITION_OR;
|
||||
import static org.jooq.Clause.DELETE;
|
||||
import static org.jooq.Clause.DELETE_DELETE;
|
||||
import static org.jooq.Clause.DELETE_WHERE;
|
||||
import static org.jooq.Clause.DROP_TABLE;
|
||||
import static org.jooq.Clause.DROP_TABLE_TABLE;
|
||||
import static org.jooq.Clause.FIELD;
|
||||
import static org.jooq.Clause.FIELD_ALIAS;
|
||||
import static org.jooq.Clause.FIELD_REFERENCE;
|
||||
@ -805,6 +807,17 @@ public class VisitContextTest extends AbstractTest {
|
||||
ctx.alterTable(TABLE1).drop(FIELD_NAME1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_DROP_TABLE() {
|
||||
assertEvents(asList(
|
||||
asList(DROP_TABLE),
|
||||
asList(DROP_TABLE, DROP_TABLE_TABLE),
|
||||
asList(DROP_TABLE, DROP_TABLE_TABLE, TABLE),
|
||||
asList(DROP_TABLE, DROP_TABLE_TABLE, TABLE, TABLE_REFERENCE)
|
||||
),
|
||||
ctx.dropTable(TABLE1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_CONDITION_simple() {
|
||||
assertEvents(asList(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user