]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
schema tweaks
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 020.schema.functions.sql
1 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
2 SELECT
3         CASE    WHEN $1 IS NULL
4                         THEN $2
5                 WHEN $2 IS NULL
6                         THEN $1
7                 ELSE $1 || ' ' || $2
8         END;
9 $$ LANGUAGE SQL STABLE;
10
11 CREATE AGGREGATE public.agg_text (
12         sfunc    = public.text_concat,
13         basetype = text,
14         stype    = text
15 );
16
17 CREATE OR REPLACE FUNCTION public.tsvector_concat ( tsvector, tsvector ) RETURNS tsvector AS $$
18 SELECT
19         CASE    WHEN $1 IS NULL
20                         THEN $2
21                 WHEN $2 IS NULL
22                         THEN $1
23                 ELSE $1 || ' ' || $2
24         END;
25 $$ LANGUAGE SQL STABLE;
26
27 CREATE AGGREGATE public.agg_tsvector (
28         sfunc    = public.tsvector_concat,
29         basetype = tsvector,
30         stype    = tsvector
31 );
32
33 CREATE FUNCTION tableoid2name ( oid ) RETURNS TEXT AS $$
34         BEGIN
35                 RETURN $1::regclass;
36         END;
37 $$ language 'plpgsql';
38
39
40 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT ) RETURNS SETOF actor.org_unit AS $$
41         SELECT  a.*
42           FROM  connectby('actor.org_unit','id','parent_ou','name',$1,'100','.')
43                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
44                 JOIN actor.org_unit a ON a.id = t.keyid
45           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
46 $$ LANGUAGE SQL STABLE;
47
48 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors ( INT ) RETURNS SETOF actor.org_unit AS $$
49         SELECT  a.*
50           FROM  connectby('actor.org_unit','parent_ou','id','name',$1,'100','.')
51                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
52                 JOIN actor.org_unit a ON a.id = t.keyid
53           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
54 $$ LANGUAGE SQL STABLE;
55
56 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
57         SELECT  a.*
58           FROM  connectby('actor.org_unit','id','parent_ou','name',
59                                 (SELECT x.id
60                                    FROM actor.org_unit_ancestors($1) x
61                                         JOIN actor.org_unit_type y ON x.ou_type = y.id
62                                   WHERE y.depth = $2)
63                 ,'100','.')
64                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
65                 JOIN actor.org_unit a ON a.id = t.keyid
66           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
67 $$ LANGUAGE SQL STABLE;
68
69 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
70         SELECT  *
71           FROM  actor.org_unit_ancestors($1)
72                         UNION
73         SELECT  *
74           FROM  actor.org_unit_descendants($1);
75 $$ LANGUAGE SQL STABLE;
76
77 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
78         SELECT  *
79           FROM  actor.org_unit_ancestors($1)
80                         UNION
81         SELECT  *
82           FROM  actor.org_unit_ancestors($2);
83 $$ LANGUAGE SQL STABLE;
84
85 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
86         SELECT  *
87           FROM  actor.org_unit_ancestors($1)
88                         INTERSECT
89         SELECT  *
90           FROM  actor.org_unit_ancestors($2);
91 $$ LANGUAGE SQL STABLE;
92
93 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
94         SELECT COUNT(id)::INT FROM (
95                 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
96                         EXCEPT
97                 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
98         ) z;
99 $$ LANGUAGE SQL STABLE;
100
101