Nick Charney Kaye
Nick Charney Kaye

Reputation: 4411

jOOQ get table name from Result<R>

I'm trying to retrieve the underlying MySQL table name, from a jOOQ Result.

Here's my best attempt, as a helper function:

private <R extends Record> String tableNameOf(Result<R> result) {
  return result.recordType().toString();
}

This returns "database"."table_name"."first_column_name"

This is purely for generating an error message, so for now I just grabbed it with a regex.

But I'm curious that I wasn't able to find a proper way to get at it.

Upvotes: 1

Views: 1193

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221145

Result doesn't know Table

First off, do note that a Result has no idea of a Table in principle! A Result is just a bunch of columns / records that was produced by an arbitrary Select statement. It may well include only expressions like A + B or 1 or NULL, which are not linked to any physical tables.

If you know you produced a Result from a physical table

Of course, you may know that a given Result was produced from a physical table, e.g. by using DSLContext.selectFrom(). In that case, your generic R type shouldn't be bound to Record but to TableRecord.

One possible implementation could be:

private <R extends TableRecord<R>> String tableNameOf(Result<R> result) {
    return ((TableRecord<?, ?>) result.field(0)).getTable();
}

Upvotes: 1

Related Questions