Skip to content

Commit

Permalink
[#5594] [#7171] Parser support for FETCH NEXT n PERCENT ROWS
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Feb 22, 2018
1 parent 00a4b5c commit 1b393d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt
Expand Up @@ -335,7 +335,7 @@ distinct = ( 'DISTINCT' | 'UNIQUE' ) [ 'ON' '(' fields ')' ] | 'ALL'
;

top =
'TOP' unsignedInteger [ 'START AT' unsignedInteger | 'WITH TIES' ]
'TOP' unsignedInteger [ 'PERCENT' ] [ 'START AT' unsignedInteger | 'WITH TIES' ]
| 'SKIP' unsignedInteger [ 'FIRST' unsignedInteger ]
;

Expand Down Expand Up @@ -380,10 +380,10 @@ orderBy =
offsetFetch =
'OFFSET' unsignedInteger
[
[ 'ROW' | 'ROWS' ] 'FETCH' ( 'FIRST' | 'NEXT' ) [ unsignedInteger ] ( 'ROW' | 'ROWS' ) ( 'ONLY' | 'WITH TIES' )
| [ 'LIMIT' unsignedInteger [ 'WITH TIES' ] ]
[ 'ROW' | 'ROWS' ] 'FETCH' ( 'FIRST' | 'NEXT' ) [ unsignedInteger ] [ 'PERCENT' ] ( 'ROW' | 'ROWS' ) ( 'ONLY' | 'WITH TIES' )
| [ 'LIMIT' unsignedInteger [ 'PERCENT' ] [ 'WITH TIES' ] ]
]
| 'LIMIT' unsignedInteger
| 'LIMIT' unsignedInteger [ 'PERCENT' ]
(
[ 'WITH TIES' ] [ 'OFFSET' unsignedInteger ]
| [ ',' unsignedInteger ]
Expand Down
15 changes: 15 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/ParserImpl.java
Expand Up @@ -760,13 +760,19 @@ else if (peekKeyword(ctx, "FETCH"))
if (offsetPostgres) {
result.addLimit(limit);

if (parseKeywordIf(ctx, "PERCENT"))
result.setLimitPercent(true);

if (parseKeywordIf(ctx, "WITH TIES"))
result.setWithTies(true);
}
else if (parseIf(ctx, ',')) {
result.addLimit(limit, inline((int) (long) parseUnsignedInteger(ctx)));
}
else {
if (parseKeywordIf(ctx, "PERCENT"))
result.setLimitPercent(true);

if (parseKeywordIf(ctx, "WITH TIES"))
result.setWithTies(true);

Expand All @@ -782,6 +788,9 @@ else if (!offsetPostgres && parseKeywordIf(ctx, "FETCH")) {

result.addLimit(inline((int) (long) defaultIfNull(parseUnsignedIntegerIf(ctx), 1L)));

if (parseKeywordIf(ctx, "PERCENT"))
result.setLimitPercent(true);

if (!parseKeywordIf(ctx, "ROW") && !parseKeywordIf(ctx, "ROWS"))
throw ctx.unexpectedToken();

Expand Down Expand Up @@ -904,12 +913,15 @@ private static final SelectQueryImpl<Record> parseQueryPrimary(ParserContext ctx

Long limit = null;
Long offset = null;
boolean percent = false;
boolean withTies = false;

// T-SQL style TOP .. START AT
if (parseKeywordIf(ctx, "TOP")) {
limit = parseUnsignedInteger(ctx);

percent = parseKeywordIf(ctx, "PERCENT");

if (parseKeywordIf(ctx, "START AT"))
offset = parseUnsignedInteger(ctx);
else if (parseKeywordIf(ctx, "WITH TIES"))
Expand Down Expand Up @@ -1062,6 +1074,9 @@ else if (parseKeywordIf(ctx, "GROUPING SETS")) {
else
result.addLimit((int) (long) limit);

if (percent)
result.setLimitPercent(percent);

if (withTies)
result.setWithTies(true);

Expand Down

0 comments on commit 1b393d5

Please sign in to comment.