diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 60cd0d770e..1e08c9b435 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -11385,6 +11385,18 @@ public interface DSLContext extends Scope { @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) T nextval(Sequence sequence) throws DataAccessException; + /** + * Convenience method to fetch several NEXTVAL for a sequence directly from + * this {@link DSLContext}'s underlying JDBC {@link Connection}. + *

+ * This is done using {@link DSL#generateSeries(int, int)}. + * + * @throws DataAccessException if something went wrong executing the query + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) + List nextvals(Sequence sequence, int size) throws DataAccessException; + /** * Convenience method to fetch the CURRVAL for a sequence directly from this * {@link DSLContext}'s underlying JDBC {@link Connection}. diff --git a/jOOQ/src/main/java/org/jooq/Sequence.java b/jOOQ/src/main/java/org/jooq/Sequence.java index c37f37f3fc..e43ce8cadf 100644 --- a/jOOQ/src/main/java/org/jooq/Sequence.java +++ b/jOOQ/src/main/java/org/jooq/Sequence.java @@ -111,16 +111,25 @@ public interface Sequence extends Qualified, Typed { Field getCache(); /** - * Get the current value of this sequence + * An expression to get the current value of this sequence. */ @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) Field currval(); /** - * Increment the sequence and get the next value + * An expression to increment the sequence and get the next value. */ @NotNull @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) Field nextval(); + + /** + * An expression to increment the sequence and get the next values. + *

+ * This is done using {@link DSL#generateSeries(int, int)}. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES }) + Select> nextvals(int size); } \ No newline at end of file diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 7af8208a6d..c40803cd49 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -4125,6 +4125,11 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return select(nextval).fetchOne(nextval); } + @Override + public List nextvals(Sequence sequence, int size) { + return fetchValues(sequence.nextvals(size)); + } + @Override public BigInteger currval(String sequence) { return currval(name(sequence)); diff --git a/jOOQ/src/main/java/org/jooq/impl/GenerateSeries.java b/jOOQ/src/main/java/org/jooq/impl/GenerateSeries.java index 80e7b5707b..37d6dcb384 100644 --- a/jOOQ/src/main/java/org/jooq/impl/GenerateSeries.java +++ b/jOOQ/src/main/java/org/jooq/impl/GenerateSeries.java @@ -38,16 +38,13 @@ package org.jooq.impl; // ... -import static org.jooq.impl.DSL.name; import static org.jooq.impl.DSL.one; import static org.jooq.impl.DSL.table; import static org.jooq.impl.Names.N_GENERATE_SERIES; import org.jooq.Clause; -import org.jooq.Configuration; import org.jooq.Context; import org.jooq.Field; -import org.jooq.QueryPart; import org.jooq.Record1; import org.jooq.TableOptions; @@ -79,16 +76,9 @@ final class GenerateSeries extends AbstractTable> { @Override public final void accept(Context ctx) { - ctx.visit(delegate(ctx.configuration())); - } + switch (ctx.family()) { - @Override // Avoid AbstractTable implementation - public final Clause[] clauses(Context ctx) { - return null; - } - private final QueryPart delegate(Configuration configuration) { - switch (configuration.family()) { @@ -115,9 +105,9 @@ final class GenerateSeries extends AbstractTable> { case POSTGRES: default: if (step == null) - return table("{generate_series}({0}, {1})", from, to); + ctx.visit(table("{generate_series}({0}, {1})", from, to)); else - return table("{generate_series}({0}, {1}, {2})", from, to, step); + ctx.visit(table("{generate_series}({0}, {1}, {2})", from, to, step)); } } @@ -129,6 +119,11 @@ final class GenerateSeries extends AbstractTable> { @Override final Fields> fields0() { - return new Fields<>(DSL.field(name("generate_series"), Integer.class)); + return new Fields<>(DSL.field(N_GENERATE_SERIES, Integer.class)); + } + + @Override // Avoid AbstractTable implementation + public final Clause[] clauses(Context ctx) { + return null; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java index 2ab71f76ff..1ee3b99c90 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SequenceImpl.java @@ -45,6 +45,7 @@ import static org.jooq.SQLDialect.H2; import static org.jooq.SQLDialect.HSQLDB; import static org.jooq.SQLDialect.MARIADB; // ... +import static org.jooq.impl.DSL.generateSeries; import static org.jooq.impl.DSL.select; import static org.jooq.impl.Keywords.K_CURRENT_VALUE_FOR; import static org.jooq.impl.Keywords.K_CURRVAL; @@ -63,8 +64,10 @@ import org.jooq.DataType; import org.jooq.Field; import org.jooq.Keyword; import org.jooq.Name; +import org.jooq.Record1; import org.jooq.SQLDialect; import org.jooq.Schema; +import org.jooq.Select; import org.jooq.Sequence; import org.jooq.exception.SQLDialectNotSupportedException; @@ -181,6 +184,11 @@ public class SequenceImpl extends AbstractTypedNamed implem return new SequenceFunction(SequenceMethod.NEXTVAL); } + @Override + public final Select> nextvals(int size) { + return DSL.select(nextval()).from(generateSeries(1, size)); + } + private enum SequenceMethod { CURRVAL(K_CURRVAL, N_CURRVAL), NEXTVAL(K_NEXTVAL, N_NEXTVAL);