简体   繁体   English

我可以使用JOOQ作为SQL解析器吗?

[英]Can I use JOOQ as an SQL parser?

I'm trying to parse a SELECT statement in Java. 我正在尝试用Java解析SELECT语句。 I'm familiar with JOOQ, and was hoping to use that. 我对JOOQ很熟悉,并希望能够使用它。 I know it's not explicitly designed as an SQL parser—it's actually a lot more than that, so I was thinking there might be a way to use its internal parsers to parse SELECT queries. 我知道它没有明确地设计为SQL解析器 - 它实际上远远不止于此,所以我想可能有一种方法可以使用它的内部解析器来解析SELECT查询。

I saw some information on how to access some of JOOQ's internals using the Visitor pattern, but I need to navigate inside the query using a tree-like structure that will allow access to each part of the query individually. 我看到了一些有关如何使用访问者模式访问JOOQ内部部件的信息,但我需要使用类似树的结构在查询内部导航,该结构将允许单独访问查询的每个部分。 I don't want to use the Visitor pattern for all use cases. 我不想对所有用例使用访问者模式。

Is this possible? 这可能吗? How would I go about doing it? 我该怎么做呢?

A full-fledged SQL parser is available from DSLContext.parser() and from DSLContext.parsingConnection() (see the manual's section about parsing connections for the latter). DSLContext.parser()DSLContext.parsingConnection()提供了一个成熟的SQL解析器(请参阅手册关于解析后者连接的部分)。

The SQL Parsing API page gives this trivial example: SQL Parsing API页面提供了这个简单的示例:

ResultQuery<?> query = 
DSL.using(configuration)
   .parser()
   .parseResultQuery("SELECT * FROM (VALUES (1, 'a'), (2, 'b')) t(a, b)");

parseResultQuery is the method you need for a single SELECT query, use parse(String) if you may have multiple queries. parseResultQuery是单个SELECT查询所需的方法,如果您有多个查询,请使用parse(String)

As of jOOQ 3.11, while you can indeed extract the expression tree using a VisitListener and some tweaking of the internals (mainly depending on internal types via reflection), what you want to do is currently not possible. 从jOOQ 3.11开始,虽然您确实可以使用VisitListener提取表达式树并对内部进行一些调整(主要取决于通过反射的内部类型),但您目前无法做到。 It will be addressed in the future, when the expression tree might be made accessible through public API (and cleanly separated from the SQL generation logic), but no promises yet. 将来可以解决这个问题,因为表达式树可以通过公共API访问(并且与SQL生成逻辑完全分离),但还没有任何承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM