Skip to content

Instantly share code, notes, and snippets.

@kwstannard
Created October 4, 2012 15:08
Show Gist options
  • Save kwstannard/3834222 to your computer and use it in GitHub Desktop.
Save kwstannard/3834222 to your computer and use it in GitHub Desktop.
evaluate in psql
mobi_dev=# create or replace function eval (expression text, arg text, val text) returns boolean
as
$body$
declare
result boolean;
begin
execute replace(replace(expression, 'arg', '''' || arg || ''''), 'val', '''' || val || '''') into result;
return result;
end;
$body$
language plpgsql;
CREATE FUNCTION
create or replace function runCondition(id_index int) returns boolean as
$body$
declare
result boolean;
c conditions%rowtype;
begin
select * into c from conditions where id = id_index;
if c.op = 'and' or c.op = 'or' then
select eval('select arg ' || c.op || ' val', cast (runCondition(cast (c.c_left as int)) as text), cast (runCondition(cast (c.c_right as int)) as text)) into result;
else
select eval(c.op, c.c_left, c.c_right) into result;
end if;
return result;
end;
$body$
language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment