Jooq dynamically change db's schema in generated query

帅比萌擦擦* 提交于 2019-12-01 03:34:21

You have several options, which can even be combined:

Use the code generator's schema mapping feature

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>
      ...

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

More details here: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-catalog-and-schema-mapping/

Use the runtime schema mapping feature

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:

Mapping the schema name:

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

Removing all schema names:

new Settings().withRenderSchema(false);

More details here: https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!