Skip to content

Commit 2b3faa0

Browse files
committed
Merge origin/master
2 parents c707582 + 31ed383 commit 2b3faa0

File tree

9 files changed

+129
-4
lines changed

9 files changed

+129
-4
lines changed

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@ public interface ExpressionVisitor {
186186
void visit(AllTableColumns allTableColumns);
187187

188188
void visit(AllValue allValue);
189+
190+
void visit(IsDistinctExpression isDistinctExpression);
189191
}

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ public void visit(AllTableColumns allTableColumns) {
504504
public void visit(AllValue allValue) {
505505
}
506506

507+
@Override
508+
public void visit(IsDistinctExpression isDistinctExpression) {
509+
visitBinaryExpression(isDistinctExpression);
510+
}
511+
507512
@Override
508513
public void visit(SelectExpressionItem selectExpressionItem) {
509514
selectExpressionItem.getExpression().accept(this);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.expression.operators.relational;
11+
12+
import net.sf.jsqlparser.expression.BinaryExpression;
13+
import net.sf.jsqlparser.expression.ExpressionVisitor;
14+
15+
public class IsDistinctExpression extends BinaryExpression {
16+
17+
private boolean not = false;
18+
19+
public boolean isNot() {
20+
return not;
21+
}
22+
23+
public void setNot(boolean b) {
24+
not = b;
25+
}
26+
27+
@Override
28+
public void accept(ExpressionVisitor expressionVisitor) {
29+
expressionVisitor.visit(this);
30+
}
31+
32+
@Override
33+
public String getStringExpression() {
34+
return " IS " + (isNot() ? "NOT " : "") + "DISTINCT FROM ";
35+
}
36+
37+
@Override
38+
public String toString() {
39+
String retval = getLeftExpression() + getStringExpression() + getRightExpression();
40+
return retval;
41+
}
42+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public void setFetch(Fetch fetch) {
9797
this.fetch = fetch;
9898
}
9999

100-
public Fetch getWithIsolation() {
101-
return fetch;
100+
public WithIsolation getWithIsolation() {
101+
return this.withIsolation;
102102
}
103103

104104
public void setWithIsolation(WithIsolation withIsolation) {

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ public void visit(AllValue allValue) {
576576

577577
}
578578

579+
@Override
580+
public void visit(IsDistinctExpression isDistinctExpression) {
581+
visitBinaryExpression(isDistinctExpression);
582+
}
583+
579584
@Override
580585
public void visit(SelectExpressionItem item) {
581586
item.getExpression().accept(this);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
5353
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
5454
import net.sf.jsqlparser.expression.operators.relational.SimilarToExpression;
55+
import net.sf.jsqlparser.expression.operators.relational.IsDistinctExpression;
5556
import net.sf.jsqlparser.expression.operators.relational.SupportsOldOracleJoinSyntax;
5657
import net.sf.jsqlparser.schema.Column;
5758
import net.sf.jsqlparser.schema.Table;
@@ -988,4 +989,11 @@ public void visit(AllTableColumns allTableColumns) {
988989
public void visit(AllValue allValue) {
989990
buffer.append(allValue);
990991
}
992+
993+
@Override
994+
public void visit(IsDistinctExpression isDistinctExpression) {
995+
buffer.append(isDistinctExpression.getLeftExpression() +
996+
isDistinctExpression.getStringExpression() +
997+
isDistinctExpression.getRightExpression());
998+
}
991999
}

src/main/java/net/sf/jsqlparser/util/validation/validator/ExpressionValidator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
4646
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
4747
import net.sf.jsqlparser.expression.operators.relational.SimilarToExpression;
48+
import net.sf.jsqlparser.expression.operators.relational.IsDistinctExpression;
4849
import net.sf.jsqlparser.expression.operators.relational.SupportsOldOracleJoinSyntax;
4950
import net.sf.jsqlparser.parser.feature.Feature;
5051
import net.sf.jsqlparser.schema.Column;
@@ -579,4 +580,10 @@ public void visit(AllValue allValue) {
579580

580581
}
581582

583+
@Override
584+
public void visit(IsDistinctExpression isDistinctExpression) {
585+
isDistinctExpression.getLeftExpression().accept(this);
586+
isDistinctExpression.getRightExpression().accept(this);
587+
}
588+
582589
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,16 +1938,24 @@ SelectBody SetOperationList() #SetOperationList: {
19381938
((PlainSelect)selects.get(0)).setUseBrackets(true);
19391939
return selects.get(0);
19401940
} else {
1941-
if (selects.size()>1 && selects.get(selects.size()-1) instanceof PlainSelect) {
1941+
if (selects.size()>1 && selects.get(selects.size()-1) instanceof PlainSelect && !brackets.get(brackets.size() - 1)) {
19421942
PlainSelect ps = (PlainSelect)selects.get(selects.size()-1);
1943-
if (ps.getOrderByElements() != null && !brackets.get(brackets.size() - 1)) {
1943+
if (ps.getOrderByElements() != null) {
19441944
list.setOrderByElements(ps.getOrderByElements());
19451945
list.setLimit(ps.getLimit());
19461946
list.setOffset(ps.getOffset());
19471947
ps.setOrderByElements(null);
19481948
ps.setLimit(null);
19491949
ps.setOffset(null);
19501950
}
1951+
if (ps.getFetch() != null) {
1952+
list.setFetch(ps.getFetch());
1953+
ps.setFetch(null);
1954+
}
1955+
if (ps.getWithIsolation() != null) {
1956+
list.setWithIsolation(ps.getWithIsolation());
1957+
ps.setWithIsolation(null);
1958+
}
19511959
}
19521960
list.setBracketsOpsAndSelects(brackets,selects,operations);
19531961
return list;
@@ -3141,6 +3149,7 @@ Expression SQLCondition():
31413149
| LOOKAHEAD(IsNullExpression()) result=IsNullExpression(left)
31423150
| LOOKAHEAD(IsBooleanExpression()) result=IsBooleanExpression(left)
31433151
| LOOKAHEAD(2) result=LikeExpression(left)
3152+
| LOOKAHEAD(IsDistinctExpression()) result=IsDistinctExpression(left)
31443153
| result=SimilarToExpression(left)
31453154
)) ]
31463155
)
@@ -3258,6 +3267,22 @@ Expression SimilarToExpression(Expression leftExpression) #SimilarToExpression:
32583267
}
32593268
}
32603269

3270+
Expression IsDistinctExpression(Expression leftExpression) #IsDistinctExpression:
3271+
{
3272+
IsDistinctExpression result = new IsDistinctExpression();
3273+
Expression rightExpression = null;
3274+
}
3275+
{
3276+
<K_IS> [<K_NOT> { result.setNot(true); } ] <K_DISTINCT> <K_FROM>
3277+
rightExpression=SimpleExpression()
3278+
{
3279+
result.setLeftExpression(leftExpression);
3280+
result.setRightExpression(rightExpression);
3281+
linkAST(result,jjtThis);
3282+
return result;
3283+
}
3284+
}
3285+
32613286
Expression IsNullExpression(Expression leftExpression):
32623287
{
32633288
IsNullExpression result = new IsNullExpression();

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,25 @@ public void testUnion() throws JSQLParserException {
898898
+ "SELECT * FROM mytable3 WHERE mytable3.col = ? UNION "
899899
+ "SELECT * FROM mytable2 LIMIT 3, 4";
900900
assertStatementCanBeDeparsedAs(select, statementToString);
901+
902+
//with fetch and with ur
903+
String statement2 = "SELECT * FROM mytable WHERE mytable.col = 9 UNION "
904+
+ "SELECT * FROM mytable3 WHERE mytable3.col = ? UNION " + "SELECT * FROM mytable2 ORDER BY COL DESC FETCH FIRST 1 ROWS ONLY WITH UR";
905+
906+
Select select2 = (Select) parserManager.parse(new StringReader(statement2));
907+
SetOperationList setList2 = (SetOperationList) select2.getSelectBody();
908+
assertEquals(3, setList2.getSelects().size());
909+
assertEquals("mytable", ((Table) ((PlainSelect) setList2.getSelects().get(0)).getFromItem()).
910+
getName());
911+
assertEquals("mytable3", ((Table) ((PlainSelect) setList2.getSelects().get(1)).getFromItem()).
912+
getName());
913+
assertEquals("mytable2", ((Table) ((PlainSelect) setList2.getSelects().get(2)).getFromItem()).
914+
getName());
915+
assertEquals(1, ((SetOperationList) setList2).getFetch().getRowCount());
916+
917+
assertEquals("UR", ((SetOperationList) setList2).getWithIsolation().getIsolation());
918+
919+
assertStatementCanBeDeparsedAs(select2, statement2);
901920
}
902921

903922
@Test
@@ -942,6 +961,18 @@ public void testDistinct() throws JSQLParserException {
942961
assertStatementCanBeDeparsedAs(select, statement);
943962
}
944963

964+
@Test
965+
public void testIsDistinctFrom() throws JSQLParserException {
966+
String stmt = "SELECT name FROM tbl WHERE name IS DISTINCT FROM foo";
967+
assertSqlCanBeParsedAndDeparsed(stmt);
968+
}
969+
970+
@Test
971+
public void testIsNotDistinctFrom() throws JSQLParserException {
972+
String stmt = "SELECT name FROM tbl WHERE name IS NOT DISTINCT FROM foo";
973+
assertSqlCanBeParsedAndDeparsed(stmt);
974+
}
975+
945976
@Test
946977
public void testDistinctTop() throws JSQLParserException {
947978
String statement = "SELECT DISTINCT TOP 5 myid, mycol FROM mytable WHERE mytable.col = 9";

0 commit comments

Comments
 (0)