JOOQ元模型:为什么没有Table.rename(String)?

JOOQ Metamodel: why is there no Table.rename(String)?

我正在动态创建和使用我只有一个元模型对象的物理数据库表。举个例子:我的元模型中有一个 JOOQ class Customer,但在运行时我有 CUSTOMER1CUSTOMER2 等。我想为这些动态表编写强类型 JOOQ 查询。以下似乎可以解决问题:

Customer CUSTOMER1 = Customer.rename("CUSTOMER1")

当然,我需要为一大堆表执行此操作。不幸的是,我不能一般地利用 rename 方法,因为它不是 Table<R> 接口的一部分。这是对我遗漏的东西的疏忽或故意措施吗?

是否有一种健壮而优雅的方式来实现我想要的,即不诉诸反射?

EDIT:这两个表从未在同一个查询中联合使用。具体使用模式如下:在任何给定的时刻,一个DB同义词CUSTOMER将指向一个(活动的),而另一个正在被修改(卷影副本)。修改完成后,通过将同义词指向另一个来交换角色,然后我们重新开始。我们这样做是为了尽量减少 "downtime" 繁重的报告结果表。

您问题的答案

标题中的问题:

why is there no Table.rename(String)?

该功能已在 jOOQ 3.3 (#2921) 中实现。问题内容如下:

As table renaming is not really a SQL DSL feature, the rename() method should be generated onto generated tables only, instead of being declared in org.jooq.Table

其实这个说法真的没什么意义。 DSL 类型上还有其他 "non-DSL" 实用程序。我不明白为什么 rename() 很特别。该方法应按照您的建议在 Table 上声明。这将在 jOOQ 3.9 (#5242) 中完成。

问题的答案

根据您问题的更新,我了解到您并不真正需要 fine-grained 重命名控件。 jOOQ 的开箱即用 multi-tenancy 功能就可以了。这个在手册中叫做"table mapping":

http://www.jooq.org/doc/latest/manual/sql-building/dsl-context/runtime-schema-mapping

对于 Configuration 的范围(大多数 fine-grained 范围:per-query 级别),您可以重写任何匹配的 table 名称:

Settings settings = new Settings()
    .withRenderMapping(new RenderMapping()
    .withSchemata(
        new MappedSchema().withInput("MY_SCHEMA")
                          .withOutput("MY_SCHEMA")
                          .withTables(
         new MappedTable().withInput("CUSTOMER")
                          .withOutput("CUSTOMER1"))));