diff --git a/jOOQ/src/main/java/org/jooq/SelectLimitAfterOffsetStep.java b/jOOQ/src/main/java/org/jooq/SelectLimitAfterOffsetStep.java index 92e6ccdd4b..5195dbac4a 100644 --- a/jOOQ/src/main/java/org/jooq/SelectLimitAfterOffsetStep.java +++ b/jOOQ/src/main/java/org/jooq/SelectLimitAfterOffsetStep.java @@ -130,6 +130,16 @@ public interface SelectLimitAfterOffsetStep extends SelectForU @Support SelectLimitPercentAfterOffsetStep limit(int numberOfRows); + /** + * Add a LIMIT clause to the query + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, this may be emulated with a ROW_NUMBER() window + * function and nested SELECT statements. + */ + @Support + SelectLimitPercentAfterOffsetStep limit(Number numberOfRows); + /** * Add a LIMIT clause to the query using named parameters *

@@ -143,6 +153,6 @@ public interface SelectLimitAfterOffsetStep extends SelectForU * statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectLimitPercentAfterOffsetStep limit(Param numberOfRows); + SelectLimitPercentAfterOffsetStep limit(Param numberOfRows); } diff --git a/jOOQ/src/main/java/org/jooq/SelectLimitStep.java b/jOOQ/src/main/java/org/jooq/SelectLimitStep.java index 469f99e967..2eac435ee5 100644 --- a/jOOQ/src/main/java/org/jooq/SelectLimitStep.java +++ b/jOOQ/src/main/java/org/jooq/SelectLimitStep.java @@ -134,6 +134,19 @@ public interface SelectLimitStep extends SelectForUpdateStep limit(int numberOfRows); + /** + * Add a LIMIT clause to the query + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, this may be emulated with a ROW_NUMBER() window + * function and nested SELECT statements. + *

+ * This is the same as calling {@link #limit(int, int)} with offset = 0, or + * calling .limit(numberOfRows).offset(0) + */ + @Support + SelectLimitPercentStep limit(Number numberOfRows); + /** * Add a LIMIT clause to the query using named parameters *

@@ -150,7 +163,7 @@ public interface SelectLimitStep extends SelectForUpdateStep.limit(numberOfRows).offset(0) */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectLimitPercentStep limit(Param numberOfRows); + SelectLimitPercentStep limit(Param numberOfRows); /** * Add a LIMIT clause to the query @@ -166,6 +179,20 @@ public interface SelectLimitStep extends SelectForUpdateStep limit(int offset, int numberOfRows); + /** + * Add a LIMIT clause to the query + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or if your RDBMS does not natively support offsets, this is + * emulated with a ROW_NUMBER() window function and nested + * SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + SelectWithTiesAfterOffsetStep limit(Number offset, Number numberOfRows); + /** * Add a LIMIT clause to the query using named parameters *

@@ -181,6 +208,21 @@ public interface SelectLimitStep extends SelectForUpdateStep limit(int offset, Param numberOfRows); + /** + * Add a LIMIT clause to the query using named parameters + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or the LIMIT or TOP clause does not + * support bind values, or if your RDBMS does not natively support offsets, + * this may be emulated with a ROW_NUMBER() window function + * and nested SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + SelectLimitPercentAfterOffsetStep limit(Number offset, Param numberOfRows); + /** * Add a LIMIT clause to the query using named parameters *

@@ -209,7 +251,22 @@ public interface SelectLimitStep extends SelectForUpdateStepSELECT statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectLimitPercentAfterOffsetStep limit(Param offset, Param numberOfRows); + SelectLimitPercentAfterOffsetStep limit(Param offset, Number numberOfRows); + + /** + * Add a LIMIT clause to the query using named parameters + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or the LIMIT or TOP clause does not + * support bind values, or if your RDBMS does not natively support offsets, + * this may be emulated with a ROW_NUMBER() window function + * and nested SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + SelectLimitPercentAfterOffsetStep limit(Param offset, Param numberOfRows); /** * Add an OFFSET clause to the query @@ -222,6 +279,17 @@ public interface SelectLimitStep extends SelectForUpdateStep offset(int offset); + /** + * Add an OFFSET clause to the query + *

+ * If there is no LIMIT .. OFFSET or TOP clause in + * your RDBMS, or if your RDBMS does not natively support offsets, this is + * emulated with a ROW_NUMBER() window function and nested + * SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + SelectLimitAfterOffsetStep offset(Number offset); + /** * Add an OFFSET clause to the query using a named parameter *

@@ -231,5 +299,5 @@ public interface SelectLimitStep extends SelectForUpdateStepSELECT statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectLimitAfterOffsetStep offset(Param offset); + SelectLimitAfterOffsetStep offset(Param offset); } diff --git a/jOOQ/src/main/java/org/jooq/SelectOffsetStep.java b/jOOQ/src/main/java/org/jooq/SelectOffsetStep.java index 499b106dcc..6da8b9d2cc 100644 --- a/jOOQ/src/main/java/org/jooq/SelectOffsetStep.java +++ b/jOOQ/src/main/java/org/jooq/SelectOffsetStep.java @@ -132,6 +132,17 @@ public interface SelectOffsetStep extends SelectForUpdateStep< @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) SelectForUpdateStep offset(int offset); + /** + * Add an OFFSET clause to the query. + *

+ * If there is no LIMIT .. OFFSET or TOP clause in + * your RDBMS, or if your RDBMS does not natively support offsets, this is + * emulated with a ROW_NUMBER() window function and nested + * SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + SelectForUpdateStep offset(Number offset); + /** * Add an OFFSET clause to the query using a named parameter. *

@@ -141,5 +152,5 @@ public interface SelectOffsetStep extends SelectForUpdateStep< * SELECT statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectForUpdateStep offset(Param offset); + SelectForUpdateStep offset(Param offset); } diff --git a/jOOQ/src/main/java/org/jooq/SelectQuery.java b/jOOQ/src/main/java/org/jooq/SelectQuery.java index 8c8d95370b..5d3d1c925f 100644 --- a/jOOQ/src/main/java/org/jooq/SelectQuery.java +++ b/jOOQ/src/main/java/org/jooq/SelectQuery.java @@ -606,6 +606,17 @@ public interface SelectQuery extends Select, ConditionProvi @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) void addOffset(int offset); + /** + * Add an OFFSET clause to the query. + *

+ * If there is no LIMIT .. OFFSET or TOP clause in + * your RDBMS, or if your RDBMS does not natively support offsets, this is + * emulated with a ROW_NUMBER() window function and nested + * SELECT statements. + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + void addOffset(Number offset); + /** * Add an OFFSET clause to the query using a named parameter. *

@@ -615,7 +626,7 @@ public interface SelectQuery extends Select, ConditionProvi * SELECT statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - void addOffset(Param offset); + void addOffset(Param offset); /** * Limit the results of this select. @@ -627,6 +638,16 @@ public interface SelectQuery extends Select, ConditionProvi @Support void addLimit(int numberOfRows); + /** + * Limit the results of this select. + *

+ * This is the same as calling {@link #addLimit(int, int)} with offset = 0 + * + * @param numberOfRows The number of rows to return + */ + @Support + void addLimit(Number numberOfRows); + /** * Limit the results of this select using named parameters. *

@@ -644,7 +665,7 @@ public interface SelectQuery extends Select, ConditionProvi * @param numberOfRows The number of rows to return */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - void addLimit(Param numberOfRows); + void addLimit(Param numberOfRows); /** * Limit the results of this select. @@ -663,6 +684,23 @@ public interface SelectQuery extends Select, ConditionProvi @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) void addLimit(int offset, int numberOfRows); + /** + * Limit the results of this select. + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or if your RDBMS does not natively support offsets, this is + * emulated with a ROW_NUMBER() window function and nested + * SELECT statements. + * + * @param offset The lowest offset starting at 0 + * @param numberOfRows The number of rows to return + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + void addLimit(Number offset, Number numberOfRows); + /** * Limit the results of this select. *

@@ -681,6 +719,24 @@ public interface SelectQuery extends Select, ConditionProvi @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) void addLimit(Param offset, int numberOfRows); + /** + * Limit the results of this select. + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or the LIMIT or TOP clause does not + * support bind values, or if your RDBMS does not natively support offsets, + * this may be emulated with a ROW_NUMBER() window function + * and nested SELECT statements. + * + * @param offset The lowest offset starting at 0 + * @param numberOfRows The number of rows to return + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + void addLimit(Param offset, Number numberOfRows); + /** * Limit the results of this select using named parameters. *

@@ -715,7 +771,25 @@ public interface SelectQuery extends Select, ConditionProvi * @param numberOfRows The number of rows to return */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - void addLimit(Param offset, Param numberOfRows); + void addLimit(Number offset, Param numberOfRows); + + /** + * Limit the results of this select using named parameters. + *

+ * Note that some dialects do not support bind values at all in + * LIMIT or TOP clauses! + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, or the LIMIT or TOP clause does not + * support bind values, or if your RDBMS does not natively support offsets, + * this may be emulated with a ROW_NUMBER() window function + * and nested SELECT statements. + * + * @param offset The lowest offset starting at 0 + * @param numberOfRows The number of rows to return + */ + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + void addLimit(Param offset, Param numberOfRows); diff --git a/jOOQ/src/main/java/org/jooq/SelectSeekLimitStep.java b/jOOQ/src/main/java/org/jooq/SelectSeekLimitStep.java index fc47c8475a..f0d781cc1e 100644 --- a/jOOQ/src/main/java/org/jooq/SelectSeekLimitStep.java +++ b/jOOQ/src/main/java/org/jooq/SelectSeekLimitStep.java @@ -127,6 +127,16 @@ public interface SelectSeekLimitStep extends SelectForUpdateSt @Support SelectForUpdateStep limit(int numberOfRows); + /** + * Add a LIMIT clause to the query. + *

+ * If there is no LIMIT or TOP clause in your + * RDBMS, this may be emulated with a ROW_NUMBER() window + * function and nested SELECT statements. + */ + @Support + SelectForUpdateStep limit(Number numberOfRows); + /** * Add a LIMIT clause to the query using named parameters. *

@@ -140,6 +150,6 @@ public interface SelectSeekLimitStep extends SelectForUpdateSt * statements. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - SelectForUpdateStep limit(Param numberOfRows); + SelectForUpdateStep limit(Param numberOfRows); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Limit.java b/jOOQ/src/main/java/org/jooq/impl/Limit.java index dc23f43b52..961739a08c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Limit.java +++ b/jOOQ/src/main/java/org/jooq/impl/Limit.java @@ -40,7 +40,6 @@ package org.jooq.impl; import static org.jooq.RenderContext.CastMode.NEVER; // ... import static org.jooq.conf.ParamType.INLINED; -import static org.jooq.impl.DSL.inline; import static org.jooq.impl.DSL.one; import static org.jooq.impl.DSL.val; import static org.jooq.impl.DSL.zero; @@ -58,6 +57,7 @@ import static org.jooq.impl.Keywords.K_START_AT; import static org.jooq.impl.Keywords.K_TO; import static org.jooq.impl.Keywords.K_TOP; import static org.jooq.impl.Keywords.K_WITH_TIES; +import static org.jooq.impl.SQLDataType.BIGINT; import static org.jooq.impl.Tools.DataKey.DATA_PREFER_TOP_OVER_FETCH; import org.jooq.Clause; @@ -81,11 +81,11 @@ final class Limit extends AbstractQueryPart { private static final Field ONE = one(); private static final Param MAX = DSL.inline(Integer.MAX_VALUE); - private Field numberOfRows; - private Field numberOfRowsOrMax = MAX; - private Field offset; - private Field offsetOrZero = ZERO; - private Field offsetPlusOne = ONE; + private Field numberOfRows; + private Field numberOfRowsOrMax = MAX; + private Field offset; + private Field offsetOrZero = ZERO; + private Field offsetPlusOne = ONE; private boolean rendersParams; private boolean withTies; @@ -149,7 +149,7 @@ final class Limit extends AbstractQueryPart { ctx.castMode(NEVER) .formatSeparator() .visit(K_ROWS) - .sql(' ').visit(getLowerRownum().add(inline(1, SQLDataType.INTEGER))) + .sql(' ').visit(getLowerRownum().add(ONE)) .sql(' ').visit(K_TO) .sql(' ').visit(getUpperRownum()) .castMode(castMode); @@ -385,7 +385,7 @@ final class Limit extends AbstractQueryPart { && !withTies() && numberOfRows instanceof Param - && Integer.valueOf(1).equals(((Param) numberOfRows).getValue()); + && Long.valueOf(1L).equals(((Param) numberOfRows).getValue()); } /** @@ -398,14 +398,14 @@ final class Limit extends AbstractQueryPart { /** * The lower bound, such that ROW_NUMBER() > getLowerRownum() */ - final Field getLowerRownum() { + final Field getLowerRownum() { return offsetOrZero; } /** * The upper bound, such that ROW_NUMBER() <= getUpperRownum() */ - final Field getUpperRownum() { + final Field getUpperRownum() { return offsetOrZero.add(numberOfRowsOrMax); } @@ -428,26 +428,26 @@ final class Limit extends AbstractQueryPart { return rendersParams; } - final void setOffset(int offset) { - if (offset != 0) { - this.offset = val(offset, SQLDataType.INTEGER); + final void setOffset(Number offset) { + if (offset.longValue() != 0L) { + this.offset = val(offset.longValue(), BIGINT); this.offsetOrZero = this.offset; - this.offsetPlusOne = val(offset + 1, SQLDataType.INTEGER); + this.offsetPlusOne = val(offset.longValue() + 1L, BIGINT); } } - final void setOffset(Param offset) { + final void setOffset(Param offset) { this.offset = offset; this.offsetOrZero = offset; this.rendersParams = rendersParams |= offset.isInline(); } - final void setNumberOfRows(int numberOfRows) { - this.numberOfRows = val(numberOfRows, SQLDataType.INTEGER); + final void setNumberOfRows(Number numberOfRows) { + this.numberOfRows = val(numberOfRows.longValue(), SQLDataType.BIGINT); this.numberOfRowsOrMax = this.numberOfRows; } - final void setNumberOfRows(Param numberOfRows) { + final void setNumberOfRows(Param numberOfRows) { this.numberOfRows = numberOfRows; this.numberOfRowsOrMax = numberOfRows; this.rendersParams |= numberOfRows.isInline(); diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java index ef35647464..75070a1f14 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java @@ -181,40 +181,40 @@ final class SelectImpl joinTable; + private transient TableLike joinTable; /** * A temporary member holding a join partition by expression */ - private transient Field[] joinPartitionBy; + private transient Field[] joinPartitionBy; /** * A temporary member holding a join type */ - private transient JoinType joinType; + private transient JoinType joinType; /** * A temporary member holding a join condition */ - private transient ConditionProviderImpl joinConditions; + private transient ConditionProviderImpl joinConditions; /** * The step that is currently receiving new conditions */ - private transient ConditionStep conditionStep; + private transient ConditionStep conditionStep; /** - * The limit that has been added in a limit(int).offset(int) construct + * The limit that has been added in a limit(Number).offset(Number) construct */ - private transient Integer limit; - private transient Param limitParam; - private transient Integer offset; - private transient Param offsetParam; + private transient Number limit; + private transient Param limitParam; + private transient Number offset; + private transient Param offsetParam; SelectImpl(Configuration configuration, WithImpl with) { this(configuration, with, false); @@ -1598,13 +1598,18 @@ final class SelectImpl l) { + public final SelectImpl limit(Param l) { limit = null; limitParam = l; return limitOffset(); @@ -1612,6 +1617,11 @@ final class SelectImpl l) { + return limit((Number) o, l); + } + + @Override + public final SelectImpl limit(Number o, Param l) { offset = o; offsetParam = null; limit = null; @@ -1630,6 +1645,11 @@ final class SelectImpl o, int l) { + return limit(o, (Number) l); + } + + @Override + public final SelectImpl limit(Param o, Number l) { offset = null; offsetParam = o; limit = l; @@ -1638,7 +1658,7 @@ final class SelectImpl o, Param l) { + public final SelectImpl limit(Param o, Param l) { offset = null; offsetParam = o; limit = null; @@ -1648,13 +1668,18 @@ final class SelectImpl o) { + public final SelectImpl offset(Param o) { offset = null; offsetParam = o; return limitOffset(); diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index 0887f8f0d1..82342d371f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -1774,44 +1774,69 @@ final class SelectQueryImpl extends AbstractResultQuery imp @Override public final void addOffset(int offset) { + addOffset((Number) offset); + } + + @Override + public final void addOffset(Number offset) { getLimit().setOffset(offset); } @Override - public final void addOffset(Param offset) { + public final void addOffset(Param offset) { getLimit().setOffset(offset); } @Override public final void addLimit(int numberOfRows) { + addLimit((Number) numberOfRows); + } + + @Override + public final void addLimit(Number numberOfRows) { getLimit().setNumberOfRows(numberOfRows); } @Override - public final void addLimit(Param numberOfRows) { + public final void addLimit(Param numberOfRows) { getLimit().setNumberOfRows(numberOfRows); } @Override public final void addLimit(int offset, int numberOfRows) { + addLimit((Number) offset, (Number) numberOfRows); + } + + @Override + public final void addLimit(Number offset, Number numberOfRows) { getLimit().setOffset(offset); getLimit().setNumberOfRows(numberOfRows); } @Override public final void addLimit(int offset, Param numberOfRows) { + addLimit((Number) offset, numberOfRows); + } + + @Override + public final void addLimit(Number offset, Param numberOfRows) { getLimit().setOffset(offset); getLimit().setNumberOfRows(numberOfRows); } @Override public final void addLimit(Param offset, int numberOfRows) { + addLimit(offset, (Number) numberOfRows); + } + + @Override + public final void addLimit(Param offset, Number numberOfRows) { getLimit().setOffset(offset); getLimit().setNumberOfRows(numberOfRows); } @Override - public final void addLimit(Param offset, Param numberOfRows) { + public final void addLimit(Param offset, Param numberOfRows) { getLimit().setOffset(offset); getLimit().setNumberOfRows(numberOfRows); }