0

There is a project using JOOQ. Example of the method:

public List<A> getA(List<String> C) {
    return context
            .select(...)
            .from(...)
            .join(...).on(...)
            .where(...)
            .fetch().map(...);
}

You need to use AspectJ to get the request text.

Everything is OK with HPQL and JDBC, according to the first - we work with the annotation, according to the second - we intercept the parameter with the query from the public JDBC class.

Example point for JDBC:

@Pointcut("target(org.springframework.jdbc.core.JdbcTemplate) && " +
        "args(String, ..) && (call(* query(..)) || call(* update(..)) || call(* batchUpdate(..)))")
public void executeMetricsRepository() { }

But what can you do here? It is with the application of the aspect, because rewriting each request is not considered.

There are two parts here - fetch() and map(). But interested in - fetch.

fetch ---> interface org.jooq.ResultQuery

Inherited classes:

AbstractResultQuery and SelectImpl. But it is package-private.

Mary
  • 35
  • 5
  • Welcome to SO. Please learn what an [MCVE](https://stackoverflow.com/help/mcve) is. Then, please provide one, ideally on GitHub. The information given here does not make the problem reproducible. – kriegaex Jun 25 '22 at 12:44

1 Answers1

2

Alternative approach without AspectJ

I'm not sure if you have to use this annotation based approach, but it is very easy to extract the SQL query for every jOOQ query in your application by adding an ExecuteListener to your jOOQ Configuration, e.g.

configuration.set(ExecuteListener.onRenderEnd(ctx -> {

    // Or, do whatever.
    System.out.println(ctx.sql());
}));

Possibility to use AspectJ

The jOOQ Query type has a Query.getSQL() method which can be used to extract the generated SQL for any Query. That's a bit less efficient than the above approach, because it will re-generate the SQL string a second time, but that might be irrelevant to your application?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Thanks! This was really helpful for me in debugging a query which returned 21 million records on my local machine... :) Enabling `DEBUG` logging for `org.jooq` wasn't enough, but setting up an `onRenderEnd()` listener like this helped. Big thanks! – Per Lundberg Jun 22 '23 at 06:14
  • @PerLundberg: Why wasn't enabling `DEBUG` logging not enough (assuming you turned on the `ExecuteListener`)? – Lukas Eder Jun 22 '23 at 06:20
  • Precisely because I didn't turn on the `ExecuteListener`, most likely. ;) (TBH, I don't even remember how to do it, I would have to read the Fine manual. I just enabled `debug` logging, presuming out would be enough, which it wasn't and then googled which led me here... :P) – Per Lundberg Jun 26 '23 at 06:23
  • @PerLundberg: It's enabled by default. [You probably turned it off with `Settings.executeLogging`](https://www.jooq.org/doc/dev/manual/sql-building/dsl-context/custom-settings/settings-execute-logging/) – Lukas Eder Jun 26 '23 at 06:57
  • @PerLundberg: You could ask a new question about this: *"Why doesn't jOOQ log anything despite enabling `DEBUG` logging"*, and I'll repeat this answer. You might find your own Q&A useful again in the future :) – Lukas Eder Jun 26 '23 at 06:58
  • 1
    Alright, here we go: https://stackoverflow.com/questions/76564333/jooq-doesnt-log-the-sql-queries-it-executes-despite-enabling-debug-logging. I checked in our application and we don't seem to be calling the `Settings.executeLogging()` method AFAICT. I guess I'll have to re-test and see how it behaves when `org.jooq` DEBUG logging is enabled... – Per Lundberg Jun 27 '23 at 11:21