简体   繁体   English

JOOQ 处理嵌套列表 UPSERT

[英]JOOQ handling nested list UPSERT

Using the documentation here I know I can use onDuplicateKeyUpdate to handle an UPSERT.使用此处的文档,我知道我可以使用onDuplicateKeyUpdate来处理 UPSERT。 But, how can I match values when inserting a list of list values?但是,在插入列表值列表时如何匹配值? I'm using Postgres as the actual implementation here.我在这里使用 Postgres 作为实际实现。

Suppose the following Java taking in a nested List, then starting a JOOQ query.假设下面的 Java 接收一个嵌套列表,然后开始一个 JOOQ 查询。

List<List<String>> values = List.of(List.of("1", "2", "3"), List.of("4", "5", "6"));

dsl.insertInto(TABLE)
        .values(values)
        .onDuplicateKeyUpdate()

Next I would have to call set() in the fluent call.接下来我必须在流利的调用中调用set() However, with this being a nested list I cannot access each item here.但是,由于这是一个嵌套列表,我无法访问此处的每个项目。

For example, suppose the database contains this row already:例如,假设数据库已经包含此行:

| col1 | col2 | col3 |
+------+------+------+
|   1  |   0  |   0  |
+------+------+------+

With Col1 being the primary key, I'd expect the above code to do an UPSERT and try to insert, but then fail on the first row.由于 Col1 是主键,我希望上面的代码执行 UPSERT 并尝试插入,但随后在第一行失败。 Updating col2 & col3 respectively.分别更新col2 & col3 Then doing a normal insert for the second.然后为第二个进行正常插入。 Becoming:变得:

| col1 | col2 | col3 |
+------+------+------+
|   1  |   2  |   3  |
+------+------+------+
|   4  |   5  |   6  |
+------+------+------+

As SQL this would be:作为 SQL 这将是:

INSERT INTO table(col1, col2 col3)
((1, 2, 3), (4, 5, 6))
ON CONFLICT (col1)
DO UPDATE SET
col1 = excluded.col1,
col2 = excluded.col2,
col3 = excluded.col3

Is there a good way to handle this in JOOQ?在 JOOQ 中是否有处理此问题的好方法? Or should I just go with raw SQL and bind in values instead?或者我应该只使用原始 SQL 的 go 并绑定值吗?

There's a pending feature request here for dynamic multi values insert statements: https://github.com/jOOQ/jOOQ/issues/6604这里有一个针对动态多值插入语句的待处理功能请求: https://github.com/jOOQ/jOOQ/issues/6604

It includes some workaround discussions.它包括一些解决方法讨论。 It's possible to do today, just not convenient.今天可以做,就是不方便。 Notice, if you're targeting PostgreSQL only, I recommend you use jOOQ's ON CONFLICT support请注意,如果您仅针对 PostgreSQL,我建议您使用 jOOQ 的ON CONFLICT支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM