test :
private String normalize(String str){
return str.replaceAll("\\s+","\t");
}
public void testParseable() throws JSQLParserException{
String sql = "SELECT CASE WHEN COUNT\(NUMBER\_OF\_TEST\) = 0 THEN 0 " + "ELSE \(CASE WHEN STATUS = 'test' THEN 0 ELSE 1 END\) / COUNT\(NUMBER\_OF\_TEST\)" + " END AS HIT\_RATIO FROM TEST"; sql = normalize\(sql\); Select select = \(Select\) parserManager.parse\(new StringReader\(sql\)\); assertEquals\(sql,normalize\(select.toString\(\)\)\); \}
I changed It to SimpleExpression, but it is valid to use subselects in CASE too.
Expression CaseWhenExpression():
{
CaseExpression caseExp = new CaseExpression();
Expression switchExp = null;
WhenClause clause;
List whenClauses = new ArrayList();
Expression elseExp = null;
}
{
<K_CASE>
(
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
[<K_ELSE> elseExp= !!! SimpleExpression()]
|
switchExp=PrimaryExpression()
( clause=WhenThenValue() { whenClauses.add(clause); } )*
[<K_ELSE> elseExp= !!! SimpleExpression()]
)
<K_END>
{
caseExp.setSwitchExpression(switchExp);
caseExp.setWhenClauses(whenClauses);
caseExp.setElseExpression(elseExp);
return caseExp;
}
}
WhenClause WhenThenSearchCondition():
{
WhenClause whenThen = new WhenClause();
Expression whenExp = null;
Expression thenExp = null;
}
{
<K_WHEN> whenExp=Expression() <K_THEN> thenExp= !!! SimpleExpression()
{
whenThen.setWhenExpression(whenExp);
whenThen.setThenExpression(thenExp);
return whenThen;
}
}