Skip to content

Commit 1956b84

Browse files
committed
refactored outer not from sqlconditions, regularconditions to condition
1 parent ba3bf22 commit 1956b84

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,32 +2151,34 @@ Expression Condition():
21512151
{
21522152
Expression result;
21532153
Token token;
2154+
boolean not = false;
21542155
}
21552156
{
2157+
[ <K_NOT> { not = true; }]
21562158
(LOOKAHEAD(SQLCondition()) result=SQLCondition()
21572159
| LOOKAHEAD(RegularCondition()) result=RegularCondition()
21582160
| LOOKAHEAD(Function()) result=Function()
2159-
| <K_NOT> result=Column() { result = new NotExpression(result); }
2161+
/* | <K_NOT> result=Column() { result = new NotExpression(result); } */
21602162
| result=Column()
21612163
| LOOKAHEAD({ "0".equals(getToken(1).image) || "1".equals(getToken(1).image) }) token=<S_LONG> { result = new LongValue(token.image); }
21622164
)
21632165

2164-
{ return result; }
2166+
{ return not?new NotExpression(result):result; }
21652167
}
21662168

21672169
Expression RegularCondition() #RegularCondition:
21682170
{
21692171
Expression result = null;
21702172
Expression leftExpression;
21712173
Expression rightExpression;
2172-
boolean not = false;
2174+
//boolean not = false;
21732175
int oracleJoin=EqualsTo.NO_ORACLE_JOIN;
21742176
int oraclePrior=EqualsTo.NO_ORACLE_PRIOR;
21752177
boolean binary = false;
21762178
}
21772179
{
21782180
[ LOOKAHEAD(2) <K_PRIOR> { oraclePrior = EqualsTo.ORACLE_PRIOR_START; }]
2179-
[ <K_NOT> { not = true; } ]
2181+
//[ <K_NOT> { not = true; } ]
21802182
leftExpression=ComparisonItem() { result = leftExpression; }
21812183

21822184
[ "(" "+" ")" { oracleJoin=EqualsTo.ORACLE_JOIN_RIGHT; } ]
@@ -2216,8 +2218,8 @@ Expression RegularCondition() #RegularCondition:
22162218
BinaryExpression regCond = (BinaryExpression) result;
22172219
regCond.setLeftExpression(leftExpression);
22182220
regCond.setRightExpression(rightExpression);
2219-
if (not)
2220-
regCond.setNot();
2221+
//if (not)
2222+
// regCond.setNot();
22212223

22222224
if (oracleJoin>0)
22232225
((SupportsOldOracleJoinSyntax)result).setOldOracleJoinSyntax(oracleJoin);
@@ -2300,18 +2302,19 @@ Expression LikeExpression() #LikeExpression:
23002302
Expression rightExpression = null;
23012303
}
23022304
{
2303-
(
2304-
LOOKAHEAD(3) (
2305+
//(
2306+
//LOOKAHEAD(3)
2307+
(
23052308
leftExpression=SimpleExpression()
23062309
[<K_NOT> { result.setNot(); } ] ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
23072310
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
23082311
)
2309-
|
2312+
/* |
23102313
(
23112314
[<K_NOT> { result.setNot(); } ] leftExpression=SimpleExpression() ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
23122315
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
23132316
)
2314-
)
2317+
)*/
23152318
{
23162319
result.setLeftExpression(leftExpression);
23172320
result.setRightExpression(rightExpression);
@@ -2327,9 +2330,9 @@ Expression IsNullExpression():
23272330
}
23282331
{
23292332
(
2330-
<K_NOT> { result.setNot(true); } leftExpression=SimpleExpression()
2333+
/* <K_NOT> { result.setNot(true); } leftExpression=SimpleExpression()
23312334
( <K_ISNULL> { result.setUseIsNull(true); } | <K_IS> <K_NULL> )
2332-
|
2335+
| */
23332336
leftExpression=SimpleExpression()
23342337
(<K_ISNULL> { result.setUseIsNull(true); } | <K_IS> [<K_NOT> { result.setNot(true); } ] <K_NULL> )
23352338
)
@@ -2346,7 +2349,8 @@ Expression ExistsExpression():
23462349
Expression rightExpression = null;
23472350
}
23482351
{
2349-
[<K_NOT> { result.setNot(true); } ] <K_EXISTS> rightExpression=SimpleExpression()
2352+
// [<K_NOT> { result.setNot(true); } ]
2353+
<K_EXISTS> rightExpression=SimpleExpression()
23502354
{
23512355
result.setRightExpression(rightExpression);
23522356
return result;

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

Lines changed: 7 additions & 5 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
@@ -3316,12 +3318,12 @@ public void testTopKeyWord3() throws JSQLParserException {
33163318

33173319
@Test
33183320
public void testNotProblem1() throws JSQLParserException {
3319-
assertSqlCanBeParsedAndDeparsed("select * from col where not v in (1,2,3,4,5,6,7)");
3321+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM col WHERE NOT v IN (1, 2, 3, 4, 5, 6, 7)");
33203322
}
33213323

33223324
@Test
33233325
public void testNotProblem2() throws JSQLParserException {
3324-
assertSqlCanBeParsedAndDeparsed("select * from col where not func(5)");
3326+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM col WHERE NOT func(5)");
33253327
}
33263328

33273329
@Test

0 commit comments

Comments
 (0)