From 5949f1c0fa6a47b1e87ffc81346d4e0d523a144b Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 24 Oct 2019 19:59:50 +0200 Subject: [PATCH] [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. --- .../java/org/jooq/DDLDiffConfiguration.java | 224 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/Sequence.java | 32 ++- jOOQ/src/main/java/org/jooq/impl/DDL.java | 16 +- .../java/org/jooq/impl/DDLInterpreter.java | 18 +- .../main/java/org/jooq/impl/SequenceImpl.java | 54 ++++- 5 files changed, 327 insertions(+), 17 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/DDLDiffConfiguration.java diff --git a/jOOQ/src/main/java/org/jooq/DDLDiffConfiguration.java b/jOOQ/src/main/java/org/jooq/DDLDiffConfiguration.java new file mode 100644 index 0000000000..c82d7c29b6 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/DDLDiffConfiguration.java @@ -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; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jOOQ/src/main/java/org/jooq/Sequence.java b/jOOQ/src/main/java/org/jooq/Sequence.java index 06a9e00d8a..ca89926ca1 100644 --- a/jOOQ/src/main/java/org/jooq/Sequence.java +++ b/jOOQ/src/main/java/org/jooq/Sequence.java @@ -80,6 +80,36 @@ public interface Sequence extends Named { */ DataType getDataType(); + /** + * Get the start value for this sequence + */ + Field getStartWith(); + + /** + * Get the increment for this sequence + */ + Field getIncrementBy(); + + /** + * Get the minimum value for this sequence + */ + Field getMinValue(); + + /** + * Get the maximum value for this sequence + */ + Field 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 getCache(); + /** * Get the current value of this sequence */ @@ -91,4 +121,4 @@ public interface Sequence extends Named { */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) Field nextval(); -} +} \ No newline at end of file diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java index f4741e92b3..d0cc5441a1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java @@ -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) { diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java index c4e1f86ee9..a0181621e1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java @@ -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 { + @SuppressWarnings("unchecked") InterpretedSequence(Schema schema) { - super(MutableSequence.this.name, schema, BIGINT, false); + super(MutableSequence.this.name, schema, BIGINT, false, + (Field) startWith, (Field) incrementBy, (Field) minValue, (Field) maxValue, cycle, (Field) cache); // [#7752] TODO: Pass additional flags like START WITH to // SequenceImpl when this is ready. diff --git a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java index e6dd63662f..c5adf1745e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java @@ -82,9 +82,15 @@ public class SequenceImpl 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 type; + private final boolean nameIsPlainSQL; + private final Schema schema; + private final DataType type; + private final Field startWith; + private final Field incrementBy; + private final Field minValue; + private final Field maxValue; + private final boolean cycle; + private final Field cache; public SequenceImpl(String name, Schema schema, DataType type) { this(name, schema, type, false); @@ -95,11 +101,23 @@ public class SequenceImpl extends AbstractNamed implements Seq } SequenceImpl(Name name, Schema schema, DataType type, boolean nameIsPlainSQL) { + this(name, schema, type, nameIsPlainSQL, null, null, null, null, false, null); + } + + SequenceImpl(Name name, Schema schema, DataType type, boolean nameIsPlainSQL, + Field startWith, Field incrementBy, Field minValue, Field maxValue, boolean cycle, Field 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 extends AbstractNamed implements Seq return type; } + @Override + public final Field getStartWith() { + return startWith; + } + + @Override + public final Field getIncrementBy() { + return incrementBy; + } + + @Override + public final Field getMinValue() { + return minValue; + } + + @Override + public final Field getMaxValue() { + return maxValue; + } + + @Override + public final boolean getCycle() { + return cycle; + } + + @Override + public final Field getCache() { + return cache; + } + @Override public final Field currval() { return new SequenceFunction(SequenceMethod.CURRVAL);