From: Thomas Berezansky Date: Thu, 11 Aug 2011 13:35:18 +0000 (-0400) Subject: New version of actor.org_unit_ancestors X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=310516f77aab635baa5d986d12166005d356e56f;p=contrib%2FConifer.git New version of actor.org_unit_ancestors Preserves order through joins Basically actor.org_unit_ancestors_distance with a join to get the entire org units, rather than just IDs and a distance. Signed-off-by: Thomas Berezansky Signed-off-by: Mike Rylander --- diff --git a/Open-ILS/src/sql/Pg/020.schema.functions.sql b/Open-ILS/src/sql/Pg/020.schema.functions.sql index 5653dedc2e..ff8751ee76 100644 --- a/Open-ILS/src/sql/Pg/020.schema.functions.sql +++ b/Open-ILS/src/sql/Pg/020.schema.functions.sql @@ -235,17 +235,14 @@ CREATE OR REPLACE FUNCTION actor.org_unit_descendants_distance( INT ) RETURNS TA $$ LANGUAGE SQL STABLE ROWS 1; CREATE OR REPLACE FUNCTION actor.org_unit_ancestors( INT ) RETURNS SETOF actor.org_unit AS $$ - WITH RECURSIVE anscestor_depth AS ( - SELECT ou.id, - ou.parent_ou - FROM actor.org_unit ou - WHERE ou.id = $1 - UNION ALL - SELECT ou.id, - ou.parent_ou - FROM actor.org_unit ou - JOIN anscestor_depth ot ON (ot.parent_ou = ou.id) - ) SELECT ou.* FROM actor.org_unit ou JOIN anscestor_depth USING (id); + WITH RECURSIVE org_unit_ancestors_distance(id, distance) AS ( + SELECT $1, 0 + UNION + SELECT ou.parent_ou, ouad.distance+1 + FROM actor.org_unit ou JOIN org_unit_ancestors_distance ouad ON (ou.id = ouad.id) + WHERE ou.parent_ou IS NOT NULL + ) + SELECT ou.* FROM actor.org_unit ou JOIN org_unit_ancestors_distance ouad USING (id) ORDER BY ouad.distance; $$ LANGUAGE SQL ROWS 1; CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_at_depth ( INT,INT ) RETURNS actor.org_unit AS $$