| 229 | Sposób na zmianę wartości jednego pola w obiekcie typu JSON: |
| 230 | {{{ |
| 231 | CREATE OR REPLACE FUNCTION "json_set_value"( |
| 232 | "json" json, |
| 233 | "key_to_set" TEXT, |
| 234 | "value_to_set" anyelement |
| 235 | ) |
| 236 | RETURNS json |
| 237 | LANGUAGE sql |
| 238 | IMMUTABLE |
| 239 | STRICT |
| 240 | AS $function$ |
| 241 | SELECT COALESCE( |
| 242 | (SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}') |
| 243 | FROM (SELECT * |
| 244 | FROM json_each("json") |
| 245 | WHERE "key" <> "key_to_set" |
| 246 | UNION ALL |
| 247 | SELECT "key_to_set", to_json("value_to_set")) AS "fields"), |
| 248 | '{}' |
| 249 | )::json |
| 250 | $function$; |
| 251 | |
| 252 | UPDATE cregisters.register_field SET params = json_set_value(params, 'do_refresh', true) WHERE id____ = 1; |
| 253 | }}} |
| 254 | |
| 255 | |