[#3355] Add support for DROP SEQUENCE statements

This commit is contained in:
Lukas Eder 2014-07-11 10:54:26 +02:00
parent c742587add
commit 1bce6cd55a
10 changed files with 264 additions and 6 deletions

View File

@ -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<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
}
public void testCreateSequence() throws Exception {
assumeNotNull(cSequences());
try {
create().createSequence("s").execute();
assertEquals(BigInteger.ONE, create().nextval("s"));
}
finally {
create().dropSequence("s").execute();
}
}
public void testDropSequence() throws Exception {
assumeNotNull(SAuthorID());
try {
create().dropSequence(SAuthorID()).execute();
create().nextval(SAuthorID());
fail();
}
catch (DataAccessException expected) {}
}
@SuppressWarnings("unchecked")
public void testAlterSequence() throws Exception {
if (cSequences() == null || asList(DERBY).contains(dialect().family())) {
log.info("SKIPPING", "Skipping ALTER SEQUENCE test");
return;
}
assumeNotNull(cSequences());
assumeFamilyNotIn(DERBY);
jOOQAbstractTest.reset = false;
Sequence<Number> S_AUTHOR_ID = (Sequence<Number>) SAuthorID();

View File

@ -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();

View File

@ -777,6 +777,22 @@ public enum Clause {
*/
CREATE_INDEX,
/**
* A complete <code>CREATE SEQUENCE</code> statement.
*/
CREATE_SEQUENCE,
/**
* A <code>SEQUENCE</code> clause within a {@link #CREATE_SEQUENCE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>CREATE SEQUENCE</code> keywords</li>
* <li>the sequence that is being created</li>
* </ul>
*/
CREATE_SEQUENCE_SEQUENCE,
/**
* A complete <code>ALTER SEQUENCE</code> statement.
*/
@ -892,6 +908,12 @@ public enum Clause {
/**
* A <code>SEQUENCE</code> clause within a {@link #DROP_SEQUENCE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>DROP SEQUENCE</code> keywords</li>
* <li>the sequence that is being dropped</li>
* </ul>
*/
DROP_SEQUENCE_SEQUENCE,

View 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;
/**
* A {@link Query} that can create sequences.
*
* @author Lukas Eder
*/
public interface CreateSequenceFinalStep extends Query {
}

View File

@ -4654,6 +4654,22 @@ public interface DSLContext {
@Support
CreateIndexStep createIndex(String index);
/**
* Create a new DSL <code>CREATE SEQUENCE</code> statement.
*
* @see DSL#createSequence(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES })
CreateSequenceFinalStep createSequence(Sequence<?> sequence);
/**
* Create a new DSL <code>CREATE SEQUENCE</code> statement.
*
* @see DSL#createSequence(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES })
CreateSequenceFinalStep createSequence(String sequence);
/**
* Create a new DSL <code>ALTER SEQUENCE</code> statement.
*

View File

@ -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;
}
}

View File

@ -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 <code>CREATE SEQUENCE</code> 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 <code>CREATE SEQUENCE</code> 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 <code>ALTER SEQUENCE</code> statement.
*

View File

@ -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 <T extends Number> AlterSequenceRestartStep<T> alterSequence(Sequence<T> sequence) {
return new AlterSequenceImpl<T>(configuration, sequence);

View File

@ -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);
}

View File

@ -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() {