Skip to content

Instantly share code, notes, and snippets.

@slattery
Forked from audreyt/apply.sql
Created October 6, 2016 13:14
Show Gist options
  • Save slattery/d2b65746053afbf3fab25fef96371df5 to your computer and use it in GitHub Desktop.
Save slattery/d2b65746053afbf3fab25fef96371df5 to your computer and use it in GitHub Desktop.
plv8 SQL for the |> and <| operators (aka LiveScript application): SELECT '{"hello": [1,2,3]}' |> 'this.hello[0]'; SELECT 'this.hello[0]' <| '{"hello": [1,2,3]}';
CREATE OR REPLACE FUNCTION plv8x.json_eval(code text) RETURNS plv8x.json AS $$
return JSON.stringify(
eval("(function(){return #code})").apply(this)
);
$$ LANGUAGE plls IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION plv8x.json_eval(data plv8x.json, code text) RETURNS plv8x.json AS $$
return JSON.stringify(
eval("(function(){return #code})").apply(JSON.parse(data))
);
$$ LANGUAGE plls IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION plv8x.json_eval(code text, data plv8x.json) RETURNS plv8x.json AS $$
return JSON.stringify(
eval("(function(){return #code})").apply(JSON.parse(data))
);
$$ LANGUAGE plls IMMUTABLE STRICT;
DROP OPERATOR IF EXISTS |> (text);
CREATE OPERATOR |> (
RIGHTARG = text,
PROCEDURE = plv8x.json_eval
);
DROP OPERATOR IF EXISTS |> (plv8x.json, text);
CREATE OPERATOR |> (
LEFTARG = plv8x.json,
RIGHTARG = text,
COMMUTATOR = <|,
PROCEDURE = plv8x.json_eval
);
DROP OPERATOR IF EXISTS <| (text, plv8x.json);
CREATE OPERATOR <| (
LEFTARG = text,
RIGHTARG = plv8x.json,
COMMUTATOR = |>,
PROCEDURE = plv8x.json_eval
);
SELECT '{"hello": [1,2,3]}' |> 'this.hello[0]';
SELECT 'this.hello[0]' <| '{"hello": [1,2,3]}';
SELECT |> '1+1';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment