简体   繁体   English

如何使用JOOQ检查WHERE子句条件是否为VALID?

[英]How to check if a WHERE clause condition is VALID or not using JOOQ?

Lets say we have a SQL fragment like below: 可以说我们有一个如下所示的SQL片段:

String condition = "field_name > 10 asd field_name1 <= 20"

Shouldn't the following line throw an error instead of accepting it? 以下行不应该抛出错误而不是接受它吗?

query.addConditions(DSL.condition(condition));

Is there a way to VALIDATE the condition grammar using JOOQ? 有没有办法使用JOOQ验证条件语法? Furthermore unlike an addLimit, the values are not replaced with ?, why is that? 此外,与addLimit不同,值不会替换为?,为什么会这样?

Shouldn't the following line throw an error instead of accepting it? 以下行不应该抛出错误而不是接受它吗?

Why should it? 为什么要这样? DSL.condition(String) is part of the plain SQL API , which allows you to pass any SQL string fragment to a jOOQ query element that you want, including: DSL.condition(String)普通SQL API的一部分 ,它允许您将任何SQL字符串片段传递给您想要的jOOQ查询元素,包括:

  • Simple SQL fragments 简单的SQL片段
  • Complex, vendor-specific expressions 特定于供应商的复杂表达式
  • Syntax errors, if you like 语法错误,如果你愿意的话

There's no validation on what you put inside a plain SQL fragment. 你在普通的SQL片段中放置了什么没有验证。 The database will ultimately validate the query. 数据库最终将验证查询。 But jOOQ doesn't know if by chance, there's an asd operator in your database. 但是jOOQ不知道你的数据库中是否存在asd运算符。

Is there a way to VALIDATE the condition grammar using JOOQ? 有没有办法使用JOOQ验证条件语法?

There's an experimental Parser API in jOOQ 3.9, which is available through DSLContext.parser() . 在jOOQ 3.9中有一个实验性的Parser API ,它可以通过DSLContext.parser() It allows you to parse SQL strings and construct expression trees from it. 它允许您解析SQL字符串并从中构造表达式树。 In this case, indeed, asd will be an unrecognised token and an exception will be thrown. 实际上,在这种情况下, asd将是一个无法识别的令牌,并且会抛出异常。

You can use it as such: 您可以这样使用它:

Condition condition = 
    ctx.parser().parseCondition("field_name > 10 asd field_name1 <= 20");

But as I said, as of jOOQ 3.9, it's experimental and there are still many bugs. 但正如我所说,从jOOQ 3.9开始,它是实验性的,但仍然存在许多错误。 jOOQ 3.10 will remove its experimental status. jOOQ 3.10将删除其实验状态。

Furthermore unlike an addLimit, the values are not replaced with ?, why is that? 此外,与addLimit不同,值不会替换为?,为什么会这样?

Because that's how the plain SQL API works. 因为这是普通SQL API的工作方式。 If you wanted bind variables, you could have written: 如果你想要绑定变量,你可以写:

Condition condition = DSL.condition("field_name > ? asd field_name1 <= ?", 10, 20);

Or: 要么:

Condition condition = 
    DSL.condition("field_name > {0} asd field_name1 <= {1}", val(10), val(20));

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

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