使用来自 json 的数据更新 table 的一行并使用 json 变量
updating a row of a table using data from json and working with json variables
考虑 table:
id
是 integer
,name
是 varchar
,age
也是 integer
id |name | age
______________
1. | one | 12
--------------
2. | two | 15
--------------
3. | thre| 16
--------------
在我的节点代码中,我有一个 json 对象:
const jsonFromNode = {
id: 1,
name: "new_one",
age: 12
}
我正在使用查询构建器 knex
,我想直接使用这个 json 来更新 table 中的行,我在想类似的东西节点中的这个(我不确定这是否有效,以下是伪代码):
const string = `
do $$
DECLARE
myjson JSON;
BEGIN
myjson := '${jsonFromNode}';
-- some psql method/function/procedure to update
-- the row where id=1 and age= 12
SomePlPgsqlFunctionOrStepOfProcedures(myjson); //update by passing json
END;
$$ language 'plpgsql';
`
所以,主要问题是:
我的想法是否可行?
用于获取我的 json 变量中的数据值的语法是什么。例如。 x = {'a': 'b'} 是我在上面 psql 中的变量,我如何在 psql 中做这样的事情: y = x['b']
将替换的步骤或功能是什么: SomePlPgsqlFunctionOrStepOfProcedures
以上
您实际上并不需要函数或过程,您可以使用 jsonb_populate_record 函数 (docs) 在单个语句中执行插入。
以下是您在 psql 控制台中的操作方式:
test# -- assign the json to a variable
test# \set j '{"id": 1, "name": "new_one", "age": 12 }'
test# insert into ages (id, name, age) select id, name, age from jsonb_populate_record(null::ages, :'j'::jsonb);
INSERT 0 1
test# table ages;
id │ name │ age
════╪═════════╪═════
1 │ new_one │ 12
回答实际问题:这个函数可以解决问题。
CREATE OR REPLACE FUNCTION json_update (_data jsonb) RETURNS void AS $$
DECLARE
_id int;
_name text;
_age int;
BEGIN
SELECT id, name, age INTO _id, _name, _age
FROM jsonb_populate_record(null::ages, _data);
UPDATE ages SET name = _name, age = _age WHERE id = _id;
END;
$$ LANGUAGE plpgsql;
或者这个版本,它使用单个记录变量而不是每列一个:
CREATE OR REPLACE FUNCTION json_update (_data jsonb) RETURNS void AS $$
DECLARE
_myrecord record;
BEGIN
SELECT id, name, age INTO _myrecord
FROM jsonb_populate_record(null::ages, _data);
UPDATE ages SET name = _myrecord.name, age = _myrecord.age WHERE id = _myrecord.id;
END;
$$ LANGUAGE plpgsql;
考虑 table:
id
是 integer
,name
是 varchar
,age
也是 integer
id |name | age
______________
1. | one | 12
--------------
2. | two | 15
--------------
3. | thre| 16
--------------
在我的节点代码中,我有一个 json 对象:
const jsonFromNode = {
id: 1,
name: "new_one",
age: 12
}
我正在使用查询构建器 knex
,我想直接使用这个 json 来更新 table 中的行,我在想类似的东西节点中的这个(我不确定这是否有效,以下是伪代码):
const string = `
do $$
DECLARE
myjson JSON;
BEGIN
myjson := '${jsonFromNode}';
-- some psql method/function/procedure to update
-- the row where id=1 and age= 12
SomePlPgsqlFunctionOrStepOfProcedures(myjson); //update by passing json
END;
$$ language 'plpgsql';
`
所以,主要问题是:
我的想法是否可行?
用于获取我的 json 变量中的数据值的语法是什么。例如。 x = {'a': 'b'} 是我在上面 psql 中的变量,我如何在 psql 中做这样的事情: y = x['b']
将替换的步骤或功能是什么:
SomePlPgsqlFunctionOrStepOfProcedures
以上
您实际上并不需要函数或过程,您可以使用 jsonb_populate_record 函数 (docs) 在单个语句中执行插入。
以下是您在 psql 控制台中的操作方式:
test# -- assign the json to a variable
test# \set j '{"id": 1, "name": "new_one", "age": 12 }'
test# insert into ages (id, name, age) select id, name, age from jsonb_populate_record(null::ages, :'j'::jsonb);
INSERT 0 1
test# table ages;
id │ name │ age
════╪═════════╪═════
1 │ new_one │ 12
回答实际问题:这个函数可以解决问题。
CREATE OR REPLACE FUNCTION json_update (_data jsonb) RETURNS void AS $$
DECLARE
_id int;
_name text;
_age int;
BEGIN
SELECT id, name, age INTO _id, _name, _age
FROM jsonb_populate_record(null::ages, _data);
UPDATE ages SET name = _name, age = _age WHERE id = _id;
END;
$$ LANGUAGE plpgsql;
或者这个版本,它使用单个记录变量而不是每列一个:
CREATE OR REPLACE FUNCTION json_update (_data jsonb) RETURNS void AS $$
DECLARE
_myrecord record;
BEGIN
SELECT id, name, age INTO _myrecord
FROM jsonb_populate_record(null::ages, _data);
UPDATE ages SET name = _myrecord.name, age = _myrecord.age WHERE id = _myrecord.id;
END;
$$ LANGUAGE plpgsql;