File: jsonb.sql

package info (click to toggle)
pljs 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,916 kB
  • sloc: ansic: 69,358; javascript: 5,408; sql: 880; makefile: 446; sh: 123
file content (20 lines) | stat: -rw-r--r-- 422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CREATE FUNCTION get_key(key text, json_data jsonb) RETURNS jsonb
LANGUAGE pljs IMMUTABLE STRICT
AS $$
  var val = json_data[key];
  var ret = {};
  ret[key] = val;
  return JSON.stringify(ret);
$$;

CREATE TABLE jsonbonly (
    data jsonb
);

COPY jsonbonly (data) FROM stdin;
{"ok": true}
\.

-- Call twice to test the function cache.
SELECT get_key('ok', data) FROM jsonbonly;
SELECT get_key('ok', data) FROM jsonbonly;