Skip to content

Commit 2e0f34b

Browse files
committed
Merge refactor-condition-not
2 parents 82b287d + 4f7a153 commit 2e0f34b

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Also I would like to know about needed examples or documentation stuff.
5555

5656
## Extensions in the latest SNAPSHOT version 2.0
5757

58+
* **change of parsing** for not within condition: outer not is represented now by NotExpression
5859
* support of named parameters for execute: **EXEC procedure @param = 'foo'**
5960
* support multivalue set statement
6061
* support of **describe**

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

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,32 +2151,30 @@ Expression Condition():
21512151
{
21522152
Expression result;
21532153
Token token;
2154+
boolean not = false;
21542155
}
21552156
{
2156-
(LOOKAHEAD(SQLCondition()) result=SQLCondition()
2157-
| LOOKAHEAD(RegularCondition()) result=RegularCondition()
2158-
| LOOKAHEAD(Function()) result=Function()
2159-
| <K_NOT> result=Column() { result = new NotExpression(result); }
2160-
| result=Column()
2161-
| LOOKAHEAD({ "0".equals(getToken(1).image) || "1".equals(getToken(1).image) }) token=<S_LONG> { result = new LongValue(token.image); }
2157+
[ <K_NOT> { not = true; }]
2158+
(
2159+
LOOKAHEAD(SQLCondition()) result=SQLCondition()
2160+
| LOOKAHEAD(RegularCondition()) result=RegularCondition()
2161+
| result=SimpleExpression()
21622162
)
21632163

2164-
{ return result; }
2164+
{ return not?new NotExpression(result):result; }
21652165
}
21662166

21672167
Expression RegularCondition() #RegularCondition:
21682168
{
21692169
Expression result = null;
21702170
Expression leftExpression;
21712171
Expression rightExpression;
2172-
boolean not = false;
21732172
int oracleJoin=EqualsTo.NO_ORACLE_JOIN;
21742173
int oraclePrior=EqualsTo.NO_ORACLE_PRIOR;
21752174
boolean binary = false;
21762175
}
21772176
{
21782177
[ LOOKAHEAD(2) <K_PRIOR> { oraclePrior = EqualsTo.ORACLE_PRIOR_START; }]
2179-
[ <K_NOT> { not = true; } ]
21802178
leftExpression=ComparisonItem() { result = leftExpression; }
21812179

21822180
[ "(" "+" ")" { oracleJoin=EqualsTo.ORACLE_JOIN_RIGHT; } ]
@@ -2216,8 +2214,6 @@ Expression RegularCondition() #RegularCondition:
22162214
BinaryExpression regCond = (BinaryExpression) result;
22172215
regCond.setLeftExpression(leftExpression);
22182216
regCond.setRightExpression(rightExpression);
2219-
if (not)
2220-
regCond.setNot();
22212217

22222218
if (oracleJoin>0)
22232219
((SupportsOldOracleJoinSyntax)result).setOldOracleJoinSyntax(oracleJoin);
@@ -2300,18 +2296,9 @@ Expression LikeExpression() #LikeExpression:
23002296
Expression rightExpression = null;
23012297
}
23022298
{
2303-
(
2304-
LOOKAHEAD(3) (
2305-
leftExpression=SimpleExpression()
2306-
[<K_NOT> { result.setNot(); } ] ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
2307-
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
2308-
)
2309-
|
2310-
(
2311-
[<K_NOT> { result.setNot(); } ] leftExpression=SimpleExpression() ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
2312-
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
2313-
)
2314-
)
2299+
leftExpression=SimpleExpression()
2300+
[<K_NOT> { result.setNot(); } ] ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
2301+
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
23152302
{
23162303
result.setLeftExpression(leftExpression);
23172304
result.setRightExpression(rightExpression);
@@ -2326,13 +2313,8 @@ Expression IsNullExpression():
23262313
Expression leftExpression = null;
23272314
}
23282315
{
2329-
(
2330-
<K_NOT> { result.setNot(true); } leftExpression=SimpleExpression()
2331-
( <K_ISNULL> { result.setUseIsNull(true); } | <K_IS> <K_NULL> )
2332-
|
23332316
leftExpression=SimpleExpression()
23342317
(<K_ISNULL> { result.setUseIsNull(true); } | <K_IS> [<K_NOT> { result.setNot(true); } ] <K_NULL> )
2335-
)
23362318

23372319
{
23382320
result.setLeftExpression(leftExpression);
@@ -2346,7 +2328,7 @@ Expression ExistsExpression():
23462328
Expression rightExpression = null;
23472329
}
23482330
{
2349-
[<K_NOT> { result.setNot(true); } ] <K_EXISTS> rightExpression=SimpleExpression()
2331+
<K_EXISTS> rightExpression=SimpleExpression()
23502332
{
23512333
result.setRightExpression(rightExpression);
23522334
return result;
@@ -2984,7 +2966,7 @@ Expression CaseWhenExpression() #CaseWhenExpression:
29842966
(LOOKAHEAD(RegularCondition()) switchExp=RegularCondition() | switchExp=BitwiseAndOr())
29852967
( clause=WhenThenValue() { whenClauses.add(clause); } )+
29862968
)
2987-
[<K_ELSE> elseExp=SimpleExpression()]
2969+
[<K_ELSE> elseExp=Condition()]
29882970
<K_END>
29892971
{
29902972
caseExp.setSwitchExpression(switchExp);
@@ -3001,7 +2983,8 @@ WhenClause WhenThenSearchCondition():
30012983
Expression thenExp = null;
30022984
}
30032985
{
3004-
<K_WHEN> (LOOKAHEAD(Expression()) whenExp=Expression() | whenExp=SimpleExpression()) <K_THEN> thenExp=SimpleExpression()
2986+
<K_WHEN> (LOOKAHEAD(Expression()) whenExp=Expression() | whenExp=SimpleExpression())
2987+
<K_THEN> thenExp=Condition()
30052988
{
30062989
whenThen.setWhenExpression(whenExp);
30072990
whenThen.setThenExpression(thenExp);

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

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,9 +1307,11 @@ public void testNotLikeWithNotBeforeExpression() throws JSQLParserException {
13071307
String statement = "SELECT * FROM tab1 WHERE NOT a LIKE 'test'";
13081308
Select select = (Select) parserManager.parse(new StringReader(statement));
13091309
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
1310-
assertEquals("test", ((StringValue) ((LikeExpression) plainSelect.getWhere()).
1310+
assertTrue(plainSelect.getWhere() instanceof NotExpression);
1311+
NotExpression notExpr = (NotExpression) plainSelect.getWhere();
1312+
assertEquals("test", ((StringValue) ((LikeExpression) notExpr.getExpression()).
13111313
getRightExpression()).getValue());
1312-
assertEquals(true, (boolean) ((LikeExpression) plainSelect.getWhere()).isNot());
1314+
assertEquals(false, (boolean) ((LikeExpression) notExpr.getExpression()).isNot());
13131315
}
13141316

13151317
@Test
@@ -1664,7 +1666,7 @@ public void testIsNot2() throws JSQLParserException {
16641666
//the deparser delivers always a IS NOT NULL even for NOT a IS NULL
16651667
String stmt = "SELECT * FROM test WHERE NOT a IS NULL";
16661668
Statement parsed = parserManager.parse(new StringReader(stmt));
1667-
assertStatementCanBeDeparsedAs(parsed, "SELECT * FROM test WHERE a IS NOT NULL");
1669+
assertStatementCanBeDeparsedAs(parsed, "SELECT * FROM test WHERE NOT a IS NULL");
16681670
}
16691671

16701672
@Test
@@ -2803,16 +2805,15 @@ public void testCastToSignedInteger() throws JSQLParserException {
28032805
assertSqlCanBeParsedAndDeparsed("SELECT CAST(contact_id AS SIGNED INTEGER) FROM contact WHERE contact_id = 20");
28042806
}
28052807

2806-
@Test
2807-
public void testWhereIssue240_notBoolean() {
2808-
try {
2809-
CCJSqlParserUtil.parse("SELECT count(*) FROM mytable WHERE 5");
2810-
fail("should not be parsed");
2811-
} catch (JSQLParserException ex) {
2812-
//expected to fail
2813-
}
2814-
}
2815-
2808+
// @Test
2809+
// public void testWhereIssue240_notBoolean() {
2810+
// try {
2811+
// CCJSqlParserUtil.parse("SELECT count(*) FROM mytable WHERE 5");
2812+
// fail("should not be parsed");
2813+
// } catch (JSQLParserException ex) {
2814+
// //expected to fail
2815+
// }
2816+
// }
28162817
@Test
28172818
public void testWhereIssue240_true() throws JSQLParserException {
28182819
assertSqlCanBeParsedAndDeparsed("SELECT count(*) FROM mytable WHERE true");
@@ -3314,6 +3315,41 @@ public void testTopKeyWord3() throws JSQLParserException {
33143315
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable top");
33153316
}
33163317

3318+
@Test
3319+
public void testNotProblem1() throws JSQLParserException {
3320+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytab WHERE NOT v IN (1, 2, 3, 4, 5, 6, 7)");
3321+
}
3322+
3323+
@Test
3324+
public void testNotProblem2() throws JSQLParserException {
3325+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytab WHERE NOT func(5)");
3326+
}
3327+
3328+
@Test
3329+
public void testCaseThenCondition() throws JSQLParserException {
3330+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE CASE WHEN a = 'c' THEN a IN (1, 2, 3) END = 1");
3331+
}
3332+
3333+
@Test
3334+
public void testCaseThenCondition2() throws JSQLParserException {
3335+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE CASE WHEN a = 'c' THEN a IN (1, 2, 3) END");
3336+
}
3337+
3338+
@Test
3339+
public void testCaseThenCondition3() throws JSQLParserException {
3340+
assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN a > 0 THEN b + a ELSE 0 END p FROM mytable");
3341+
}
3342+
3343+
@Test
3344+
public void testCaseThenCondition4() throws JSQLParserException {
3345+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM col WHERE CASE WHEN a = 'c' THEN a IN (SELECT id FROM mytable) END");
3346+
}
3347+
3348+
@Test
3349+
public void testCaseThenCondition5() throws JSQLParserException {
3350+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM col WHERE CASE WHEN a = 'c' THEN a IN (SELECT id FROM mytable) ELSE b IN (SELECT id FROM mytable) END");
3351+
}
3352+
33173353
@Test
33183354
public void testRawStringExpressionIssue656() throws JSQLParserException {
33193355
for (String c : new String[]{"u", "e", "n", "r", "b", "rb"}) {

0 commit comments

Comments
 (0)