JSqlParser - 漂亮的打印 where 子句 [英] JSqlParser - Pretty print where clause

查看:38
本文介绍了JSqlParser - 漂亮的打印 where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用 JSqlParser,我可以解析 Where 子句,但我无法继续使用它.

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

JSqlParser github 链接

确实,我试图覆盖访问方法,但不明白如何达到我的目标.

Indeed, i have tried to Override visit methods but don't understand how to reach my goal.

假设我有:(a=1 AND (b=2 OR (c=3 AND d=4))) OR e=2 作为输入,我想作为输出:

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.

我已经开始一段代码,解析where子句:

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?

谢谢!

推荐答案

访客方式一如既往地是一种方式.如果你想获得括号深度,你可以坚持:

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)

要获得您想要的输出,有点棘手.我实现了一个简单的例子,只考虑了 AND 和 OR.我不使用 ExpressionVisitorAdapter,而是使用负责 Expression 打印的 ExpressionDeParser.通过这种方式,您可以根据需要修改生成的输出.

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);
}

如您所见,括号访问者更改了深度.困难的部分在 visitBinaryExpression 中.复杂的 instanceof 逻辑源自使用 And/OrExpression 进行输出.因为对于一个文本行,可能会发生多次对 visitBinaryExpression 的调用,所以缩进必须从最外层开始.

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.

如果您想改进 JSqlParser 解析语句的打印,您的更新应该转到解析器.

If you would like to improve the printing of an JSqlParser parsed statement your updates should go to the deparsers.

这篇关于JSqlParser - 漂亮的打印 where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆