Search code examples
javajooq

Parse a SELECT query and get the JOINS with JOOQ


Using JOOQ, is it possible to parse an SQL query and then obtain the parts of the query? Fields, From, Joins, Where... Something like that:

String sql = "SELECT x.a, x.b, y.c
FROM my_table x
LEFT JOIN other_table y
   on x.a = y.a
WHERE x.a > 1000";

SelectQuery<Record> query = (SelectQuery<Record>) context.parser().parseSelect(sql);
query.getFrom();
query.getFields();
query.getJoins();
query.getWhere();
...

Solution

  • With jOOQ 3.15, you cannot access the internals of the jOOQ expression tree. There is a big project on the way to re-design these internals to make them publicly accessible precisely for this use-case:

    Until then, you need to either work around this by using reflection, putting some logic in the org.jooq.impl package to access package-private internals on the classpath (not possible on the module path), or by patching jOOQ - each at your own risk.