Skip to content

[FEATURE] How can i replace a Expression #1760

@JosephC666

Description

@JosephC666

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions