gpt4 book ai didi

java - 使用ANTLR4解析mysql简单例子

转载 作者:行者123 更新时间:2023-11-29 05:55:29 26 4
gpt4 key购买 nike

我在这里使用 mysql 语法:https://github.com/antlr/grammars-v4/tree/master/mysql并使用 Maven 生成了 java 文件。现在,我试图解析一个查询,但不知道如何解析。

我基本上想“获取”查询的所有不同组成部分,例如所选的列表列、条件、子查询、表名等。但我不知道如何进行。到目前为止,我已经编写了以下代码。有人可以用一个简单的例子提出建议,以便我可以理解用法并处理更复杂的任务吗?这是我的代码:

public static void main( String[] args )
{
String sql="select cust_name from database..table where cust_name like 'Kash%'";
ANTLRInputStream input = new ANTLRInputStream(sql);
MySqlLexer mySqlLexer = new MySqlLexer(input);
CommonTokenStream tokens = new CommonTokenStream(mySqlLexer);
MySqlParser mySqlParser = new MySqlParser(tokens);

ParseTree tree = mySqlParser.dmlStatement();
ParseTreeWalker walker = new ParseTreeWalker();
MySqlParserBaseListener listener=new MySqlParserBaseListener();
ParseTreeWalker.DEFAULT.walk(listener, tree);


System.out.println(?);
}

使用上面的代码,我得到以下输出:

line 1:11 no viable alternative at input '_'
(dmlStatement _ . . _ 'Kash%')

感谢帮助:)

最佳答案

I basically want to 'get' all the different components of a query, like the list columns selected, where conditions, sub queries, table names, etc.

您的 tree 变量保存所有数据:ParseTree tree = mySqlParser.dmlStatement();

line 1:11 no viable alternative at input '_'

如果您查看词法分析器规则:

SELECT:                              'SELECT';

ID: ID_LITERAL;

fragment ID_LITERAL: [A-Z_$0-9]*?[A-Z_$]+?[A-Z_$0-9]*;

看来关键字和标识符不能包含小写字母。

如果你这样运行它:

String sql = "SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_NAME LIKE 'Kash%'";

MySqlLexer lexer = new MySqlLexer(CharStreams.fromString(sql));
MySqlParser parser = new MySqlParser(new CommonTokenStream(lexer));
ParseTree root = parser.dmlStatement();

System.out.println(root.toStringTree(parser));

您将看到以下输出(为了便于阅读而缩进):

(dmlStatement 
(selectStatement
(querySpecification SELECT
(selectElements
(selectElement
(fullColumnName
(uid
(simpleId CUST_NAME)))))
(fromClause FROM
(tableSources
(tableSource
(tableSourceItem
(tableName
(fullId
(uid
(simpleId CUSTOMERS))))))) WHERE
(expression
(predicate
(predicate
(expressionAtom
(fullColumnName
(uid
(simpleId CUST_NAME))))) LIKE
(predicate
(expressionAtom
(constant
(stringLiteral 'Kash%'))))))))))

关于java - 使用ANTLR4解析mysql简单例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49769147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com