Skip to content

Commit 957f39c

Browse files
committed
fixes #375
fixes #371
1 parent 8c2621a commit 957f39c

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
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
@@ -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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ public void visit(MultiExpressionList multiExprList) {
341341
}
342342
}
343343

344+
@Override
345+
public void visit(NotExpression notExpr) {
346+
notExpr.getExpression().accept(this);
347+
}
348+
344349
protected void visitBinaryExpression(BinaryExpression expr) {
345350
expr.getLeftExpression().accept(this);
346351
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/test/select/SelectTest.java

Lines changed: 20 additions & 0 deletions
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";
@@ -2281,4 +2289,16 @@ public void testProblemKeywordCommitIssue341() throws JSQLParserException {
22812289
public void testProblemIsIssue331() throws JSQLParserException {
22822290
assertSqlCanBeParsedAndDeparsed("SELECT C_DocType.C_DocType_ID,NULL,COALESCE(C_DocType_Trl.Name,C_DocType.Name) AS Name,C_DocType.IsActive FROM C_DocType LEFT JOIN C_DocType_TRL ON (C_DocType.C_DocType_ID=C_DocType_Trl.C_DocType_ID AND C_DocType_Trl.AD_Language='es_AR') WHERE C_DocType.AD_Client_ID=1010016 AND C_DocType.AD_Client_ID IN (0,1010016) AND C_DocType.c_doctype_id in ( select c_doctype2.c_doctype_id from c_doctype as c_doctype2 where substring( c_doctype2.printname,6, length(c_doctype2.printname) ) = ( select letra from c_letra_comprobante as clc where clc.c_letra_comprobante_id = 1010039) ) AND ( (1010094!=0 AND C_DocType.ad_org_id = 1010094) OR 1010094=0 ) ORDER BY 3 LIMIT 2000", true);
22832291
}
2292+
2293+
public void testProblemIssue375() throws JSQLParserException {
2294+
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);
2295+
}
2296+
2297+
public void testProblemIssue375Simplified() throws JSQLParserException {
2298+
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);
2299+
}
2300+
2301+
public void testProblemIssue375Simplified2() 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", true);
2303+
}
22842304
}

0 commit comments

Comments
 (0)