generated_coverage, textual_holdings, show_generated
FROM serial.supplement_summary ;
---
--- SELECT
--- sasum.summary_type || '-' || sasum.id AS summary_key,
--- sasum.generated_coverage, aou.shortname, siss.label
--- FROM serial.any_summary sasum
--- JOIN serial.distribution sdist ON (sdist.id = sasum.distribution)
--- JOIN actor.org_unit aou ON (aou.id = sdist.holding_lib) -- XXX remove later
--- JOIN serial.stream sstr ON (sstr.distribution = sdist.id)
--- JOIN serial.item sitem
--- ON (sitem.status = 'Received' AND sitem.stream = sstr.id)
--- JOIN serial.issuance siss ON (siss.id = sitem.issuance)
--- ORDER BY 1, 2, siss.date_published;
---
+-- XXX wait, this is cool, but will I actually need this? The problem I really
+-- set out to solve was how to order a set of rows that have OU ids in them by
+-- those OU's positions in a depth-first tree. can i just sort by something
+-- like the path here? i think so
+CREATE OR REPLACE FUNCTION actor.org_unit_descendants_depth_first( INT ) RETURNS SETOF actor.org_unit AS $$
+ WITH RECURSIVE descendant_depth(id, parent_ou, depth, path) AS (
+ SELECT ou.id,
+ ou.parent_ou,
+ out.depth,
+ ARRAY[ou.id]
+ FROM actor.org_unit ou
+ JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
+ WHERE ou.id = $1
+ UNION ALL
+ SELECT ou.id,
+ ou.parent_ou,
+ out.depth,
+ ot.path || ARRAY[ou.id]
+ FROM actor.org_unit ou
+ JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
+ JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
+ ) SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth dd USING (id) ORDER BY dd.path;
+$$ LANGUAGE SQL ROWS 1;
+
COMMIT;