[jOOQ/jOOQ#7752] Add common flags to Sequence type

Adds the following methods to the `Sequence` interface:
`getStartWith()`, `getIncrementBy()`, `getMinValue()`, `getMaxValue()`,
`getCycle()`, and `getCache()`.

The `Sequence` objects returned by the `DDLInterpreter` now return the
expected values when these methods are called. And `DDL` (aka
`DSLContext#ddl()`) queries these methods to create a corresponding
`CREATE SEQUENCE` query.
This commit is contained in:
Knut Wannheden 2019-10-24 19:59:50 +02:00
parent 3896681de6
commit 5949f1c0fa
5 changed files with 327 additions and 17 deletions

View File

@ -0,0 +1,224 @@
/*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;

View File

@ -80,6 +80,36 @@ public interface Sequence<T extends Number> extends Named {
*/
DataType<T> getDataType();
/**
* Get the start value for this sequence
*/
Field<T> getStartWith();
/**
* Get the increment for this sequence
*/
Field<T> getIncrementBy();
/**
* Get the minimum value for this sequence
*/
Field<T> getMinValue();
/**
* Get the maximum value for this sequence
*/
Field<T> getMaxValue();
/**
* Returns {@code true} if this sequence cycles when it reaches {@link #getMaxValue()}
*/
boolean getCycle();
/**
* Get the number of sequence values to cache for this sequence
*/
Field<T> getCache();
/**
* Get the current value of this sequence
*/
@ -91,4 +121,4 @@ public interface Sequence<T extends Number> extends Named {
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
Field<T> nextval();
}
}

View File

@ -54,6 +54,7 @@ import java.util.Collections;
import java.util.List;
import org.jooq.Constraint;
import org.jooq.CreateSequenceFlagsStep;
import org.jooq.DDLExportConfiguration;
import org.jooq.DDLFlag;
import org.jooq.DSLContext;
@ -94,9 +95,22 @@ final class DDL {
}
private final Query createSequence(Sequence<?> sequence) {
return configuration.createSequenceIfNotExists()
CreateSequenceFlagsStep result = configuration.createSequenceIfNotExists()
? ctx.createSequenceIfNotExists(sequence)
: ctx.createSequence(sequence);
if (sequence.getStartWith() != null)
result = result.startWith(sequence.getStartWith());
if (sequence.getIncrementBy() != null)
result = result.incrementBy(sequence.getIncrementBy());
if (sequence.getMinValue() != null)
result = result.minvalue(sequence.getMinValue());
if (sequence.getMaxValue() != null)
result = result.maxvalue(sequence.getMaxValue());
if (sequence.getCycle())
result = result.cycle();
if (sequence.getCache() != null)
result = result.cache(sequence.getCache());
return result;
}
private final Query createTable(Table<?> table) {

View File

@ -622,14 +622,10 @@ final class DDLInterpreter {
ms.startWith = query.$startWith();
ms.incrementBy = query.$incrementBy();
ms.minValue = query.$minvalue();
ms.noMinvalue = query.$noMinvalue();
ms.maxValue = query.$maxvalue();
ms.noMaxvalue = query.$noMaxvalue();
ms.minValue = query.$noMinvalue() ? null : query.$minvalue();
ms.maxValue = query.$noMaxvalue() ? null : query.$maxvalue();
ms.cycle = query.$cycle();
ms.noCache = query.$noCycle();
ms.cache = query.$cache();
ms.noCache = query.$noCache();
ms.cache = query.$noCache() ? null : query.$cache();
}
private final void accept0(AlterSequenceImpl<?> query) {
@ -1293,13 +1289,9 @@ final class DDLInterpreter {
MutableSchema schema;
Field<?> startWith;
Field<?> incrementBy;
boolean noMinvalue;
Field<?> minValue;
boolean noMaxvalue;
Field<?> maxValue;
boolean noCycle;
boolean cycle;
boolean noCache;
Field<?> cache;
MutableSequence(UnqualifiedName name, MutableSchema schema) {
@ -1310,8 +1302,10 @@ final class DDLInterpreter {
}
private final class InterpretedSequence extends SequenceImpl<Long> {
@SuppressWarnings("unchecked")
InterpretedSequence(Schema schema) {
super(MutableSequence.this.name, schema, BIGINT, false);
super(MutableSequence.this.name, schema, BIGINT, false,
(Field<Long>) startWith, (Field<Long>) incrementBy, (Field<Long>) minValue, (Field<Long>) maxValue, cycle, (Field<Long>) cache);
// [#7752] TODO: Pass additional flags like START WITH to
// SequenceImpl when this is ready.

View File

@ -82,9 +82,15 @@ public class SequenceImpl<T extends Number> extends AbstractNamed implements Seq
private static final long serialVersionUID = 6224349401603636427L;
private static final Clause[] CLAUSES = { SEQUENCE, SEQUENCE_REFERENCE };
final boolean nameIsPlainSQL;
final Schema schema;
final DataType<T> type;
private final boolean nameIsPlainSQL;
private final Schema schema;
private final DataType<T> type;
private final Field<T> startWith;
private final Field<T> incrementBy;
private final Field<T> minValue;
private final Field<T> maxValue;
private final boolean cycle;
private final Field<T> cache;
public SequenceImpl(String name, Schema schema, DataType<T> type) {
this(name, schema, type, false);
@ -95,11 +101,23 @@ public class SequenceImpl<T extends Number> extends AbstractNamed implements Seq
}
SequenceImpl(Name name, Schema schema, DataType<T> type, boolean nameIsPlainSQL) {
this(name, schema, type, nameIsPlainSQL, null, null, null, null, false, null);
}
SequenceImpl(Name name, Schema schema, DataType<T> type, boolean nameIsPlainSQL,
Field<T> startWith, Field<T> incrementBy, Field<T> minValue, Field<T> maxValue, boolean cycle, Field<T> cache) {
super(qualify(schema, name), CommentImpl.NO_COMMENT);
this.schema = schema;
this.type = type;
this.nameIsPlainSQL = nameIsPlainSQL;
this.startWith = startWith;
this.incrementBy = incrementBy;
this.minValue = minValue;
this.maxValue = maxValue;
this.cycle = cycle;
this.cache = cache;
}
@Override
@ -117,6 +135,36 @@ public class SequenceImpl<T extends Number> extends AbstractNamed implements Seq
return type;
}
@Override
public final Field<T> getStartWith() {
return startWith;
}
@Override
public final Field<T> getIncrementBy() {
return incrementBy;
}
@Override
public final Field<T> getMinValue() {
return minValue;
}
@Override
public final Field<T> getMaxValue() {
return maxValue;
}
@Override
public final boolean getCycle() {
return cycle;
}
@Override
public final Field<T> getCache() {
return cache;
}
@Override
public final Field<T> currval() {
return new SequenceFunction(SequenceMethod.CURRVAL);