Available in versions: Dev (3.20) | Latest (3.19) | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10

Pretty printing SQL

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Did you know you can pretty print arbitrary SQL with our translator at https://www.jooq.org/translate?

As mentioned in the previous chapter about SQL rendering, there are some elements in the org.jooq.RenderContext that are used for formatting / pretty-printing rendered SQL. In order to obtain pretty-printed SQL, just use the following custom settings:

// Create a DSLContext that will render "formatted" SQL
DSLContext pretty = DSL.using(dialect, new Settings().withRenderFormatted(true));

And then, use the above DSLContext to render pretty-printed SQL:

  count(*) "c"
from "TEST"."BOOK"
  join "TEST"."AUTHOR"
where "TEST"."BOOK"."TITLE" <> '1984'
group by "TEST"."AUTHOR"."LAST_NAME"
having count(*) = 2
String sql = pretty.select(
                       AUTHOR.LAST_NAME, count().as("c"))

The section about ExecuteListeners shows an example of how such pretty printing can be used to log readable SQL to the stdout.


Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo