This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
CREATE FUNCTION valid_json(json text) RETURNS boolean | ||
LANGUAGE plv8 IMMUTABLE STRICT | ||
AS $$ | ||
try { | ||
JSON.parse(json); | ||
return true; | ||
} catch(e) { | ||
return false; | ||
} | ||
$$; | ||
CREATE DOMAIN json AS text | ||
CONSTRAINT json_check CHECK (valid_json(VALUE)); | ||
CREATE FUNCTION get_key(key text, json_raw text) RETURNS json | ||
LANGUAGE plv8 IMMUTABLE STRICT | ||
AS $$ | ||
var val = JSON.parse(json_raw)[key]; | ||
var ret = {}; | ||
ret[key] = val; | ||
return JSON.stringify(ret); | ||
$$; | ||
CREATE TABLE jsononly ( | ||
data json | ||
); | ||
COPY jsononly (data) FROM stdin; | ||
-- Call twice to test the function cache. | ||
SELECT get_key('ok', data) FROM jsononly; | ||
get_key | ||
------------- | ||
{"ok":true} | ||
(1 row) | ||
|
||
SELECT get_key('ok', data) FROM jsononly; | ||
get_key | ||
------------- | ||
{"ok":true} | ||
(1 row) | ||
|
Oops, something went wrong.