Robin Trietsch
Robin Trietsch

Reputation: 1852

jOOQ PlainSQL custom keywords / custom dialect

I'm using jOOQ to generate PlainSQL queries, that mostly follow ANSI SQL. What if I'm dealing with a SQL statement for a field, that needs to be parsed to verify if it's correct SQL (I'm using DSL.using(SQLDialect.DEFAULT).parser().parseField(sqlString)), but the resulting sql from this field should use the platform native syntax and keywords?

Consider an example SQL statement for a field TO_HEX(SHA256(CAST(userId AS string))) (note that I've set the DSLContext to ignore unknown functions). When parsing this using parseField and inspecting the resulting SQL, this is transformed into to_hex(SHA256(cast(userId as varchar))). The target platform however, does not support varchar. Is there any option to specify how e.g. a cast like this should be rendered? I've looked into the documentation, but I can't seem to find how to override specific keywords / define my own dialect.

Upvotes: 3

Views: 136

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221155

There are 2 dialects you should configure:

  • The dialect you're passing to DSL.using() (corresponding to Configuration.dialect()) is the output dialect, i.e. you should set that to your target dialect
  • The Settings.parseDialect is the input dialect. If your dialect isn't supported, you can leave this untouched.

If you need to support custom syntax in the parser, there's a ParseListener SPI, which allows for overriding input/output behaviour for certain syntax elements. You can't use this to override everything, but it should work at least for:

  • Field expressions
  • Condition expressions
  • Table expressions

Upvotes: 2

Related Questions