【问题标题】:How to use an insert...returning statement as a "table-like" query in jooq?如何在 jooq 中使用 insert...returning 语句作为“类表”查询?
【发布时间】:2019-08-21 16:17:38
【问题描述】:

我正在编写一个INSERT INTO ... RETURNING形式的SQL查询,然后想要聚合结果。这可以通过 postgres 实现:

DROP TABLE IF EXISTS __test;
CREATE TEMPORARY TABLE __test
(
    id    int  not null,
    value text not null
);

WITH things AS (
    INSERT INTO __test
        VALUES (1, 'foo'),
               (2, 'bar'),
               (2, 'baz')
        RETURNING *
)
SELECT id,
       array_agg(value)
FROM things
GROUP BY id;

虽然我正在努力寻找一种使用 jooq 的方法,但 InsertResultStep 没有实现 Select,也没有实现 TableTableLike 接口。有没有规范的方法来实现这一点,还是我应该寻找解决方法?

val insertQuery = DSL
     .insertInto(otherTable)
     .select(
         DSL.select(recordPkColumns)
         .from(recordTable)
         .crossJoin(permValues)
         .where(filter(command.command))
     )
     .onConflictDoNothing()
     .returning()

DSL.select()
    .from(insertQuery) // problem is here!

【问题讨论】:

    标签: postgresql jooq


    【解决方案1】:

    从 jOOQ 3.11(以及很快的 3.12)开始,如果不求助于 plain SQL templating,这在 jOOQ 中是不可能的。

    相关功能请求在这里:https://github.com/jOOQ/jOOQ/issues/4474

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多