Skip to content

Commit 26524ac

Browse files
author
LionelNirva
committed
Manage OFFSET and FETCH clauses in dedicated classes and rules in
jsqlparsercc.jj Manage also jdbc parameter in these clauses.
1 parent dc215bb commit 26524ac

File tree

8 files changed

+338
-172
lines changed

8 files changed

+338
-172
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+
}

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

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
/**
2525
* A limit clause in the form [LIMIT {[offset,] row_count) | (row_count | ALL)
2626
* OFFSET offset}]
27-
* or in the form [OFFSET offset (ROW | ROWS) [FETCH (FIRST | NEXT) row_count (ROW | ROWS) ONLY]]
28-
* or in the form FETCH (FIRST | NEXT) row_count (ROW | ROWS) ONLY
2927
*/
3028
public class Limit {
3129

@@ -35,12 +33,6 @@ public class Limit {
3533
private boolean offsetJdbcParameter = false;
3634
private boolean limitAll;
3735
private boolean limitNull = false;
38-
private boolean oracleSqlServerVersion = false;
39-
private boolean hasOffset = false;
40-
private boolean isOffsetParamRows = false;
41-
private boolean hasFetch = false;
42-
private boolean isFetchParamRows = false;
43-
private boolean isFetchParamFirst = false;
4436

4537
public long getOffset() {
4638
return offset;
@@ -66,29 +58,6 @@ public boolean isRowCountJdbcParameter() {
6658
return rowCountJdbcParameter;
6759
}
6860

69-
public boolean isOracleSqlServerVersion() {
70-
return oracleSqlServerVersion;
71-
}
72-
73-
public boolean isHasOffset() {
74-
return hasOffset;
75-
}
76-
77-
public boolean isHasFetch() {
78-
return hasFetch;
79-
}
80-
public boolean isOffsetParamRows() {
81-
return isOffsetParamRows;
82-
}
83-
84-
public boolean isFetchParamRows() {
85-
return isFetchParamRows;
86-
}
87-
88-
public boolean isFetchParamFirst() {
89-
return isFetchParamFirst;
90-
}
91-
9261
public void setOffsetJdbcParameter(boolean b) {
9362
offsetJdbcParameter = b;
9463
}
@@ -97,30 +66,6 @@ public void setRowCountJdbcParameter(boolean b) {
9766
rowCountJdbcParameter = b;
9867
}
9968

100-
public void setOracleSqlServerVersion(boolean b) {
101-
oracleSqlServerVersion = b;
102-
}
103-
104-
public void setHasOffset(boolean b) {
105-
hasOffset = b;
106-
}
107-
108-
public void setHasFetch(boolean b) {
109-
hasFetch = b;
110-
}
111-
112-
public void setOffsetParamRows(boolean isOffsetParamRows) {
113-
this.isOffsetParamRows = isOffsetParamRows;
114-
}
115-
116-
public void setFetchParamRows(boolean isFetchParamRows) {
117-
this.isFetchParamRows = isFetchParamRows;
118-
}
119-
120-
public void setFetchParamFirst(boolean isFetchParamFirst) {
121-
this.isFetchParamFirst = isFetchParamFirst;
122-
}
123-
12469
/**
12570
* @return true if the limit is "LIMIT ALL [OFFSET ...])
12671
*/
@@ -142,22 +87,13 @@ public void setLimitAll(boolean b) {
14287
@Override
14388
public String toString() {
14489
String retVal = "";
145-
if (oracleSqlServerVersion) {
146-
if (hasOffset) {
147-
retVal = " OFFSET " + offset + " "+(isOffsetParamRows ? "ROWS" : "ROW");
148-
}
149-
if (hasFetch) {
150-
retVal += " FETCH "+(isFetchParamFirst ? "FIRST" : "NEXT")+" " + rowCount + " "+(isFetchParamRows ? "ROWS" : "ROW")+" ONLY";
151-
}
152-
} else {
153-
if (limitNull) {
154-
retVal += " LIMIT NULL";
155-
} else if (rowCount >= 0 || rowCountJdbcParameter) {
156-
retVal += " LIMIT " + (rowCountJdbcParameter ? "?" : rowCount + "");
157-
}
158-
if (offset > 0 || offsetJdbcParameter) {
159-
retVal += " OFFSET " + (offsetJdbcParameter ? "?" : offset + "");
160-
}
90+
if (limitNull) {
91+
retVal += " LIMIT NULL";
92+
} else if (rowCount >= 0 || rowCountJdbcParameter) {
93+
retVal += " LIMIT " + (rowCountJdbcParameter ? "?" : rowCount + "");
94+
}
95+
if (offset > 0 || offsetJdbcParameter) {
96+
retVal += " OFFSET " + (offsetJdbcParameter ? "?" : offset + "");
16197
}
16298
return retVal;
16399
}
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

0 commit comments

Comments
 (0)