更新 JSONB 列并使用连接运算符。

huangapple go评论77阅读模式
英文:

JOOQ update JSONB column with concat operator

问题

我有一个表格,其中一个列的数据类型是JSONB,并且我试图在PostgreSQL中使用 JSONB concat (||) 运算符来更新jsonb列的内容。以下是我想要转换为jOOQ的示例纯SQL查询(插入/更新示例)。

  1. create table foobar(id text primary key, address jsonb not null);
  2. insert into foobar(id, address)
  3. values ('1234', '["a", "b"]'::jsonb);
  4. -- insert / update example
  5. insert into foobar(id, address)
  6. values ('1234', '["a", "b"]'::jsonb)
  7. on conflict (id) DO UPDATE
  8. set address = foobar.address || '["C"]'::jsonb
  9. where foobar.id = '1234';
  10. -- explicit update example
  11. update foobar
  12. set address = address || '["D"]'::jsonb
  13. where id = '1234';
  1. select * from foobar;
  2. 以上查询的结果:
  3. 1234,"["a", "b", "C", "D"]"

我看到这个问题中支持concatenation仍然未解决,但想检查是否有任何可以使用的解决方法。

英文:

I have a table with one of the column data type as JSONB and I'm trying to use the JSONB concat (||) operator in postgresql to update the content for the jsonb column. Below is the sample plain sql query (insert / update example) which I want to convert to jooq.

  1. create table foobar(id text primary key, address jsonb not null);
  2. insert into foobar(id, address)
  3. values ('1234', '["a", "b"]'::jsonb);
  4. -- insert / update example
  5. insert into foobar(id, address)
  6. values ('1234', '["a", "b"]'::jsonb)
  7. on conflict (id) DO UPDATE
  8. set address = foobar.address || '["C"]'::jsonb
  9. where foobar.id = '1234';
  10. -- explicit update example
  11. update foobar
  12. set address = address || '["D"]'::jsonb
  13. where id = '1234';
  1. select * from foobar;
  2. Result for above query:
  3. 1234,"["a", "b", "C", "D"]"

I did see this issue for supporting concatenation is still open but want to check if there is any work around I can use.

答案1

得分: 0

在 jOOQ 中,如果您缺少对某些特定供应商功能的支持,始终可以使用 纯 SQL 模板化 解决方法:

  1. Field<JSONB> jsonbConcat(Field<JSONB> f1, Field<JSONB> f2) {
  2. DSL.field("({1} || {2})", f1.getDataType(), f1, f2);
  3. }
英文:

There's always the plain SQL templating workaround in jOOQ, if you're missing support for some vendor specific functionality:

  1. Field<JSONB> jsonbConcat(Field<JSONB> f1, Field<JSONB> f2) {
  2. DSL.field("({1} || {2})", f1.getDataType(), f1, f2);
  3. }

huangapple
  • 本文由 发表于 2023年6月9日 07:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定