-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[FEATURE] How can i replace a Expression #1761
-
Problem
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?
......
}
}
}
}
Additional context
The used JSQLParser Version: 4.3
Be very grateful
Beta Was this translation helpful? Give feedback.
All reactions
Both is possible via the Java API:
- modify the Expression of the
Parenthesis
or - create any new, different Expression for
Parenthesis.setExpression(..)
Replies: 1 comment 3 replies
-
Greetings.
Your CASE
expression has a WHEN
clause, holding the second CASE
Expression within a GreaterThan
and Parenthesis
:
image
You can simply replace that second CASE
expression of the Parenthesis
by any other Expression.
You can generate that AST here.
Beta Was this translation helpful? Give feedback.
All reactions
-
Excited to hear feedback from u!
Now,I execute parenthesis.setExpression(Expression)
to replace it,but i need save the formula result let Parenthesis
choose modify.
so , If i can directly modify the Expression or recreate node in CaseExpression
that will be great.
Beta Was this translation helpful? Give feedback.
All reactions
-
Both is possible via the Java API:
- modify the Expression of the
Parenthesis
or - create any new, different Expression for
Parenthesis.setExpression(..)
Beta Was this translation helpful? Give feedback.
All reactions
-
Here is a Sample how to build any Expression or Statement using the API: https://www.manticore-projects.com/JSQLParser/usage.html#build-a-sql-statement
Beta Was this translation helpful? Give feedback.