Skip to content

Commit 5136523

Browse files
committed
- solved critical grammar bug regarding concat expressions and parenthesis parsing
- introduced some tests to cover the above - corrected ExpressionDeparser to deliver same result as toString for substractions
1 parent 3bc8dd3 commit 5136523

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public void visit(StringValue stringValue) {
292292

293293
@Override
294294
public void visit(Subtraction subtraction) {
295-
visitBinaryExpression(subtraction, "-");
295+
visitBinaryExpression(subtraction, " - ");
296296

297297
}
298298

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ Expression MultiplicativeExpression():
14341434
LOOKAHEAD(BitwiseXor())
14351435
leftExpression=BitwiseXor()
14361436
|
1437-
"(" leftExpression=AdditiveExpression() ")" {leftExpression = new Parenthesis(leftExpression); }
1437+
"(" leftExpression=ConcatExpression() ")" {leftExpression = new Parenthesis(leftExpression); }
14381438
)
14391439
{ result = leftExpression; }
14401440
(
@@ -1447,7 +1447,7 @@ Expression MultiplicativeExpression():
14471447
LOOKAHEAD(BitwiseXor())
14481448
rightExpression=BitwiseXor()
14491449
|
1450-
"(" rightExpression=AdditiveExpression() ")" {rightExpression = new Parenthesis(rightExpression); }
1450+
"(" rightExpression=ConcatExpression() ")" {rightExpression = new Parenthesis(rightExpression); }
14511451
)
14521452

14531453
{

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,55 @@ public void testConcat() throws JSQLParserException {
547547
assertSqlCanBeParsedAndDeparsed(statement);
548548
}
549549

550+
public void testConcatProblem2() throws JSQLParserException {
551+
String stmt = "SELECT MAX(((((" +
552+
"(SPA.SOORTAANLEVERPERIODE)::VARCHAR (2) || (VARCHAR(SPA.AANLEVERPERIODEJAAR))::VARCHAR (4)" +
553+
") || TO_CHAR(SPA.AANLEVERPERIODEVOLGNR, 'FM09'::VARCHAR)" +
554+
") || TO_CHAR((10000 - SPA.VERSCHIJNINGSVOLGNR), 'FM0999'::VARCHAR)" +
555+
") || (SPA.GESLACHT)::VARCHAR (1))) AS GESLACHT_TMP FROM testtable";
556+
assertSqlCanBeParsedAndDeparsed(stmt);
557+
}
558+
559+
public void testConcatProblem2_1() throws JSQLParserException {
560+
String stmt = "SELECT TO_CHAR(SPA.AANLEVERPERIODEVOLGNR, 'FM09'::VARCHAR) FROM testtable";
561+
assertSqlCanBeParsedAndDeparsed(stmt);
562+
}
563+
564+
public void testConcatProblem2_2() throws JSQLParserException {
565+
String stmt = "SELECT MAX((SPA.SOORTAANLEVERPERIODE)::VARCHAR (2) || (VARCHAR(SPA.AANLEVERPERIODEJAAR))::VARCHAR (4)) AS GESLACHT_TMP FROM testtable";
566+
assertSqlCanBeParsedAndDeparsed(stmt);
567+
}
568+
569+
public void testConcatProblem2_3() throws JSQLParserException {
570+
String stmt = "SELECT TO_CHAR((10000 - SPA.VERSCHIJNINGSVOLGNR), 'FM0999'::VARCHAR) FROM testtable";
571+
assertSqlCanBeParsedAndDeparsed(stmt);
572+
}
573+
574+
public void testConcatProblem2_4() throws JSQLParserException {
575+
String stmt = "SELECT (SPA.GESLACHT)::VARCHAR (1) FROM testtable";
576+
assertSqlCanBeParsedAndDeparsed(stmt);
577+
}
578+
579+
public void testConcatProblem2_5() throws JSQLParserException {
580+
String stmt = "SELECT max((a || b) || c) FROM testtable";
581+
assertSqlCanBeParsedAndDeparsed(stmt);
582+
}
583+
584+
public void testConcatProblem2_5_1() throws JSQLParserException {
585+
String stmt = "SELECT (a || b) || c FROM testtable";
586+
assertSqlCanBeParsedAndDeparsed(stmt);
587+
}
588+
589+
public void testConcatProblem2_5_2() throws JSQLParserException {
590+
String stmt = "SELECT (a + b) + c FROM testtable";
591+
assertSqlCanBeParsedAndDeparsed(stmt);
592+
}
593+
594+
public void testConcatProblem2_6() throws JSQLParserException {
595+
String stmt = "SELECT max(a || b || c) FROM testtable";
596+
assertSqlCanBeParsedAndDeparsed(stmt);
597+
}
598+
550599
public void testMatches() throws JSQLParserException {
551600
String statement = "SELECT * FROM team WHERE team.search_column @@ to_tsquery('new & york & yankees')";
552601
assertSqlCanBeParsedAndDeparsed(statement);

0 commit comments

Comments
 (0)