I have a function in Postgres 9.6 that accepts an int[]
parameter. I'd like to have the function accept a single int as well (and convert it to a single element array if necessary).
CREATE OR REPLACE FUNCTION get_subordinates(inp_persona_ids integer[])
-- Get all subordnates of the people passed in as array
-- TODO allow a single persona ID (int) to be passed in as inp_persona_ids
RETURNS TABLE (persona_id int) AS
$$
BEGIN
RETURN QUERY(
WITH RECURSIVE children AS (
-- passed in persona_id
SELECT
id AS persona_id,
manager_id
FROM
personas
WHERE
id = ANY(inp_persona_ids)
UNION
-- and all subordinates
SELECT
p.id AS persona_id,
p.manager_id
FROM
personas p
JOIN children c ON p.manager_id = c.persona_id
)
SELECT
children.persona_id
FROM
children
LEFT JOIN
personas on children.persona_id = personas.id
WHERE personas.disabled IS NOT TRUE
);
END;
$$ LANGUAGE plpgsql
How would I change the function definition and also add some conditional logic to test for int
and change to ARRAY[int]
if necessary?