Skip to content

Commit 3e97652

Browse files
authored
support for db2 with ru (#1446)
1 parent 1387891 commit 3e97652

File tree

6 files changed

+103
-1
lines changed

6 files changed

+103
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class PlainSelect extends ASTNodeAccessImpl implements SelectBody {
5454
private KSQLWindow ksqlWindow = null;
5555
private boolean noWait = false;
5656
private boolean emitChanges = false;
57+
private WithIsolation withIsolation;
5758

5859
public boolean isUseBrackets() {
5960
return useBrackets;
@@ -330,6 +331,15 @@ public boolean isEmitChanges() {
330331
return emitChanges;
331332
}
332333

334+
335+
public WithIsolation getWithIsolation() {
336+
return withIsolation;
337+
}
338+
339+
public void setWithIsolation(WithIsolation withIsolation) {
340+
this.withIsolation = withIsolation;
341+
}
342+
333343
@Override
334344
@SuppressWarnings({"PMD.CyclomaticComplexity" , "PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
335345
public String toString() {
@@ -421,6 +431,10 @@ public String toString() {
421431
if (fetch != null) {
422432
sql.append(fetch);
423433
}
434+
435+
if (withIsolation != null) {
436+
sql.append(withIsolation);
437+
}
424438
if (isForUpdate()) {
425439
sql.append(" FOR UPDATE");
426440

@@ -455,6 +469,9 @@ public String toString() {
455469
if (fetch != null) {
456470
sql.append(fetch);
457471
}
472+
if (withIsolation != null) {
473+
sql.append(withIsolation);
474+
}
458475
}
459476
if (forXmlPath != null) {
460477
sql.append(" FOR XML PATH(").append(forXmlPath).append(")");

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class SetOperationList implements SelectBody {
2424
private Limit limit;
2525
private Offset offset;
2626
private Fetch fetch;
27+
private WithIsolation withIsolation;
2728

2829
@Override
2930
public void accept(SelectVisitor selectVisitor) {
@@ -96,7 +97,16 @@ public void setFetch(Fetch fetch) {
9697
this.fetch = fetch;
9798
}
9899

100+
public Fetch getWithIsolation() {
101+
return fetch;
102+
}
103+
104+
public void setWithIsolation(WithIsolation withIsolation) {
105+
this.withIsolation = withIsolation;
106+
}
107+
99108
@Override
109+
@SuppressWarnings({"PMD.CyclomaticComplexity"})
100110
public String toString() {
101111
StringBuilder buffer = new StringBuilder();
102112

@@ -123,6 +133,9 @@ public String toString() {
123133
if (fetch != null) {
124134
buffer.append(fetch.toString());
125135
}
136+
if (withIsolation != null) {
137+
buffer.append(withIsolation.toString());
138+
}
126139
return buffer.toString();
127140
}
128141

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.select;
11+
12+
13+
public class WithIsolation {
14+
15+
private String isolation = "UR";
16+
17+
public String getIsolation() {
18+
return this.isolation;
19+
}
20+
public void setIsolation(String s) {
21+
this.isolation = s;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return " WITH " + this.isolation;
27+
}
28+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ public void visit(PlainSelect plainSelect) {
199199
if (plainSelect.getFetch() != null) {
200200
deparseFetch(plainSelect.getFetch());
201201
}
202+
if (plainSelect.getWithIsolation() != null) {
203+
buffer.append(plainSelect.getWithIsolation().toString());
204+
}
202205
if (plainSelect.isForUpdate()) {
203206
buffer.append(" FOR UPDATE");
204207
if (plainSelect.getForUpdateTable() != null) {
@@ -480,6 +483,9 @@ public void visit(SetOperationList list) {
480483
if (list.getFetch() != null) {
481484
deparseFetch(list.getFetch());
482485
}
486+
if (list.getWithIsolation() != null) {
487+
buffer.append(list.getWithIsolation().toString());
488+
}
483489
}
484490

485491
@Override

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
212212
| <K_EXTENDED:"EXTENDED">
213213
| <K_EXTRACT:"EXTRACT">
214214
| <K_FETCH:"FETCH">
215+
| <K_ISOLATION:("UR" | "RS" | "RR" | "CS")>
215216
| <K_FILTER: "FILTER">
216217
| <K_FIRST: "FIRST">
217218
| <K_FALSE: "FALSE">
@@ -1808,6 +1809,7 @@ PlainSelect PlainSelect() #PlainSelect:
18081809
Limit limit = null;
18091810
Offset offset = null;
18101811
Fetch fetch = null;
1812+
WithIsolation withIsolation = null;
18111813
OptimizeFor optimize = null;
18121814
Top top = null;
18131815
Skip skip = null;
@@ -1875,7 +1877,7 @@ PlainSelect PlainSelect() #PlainSelect:
18751877
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { plainSelect.setOffset(offset); } ]
18761878
[LOOKAHEAD(<K_LIMIT>, { limit==null }) limit = LimitWithOffset() { plainSelect.setLimit(limit); } ]
18771879
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { plainSelect.setFetch(fetch); } ]
1878-
1880+
[LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { plainSelect.setWithIsolation(withIsolation); } ]
18791881
[LOOKAHEAD(2) <K_FOR> <K_UPDATE> { plainSelect.setForUpdate(true); }
18801882
[ <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ]
18811883
[ LOOKAHEAD(<K_WAIT>) wait = Wait() { plainSelect.setWait(wait); } ]
@@ -1901,6 +1903,7 @@ SelectBody SetOperationList() #SetOperationList: {
19011903
Limit limit = null;
19021904
Offset offset = null;
19031905
Fetch fetch = null;
1906+
WithIsolation withIsolation = null;
19041907
SelectBody select = null;
19051908
List<SelectBody> selects = new ArrayList<SelectBody>();
19061909
List<SetOperation> operations = new ArrayList<SetOperation>();
@@ -1925,6 +1928,7 @@ SelectBody SetOperationList() #SetOperationList: {
19251928
[LOOKAHEAD(<K_LIMIT>) limit=LimitWithOffset() {list.setLimit(limit);} ]
19261929
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { list.setOffset(offset);} ]
19271930
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { list.setFetch(fetch);} ]
1931+
[LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { list.setWithIsolation(withIsolation);} ]
19281932

19291933
{
19301934
if (selects.size()==1 && selects.get(0) instanceof PlainSelect && orderByElements==null) {
@@ -1956,6 +1960,7 @@ SelectBody SetOperationListWithoutIntialSelect(FromItem fromItem) #SetOperationL
19561960
Limit limit = null;
19571961
Offset offset = null;
19581962
Fetch fetch = null;
1963+
WithIsolation withIsolation = null;
19591964
SelectBody select;
19601965
List<SelectBody> selects = new ArrayList<SelectBody>();
19611966
List<SetOperation> operations = new ArrayList<SetOperation>();
@@ -2835,6 +2840,24 @@ Fetch Fetch():
28352840
}
28362841
}
28372842

2843+
WithIsolation WithIsolation():
2844+
{
2845+
WithIsolation withIsolation = new WithIsolation();
2846+
Token token = null;
2847+
JdbcParameter jdbc;
2848+
}
2849+
{
2850+
(
2851+
//with (ur | cs | rs | rr)
2852+
<K_WITH>
2853+
token=<K_ISOLATION> { withIsolation.setIsolation(token.image); }
2854+
2855+
)
2856+
{
2857+
return withIsolation;
2858+
}
2859+
}
2860+
28382861
OptimizeFor OptimizeFor():
28392862
{
28402863
Token token;

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4980,4 +4980,19 @@ public void testKeywordAtIssue1414() throws JSQLParserException {
49804980
public void testIgnoreNullsForWindowFunctionsIssue1429() throws JSQLParserException {
49814981
assertSqlCanBeParsedAndDeparsed("SELECT lag(mydata) IGNORE NULLS OVER (ORDER BY sortorder) AS previous_status FROM mytable");
49824982
}
4983+
4984+
@Test
4985+
public void testWithIsolation() throws JSQLParserException {
4986+
String statement = "SELECT * FROM mytable WHERE mytable.col = 9 WITH ur";
4987+
Select select = (Select) parserManager.parse(new StringReader(statement));
4988+
String isolation = ((PlainSelect) select.getSelectBody()).getWithIsolation().getIsolation();
4989+
assertEquals("ur", isolation);
4990+
assertSqlCanBeParsedAndDeparsed(statement);
4991+
4992+
statement = "SELECT * FROM mytable WHERE mytable.col = 9 WITH Cs";
4993+
select = (Select) parserManager.parse(new StringReader(statement));
4994+
isolation = ((PlainSelect) select.getSelectBody()).getWithIsolation().getIsolation();
4995+
assertEquals("Cs", isolation);
4996+
assertSqlCanBeParsedAndDeparsed(statement);
4997+
}
49834998
}

0 commit comments

Comments
 (0)