Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created May 4, 2011 18:31
Show Gist options
  • Save sycobuny/955727 to your computer and use it in GitHub Desktop.
Save sycobuny/955727 to your computer and use it in GitHub Desktop.
Basic PL/Ruby example
/*
* FUNCTION coleoptera.find_or_create_instructions(
* TEXT[] -- instruction labels
* )
* RETURNS coleoptera.instructions[]
*/
-- as plpgsql
CREATE OR REPLACE
FUNCTION coleoptera.find_or_create_instructions(IN TEXT[], OUT coleoptera.instructions[])
RETURNS coleoptera.instructions[]
LANGUAGE PLPGSQL VOLATILE
AS $BODY$
BEGIN
IF ARRAY_LOWER($1, 1) IS NULL THEN
RETURN;
END IF;
FOR xIndex IN ARRAY_LOWER($1, 1) .. ARRAY_UPPER($1, 1) LOOP
$2 := $2 || coleoptera.find_or_create_instruction($1[xIndex]);
END LOOP;
END;
$BODY$;
-- as plruby, though I can't tell if I'm allowed to call the other DB function
-- this way, certainly I have no control over the path, so nfi
CREATE OR REPLACE
FUNCTION coleoptera.find_or_create_instructions(instructions TEXT[])
RETURNS coleoptera.instructions[]
LANGUAGE PLRUBY VOLATILE
AS $BODY$
return if instructions.empty?
instructions.collect { |elem| find_or_create_instruction(elem) }
$BODY$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment