如何在jooq中使用insert ... returning语句作为“类似表的查询”?

时间:2019-08-21 03:11:47

标签: postgresql jooq

我正在以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!

1 个答案:

答案 0 :(得分:1)

从jOOQ 3.11(以及不久的3.12)开始,如果不依靠plain SQL templating,这在jOOQ中是不可能的。

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