JSqlParser - Pretty print where clause

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I have started to use JSqlParser, i can parse a Where clause but i don't manage to go further with it.

JSqlParser github link

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 !

回答1:

The visitor way is as always one way to go. If you want to get the parenthesis depth, you could stick with:

public void visit(Parenthesis parenthesis) 

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.

public static void main(String args[]) throws JSQLParserException {     Expression expr = CCJSqlParserUtil.parseCondExpression("(a=1 AND (b=2 OR (c=3 AND d=4))) OR e=2");     StringBuilder b = new StringBuilder();     expr.accept(new ExpressionDeParser(null, b) {         int depth = 0;          @Override         public void visit(Parenthesis parenthesis) {             if (parenthesis.isNot()) {                 getBuffer().append("NOT");             }              depth++;             parenthesis.getExpression().accept(this);             depth--;         }          @Override         public void visit(OrExpression orExpression) {             visitBinaryExpr(orExpression, "OR");         }          @Override         public void visit(AndExpression andExpression) {             visitBinaryExpr(andExpression, "AND");         }          private void visitBinaryExpr(BinaryExpression expr, String operator) {             if (expr.isNot()) {                 getBuffer().append("NOT");             }             if (!(expr.getLeftExpression() instanceof OrExpression)                      && !(expr.getLeftExpression() instanceof AndExpression)                      && !(expr.getLeftExpression() instanceof Parenthesis)) {                 getBuffer().append(StringUtils.repeat("-", depth)).append(">");             }             expr.getLeftExpression().accept(this);             getBuffer().append("\n").append(StringUtils.repeat("-", depth)).append(">");             getBuffer().append(operator).append("\n");             if (!(expr.getRightExpression() instanceof OrExpression)                      && !(expr.getRightExpression() instanceof AndExpression)                      && !(expr.getRightExpression() instanceof Parenthesis)) {                 getBuffer().append(StringUtils.repeat("-", depth)).append(">");             }             expr.getRightExpression().accept(this);         }     });      System.out.println(b); } 

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 to visitBinaryExpression 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!