Skip to content

Commit 335286c

Browse files
committed
Merge origin/master
Conflicts: src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
2 parents cc35549 + 1899cbf commit 335286c

File tree

9 files changed

+113
-5
lines changed

9 files changed

+113
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Also I would like to know about needed examples or documentation stuff.
4343

4444
## Extensions in the latest SNAPSHOT version 0.9.7
4545

46+
* introduced NOT without parenthesis for column only conditions
47+
* introduced more complex expressions within CASE - statements
4648
* improved Postgresql JSON - support
4749
* integrated some Postgresql create table contraints
4850

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,6 @@ public interface ExpressionVisitor {
172172

173173
void visit(DateTimeLiteralExpression literal);
174174

175+
public void visit(NotExpression aThis);
176+
175177
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ public void visit(Column column) {
213213
@Override
214214
public void visit(SubSelect subSelect) {
215215
if (selectVisitor != null) {
216-
for (WithItem item : subSelect.getWithItemsList()) {
217-
item.accept(selectVisitor);
216+
if (subSelect.getWithItemsList() != null) {
217+
for (WithItem item : subSelect.getWithItemsList()) {
218+
item.accept(selectVisitor);
219+
}
218220
}
219221
subSelect.getSelectBody().accept(selectVisitor);
220222
}
@@ -339,6 +341,11 @@ public void visit(MultiExpressionList multiExprList) {
339341
}
340342
}
341343

344+
@Override
345+
public void visit(NotExpression notExpr) {
346+
notExpr.getExpression().accept(this);
347+
}
348+
342349
protected void visitBinaryExpression(BinaryExpression expr) {
343350
expr.getLeftExpression().accept(this);
344351
expr.getRightExpression().accept(this);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2017 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.expression;
23+
24+
/**
25+
* It represents a "-" or "+" before an expression
26+
*/
27+
public class NotExpression implements Expression {
28+
29+
private Expression expression;
30+
31+
public NotExpression(Expression expression) {
32+
setExpression(expression);
33+
}
34+
35+
public Expression getExpression() {
36+
return expression;
37+
}
38+
39+
public final void setExpression(Expression expression) {
40+
this.expression = expression;
41+
}
42+
43+
@Override
44+
public void accept(ExpressionVisitor expressionVisitor) {
45+
expressionVisitor.visit(this);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "NOT " + expression.toString();
51+
}
52+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ public void visit(Subtraction subtraction) {
282282
visitBinaryExpression(subtraction);
283283
}
284284

285+
@Override
286+
public void visit(NotExpression notExpr) {
287+
notExpr.getExpression().accept(this);
288+
}
289+
285290
public void visitBinaryExpression(BinaryExpression binaryExpression) {
286291
binaryExpression.getLeftExpression().accept(this);
287292
binaryExpression.getRightExpression().accept(this);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public void visit(HexValue hexValue) {
118118
buffer.append(hexValue.toString());
119119
}
120120

121+
@Override
122+
public void visit(NotExpression notExpr) {
123+
buffer.append(NOT);
124+
notExpr.getExpression().accept(this);
125+
}
126+
121127
public void visitOldOracleJoinBinaryExpression(OldOracleJoinBinaryExpression expression, String operator) {
122128
if (expression.isNot()) {
123129
buffer.append(NOT);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ Expression Condition():
16911691
(LOOKAHEAD(SQLCondition()) result=SQLCondition()
16921692
| LOOKAHEAD(RegularCondition()) result=RegularCondition()
16931693
| LOOKAHEAD(Function()) result=Function()
1694+
| <K_NOT> result=Column() { result = new NotExpression(result); }
16941695
| result=Column()
16951696
| LOOKAHEAD({ "0".equals(getToken(1).image) || "1".equals(getToken(1).image) }) token=<S_LONG> { result = new LongValue(token.image); }
16961697
)
@@ -2433,8 +2434,8 @@ Expression CaseWhenExpression() #CaseWhenExpression:
24332434
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
24342435
[<K_ELSE> elseExp=SimpleExpression()]
24352436
|
2436-
switchExp=PrimaryExpression()
2437-
( clause=WhenThenValue() { whenClauses.add(clause); } )*
2437+
(LOOKAHEAD(RegularCondition()) switchExp=RegularCondition() | switchExp=BitwiseAndOr())
2438+
( clause=WhenThenValue() { whenClauses.add(clause); } )+
24382439
[<K_ELSE> elseExp=SimpleExpression()]
24392440
)
24402441
<K_END>

src/test/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapterTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import net.sf.jsqlparser.schema.Column;
2828
import net.sf.jsqlparser.statement.select.PlainSelect;
2929
import net.sf.jsqlparser.statement.select.Select;
30+
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
3031
import org.junit.After;
3132
import org.junit.AfterClass;
3233
import static org.junit.Assert.assertNotNull;
3334
import static org.junit.Assert.assertNull;
3435
import static org.junit.Assert.assertTrue;
3536
import static org.junit.Assert.assertFalse;
3637
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.fail;
3739
import org.junit.Before;
3840
import org.junit.BeforeClass;
3941
import org.junit.Test;
@@ -169,4 +171,19 @@ public void visit(Column column) {
169171
assertEquals(1, columnList.size());
170172
assertEquals("bar", columnList.get(0));
171173
}
174+
175+
@Test
176+
public void testSubSelectExpressionProblem() throws JSQLParserException {
177+
Select select = (Select) CCJSqlParserUtil.parse( "SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2.col2 = t1.col1)" );
178+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
179+
Expression where = plainSelect.getWhere();
180+
ExpressionVisitorAdapter adapter = new ExpressionVisitorAdapter();
181+
adapter.setSelectVisitor(new SelectVisitorAdapter());
182+
try {
183+
where.accept(adapter);
184+
} catch (NullPointerException npe){
185+
fail();
186+
}
187+
}
188+
172189
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,14 @@ public void testCase() throws JSQLParserException {
941941
assertSqlCanBeParsedAndDeparsed(statement);
942942

943943
}
944+
945+
public void testIssue371SimplifiedCase() throws JSQLParserException {
946+
assertSqlCanBeParsedAndDeparsed("SELECT CASE col + 4 WHEN 2 THEN 1 ELSE 0 END");
947+
}
948+
949+
public void testIssue371SimplifiedCase2() throws JSQLParserException {
950+
assertSqlCanBeParsedAndDeparsed("SELECT CASE col > 4 WHEN true THEN 1 ELSE 0 END");
951+
}
944952

945953
public void testReplaceAsFunction() throws JSQLParserException {
946954
String statement = "SELECT REPLACE(a, 'b', c) FROM tab1";
@@ -2287,6 +2295,14 @@ public void testProblemIsIssue331() throws JSQLParserException {
22872295
}
22882296

22892297
public void testProblemIssue375() throws JSQLParserException {
2290-
assertSqlCanBeParsedAndDeparsed("select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, c.relhasrules, c.relkind, c.oid, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum");
2298+
assertSqlCanBeParsedAndDeparsed("select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, c.relhasrules, c.relkind, c.oid, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum", true);
2299+
}
2300+
2301+
public void testProblemIssue375Simplified() throws JSQLParserException {
2302+
assertSqlCanBeParsedAndDeparsed("select * from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum", true);
2303+
}
2304+
2305+
public void testProblemIssue375Simplified2() throws JSQLParserException {
2306+
assertSqlCanBeParsedAndDeparsed("select * from (pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid", true);
22912307
}
22922308
}

0 commit comments

Comments
 (0)