You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use JSqlParse to parse some SQL for get formula result;
For example: SELECT CASE WHEN (CASE WHEN 'abcdef' like '%b%e%' THEN 1 ELSE 2 END)>1 THEN 3 ELSE 4 END;
When I implements ExpressionVisitor and override the function visit(CaseExpression caseExpression) ,after I calculated the second CASE WHEN get result is 1 , how can i replace the Expression ' (CASE WHEN 'abcdef' like '%b%e%' THEN 1 ELSE 2 END) ' to new Expression 1。-> SELECT CASE WHEN 1>1 THEN 3 ELSE 4 END;
The JSQLParser has some Function to replace an Expression Node ?
Some Code
public class SelectItemService SelectItemVisitor{
...override
@Override
public void visit(SelectExpressionItem selectExpressionItem){
Expression expression = selectExpressionItem.getExpression();
if (expression instance CaseExpression){
CaseExpression caseExpression = (CaseExpression) expression;
for (WhenClause whenClause : caseExperssion.getWhenClause()){
Expression whenExpression = whenClause.getWhenExpression();
whenExpression.accept(new ExpressionService());
}
}
}
}
public class ExpressionService implements ExpressionVisitor {
...override
@Override
public void visit(CaseExpression caseExpression){
List<WhenClause> whenClauses = caseExpression.getWhenClauses();
for (WhenClause whenClause : whenClauses){
Expression whenExpression = whenClause.getWhenExpression();
if (whenExpression instanceof LikeExpression){
// ignore other visit(....)
whenExpression.accept(this);
String leftExp = ((LikeExpression) whenExpression).getLeftExpression().toString();
String rightExp = ((LikeExpression) whenExpression).getRightExpression().toString();
// here I'm assuming that these two result are the same
// how can i replace the caseExpression to (Expression)LongValue or other Expression?
......
}
}
}
}
Activity
manticore-projects commentedon Apr 10, 2023
Greetings.
Your

CASEexpression has aWHENclause, holding the secondCASEExpression within aGreaterThanandParenthesis:You can simply replace that second
CASEexpression of theParenthesisby any other Expression.You can generate that AST here.