[jOOQ/jOOQ#10658] Add Sequence.nextvals(int):Select<Record1<T>> and DSLContext.nextvals(Sequence<T>, int):List<T> to fetch a number of sequence values in one go

This commit is contained in:
Lukas Eder 2020-09-21 10:15:02 +02:00
parent e342269749
commit d8b16a9c2d
5 changed files with 45 additions and 16 deletions

View File

@ -11385,6 +11385,18 @@ public interface DSLContext extends Scope {
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
<T extends Number> T nextval(Sequence<T> sequence) throws DataAccessException;
/**
* Convenience method to fetch several NEXTVAL for a sequence directly from
* this {@link DSLContext}'s underlying JDBC {@link Connection}.
* <p>
* 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 })
<T extends Number> List<T> nextvals(Sequence<T> sequence, int size) throws DataAccessException;
/**
* Convenience method to fetch the CURRVAL for a sequence directly from this
* {@link DSLContext}'s underlying JDBC {@link Connection}.

View File

@ -111,16 +111,25 @@ public interface Sequence<T extends Number> extends Qualified, Typed<T> {
Field<T> 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<T> 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<T> nextval();
/**
* An expression to increment the sequence and get the next values.
* <p>
* This is done using {@link DSL#generateSeries(int, int)}.
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
Select<Record1<T>> nextvals(int size);
}

View File

@ -4125,6 +4125,11 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
return select(nextval).fetchOne(nextval);
}
@Override
public <T extends Number> List<T> nextvals(Sequence<T> sequence, int size) {
return fetchValues(sequence.nextvals(size));
}
@Override
public BigInteger currval(String sequence) {
return currval(name(sequence));

View File

@ -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<Record1<Integer>> {
@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<Record1<Integer>> {
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<Record1<Integer>> {
@Override
final Fields<Record1<Integer>> 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;
}
}

View File

@ -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<T extends Number> extends AbstractTypedNamed<T> implem
return new SequenceFunction(SequenceMethod.NEXTVAL);
}
@Override
public final Select<Record1<T>> nextvals(int size) {
return DSL.select(nextval()).from(generateSeries(1, size));
}
private enum SequenceMethod {
CURRVAL(K_CURRVAL, N_CURRVAL),
NEXTVAL(K_NEXTVAL, N_NEXTVAL);