How can I check if the condition of the clause is WHERE VALID or not using JOOQ?

Let's assume we have a SQL snippet as shown below:

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

      

Shouldn't the next line throw an error rather than accept it?

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

      

Is there a way to VALUE a state grammar using JOOQ? Also, unlike addLimit, the values ​​are not replaced with ?, why is that?

+3


source to share


1 answer


Shouldn't the next line throw an error rather than accept it?

Why? DSL.condition(String)

is part of the regular SQL API that allows you to pass any piece of SQL string to the jOOQ query element you want, including:

  • Simple SQL snippets
  • Complex expressions for specific suppliers
  • Syntax errors if you like

There is no validation for what you put inside a simple SQL snippet. Ultimately the database will validate the request. But jOOQ doesn't know if there is an operator in your database by accident asd

.

Is there a way to VALIDATE the state grammar using JOOQ?

There's an experimental Parser API in jOOQ 3.9 which is available via DSLContext.parser()

. It allows you to parse SQL strings and build expression trees from it. In this case, it asd

will indeed be an unrecognized 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 since jOOQ 3.9 it is experimental and there are many more bugs. jOOQ 3.10 will remove its experimental status.

Also, unlike addLimit, the values ​​are not replaced with?, Why is that?

Since the simple SQL API works. If you need bind variables, you could write:

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

      

+1


source







All Articles