简体   繁体   English

如何使用jOOQ使用自定义函数解析SQL字符串?

[英]How to parser SQL string with self defined functions using jOOQ?

I am trying to parse SQL string with jOOQ SQL parser . 我正在尝试使用jOOQ SQL解析器解析SQL字符串。 The SQL contains self defined functions like this: SQL包含如下自定义函数:

select dateadd([*]'20190809', 'yyyymmdd', 1) from table1

It can't be parsed with any dialect. 不能用任何方言进行解析。 I want to get the select fields and manipulate the limit-offset clause. 我想获取选择字段并操作limit-offset子句。

Is there any way to achieve this? 有什么办法可以做到这一点?

The somewhat standard DATEADD() function ( DATE_ADD() in some SQL dialects) takes very many different forms, depending on the specific SQL dialect. 某些标准的DATEADD()函数(在某些SQL方言中为DATE_ADD() )采用许多不同的形式,具体取决于特定的SQL方言。

jOOQ's SQL parser currently only supports one of these variations: DATEADD(<datepart>, <interval>, <date>) , which corresponds to the syntax in SQL Server (and some other dialects). jOOQ的SQL解析器当前仅支持以下变体之一: DATEADD(<datepart>, <interval>, <date>) ,它对应于SQL Server(和某些其他方言)中的语法。

Applied to your example (assuming you want to add one day) this would be: 应用于您的示例(假设您想添加一天),将为:

select dateadd(DAY, 1, date '2019-08-09') from table1

Note that the '20190809' isn't a proper date literal in most dialects, which is why I substituted it with date '2019-08-09' . 请注意,在大多数方言中'20190809'不是正确的日期文字,这就是为什么我将其替换为date '2019-08-09'

Also note that you can play around with these things using the jOOQ translator . 还要注意,您可以使用jOOQ转换器处理这些事情。

Assuming you have your own user-defined stored procedure also called DATEADD , then you are indeed out of luck, as jOOQ's parser attempts to parse the expression as described above. 假设您有自己的用户定义存储过程(也称为DATEADD ,那么您确实不走运,因为jOOQ的解析器试图如上所述解析表达式。 With any other name (eg DATEADD1 ) jOOQ's parser would not have any problem parsing this expression. 使用任何其他名称(例如DATEADD1 ),jOOQ的解析器在解析此表达式时都不会有任何问题。 You might however want to configure how jOOQ should deal with unknown functions . 但是,您可能想要配置jOOQ应该如何处理未知函数

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

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