zoukankan      html  css  js  c++  java
  • anltr 解析MYSQL

    String sql = "select a as c,b as s,'zhange' F, 1/5 as e from t_order";
    
            final MysqlQueryLexer mySqlLexer = new MysqlQueryLexer(CharStreams.fromString(sql));
    
            final CommonTokenStream commonTokenStream = new CommonTokenStream(mySqlLexer);
    
            final MysqlQueryParser mySqlParser = new MysqlQueryParser(commonTokenStream);
    
            mySqlParser.addParseListener(new MysqlQueryBaseListener());
    
            final MysqlQueryParser.SelectStatementContext selectStatementContext = mySqlParser.selectStatement();
            for (ParseTree child : selectStatementContext.children) {
                if (child instanceof MysqlQueryParser.SelectElementsContext) {
                    List<ParseTree>  columnElementContextList = ((MysqlQueryParser.SelectElementsContext) child).children;
                    for (int i = 0; i < columnElementContextList.size(); i++) {
                        ParseTree tree = columnElementContextList.get(i);
                        if (tree instanceof MysqlQueryParser.SelectColumnElementContext) {
                            MysqlQueryParser.SelectColumnElementContext columnElementContext = (MysqlQueryParser.SelectColumnElementContext) tree;
                            System.out.println(columnElementContext.fullColumnName());
                            System.out.println(columnElementContext.start.getText() + "----------" + columnElementContext.start.getType());
                            System.out.println(columnElementContext.stop.getText());
                        }
                    }
                }
                System.out.println(child.getText() + "------"  );
    
            }
    
  • 相关阅读:
    K
    士兵队列训练问题
    分析A + B Problem II
    C++中sort()的用法
    swing初级应用创建一个窗体
    java生成随机数
    JAVA数字格式化
    CodeForces
    POJ
    51Nod
  • 原文地址:https://www.cnblogs.com/masterZ/p/15126424.html
Copyright © 2011-2022 走看看