Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FEATURE] How can i replace a Expression #1761

Discussion options

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

You must be logged in to vote

Both is possible via the Java API:

  1. modify the Expression of the Parenthesis or
  2. create any new, different Expression for Parenthesis.setExpression(..)

Replies: 1 comment 3 replies

Comment options

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.

You must be logged in to vote
3 replies
Comment options

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.

Comment options

Both is possible via the Java API:

  1. modify the Expression of the Parenthesis or
  2. create any new, different Expression for Parenthesis.setExpression(..)
Answer selected by JosephC666
Comment options

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #1760 on April 10, 2023 04:23.

AltStyle によって変換されたページ (->オリジナル) /