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

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

JOOQ update JSONB column with concat operator

问题

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

create table foobar(id text primary key, address jsonb not null);

insert into foobar(id, address)
values ('1234', '["a", "b"]'::jsonb);


-- insert / update example
insert into foobar(id, address)
values ('1234', '["a", "b"]'::jsonb)
on conflict (id) DO UPDATE
set address = foobar.address || '["C"]'::jsonb
where foobar.id = '1234';

-- explicit update example
update foobar
set address = address || '["D"]'::jsonb
where id = '1234';
select * from foobar; 

以上查询的结果:

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.

create table foobar(id text primary key, address jsonb not null);

insert into foobar(id, address)
values ('1234', '["a", "b"]'::jsonb);


-- insert / update example
insert into foobar(id, address)
values ('1234', '["a", "b"]'::jsonb)
on conflict (id) DO UPDATE
set address = foobar.address || '["C"]'::jsonb
where foobar.id = '1234';

-- explicit update example
update foobar
set address = address || '["D"]'::jsonb
where id = '1234';

select * from foobar; 

Result for above query:

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 模板化 解决方法:

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

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

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

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:

确定