From 1bce6cd55a6a4f62ef915df69fddd1dd83d6e24c Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 11 Jul 2014 10:54:26 +0200 Subject: [PATCH] [#3355] Add support for DROP SEQUENCE statements --- .../org/jooq/test/all/testcases/DDLTests.java | 32 ++++++- .../java/org/jooq/test/jOOQAbstractTest.java | 10 ++ jOOQ/src/main/java/org/jooq/Clause.java | 22 +++++ .../org/jooq/CreateSequenceFinalStep.java | 50 ++++++++++ jOOQ/src/main/java/org/jooq/DSLContext.java | 16 ++++ .../org/jooq/impl/CreateSequenceImpl.java | 91 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 21 +++++ .../java/org/jooq/impl/DefaultDSLContext.java | 11 +++ .../java/org/jooq/impl/DropSequenceImpl.java | 4 +- .../java/org/jooq/test/VisitContextTest.java | 13 +++ 10 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/CreateSequenceFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/CreateSequenceImpl.java 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 3e7904b526..c0e7b3661b 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 @@ -48,7 +48,9 @@ import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.table; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.junit.Assume.assumeNotNull; +import java.math.BigInteger; import java.sql.Date; import org.jooq.Record1; @@ -124,12 +126,34 @@ extends BaseTest S_AUTHOR_ID = (Sequence) SAuthorID(); 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 a5a746dae1..95afffcec2 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java @@ -1299,11 +1299,21 @@ public abstract class jOOQAbstractTest< new DDLTests(this).testDropIndex(); } + @Test + public void testCreateSequence() throws Exception { + new DDLTests(this).testCreateSequence(); + } + @Test public void testAlterSequence() throws Exception { new DDLTests(this).testAlterSequence(); } + @Test + public void testDropSequence() throws Exception { + new DDLTests(this).testDropSequence(); + } + @Test public void testAlterTableAdd() throws Exception { new DDLTests(this).testAlterTableAdd(); diff --git a/jOOQ/src/main/java/org/jooq/Clause.java b/jOOQ/src/main/java/org/jooq/Clause.java index 2299654767..162e0b9770 100644 --- a/jOOQ/src/main/java/org/jooq/Clause.java +++ b/jOOQ/src/main/java/org/jooq/Clause.java @@ -777,6 +777,22 @@ public enum Clause { */ CREATE_INDEX, + /** + * A complete CREATE SEQUENCE statement. + */ + CREATE_SEQUENCE, + + /** + * A SEQUENCE clause within a {@link #CREATE_SEQUENCE} statement. + *

+ * This clause surrounds + *

    + *
  • the CREATE SEQUENCE keywords
  • + *
  • the sequence that is being created
  • + *
+ */ + CREATE_SEQUENCE_SEQUENCE, + /** * A complete ALTER SEQUENCE statement. */ @@ -892,6 +908,12 @@ public enum Clause { /** * A SEQUENCE clause within a {@link #DROP_SEQUENCE} statement. + *

+ * This clause surrounds + *

    + *
  • the DROP SEQUENCE keywords
  • + *
  • the sequence that is being dropped
  • + *
*/ DROP_SEQUENCE_SEQUENCE, diff --git a/jOOQ/src/main/java/org/jooq/CreateSequenceFinalStep.java b/jOOQ/src/main/java/org/jooq/CreateSequenceFinalStep.java new file mode 100644 index 0000000000..9831a05a5b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateSequenceFinalStep.java @@ -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; + +/** + * A {@link Query} that can create sequences. + * + * @author Lukas Eder + */ +public interface CreateSequenceFinalStep extends Query { + +} diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 7b6a79e944..28611c323d 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -4654,6 +4654,22 @@ public interface DSLContext { @Support CreateIndexStep createIndex(String index); + /** + * Create a new DSL CREATE SEQUENCE statement. + * + * @see DSL#createSequence(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES }) + CreateSequenceFinalStep createSequence(Sequence sequence); + + /** + * Create a new DSL CREATE SEQUENCE statement. + * + * @see DSL#createSequence(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES }) + CreateSequenceFinalStep createSequence(String sequence); + /** * Create a new DSL ALTER SEQUENCE statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateSequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateSequenceImpl.java new file mode 100644 index 0000000000..451da5cc2b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CreateSequenceImpl.java @@ -0,0 +1,91 @@ +/** + * 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.CREATE_SEQUENCE; +import static org.jooq.Clause.CREATE_SEQUENCE_SEQUENCE; + +import org.jooq.Clause; +import org.jooq.Configuration; +import org.jooq.Context; +import org.jooq.CreateSequenceFinalStep; +import org.jooq.Sequence; + +/** + * @author Lukas Eder + */ +class CreateSequenceImpl extends AbstractQuery implements + + // Cascading interface implementations for CREATE SEQUENCE behaviour + CreateSequenceFinalStep { + + /** + * Generated UID + */ + private static final long serialVersionUID = 8904572826501186329L; + private static final Clause[] CLAUSES = { CREATE_SEQUENCE }; + + private final Sequence sequence; + + CreateSequenceImpl(Configuration configuration, Sequence sequence) { + super(configuration); + + this.sequence = sequence; + } + + // ------------------------------------------------------------------------ + // XXX: QueryPart API + // ------------------------------------------------------------------------ + + @Override + public final void accept(Context ctx) { + ctx.start(CREATE_SEQUENCE_SEQUENCE) + .keyword("create sequence") + .sql(" ") + .visit(sequence) + .end(CREATE_SEQUENCE_SEQUENCE); + } + + @Override + public final Clause[] clauses(Context ctx) { + return CLAUSES; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index b240ff51c8..f1f986318c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -90,6 +90,7 @@ import org.jooq.Condition; import org.jooq.Configuration; import org.jooq.ConnectionProvider; import org.jooq.CreateIndexStep; +import org.jooq.CreateSequenceFinalStep; import org.jooq.DSLContext; import org.jooq.DataType; import org.jooq.DatePart; @@ -4214,6 +4215,26 @@ public class DSL { return using(new DefaultConfiguration()).createIndex(index); } + /** + * Create a new DSL CREATE SEQUENCE statement. + * + * @see DSLContext#createSequence(Sequence) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES }) + public static CreateSequenceFinalStep createSequence(Sequence sequence) { + return using(new DefaultConfiguration()).createSequence(sequence); + } + + /** + * Create a new DSL CREATE SEQUENCE statement. + * + * @see DSLContext#createSequence(String) + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES }) + public static CreateSequenceFinalStep createSequence(String sequence) { + return using(new DefaultConfiguration()).createSequence(sequence); + } + /** * Create a new DSL ALTER SEQUENCE statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index f7e837bd28..72bffec44d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -82,6 +82,7 @@ import org.jooq.Condition; import org.jooq.Configuration; import org.jooq.ConnectionProvider; import org.jooq.CreateIndexStep; +import org.jooq.CreateSequenceFinalStep; import org.jooq.Cursor; import org.jooq.DSLContext; import org.jooq.DataType; @@ -1578,6 +1579,16 @@ public class DefaultDSLContext implements DSLContext, Serializable { return new CreateIndexImpl(configuration, index); } + @Override + public CreateSequenceFinalStep createSequence(Sequence sequence) { + return new CreateSequenceImpl(configuration, sequence); + } + + @Override + public CreateSequenceFinalStep createSequence(String sequence) { + return createSequence(sequence(sequence)); + } + @Override public AlterSequenceRestartStep alterSequence(Sequence sequence) { return new AlterSequenceImpl(configuration, sequence); diff --git a/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java index 9c8deed290..829df57321 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DropSequenceImpl.java @@ -77,9 +77,9 @@ class DropSequenceImpl extends AbstractQuery implements @Override public final void accept(Context ctx) { - ctx.keyword("drop sequence") + ctx.start(DROP_SEQUENCE_SEQUENCE) + .keyword("drop sequence") .sql(" ") - .start(DROP_SEQUENCE_SEQUENCE) .visit(sequence) .end(DROP_SEQUENCE_SEQUENCE); } diff --git a/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java b/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java index e8fedc0d71..5e9b233b1b 100644 --- a/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java +++ b/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java @@ -66,6 +66,8 @@ import static org.jooq.Clause.CONDITION_NOT_EXISTS; import static org.jooq.Clause.CONDITION_NOT_IN; import static org.jooq.Clause.CONDITION_OR; import static org.jooq.Clause.CREATE_INDEX; +import static org.jooq.Clause.CREATE_SEQUENCE; +import static org.jooq.Clause.CREATE_SEQUENCE_SEQUENCE; import static org.jooq.Clause.DELETE; import static org.jooq.Clause.DELETE_DELETE; import static org.jooq.Clause.DELETE_WHERE; @@ -763,6 +765,17 @@ public class VisitContextTest extends AbstractTest { ctx.dropIndex("i")); } + @Test + public void test_CREATE_SEQUENCE() { + assertEvents(asList( + asList(CREATE_SEQUENCE), + asList(CREATE_SEQUENCE, CREATE_SEQUENCE_SEQUENCE), + asList(CREATE_SEQUENCE, CREATE_SEQUENCE_SEQUENCE, SEQUENCE), + asList(CREATE_SEQUENCE, CREATE_SEQUENCE_SEQUENCE, SEQUENCE, SEQUENCE_REFERENCE) + ), + ctx.createSequence("i")); + } + @Test public void test_ALTER_SEQUENCE() {