From 791c69a9c77e6ec7968ff353c4f70686fbf0bfc3 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 27 Nov 2019 14:14:42 +0100 Subject: [PATCH] [jOOQ/jOOQ#9442] Add sequence flags to code generator Adds an overload factory method `Internal#createSequence()` which accepts the sequence flags as individual parameters. The Java and Scala code generators now generate code using this factory method. So far only the `H2Database` knows how to supply the sequence flags to the code generator. Other dialects supporting sequences will follow in follow-up commits. Also note that support for the `ORDER` flag will be added separately (see jOOQ/jOOQ#9346). --- .../java/org/jooq/codegen/JavaGenerator.java | 26 ++++++++-- .../jooq/meta/DefaultSequenceDefinition.java | 48 +++++++++++++++++++ .../org/jooq/meta/SequenceDefinition.java | 36 ++++++++++++++ .../java/org/jooq/meta/h2/H2Database.java | 24 +++++++++- .../src/main/java/org/jooq/impl/Internal.java | 7 +++ .../main/java/org/jooq/impl/SequenceImpl.java | 1 + 6 files changed, 136 insertions(+), 6 deletions(-) diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java index 54c49c513a..8d8e3f2aa8 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java @@ -118,7 +118,6 @@ import org.jooq.impl.Internal; import org.jooq.impl.PackageImpl; import org.jooq.impl.SQLDataType; import org.jooq.impl.SchemaImpl; -import org.jooq.impl.SequenceImpl; import org.jooq.impl.TableImpl; import org.jooq.impl.TableRecordImpl; import org.jooq.impl.UDTImpl; @@ -4625,9 +4624,22 @@ public class JavaGenerator extends AbstractGenerator { out.tab(1).javadoc("The sequence %s", sequence.getQualifiedOutputName()); if (scala) - out.tab(1).println("val %s : %s[%s] = new %s[%s](\"%s\", %s, %s)", seqId, Sequence.class, seqType, SequenceImpl.class, seqType, seqName, schemaId, typeRef); + out.tab(1).println("val %s : %s[%s] = %s.createSequence(\"%s\", %s, %s, %s, %s, %s, %s, %s, %s)", + seqId, + Sequence.class, + seqType, + Internal.class, + seqName, + schemaId, + typeRef, + sequence.getStartWith() != null ? sequence.getStartWith() + "L" : "null", + sequence.getIncrementBy() != null ? sequence.getIncrementBy() + "L" : "null", + sequence.getMinValue() != null ? sequence.getMinValue() + "L" : "null", + sequence.getMaxValue() != null ? sequence.getMaxValue() + "L" : "null", + sequence.getCycle(), + sequence.getCache() != null ? sequence.getCache() + "L" : "null"); else - out.tab(1).println("public static final %s<%s> %s = %s.<%s> createSequence(\"%s\", %s, %s);", + out.tab(1).println("public static final %s<%s> %s = %s.<%s> createSequence(\"%s\", %s, %s, %s, %s, %s, %s, %s, %s);", Sequence.class, seqType, seqId, @@ -4635,7 +4647,13 @@ public class JavaGenerator extends AbstractGenerator { seqType, seqName, schemaId, - typeRef + typeRef, + sequence.getStartWith() != null ? sequence.getStartWith() + "L" : "null", + sequence.getIncrementBy() != null ? sequence.getIncrementBy() + "L" : "null", + sequence.getMinValue() != null ? sequence.getMinValue() + "L" : "null", + sequence.getMaxValue() != null ? sequence.getMaxValue() + "L" : "null", + sequence.getCycle(), + sequence.getCache() != null ? sequence.getCache() + "L" : "null" ); } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultSequenceDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultSequenceDefinition.java index 5ff350cb9f..2fc6e5cf64 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultSequenceDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultSequenceDefinition.java @@ -44,11 +44,59 @@ public class DefaultSequenceDefinition extends AbstractTypedElementDefinition implements SequenceDefinition { + private Long startWith; + private Long incrementBy; + private Long minValue; + private Long maxValue; + private boolean cycle; + private Long cache; + public DefaultSequenceDefinition(SchemaDefinition schema, String name, DataTypeDefinition type) { this(schema, name, type, null); } public DefaultSequenceDefinition(SchemaDefinition schema, String name, DataTypeDefinition type, String comment) { + this(schema, name, type, comment, null, null, null, null, false, null); + } + + public DefaultSequenceDefinition(SchemaDefinition schema, String name, DataTypeDefinition type, String comment, Long startWith, Long incrementBy, Long minValue, Long maxValue, boolean cycle, Long cache) { super(schema, name, -1, type, comment); + + this.startWith = startWith; + this.incrementBy = incrementBy; + this.minValue = minValue; + this.maxValue = maxValue; + this.cycle = cycle; + this.cache = cache; + } + + @Override + public Long getStartWith() { + return startWith; + } + + @Override + public Long getIncrementBy() { + return incrementBy; + } + + @Override + public Long getMinValue() { + return minValue; + } + + @Override + public Long getMaxValue() { + return maxValue; + } + + @Override + public boolean getCycle() { + return cycle; + } + + @Override + public Long getCache() { + return cache; } } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/SequenceDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/SequenceDefinition.java index 736163a035..7ac50e40c9 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/SequenceDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/SequenceDefinition.java @@ -44,4 +44,40 @@ package org.jooq.meta; */ public interface SequenceDefinition extends TypedElementDefinition { + /** + * Get the start value for this sequence or null, if no such + * value is specified. + */ + Long getStartWith(); + + /** + * Get the increment for this sequence or null, if no such + * value is specified. + */ + Long getIncrementBy(); + + /** + * Get the minimum value for this sequence or null, if no such + * value is specified. + */ + Long getMinValue(); + + /** + * Get the maximum value for this sequence or null, if no such + * value is specified. + */ + Long getMaxValue(); + + /** + * Returns {@code true} if this sequence cycles to {@link #getMinValue()} + * when it reaches {@link #getMaxValue()}. + */ + boolean getCycle(); + + /** + * Get the number of sequence values to cache for this sequence or + * null, if no such value is specified. + */ + Long getCache(); + } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/h2/H2Database.java b/jOOQ-meta/src/main/java/org/jooq/meta/h2/H2Database.java index 4d4e13e558..c8437c0c51 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/h2/H2Database.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/h2/H2Database.java @@ -40,6 +40,7 @@ package org.jooq.meta.h2; import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.inline; import static org.jooq.impl.DSL.name; +import static org.jooq.impl.DSL.nullif; import static org.jooq.impl.DSL.one; import static org.jooq.impl.DSL.select; import static org.jooq.meta.h2.information_schema.tables.Columns.COLUMNS; @@ -53,6 +54,7 @@ import static org.jooq.meta.h2.information_schema.tables.Tables.TABLES; import static org.jooq.meta.h2.information_schema.tables.TypeInfo.TYPE_INFO; import java.io.StringReader; +import java.math.BigInteger; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -110,6 +112,8 @@ import org.jooq.util.h2.H2DataType; */ public class H2Database extends AbstractDatabase { + private static final BigInteger DEFAULT_SEQUENCE_MAXVALUE = new BigInteger("9223372036854775807"); + @Override protected DSLContext create0() { return DSL.using(getConnection(), SQLDialect.H2); @@ -384,7 +388,13 @@ public class H2Database extends AbstractDatabase { for (Record record : create().select( Sequences.SEQUENCE_SCHEMA, - Sequences.SEQUENCE_NAME) + Sequences.SEQUENCE_NAME, + Sequences.INCREMENT, + Sequences.MIN_VALUE, + nullif(Sequences.MAX_VALUE, inline(DEFAULT_SEQUENCE_MAXVALUE)).as(Sequences.MAX_VALUE), + Sequences.IS_CYCLE, + Sequences.CACHE + ) .from(SEQUENCES) .where(Sequences.SEQUENCE_SCHEMA.in(getInputSchemata())) .and(Sequences.SEQUENCE_NAME.upper().notLike("SYSTEM!_SEQUENCE!_%", '!')) @@ -404,7 +414,17 @@ public class H2Database extends AbstractDatabase { H2DataType.BIGINT.getTypeName() ); - result.add(new DefaultSequenceDefinition(schema, name, type)); + result.add(new DefaultSequenceDefinition( + schema, + name, + type, + null, + record.get(Sequences.MIN_VALUE), + record.get(Sequences.INCREMENT), + record.get(Sequences.MIN_VALUE), + record.get(Sequences.MAX_VALUE), + record.get(Sequences.IS_CYCLE), + record.get(Sequences.CACHE))); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Internal.java b/jOOQ/src/main/java/org/jooq/impl/Internal.java index 67417b82d1..daa4fba939 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Internal.java +++ b/jOOQ/src/main/java/org/jooq/impl/Internal.java @@ -145,6 +145,13 @@ public final class Internal { return new SequenceImpl<>(name, schema, type, false); } + /** + * Factory method for sequences. + */ + public static final Sequence createSequence(String name, Schema schema, DataType type, Long startWith, Long incrementBy, Long minValue, Long maxValue, boolean cycle, Long cache) { + return new SequenceImpl<>(DSL.name(name), schema, type, false, Tools.field(startWith, type), Tools.field(incrementBy, type), Tools.field(minValue, type), Tools.field(maxValue, type), cycle, Tools.field(cache, type)); + } + /** * Factory method for check constraints. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java index e1a7fc0053..851bd27d08 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java @@ -94,6 +94,7 @@ public class SequenceImpl extends AbstractNamed implements Seq private final boolean cycle; private final Field cache; + @Deprecated public SequenceImpl(String name, Schema schema, DataType type) { this(name, schema, type, false); }