【问题标题】:How to rewrite the table qualifier at runtime with JOOQ如何在运行时使用 JOOQ 重写表限定符
【发布时间】:2020-02-27 18:09:31
【问题描述】:

使用 JOOQ 为 SQL Server 数据库生成代码时,生成会创建三部分限定符,例如:[catalog].[schema].[table]。这正是我在使用 SQL Server 数据库时想要的,但在将生成的代码与另一个数据库(如内存数据库中的 H2)一起使用以进行单元测试时却是一个问题。

H2 方言不支持这些三部分限定符,H2 需要类似 [catalog].[table] 的内容。这会在对 H2 执行如下命令时导致语法错误:

context.createTable(TBLBUSINESSENTITY).columns(TBLBUSINESSENTITY.fields()).execute();

为了解决这个问题,我需要在运行时更改限定符,我认为这可以使用渲染映射和映射模式来完成。不幸的是,这似乎只能像这样修改限定符的架构部分:

Settings settings = new Settings().withRenderMapping(new RenderMapping().withSchemata(
      new MappedSchema().withInput("dbo").withOutput("mySchema")));

鉴于限定符[MyDatabase].[dbo].[MyTable],这映射到[MyDatabase].[mySchema].[MyTable],但我不知道如何完全删除该部分。

有没有办法将映射重写为[MyDatabase].[MyTable]

【问题讨论】:

    标签: jooq


    【解决方案1】:

    改用此设置:

    Settings settings = new Settings()
      .withRenderCatalog(false)
      .withRenderMapping(new RenderMapping()
        .withCatalogs(new MappedCatalog()
          .withInput("MyDatabase")
          .withSchemata(new MappedSchema()
            .withInput("dbo")
            .withOutput("MyDatabase"))));
    

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 2016-02-12
      • 2016-06-23
      • 2021-09-09
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2011-02-14
      相关资源
      最近更新 更多