I have started to use JSqlParser, i can parse a Where clause but i don't manage to go further with it.
Indeed, i have tried to Override visit methods but don't understand how to reach my goal.
Let's say i have: (a=1 AND (b=2 OR (c=3 AND d=4))) OR e=2
as input and i would like as output:
-> a=1
-> AND
--> b=2
--> OR
---> c=3
---> AND
---> d=4
-> OR
-> e=5
My final goal is not a pretty print, but to understand how to know the current depth of the print at a given state. In order to dig in the tree as i want.
I have started a piece of code, parsing the where clause:
Expression expr = CCJSqlParserUtil.parseCondExpression("(a=1 AND (b=2 OR >(c=3 AND d=4))) OR e=2");
expr.accept(new ExpressionVisitorAdapter() {
@Override
public void visit(ExpressionClassToChoose expr) {
some code here...
}
});
Could you show me the way to start this kind of thing? Do i have to work with visit methods or am i wrong?
Thanks !
The visitor way is as always one way to go. If you want to get the parenthesis depth, you could stick with:
To get the ouput you want, is a little bit trickier. I implemented a simple example only taking ANDs and ORs into account. I do not use the ExpressionVisitorAdapter, but the ExpressionDeParser which is responsible for Expression printing. This way you can modify the generated output to your needs.
As you can see, the parenthesis visitor changes the depth. The hard part is within
visitBinaryExpression
. The complex instanceof logic derives from using the And/OrExpression for output. Since for one text line multiple calls tovisitBinaryExpression
could happen, the indent has to be done from the most outer part.If you would like to improve the printing of an JSqlParser parsed statement your updates should go to the deparsers.