以下是我用来解析MySQL语句的配置。我能够很好地解析和处理DML语句,但似乎无法解析任何DDL语句(CREATE TABLE,ALTER TABLE,DROP TABLE等)。
public static SqlNode parse(String query) {
SqlParser.Config sqlParserConfig = SqlParser.configBuilder()
.setConformance(SqlConformanceEnum.MYSQL_5)
.setLex(Lex.MYSQL)
.build();
SqlParser parser = SqlParser.create(query, sqlParserConfig);
try {
return parser.parseStmt();
} catch (SqlParseException e) {
lastErrorMessage = e.getMessage();
return null;
}
}
当我尝试解析CREATE TABLE
语句时,它给我以下错误消息。
Encountered "ALTER TABLE" at line 1, column 1.
Was expecting one of:
"SET" ...
"RESET" ...
"ALTER" "SYSTEM" ...
"ALTER" "SESSION" ...
"WITH" ...
"+" ...
"-" ...
"NOT" ...
"EXISTS" ...
<UNSIGNED_INTEGER_LITERAL> ...
<DECIMAL_NUMERIC_LITERAL> ...
<APPROX_NUMERIC_LITERAL> ...
<BINARY_STRING_LITERAL> ...
<PREFIXED_STRING_LITERAL> ...
<QUOTED_STRING> ...
<UNICODE_STRING_LITERAL> ...
...
我可以在库中看到类似SqlAlter
,SqlCreate
,SqlDrop
之类的类。有没有其他解析DDL的方法?
我正在使用1.17.0
版本。
答案 0 :(得分:0)
我认为您只是缺少适当的解析器工厂。请尝试以下操作:
SqlParser.Config sqlParserConfig = SqlParser.configBuilder()
.setParserFactory(SqlDdlParserImpl.FACTORY)
.setConformance(SqlConformanceEnum.MYSQL_5)
.setLex(Lex.MYSQL)
.build();
SqlParser parser = SqlParser.create(query, sqlParserConfig);