Jooq在生成的查询中动态更改db的架构 [英] Jooq dynamically change db's schema in generated query

查看:610
本文介绍了Jooq在生成的查询中动态更改db的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在简单数据库中有2个类似的模式 - develop和stage。我已经使用Jooq为其中一个模式生成了Java类(例如develop)。当jooq生成对db的查询时,它会隐式地将模式的名称添加到所有查询的别名

I have 2 similar schemas in simple database - "develop" and "stage". I have generated Java classes with Jooq for one of that schemas ("develop" for example). When jooq generates query to db, it implicitly add schema's name to all query's aliases

select "develop"."image"."id", "develop"."image"."image_data" 
from "develop"."image" 
where "develop"."image"."id" = ?

所以我的问题是,是否有办法改变jooq模式名称(对于stage为在生成的查询中没有为stage模式重新生成jooq的类?

So my question is, whether there are the way to change jooq schema name (for "stage" as an example) in generated query without regenerating jooq's classes for "stage" schema?

推荐答案

你有几个选项,甚至可以合并:

You have several options, which can even be combined:

如果你想避免硬连接develop将模式名称添加到生成的类中,您可以将其重写为其他模式名称,如下所示:

If you want to avoid hard-wiring the "develop" schema name into your generated classes, you can rewrite that to some other schema name like this:

<configuration>
  <generator>
    <database>
      <schemata>
        <schema>
          <inputSchema>develop</inputSchema>
          <outputSchema>stage</outputSchema>
        </schema>
      ...

当然,这只是推迟了问题,因为模式名称是仍在生成的代码中。您可以使用以下选项从生成的代码中完全删除名称:

Of course, this just postpones the problem, because the schema name is still in the generated code. You can remove the name entirely from generated code by using the following option:

<configuration>
  <generator>
    <database>
      <schemata>
        <schema>
          <inputSchema>develop</inputSchema>
          <outputSchemaToDefault>true</outputSchemaToDefault>
        </schema>
      ...

这将从生成的代码中删除任何模式引用,因此生成类可以在你的所有模式上运行(当然要确保使用正确的连接和search_path!)

This will now remove any schema references from generated code, so the generated classes can run on all your schemas (be sure to use the correct connection and search_path, of course!)

我认为这是最好的选择为你

I think this is the best option for you

更多详情:
https://www.jooq.org/doc/latest/manual/code-generation / codegen-advanced / codegen-config-catalog-and-schema-mapping /

您可以保留生成的代码,并在运行时使用 设置 (您提供给您的jOOQ运行时 配置 )。同样,您有两个相同的选项:

You can leave generated code as it is and rewrite all object references at runtime using Settings (which you supply to your jOOQ runtime Configuration). Again, you have the same two options:

映射架构名称:

new Settings().withRenderMapping(new RenderMapping()
  .withSchemata(new MappedSchema()
    .withInput("develop")
    .withOutput("stage")
  )
);

删除所有模式名称:

new Settings().withRenderSchema(false);

此处有更多详情:
https://www.jooq.org/doc/latest/manual/sql -building / dsl-context / custom-settings / settings-render-mapping /

这篇关于Jooq在生成的查询中动态更改db的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆