jOOQ: 获取别名字段的最佳方式(来自#as(alias, aliasFunction))。[英] jOOQ: best way to get aliased fields (from #as(alias, aliasFunction))

本文是小编为大家收集整理的关于jOOQ: 获取别名字段的最佳方式(来自#as(alias, aliasFunction))。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我必须从" root"表中访问同一表以获取多个引用.为了这样做,我正在为这些表创建别名:

protected final Table<XyzRecord> foo = Tables.XYZ.as("foo", <foo-alias-function>);
protected final Table<XyzRecord> bar = Tables.XYZ.as("bar", <bar-alias-function>);

bar-alias-function将声明如下_

protected final Function<Field<?>, String> fooFieldAliasFunction = f -> "foo_" + f.getName();

现在,由于我想从类型的安全查询中受益,因此我需要在查询中重新使用相同的alias-function来访问字段:

jooq.select()
  .from    (root)
  .leftJoin(foo).on(
         checklistTarget.field(fooFieldAliasFunction.apply(Tables.XYZ.ID), Tables.XYZ.ID.getType())
     .eq(root.FOO_ID)
   )
  .leftJoin(bar).on(
         checklistTarget.field(barFieldAliasFunction.apply(Tables.XYZ.ID), Tables.XYZ.ID.getType())
     .eq(root.BAR_ID)
   )
   ...
;

这似乎非常笨拙(很多代码),而不是非常有效(由于别名字段名称可能与别名表一起存储).

我以为别名会直接给我别名字段的方法(例如foo.getField(Tables.XYZ.ID),但事实并非如此.

当然,如果我想选择特定字段,问题就会放大...

我想念什么吗?推荐的方法是什么?

谢谢!

推荐答案

我以为别名会直接给我别名字段(例如foo.getfield(tak tak.xyz.id)),但事实似乎并非如此.

这种API确实确实很有用,尽管现有的Table.field(Field)方法不应重新改造以假定此行为.可能会引入一种新方法.另一方面,您可以写一个简单的实用程序:

<T, R extends Record> Field<T> field(Table<R> table, TableField<R, T> field) {
    if (table == foo)
        return foo.field(fooFieldAliasFunction.apply(field), field.getType());
    else if (table == bar)
        return foo.field(barFieldAliasFunction.apply(field), field.getType());
    else
        throw IllegalArgumentException();
}

然后这样称呼:

jooq.select()
    .from    (root)
    .leftJoin(foo).on(field(foo, XYZ.ID).eq(root.FOO_ID))
    .leftJoin(bar).on(field(bar, XYZ.ID).eq(root.BAR_ID))
   ...
;

本文地址:https://itbaoku.cn/post/1784116.html

问题描述

I have to access the same table for multiple references from a "root" table. In order to do so, I'm creating aliases for these tables:

protected final Table<XyzRecord> foo = Tables.XYZ.as("foo", <foo-alias-function>);
protected final Table<XyzRecord> bar = Tables.XYZ.as("bar", <bar-alias-function>);

bar-alias-function would be declared as follows_

protected final Function<Field<?>, String> fooFieldAliasFunction = f -> "foo_" + f.getName();

Now since I'd like to benefit from type safe queries, I need to re-use the same alias-function in my queries to access the fields:

jooq.select()
  .from    (root)
  .leftJoin(foo).on(
         checklistTarget.field(fooFieldAliasFunction.apply(Tables.XYZ.ID), Tables.XYZ.ID.getType())
     .eq(root.FOO_ID)
   )
  .leftJoin(bar).on(
         checklistTarget.field(barFieldAliasFunction.apply(Tables.XYZ.ID), Tables.XYZ.ID.getType())
     .eq(root.BAR_ID)
   )
   ...
;

This seems awefully clumsy (a lot of code) and not terribly efficient (since the aliased field names are probably stored with the aliased table).

I assumed there would be a method on the alias that would give me the aliased field directly (e.g. foo.getField(Tables.XYZ.ID), but that doesn't seem to be the case.

Of course the problem is amplified if I want to select specific fields...

Am I missing something? What's the recommended way of doing this?

Thank you!

推荐答案

I assumed there would be a method on the alias that would give me the aliased field directly (e.g. foo.getField(Tables.XYZ.ID), but that doesn't seem to be the case.

This kind of API would be useful indeed, although the existing Table.field(Field) method shouldn't be retrofitted to assume this behaviour. A new method might be introduced. On the other hand, you could write a simple utility:

<T, R extends Record> Field<T> field(Table<R> table, TableField<R, T> field) {
    if (table == foo)
        return foo.field(fooFieldAliasFunction.apply(field), field.getType());
    else if (table == bar)
        return foo.field(barFieldAliasFunction.apply(field), field.getType());
    else
        throw IllegalArgumentException();
}

And then call it like this:

jooq.select()
    .from    (root)
    .leftJoin(foo).on(field(foo, XYZ.ID).eq(root.FOO_ID))
    .leftJoin(bar).on(field(bar, XYZ.ID).eq(root.BAR_ID))
   ...
;