Created
October 4, 2012 15:08
-
-
Save kwstannard/3834222 to your computer and use it in GitHub Desktop.
evaluate in psql
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
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