【问题标题】:jOOQ idempotent batch insertjOOQ 幂等批量插入
【发布时间】:2021-03-27 03:17:08
【问题描述】:

我正在尝试实现幂等插入,这个查询看起来很适合这个任务

insert into test_table
select *
from (
         values (1, 2, 3, 4),
                (3, 4, 5, 6),
                (1, 2, 3, 4),
                (3, 4, 5, 6)
     ) as data(a, b, c, d)
where not exists(select 1
                 from test_table
                 where test_table.a = data.a
                   and test_table.b = data.b
                   and test_table.c = data.c);

请帮助将此查询转换为 jOOQ DSL
我用的是Greenplum数据库,不支持ON CONFLICT语法

【问题讨论】:

    标签: java sql jooq


    【解决方案1】:
    ctx.insertInto(TEST_TABLE)
       .select(
           select()
           .from(values(
               row(1, 2, 3, 4),
               row(3, 4, 5, 6),
               row(1, 2, 3, 4),
               row(3, 4, 5, 6)
           ).as("data", "a", "b", "c", "d"))
           .whereNotExists(
               selectOne()
               .from(TEST_TABLE)
               .where(TEST_TABLE.A.eq(field(name("data", "a"), TEST_TABLE.A.getDataType())))
               .and(TEST_TABLE.B.eq(field(name("data", "b"), TEST_TABLE.A.getDataType())))
               .and(TEST_TABLE.C.eq(field(name("data", "c"), TEST_TABLE.A.getDataType())))
           )
       )
       .execute();
    

    此答案假设您正在使用 TEST_DATA 的代码生成器(否则,请手动构建标识符,如上所示 name("data", "a")or as shown here)。此外,它假设:

    import static org.jooq.impl.DSL.*;
    

    When Greenplum is formally supported, see #4700,然后ON CONFLICTON DUPLICATE KEY IGNORE 可以为您模拟。

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2020-05-22
      • 2017-12-29
      • 2021-05-23
      • 2020-03-18
      • 2021-01-20
      • 2013-07-02
      • 2021-09-08
      • 2023-01-14
      相关资源
      最近更新 更多