Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/net/sf/jsqlparser/expression/CaseExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
public class CaseExpression extends ASTNodeAccessImpl implements Expression {

private Expression caseExpression;
private Expression switchExpression;
private List<WhenClause> whenClauses;
private Expression elseExpression;
Expand All @@ -57,6 +58,14 @@ public void setSwitchExpression(Expression switchExpression) {
this.switchExpression = switchExpression;
}

public Expression getCaseExpression() {
return caseExpression;
}

public void setCaseExpression(Expression caseExpression) {
this.caseExpression = caseExpression;
}

/**
* @return Returns the elseExpression.
*/
Expand Down Expand Up @@ -87,7 +96,8 @@ public void setWhenClauses(List<WhenClause> whenClauses) {

@Override
public String toString() {
return "CASE " + ((switchExpression != null) ? switchExpression + " " : "")
return "CASE " + ((caseExpression != null) ? caseExpression + " " : "") +
((switchExpression != null) ? switchExpression + " " : "")
+ PlainSelect.getStringList(whenClauses, false, false) + " "
+ ((elseExpression != null) ? "ELSE " + elseExpression + " " : "") + "END";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ public void visit(SubSelect subSelect) {

@Override
public void visit(CaseExpression expr) {
if (expr.getCaseExpression() != null) {
expr.getCaseExpression().accept(this);
}
if (expr.getSwitchExpression() != null) {
expr.getSwitchExpression().accept(this);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ public void visit(TimeValue timeValue) {
*/
@Override
public void visit(CaseExpression caseExpression) {
if (caseExpression.getCaseExpression() != null) {
caseExpression.getCaseExpression().accept(this);
}
if (caseExpression.getSwitchExpression() != null) {
caseExpression.getSwitchExpression().accept(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,13 @@ public void visit(TimeValue timeValue) {
@Override
public void visit(CaseExpression caseExpression) {
buffer.append("CASE ");

Expression caseExp = caseExpression.getCaseExpression();
if (caseExp != null) {
caseExp.accept(this);
buffer.append(" ");
}

Expression switchExp = caseExpression.getSwitchExpression();
if (switchExp != null) {
switchExp.accept(this);
Expand Down
4 changes: 3 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3283,12 +3283,13 @@ Expression CaseWhenExpression() #CaseWhenExpression:
{
CaseExpression caseExp = new CaseExpression();
Expression switchExp = null;
Expression caseExpr = null;
WhenClause clause;
List<WhenClause> whenClauses = new ArrayList<WhenClause>();
Expression elseExp = null;
}
{
<K_CASE>
<K_CASE> [ (LOOKAHEAD(Expression()) caseExpr=Expression() | caseExpr=SimpleExpression()) ]
(
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
|
Expand All @@ -3298,6 +3299,7 @@ Expression CaseWhenExpression() #CaseWhenExpression:
[<K_ELSE> elseExp=Condition()]
<K_END>
{
caseExp.setCaseExpression(caseExpr);
caseExp.setSwitchExpression(switchExp);
caseExp.setWhenClauses(whenClauses);
caseExp.setElseExpression(elseExp);
Expand Down
25 changes: 15 additions & 10 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,11 @@ public void testExpressionsInIntervalExpression() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT DATE_SUB(mydate, INTERVAL DAY(anotherdate) - 1 DAY) FROM tbl");
}

@Test
public void testExpressionsInCaseBeforeWhen() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT a FROM tbl1 LEFT JOIN tbl2 ON CASE tbl1.col1 WHEN tbl1.col1 = 1 THEN tbl1.col2 = tbl2.col2 ELSE tbl1.col3 = tbl2.col3 END");
}

@Test
public void testReplaceAsFunction() throws JSQLParserException {
String statement = "SELECT REPLACE(a, 'b', c) FROM tab1";
Expand Down Expand Up @@ -3996,53 +4001,53 @@ public void testCaseWithComplexWhenExpression() throws JSQLParserException {
+ "ELSE 1 = 1\n"
+ "END\n", true);
}

@Test
public void testOrderKeywordIssue932() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT order FROM tmp3");
assertSqlCanBeParsedAndDeparsed("SELECT tmp3.order FROM tmp3");
}

@Test
public void testOrderKeywordIssue932_2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT group FROM tmp3");
assertSqlCanBeParsedAndDeparsed("SELECT tmp3.group FROM tmp3");
}

@Test
public void testTableFunctionInExprIssue923() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE func(a) IN func(b)");
}

// @Test
// public void testTableFunctionInExprIssue923_2() throws JSQLParserException, IOException {
// String stmt = IOUtils.toString(
// SelectTest.class.getResourceAsStream("large-sql-issue-923.txt"), "UTF-8")
// .replace("@Prompt", "MyFunc");
// assertSqlCanBeParsedAndDeparsed(stmt, true);
// }

@Test
public void testTableFunctionInExprIssue923_3() throws JSQLParserException, IOException {
String stmt = IOUtils.toString(
SelectTest.class.getResourceAsStream("large-sql-issue-923-2.txt"), "UTF-8");
assertSqlCanBeParsedAndDeparsed(stmt, true);
}

@Test
public void testTableFunctionInExprIssue923_4() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT MAX(CASE WHEN DUPLICATE_CLAIM_NUMBER IN '1' THEN COALESCE(CLAIM_STATUS2,CLAIM_STATUS1) ELSE NULL END) AS DUPE_1_KINAL_CLAIM_STATUS", true);
}

@Test
public void testTableFunctionInExprIssue923_5() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN DUPLICATE_CLAIM_NUMBER IN '1' THEN COALESCE(CLAIM_STATUS2,CLAIM_STATUS1) ELSE NULL END", true);
}

@Test
public void testTableFunctionInExprIssue923_6() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE func(a) IN '1'");
}


}