Java 的 SQL 解析器库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:32:05  来源:igfitidea点击:

SQL parser library for Java

javasqlparsing

提问by Thilo

Is there an open-source Java library for parsing SQL statements?

是否有用于解析 SQL 语句的开源 Java 库?

If possible, it should be customizable or flexible enough to also be able to parse (or at least ignore) vendor-specific syntax (such as Oracle tablespace definitions or MySQL's LIMIT clause).

如果可能,它应该是可定制的或足够灵活,以便还能够解析(或至少忽略)特定于供应商的语法(例如 Oracle 表空间定义或 MySQL 的 LIMIT 子句)。

If not, strict adherence to the SQL standard is also fine.

如果没有,严格遵守 SQL 标准也没有问题。

Update:I need this for two things:

更新:我需要这两个东西:

  • providing an SQL interface to a non-SQL database (mapping to internal API calls)
  • rewriting SQL before it goes to the actual database (e.g. Oracle)
  • 为非 SQL 数据库提供 SQL 接口(映射到内部 API 调用)
  • 在进入实际数据库(例如 Oracle)之前重写 SQL

回答by duffymo

ANTLR3has an ANSI SQL grammar available. You can use that to create your own parser.

ANTLR3有一个可用的 ANSI SQL 语法。您可以使用它来创建自己的解析器。

ANTLR4 has a SQL grammar.

ANTLR4 有一个SQL 语法

回答by Thomas Jones-Low

What you want to do with the parsed SQL? I can recommend a few Java implementation of Lex/Yacc (BYACC/J, Java Cup) that you can use an existing SQL grammar with.

你想用解析的 SQL 做什么?我可以推荐一些 Lex/Yacc ( BYACC/J, Java Cup) 的Java 实现,你可以使用现有的 SQL 语法。

If you want to actually do something with the resulting parsed grammar, I might suggest looking at Derby, an open source SQL database written in Java.

如果您想对解析后的语法进行实际操作,我可能建议您查看Derby,这是一个用 Java 编写的开源 SQL 数据库。

回答by Elijah

Parser

解析器

If you need a parser there should be a parser in the code base of Apache Derby.

如果您需要解析器,Apache Derby的代码库中应该有一个解析器。

Dealing with vendor-specific SQL

处理特定于供应商的 SQL

You may want to look at the .native()method on the jdbc Connection object which you can pass it vendor neutral queries that will get postprocessed into vendor specific queries.

您可能想查看jdbc Connection 对象上的.native()方法,您可以将其传递给供应商中立查询,这些查询将被后处理为供应商特定查询。

回答by David Phillips

  • JSqlParser
  • Presto's parseris written using ANTLR4 and has its own immutable AST classes that are built from the parser. The AST has a visitor and pretty printer.
  • 解析器
  • Presto 的解析器是使用 ANTLR4 编写的,并且有自己的不可变 AST 类,这些类是从解析器构建的。AST 有一个访客和漂亮的打印机。

回答by jagamot

Try Zql

试试Zql

回答by Han Zheng

Hibernate uses ANTLR for sql and hql parsing.

Hibernate 使用 ANTLR 进行 sql 和 hql 解析。

JSqlParser is also a good option.although it has some bugs(or some features not implemented) while parsing oracle pl/sql. see its forum for detail.

JSqlParser 也是一个不错的选择。尽管它在解析 oracle pl/sql 时有一些错误(或一些未实现的功能)。详情请参阅其论坛。

so if you're parsing oracle pl/sql, ANTLR is recommended.

所以如果你在解析oracle pl/sql,推荐使用ANTLR。

回答by James

General SQL Parser for Javais not open source, but is exactly what you are looking after.

适用于 Java 的通用 SQL 解析器不是开源的,但正是您所需要的。