Skip to content

Commit

Permalink
support for db2 with ru (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
chiangcho authored Dec 20, 2021
1 parent 1387891 commit 3e97652
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class PlainSelect extends ASTNodeAccessImpl implements SelectBody {
private KSQLWindow ksqlWindow = null;
private boolean noWait = false;
private boolean emitChanges = false;
private WithIsolation withIsolation;

public boolean isUseBrackets() {
return useBrackets;
Expand Down Expand Up @@ -330,6 +331,15 @@ public boolean isEmitChanges() {
return emitChanges;
}


public WithIsolation getWithIsolation() {
return withIsolation;
}

public void setWithIsolation(WithIsolation withIsolation) {
this.withIsolation = withIsolation;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity" , "PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
public String toString() {
Expand Down Expand Up @@ -421,6 +431,10 @@ public String toString() {
if (fetch != null) {
sql.append(fetch);
}

if (withIsolation != null) {
sql.append(withIsolation);
}
if (isForUpdate()) {
sql.append(" FOR UPDATE");

Expand Down Expand Up @@ -455,6 +469,9 @@ public String toString() {
if (fetch != null) {
sql.append(fetch);
}
if (withIsolation != null) {
sql.append(withIsolation);
}
}
if (forXmlPath != null) {
sql.append(" FOR XML PATH(").append(forXmlPath).append(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SetOperationList implements SelectBody {
private Limit limit;
private Offset offset;
private Fetch fetch;
private WithIsolation withIsolation;

@Override
public void accept(SelectVisitor selectVisitor) {
Expand Down Expand Up @@ -96,7 +97,16 @@ public void setFetch(Fetch fetch) {
this.fetch = fetch;
}

public Fetch getWithIsolation() {
return fetch;
}

public void setWithIsolation(WithIsolation withIsolation) {
this.withIsolation = withIsolation;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public String toString() {
StringBuilder buffer = new StringBuilder();

Expand All @@ -123,6 +133,9 @@ public String toString() {
if (fetch != null) {
buffer.append(fetch.toString());
}
if (withIsolation != null) {
buffer.append(withIsolation.toString());
}
return buffer.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;


public class WithIsolation {

private String isolation = "UR";

public String getIsolation() {
return this.isolation;
}
public void setIsolation(String s) {
this.isolation = s;
}

@Override
public String toString() {
return " WITH " + this.isolation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public void visit(PlainSelect plainSelect) {
if (plainSelect.getFetch() != null) {
deparseFetch(plainSelect.getFetch());
}
if (plainSelect.getWithIsolation() != null) {
buffer.append(plainSelect.getWithIsolation().toString());
}
if (plainSelect.isForUpdate()) {
buffer.append(" FOR UPDATE");
if (plainSelect.getForUpdateTable() != null) {
Expand Down Expand Up @@ -480,6 +483,9 @@ public void visit(SetOperationList list) {
if (list.getFetch() != null) {
deparseFetch(list.getFetch());
}
if (list.getWithIsolation() != null) {
buffer.append(list.getWithIsolation().toString());
}
}

@Override
Expand Down
25 changes: 24 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_EXTENDED:"EXTENDED">
| <K_EXTRACT:"EXTRACT">
| <K_FETCH:"FETCH">
| <K_ISOLATION:("UR" | "RS" | "RR" | "CS")>
| <K_FILTER: "FILTER">
| <K_FIRST: "FIRST">
| <K_FALSE: "FALSE">
Expand Down Expand Up @@ -1808,6 +1809,7 @@ PlainSelect PlainSelect() #PlainSelect:
Limit limit = null;
Offset offset = null;
Fetch fetch = null;
WithIsolation withIsolation = null;
OptimizeFor optimize = null;
Top top = null;
Skip skip = null;
Expand Down Expand Up @@ -1875,7 +1877,7 @@ PlainSelect PlainSelect() #PlainSelect:
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { plainSelect.setOffset(offset); } ]
[LOOKAHEAD(<K_LIMIT>, { limit==null }) limit = LimitWithOffset() { plainSelect.setLimit(limit); } ]
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { plainSelect.setFetch(fetch); } ]

[LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { plainSelect.setWithIsolation(withIsolation); } ]
[LOOKAHEAD(2) <K_FOR> <K_UPDATE> { plainSelect.setForUpdate(true); }
[ <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ]
[ LOOKAHEAD(<K_WAIT>) wait = Wait() { plainSelect.setWait(wait); } ]
Expand All @@ -1901,6 +1903,7 @@ SelectBody SetOperationList() #SetOperationList: {
Limit limit = null;
Offset offset = null;
Fetch fetch = null;
WithIsolation withIsolation = null;
SelectBody select = null;
List<SelectBody> selects = new ArrayList<SelectBody>();
List<SetOperation> operations = new ArrayList<SetOperation>();
Expand All @@ -1925,6 +1928,7 @@ SelectBody SetOperationList() #SetOperationList: {
[LOOKAHEAD(<K_LIMIT>) limit=LimitWithOffset() {list.setLimit(limit);} ]
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { list.setOffset(offset);} ]
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { list.setFetch(fetch);} ]
[LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { list.setWithIsolation(withIsolation);} ]

{
if (selects.size()==1 && selects.get(0) instanceof PlainSelect && orderByElements==null) {
Expand Down Expand Up @@ -1956,6 +1960,7 @@ SelectBody SetOperationListWithoutIntialSelect(FromItem fromItem) #SetOperationL
Limit limit = null;
Offset offset = null;
Fetch fetch = null;
WithIsolation withIsolation = null;
SelectBody select;
List<SelectBody> selects = new ArrayList<SelectBody>();
List<SetOperation> operations = new ArrayList<SetOperation>();
Expand Down Expand Up @@ -2835,6 +2840,24 @@ Fetch Fetch():
}
}

WithIsolation WithIsolation():
{
WithIsolation withIsolation = new WithIsolation();
Token token = null;
JdbcParameter jdbc;
}
{
(
//with (ur | cs | rs | rr)
<K_WITH>
token=<K_ISOLATION> { withIsolation.setIsolation(token.image); }

)
{
return withIsolation;
}
}

OptimizeFor OptimizeFor():
{
Token token;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4980,4 +4980,19 @@ public void testKeywordAtIssue1414() throws JSQLParserException {
public void testIgnoreNullsForWindowFunctionsIssue1429() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT lag(mydata) IGNORE NULLS OVER (ORDER BY sortorder) AS previous_status FROM mytable");
}

@Test
public void testWithIsolation() throws JSQLParserException {
String statement = "SELECT * FROM mytable WHERE mytable.col = 9 WITH ur";
Select select = (Select) parserManager.parse(new StringReader(statement));
String isolation = ((PlainSelect) select.getSelectBody()).getWithIsolation().getIsolation();
assertEquals("ur", isolation);
assertSqlCanBeParsedAndDeparsed(statement);

statement = "SELECT * FROM mytable WHERE mytable.col = 9 WITH Cs";
select = (Select) parserManager.parse(new StringReader(statement));
isolation = ((PlainSelect) select.getSelectBody()).getWithIsolation().getIsolation();
assertEquals("Cs", isolation);
assertSqlCanBeParsedAndDeparsed(statement);
}
}

0 comments on commit 3e97652

Please sign in to comment.