Skip to content

Commit c743ad4

Browse files
committed
Merge pull request #81 from LionelNirva/patch2
Support for SQL Server and Oracle OFFSET ... FETCH ... clauses
2 parents ccefbeb + 26524ac commit c743ad4

File tree

7 files changed

+360
-17
lines changed

7 files changed

+360
-17
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.select;
23+
24+
/**
25+
* A fetch clause in the form FETCH (FIRST | NEXT) row_count (ROW | ROWS) ONLY
26+
*/
27+
public class Fetch {
28+
29+
private long rowCount;
30+
private boolean fetchJdbcParameter = false;
31+
private boolean isFetchParamFirst = false;
32+
private String fetchParam = "ROW";
33+
34+
public long getRowCount() {
35+
return rowCount;
36+
}
37+
38+
public void setRowCount(long l) {
39+
rowCount = l;
40+
}
41+
42+
public boolean isFetchJdbcParameter() {
43+
return fetchJdbcParameter;
44+
}
45+
46+
public String getFetchParam() {
47+
return fetchParam;
48+
}
49+
50+
public boolean isFetchParamFirst() {
51+
return isFetchParamFirst;
52+
}
53+
54+
public void setFetchJdbcParameter(boolean b) {
55+
fetchJdbcParameter = b;
56+
}
57+
58+
public void setFetchParam(String s) {
59+
this.fetchParam = s;
60+
}
61+
62+
public void setFetchParamFirst(boolean b) {
63+
this.isFetchParamFirst = b;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return " FETCH " + (isFetchParamFirst ? "FIRST" : "NEXT") + " " + (fetchJdbcParameter ? "?" : rowCount + "") + " "+ fetchParam + " ONLY";
69+
}
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.select;
23+
24+
/**
25+
* An offset clause in the form OFFSET offset
26+
* or in the form OFFSET offset (ROW | ROWS)
27+
*/
28+
public class Offset {
29+
30+
private long offset;
31+
private boolean offsetJdbcParameter = false;
32+
private String offsetParam = null;
33+
34+
public long getOffset() {
35+
return offset;
36+
}
37+
38+
public String getOffsetParam() {
39+
return offsetParam;
40+
}
41+
42+
public void setOffset(long l) {
43+
offset = l;
44+
}
45+
46+
public void setOffsetParam(String s) {
47+
offsetParam = s;
48+
}
49+
50+
public boolean isOffsetJdbcParameter() {
51+
return offsetJdbcParameter;
52+
}
53+
54+
public void setOffsetJdbcParameter(boolean b) {
55+
offsetJdbcParameter = b;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return " OFFSET " + (offsetJdbcParameter ? "?" : offset) + (offsetParam != null ? " "+offsetParam : "");
61+
}
62+
}

src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class PlainSelect implements SelectBody {
4545
private List<OrderByElement> orderByElements;
4646
private Expression having;
4747
private Limit limit;
48+
private Offset offset;
49+
private Fetch fetch;
4850
private Top top;
4951
private OracleHierarchicalExpression oracleHierarchical = null;
5052
private boolean oracleSiblings = false;
@@ -133,6 +135,22 @@ public void setLimit(Limit limit) {
133135
this.limit = limit;
134136
}
135137

138+
public Offset getOffset() {
139+
return offset;
140+
}
141+
142+
public void setOffset(Offset offset) {
143+
this.offset = offset;
144+
}
145+
146+
public Fetch getFetch() {
147+
return fetch;
148+
}
149+
150+
public void setFetch(Fetch fetch) {
151+
this.fetch = fetch;
152+
}
153+
136154
public Top getTop() {
137155
return top;
138156
}
@@ -236,6 +254,12 @@ public String toString() {
236254
if (limit != null) {
237255
sql.append(limit);
238256
}
257+
if (offset != null) {
258+
sql.append(offset);
259+
}
260+
if (fetch != null) {
261+
sql.append(fetch);
262+
}
239263
}
240264
return sql.toString();
241265
}

src/main/java/net/sf/jsqlparser/statement/select/SetOperationList.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class SetOperationList implements SelectBody {
3636
private List<SetOperation> operations;
3737
private List<OrderByElement> orderByElements;
3838
private Limit limit;
39+
private Offset offset;
40+
private Fetch fetch;
3941

4042
@Override
4143
public void accept(SelectVisitor selectVisitor) {
@@ -75,6 +77,22 @@ public void setLimit(Limit limit) {
7577
this.limit = limit;
7678
}
7779

80+
public Offset getOffset() {
81+
return offset;
82+
}
83+
84+
public void setOffset(Offset offset) {
85+
this.offset = offset;
86+
}
87+
88+
public Fetch getFetch() {
89+
return fetch;
90+
}
91+
92+
public void setFetch(Fetch fetch) {
93+
this.fetch = fetch;
94+
}
95+
7896
@Override
7997
public String toString() {
8098
StringBuilder buffer = new StringBuilder();
@@ -92,6 +110,12 @@ public String toString() {
92110
if (limit != null) {
93111
buffer.append(limit.toString());
94112
}
113+
if (offset != null) {
114+
buffer.append(offset.toString());
115+
}
116+
if (fetch != null) {
117+
buffer.append(fetch.toString());
118+
}
95119
return buffer.toString();
96120
}
97121

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ public void visit(PlainSelect plainSelect) {
134134
if (plainSelect.getLimit() != null) {
135135
deparseLimit(plainSelect.getLimit());
136136
}
137+
if (plainSelect.getOffset() != null) {
138+
deparseOffset(plainSelect.getOffset());
139+
}
140+
if (plainSelect.getFetch() != null) {
141+
deparseFetch(plainSelect.getFetch());
142+
}
137143

138144
}
139145

@@ -266,6 +272,38 @@ public void deparseLimit(Limit limit) {
266272

267273
}
268274

275+
public void deparseOffset(Offset offset) {
276+
// OFFSET offset
277+
// or OFFSET offset (ROW | ROWS)
278+
if (offset.isOffsetJdbcParameter()) {
279+
buffer.append(" OFFSET ?");
280+
} else if (offset.getOffset() != 0) {
281+
buffer.append(" OFFSET ");
282+
buffer.append(offset.getOffset());
283+
}
284+
if (offset.getOffsetParam() != null) {
285+
buffer.append(" ").append(offset.getOffsetParam());
286+
}
287+
288+
}
289+
290+
public void deparseFetch(Fetch fetch) {
291+
// FETCH (FIRST | NEXT) row_count (ROW | ROWS) ONLY
292+
buffer.append(" FETCH ");
293+
if (fetch.isFetchParamFirst()) {
294+
buffer.append("FIRST ");
295+
} else {
296+
buffer.append("NEXT ");
297+
}
298+
if (fetch.isFetchJdbcParameter()) {
299+
buffer.append("?");
300+
} else {
301+
buffer.append(fetch.getRowCount());
302+
}
303+
buffer.append(" ").append(fetch.getFetchParam()).append(" ONLY");
304+
305+
}
306+
269307
public StringBuilder getBuffer() {
270308
return buffer;
271309
}
@@ -359,6 +397,12 @@ public void visit(SetOperationList list) {
359397
if (list.getLimit() != null) {
360398
deparseLimit(list.getLimit());
361399
}
400+
if (list.getOffset() != null) {
401+
deparseOffset(list.getOffset());
402+
}
403+
if (list.getFetch() != null) {
404+
deparseFetch(list.getFetch());
405+
}
362406
}
363407

364408
@Override

0 commit comments

Comments
 (0)