Skip to content
Merged
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
13 changes: 11 additions & 2 deletions pmd-rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ under the License.
<rule ref="category/java/codestyle.xml/UselessParentheses" />
<rule ref="category/java/codestyle.xml/UselessQualifiedThis" />

<!-- for Codazy -->
<rule ref="category/java/codestyle.xml/MethodNamingConventions" />
<!-- for Codazy -->
<rule ref="category/java/codestyle.xml/MethodNamingConventions">
<properties>
<property name="methodPattern" value="[a-z][a-zA-Z0-9]*" />
<property name="staticPattern" value="[a-z][a-zA-Z0-9]*" />
<property name="nativePattern" value="[a-z][a-zA-Z0-9]*" />
<property name="junit3TestPattern" value="test[A-Z0-9][a-zA-Z0-9]*" />
<property name="junit4TestPattern" value="[a-z][a-zA-Z0-9]*" />
<property name="junit5TestPattern" value="[a-z][a-zA-Z0-9]*" />
</properties>
</rule>
<rule ref="category/java/codestyle.xml/ClassNamingConventions" />

<rule ref="category/java/design.xml/CollapsibleIfStatements" />
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/net/sf/jsqlparser/expression/CaseExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
*/
public class CaseExpression extends ASTNodeAccessImpl implements Expression {

private boolean usingBrackets = false;
private Expression switchExpression;
private List<WhenClause> whenClauses;
private Expression elseExpression;
Expand Down Expand Up @@ -90,9 +91,9 @@ public void setWhenClauses(List<WhenClause> whenClauses) {

@Override
public String toString() {
return "CASE " + ((switchExpression != null) ? switchExpression + " " : "")
return (usingBrackets ? "(" : "") + "CASE " + ((switchExpression != null) ? switchExpression + " " : "")
+ PlainSelect.getStringList(whenClauses, false, false) + " "
+ ((elseExpression != null) ? "ELSE " + elseExpression + " " : "") + "END";
+ ((elseExpression != null) ? "ELSE " + elseExpression + " " : "") + "END" + (usingBrackets ? ")" : "");
}

public CaseExpression withSwitchExpression(Expression switchExpression) {
Expand Down Expand Up @@ -129,4 +130,26 @@ public <E extends Expression> E getSwitchExpression(Class<E> type) {
public <E extends Expression> E getElseExpression(Class<E> type) {
return type.cast(getElseExpression());
}

/**
* @return the usingBrackets
*/
public boolean isUsingBrackets() {
return usingBrackets;
}

/**
* @param usingBrackets the usingBrackets to set
*/
public void setUsingBrackets(boolean usingBrackets) {
this.usingBrackets = usingBrackets;
}

/**
* @param usingBrackets the usingBrackets to set
*/
public CaseExpression withUsingBrackets(boolean usingBrackets) {
this.usingBrackets=usingBrackets;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ public void visit(TimeValue timeValue) {

@Override
public void visit(CaseExpression caseExpression) {
buffer.append("CASE ");
buffer.append(caseExpression.isUsingBrackets() ? "(" : "").append("CASE ");
Expression switchExp = caseExpression.getSwitchExpression();
if (switchExp != null) {
switchExp.accept(this);
Expand All @@ -622,7 +622,7 @@ public void visit(CaseExpression caseExpression) {
buffer.append(" ");
}

buffer.append("END");
buffer.append("END").append(caseExpression.isUsingBrackets() ? ")" : "");
}

@Override
Expand Down
17 changes: 12 additions & 5 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import java.util.*;
* The parser generated by JavaCC
*/
public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
public int bracketsCounter = 0;
public int caseCounter = 0;

public CCJSqlParser withConfiguration(FeatureConfiguration configuration) {
token_source.configuration = configuration;
Expand Down Expand Up @@ -3697,11 +3699,14 @@ Expression CaseWhenExpression() #CaseWhenExpression:
Expression elseExp = null;
}
{
<K_CASE>
<K_CASE> { caseCounter++; }
[ switchExp=Condition() ]
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
[<K_ELSE> elseExp=Condition()]
<K_END>
[<K_ELSE> (LOOKAHEAD( ["("] CaseWhenExpression() [")"] ) ["("] elseExp=CaseWhenExpression() [")" { ((CaseExpression) elseExp).setUsingBrackets(true); } ]
| elseExp=Condition()
)
]
<K_END> { caseCounter--; }
{
caseExp.setSwitchExpression(switchExp);
caseExp.setWhenClauses(whenClauses);
Expand All @@ -3717,8 +3722,10 @@ WhenClause WhenThenSearchCondition():
Expression thenExp = null;
}
{
<K_WHEN> (LOOKAHEAD(Expression()) whenExp=Expression() | whenExp=SimpleExpression())
<K_THEN> thenExp=Expression()
<K_WHEN> whenExp=Expression()
<K_THEN> (LOOKAHEAD( ["("] CaseWhenExpression() [")"] ) ["("] thenExp=CaseWhenExpression() [")" { ((CaseExpression) thenExp).setUsingBrackets(true); }]
| thenExp=Expression()
)
{
whenThen.setWhenExpression(whenExp);
whenThen.setThenExpression(thenExp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,73 @@ public void testIssue235() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN ( CASE WHEN ( CASE WHEN ( CASE WHEN ( 1 ) THEN 0 END ) THEN 0 END ) THEN 0 END ) THEN 0 END FROM a", true);
}

@Test(timeout = 100000)
@Test
public void testNestedCaseWhenWithoutBracketsIssue1162() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE VIEW VIEW_NAME1 AS\n" +
"SELECT CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE CASE WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT' ELSE '0' END END END END END END END END END END END END END END COLUMNALIAS\n" +
"FROM TABLE1", true);
}

@Test
public void testNestedCaseWhenWithBracketsIssue1162() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE VIEW VIEW_NAME1 AS\n" +
"SELECT CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE\n" +
"WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT'\n" +
"ELSE (CASE WHEN WDGFLD.PORTTYPE = 1 THEN 'INPUT PORT' ELSE '0' END) END) END) END) END) END) END) END) END) END) END) END) END) END COLUMNALIAS\n" +
"FROM TABLE1", true);
}

@Test(timeout = 200000)
@Ignore
public void testIssue496() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select isNull(charLen(TEST_ID,0)+ isNull(charLen(TEST_DVC,0)+ isNull(charLen(TEST_NO,0)+ isNull(charLen(ATEST_ID,0)+ isNull(charLen(TESTNO,0)+ isNull(charLen(TEST_CTNT,0)+ isNull(charLen(TEST_MESG_CTNT,0)+ isNull(charLen(TEST_DTM,0)+ isNull(charLen(TEST_DTT,0)+ isNull(charLen(TEST_ADTT,0)+ isNull(charLen(TEST_TCD,0)+ isNull(charLen(TEST_PD,0)+ isNull(charLen(TEST_VAL,0)+ isNull(charLen(TEST_YN,0)+ isNull(charLen(TEST_DTACM,0)+ isNull(charLen(TEST_MST,0) from test_info_m");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,12 @@ public void testCase() throws JSQLParserException {
+ // "WHEN (SELECT c FROM tab2 WHERE d = 2) = 3 THEN 'AAA' " +
"END) FROM tab1";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testNestedCaseCondition() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT CASE WHEN CASE WHEN 1 THEN 10 ELSE 20 END > 15 THEN 'BBB' END FROM tab1");
assertSqlCanBeParsedAndDeparsed("SELECT (CASE WHEN (CASE a WHEN 1 THEN 10 ELSE 20 END) > 15 THEN 'BBB' END) FROM tab1");
}

@Test
Expand Down