Reputation: 31
Lets say we have a SQL fragment like below:
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? Furthermore unlike an addLimit, the values are not replaced with ?, why is that?
Upvotes: 3
Views: 1172
Reputation: 221145
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:
There's no validation on what you put inside a plain SQL fragment. The database will ultimately validate the query. But jOOQ doesn't know if by chance, there's an asd
operator in your database.
Is there a way to VALIDATE the condition grammar using JOOQ?
There's an experimental Parser API in jOOQ 3.9, which is available through DSLContext.parser()
. It allows you to parse SQL strings and construct expression trees from it. In this case, indeed, asd
will be an unrecognised token and an exception will be thrown.
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.10 will remove its experimental status.
Furthermore unlike an addLimit, the values are not replaced with ?, why is that?
Because that's how the plain SQL API works. 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));
Upvotes: 1