Question

Suppose I have an expression of the format

a>10 and b>20 and c>30

I want to get a list of expressions as follows

a>10
b>20
c>30

If I use the expression visitor pattern, for the AndExpression visitor, I wrote something like

public void visit(AndExpression andExpression) {
    andExpression.getLeftExpression().accept(this);
    andExpression.getRightExpression().accept(this);
}

but this however recursively runs and goes into each of the other visitor pattern methods. How do I stop when a smaller expression is obtained?

If i just use getLeftExpressions() and getRightExpression(), what I get in the first run is "a>10" as the left and "b>20 and c>30" as the right.

Was it helpful?

Solution 2

In your visit method, you can return a list of Expression for a given And expressions. So just return a list of Expression object as follows:

public List<Expression> visit(AndExpression andExpression) {
    List<Expression> list = new ArrayList<Expression>();
    lex = andExpression.getLeftExpression();
    list.add(lex);
    rex = andExpression.getRightExpression();
    subEx1 = rex.getLeftExpression();
    subEx2 = rex.getRightExpression();
    list.add(subEx1);
    list.add(subEx2);
    return list;
}

The two sub-expressions are obtained from the first right expression.

OTHER TIPS

JSqlParser parses your expression in this way:

AndExpression
   AndExpression
       a>10
       b>20
   c>30

I am curious to see which version you use, if the recursion will be indeed in the right item not the left. I use JSqlParser 0.9-SNAPSHOT. Nevertheless in my version of JSqlParser (and I think in all, because it is an LL parser), the in the other answers comment proposed codeblock

while(ex.getLeftExpression() != null) {
    list.add(ex.getLeftExpression()); 
    ex = ex.getRightExpression();
}

will not work. It finds (or collects):

a > 10 AND b > 20

which is an AndExpression and fails on ex = ex.getRightExpression();, because the right will never be an AndExpression. That is how JSqlParser works. The AndExpression recursion would be in the leftExpression.

So here is a solution and a litte test around it, using JSqlParser V0.9-SNAPSHOT, which uses the recursive way to visit these AndExpressions and prints out the found expression. I did not add it to a List. Forgive me for that.

public class JSqlParser1 {

    public static void main(String[] args) throws JSQLParserException {
        Expression expr = CCJSqlParserUtil.parseCondExpression("a>10 and b>20 and c>30");
        expr.accept(new ExpressionVisitorAdapter() {

            @Override
            public void visit(AndExpression expr) {
                if (expr.getLeftExpression() instanceof AndExpression) {
                    expr.getLeftExpression().accept(this);
                } else {
                    System.out.println(expr.getLeftExpression());
                }

                System.out.println(expr.getRightExpression());
            }

        });
    }
}

This one prints:

a > 10
b > 20
c > 30
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top