]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.relax_rank_ou.sql
rank_ou() honors pref-lib in non-global context
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.relax_rank_ou.sql
1 BEGIN;
2
3 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL)
4 RETURNS INTEGER AS $$
5     SELECT COALESCE(
6
7         -- lib matches search_lib
8         (SELECT CASE WHEN $1 = $2 THEN -20000 END),
9
10         -- lib matches pref_lib
11         (SELECT CASE WHEN $1 = $3 THEN -10000 END),
12
13
14         -- pref_lib is a child of search_lib and lib is a child of pref lib.  
15         (SELECT distance - 5000
16             FROM actor.org_unit_descendants_distance($3) 
17             WHERE id = $1 AND $3 IN (
18                 SELECT id FROM actor.org_unit_descendants($2))),
19
20         -- lib is a child of search_lib
21         (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
22
23         -- all others pay cash
24         1000
25     );
26 $$ LANGUAGE SQL STABLE;
27
28 COMMIT;
29