Skip to content

Commit

Permalink
- solved critical grammar bug regarding concat expressions and parent…
Browse files Browse the repository at this point in the history
…hesis parsing

- introduced some tests to cover the above
- corrected ExpressionDeparser to deliver same result as toString for substractions
  • Loading branch information
wumpz committed May 16, 2013
1 parent 3bc8dd3 commit 5136523
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public void visit(StringValue stringValue) {

@Override
public void visit(Subtraction subtraction) {
visitBinaryExpression(subtraction, "-");
visitBinaryExpression(subtraction, " - ");

}

Expand Down
4 changes: 2 additions & 2 deletions src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ Expression MultiplicativeExpression():
LOOKAHEAD(BitwiseXor())
leftExpression=BitwiseXor()
|
"(" leftExpression=AdditiveExpression() ")" {leftExpression = new Parenthesis(leftExpression); }
"(" leftExpression=ConcatExpression() ")" {leftExpression = new Parenthesis(leftExpression); }
)
{ result = leftExpression; }
(
Expand All @@ -1447,7 +1447,7 @@ Expression MultiplicativeExpression():
LOOKAHEAD(BitwiseXor())
rightExpression=BitwiseXor()
|
"(" rightExpression=AdditiveExpression() ")" {rightExpression = new Parenthesis(rightExpression); }
"(" rightExpression=ConcatExpression() ")" {rightExpression = new Parenthesis(rightExpression); }
)

{
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,55 @@ public void testConcat() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(statement);
}

public void testConcatProblem2() throws JSQLParserException {
String stmt = "SELECT MAX(((((" +
"(SPA.SOORTAANLEVERPERIODE)::VARCHAR (2) || (VARCHAR(SPA.AANLEVERPERIODEJAAR))::VARCHAR (4)" +
") || TO_CHAR(SPA.AANLEVERPERIODEVOLGNR, 'FM09'::VARCHAR)" +
") || TO_CHAR((10000 - SPA.VERSCHIJNINGSVOLGNR), 'FM0999'::VARCHAR)" +
") || (SPA.GESLACHT)::VARCHAR (1))) AS GESLACHT_TMP FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_1() throws JSQLParserException {
String stmt = "SELECT TO_CHAR(SPA.AANLEVERPERIODEVOLGNR, 'FM09'::VARCHAR) FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_2() throws JSQLParserException {
String stmt = "SELECT MAX((SPA.SOORTAANLEVERPERIODE)::VARCHAR (2) || (VARCHAR(SPA.AANLEVERPERIODEJAAR))::VARCHAR (4)) AS GESLACHT_TMP FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_3() throws JSQLParserException {
String stmt = "SELECT TO_CHAR((10000 - SPA.VERSCHIJNINGSVOLGNR), 'FM0999'::VARCHAR) FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_4() throws JSQLParserException {
String stmt = "SELECT (SPA.GESLACHT)::VARCHAR (1) FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_5() throws JSQLParserException {
String stmt = "SELECT max((a || b) || c) FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_5_1() throws JSQLParserException {
String stmt = "SELECT (a || b) || c FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_5_2() throws JSQLParserException {
String stmt = "SELECT (a + b) + c FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testConcatProblem2_6() throws JSQLParserException {
String stmt = "SELECT max(a || b || c) FROM testtable";
assertSqlCanBeParsedAndDeparsed(stmt);
}

public void testMatches() throws JSQLParserException {
String statement = "SELECT * FROM team WHERE team.search_column @@ to_tsquery('new & york & yankees')";
assertSqlCanBeParsedAndDeparsed(statement);
Expand Down

0 comments on commit 5136523

Please sign in to comment.