jOOQ 从结果<R> 中获取table 名称

jOOQ get table name from Result<R>

我正在尝试从 jOOQ Result 中检索基础 MySQL table 名称。

这是我最好的尝试,作为辅助函数:

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

这个returns"database"."table_name"."first_column_name"

这纯粹是为了生成错误消息,所以现在我只是用正则表达式抓取它。

但我很好奇我无法找到正确的方法。

Result不知道Table

首先,请注意 Result 原则上不知道 TableResult 只是一堆由任意 Select 语句生成的列/记录。它很可能只包含像 A + B1NULL 这样的表达式,这些表达式没有链接到任何物理 tables.

如果你知道你从物理table

中产生了Result

当然,您可能知道给定的Result是从物理table中产生的,例如通过使用 DSLContext.selectFrom(). In that case, your generic R type shouldn't be bound to Record but to TableRecord.

一种可能的实现方式是:

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