Can I use JOOQ as an SQL parser?

烈酒焚心 提交于 2020-01-03 09:08:32

问题


I'm trying to parse a SELECT statement in Java. I'm familiar with JOOQ, and was hoping to use that. 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.

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. I don't want to use the Visitor pattern for all use cases.

Is this possible? How would I go about doing it?


回答1:


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. 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.




回答2:


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).

The SQL Parsing API page gives this trivial example:

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.



来源:https://stackoverflow.com/questions/55355879/can-i-use-jooq-as-an-sql-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!