[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).
This commit is contained in:
Knut Wannheden 2019-11-27 14:14:42 +01:00
parent a2674732c9
commit 791c69a9c7
6 changed files with 136 additions and 6 deletions

View File

@ -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 <code>%s</code>", 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"
);
}

View File

@ -44,11 +44,59 @@ public class DefaultSequenceDefinition
extends AbstractTypedElementDefinition<SchemaDefinition>
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;
}
}

View File

@ -44,4 +44,40 @@ package org.jooq.meta;
*/
public interface SequenceDefinition extends TypedElementDefinition<SchemaDefinition> {
/**
* Get the start value for this sequence or <code>null</code>, if no such
* value is specified.
*/
Long getStartWith();
/**
* Get the increment for this sequence or <code>null</code>, if no such
* value is specified.
*/
Long getIncrementBy();
/**
* Get the minimum value for this sequence or <code>null</code>, if no such
* value is specified.
*/
Long getMinValue();
/**
* Get the maximum value for this sequence or <code>null</code>, 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
* <code>null</code>, if no such value is specified.
*/
Long getCache();
}

View File

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

View File

@ -145,6 +145,13 @@ public final class Internal {
return new SequenceImpl<>(name, schema, type, false);
}
/**
* Factory method for sequences.
*/
public static final <T extends Number> Sequence<T> createSequence(String name, Schema schema, DataType<T> 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.
*/

View File

@ -94,6 +94,7 @@ public class SequenceImpl<T extends Number> extends AbstractNamed implements Seq
private final boolean cycle;
private final Field<T> cache;
@Deprecated
public SequenceImpl(String name, Schema schema, DataType<T> type) {
this(name, schema, type, false);
}