]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
b9ffb1690feb0f56cb685caa03db5db5489a8eeb
[Evergreen.git] / Open-ILS / src / sql / Pg / 020.schema.functions.sql
1 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
2         SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
3 $$ LANGUAGE SQL STABLE;
4
5 CREATE AGGREGATE public.first (
6         sfunc    = public.first_agg,
7         basetype = anyelement,
8         stype    = anyelement
9 );
10
11 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
12         SELECT $2;
13 $$ LANGUAGE SQL STABLE;
14
15 CREATE AGGREGATE public.last (
16         sfunc    = public.last_agg,
17         basetype = anyelement,
18         stype    = anyelement
19 );
20
21 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
22 SELECT
23         CASE    WHEN $1 IS NULL
24                         THEN $2
25                 WHEN $2 IS NULL
26                         THEN $1
27                 ELSE $1 || ' ' || $2
28         END;
29 $$ LANGUAGE SQL STABLE;
30
31 CREATE AGGREGATE public.agg_text (
32         sfunc    = public.text_concat,
33         basetype = text,
34         stype    = text
35 );
36
37 CREATE OR REPLACE FUNCTION public.tsvector_concat ( tsvector, tsvector ) RETURNS tsvector AS $$
38 SELECT
39         CASE    WHEN $1 IS NULL
40                         THEN $2
41                 WHEN $2 IS NULL
42                         THEN $1
43                 ELSE $1 || ' ' || $2
44         END;
45 $$ LANGUAGE SQL STABLE;
46
47 CREATE AGGREGATE public.agg_tsvector (
48         sfunc    = public.tsvector_concat,
49         basetype = tsvector,
50         stype    = tsvector
51 );
52
53 CREATE FUNCTION tableoid2name ( oid ) RETURNS TEXT AS $$
54         BEGIN
55                 RETURN $1::regclass;
56         END;
57 $$ language 'plpgsql';
58
59
60 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT ) RETURNS SETOF actor.org_unit AS $$
61         SELECT  a.*
62           FROM  connectby('actor.org_unit','id','parent_ou','name',$1,'100','.')
63                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
64                 JOIN actor.org_unit a ON a.id = t.keyid
65           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
66 $$ LANGUAGE SQL STABLE;
67
68 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors ( INT ) RETURNS SETOF actor.org_unit AS $$
69         SELECT  a.*
70           FROM  connectby('actor.org_unit','parent_ou','id','name',$1,'100','.')
71                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
72                 JOIN actor.org_unit a ON a.id = t.keyid
73           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
74 $$ LANGUAGE SQL STABLE;
75
76 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
77         SELECT  a.*
78           FROM  connectby('actor.org_unit','id','parent_ou','name',
79                                 (SELECT x.id
80                                    FROM actor.org_unit_ancestors($1) x
81                                         JOIN actor.org_unit_type y ON x.ou_type = y.id
82                                   WHERE y.depth = $2)
83                 ,'100','.')
84                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
85                 JOIN actor.org_unit a ON a.id = t.keyid
86           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
87 $$ LANGUAGE SQL STABLE;
88
89 CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_at_depth ( INT,INT ) RETURNS actor.org_unit AS $$
90         SELECT  a.*
91           FROM  actor.org_unit a
92           WHERE id = ( SELECT FIRST(x.id)
93                          FROM   actor.org_unit_ancestors($1) x
94                                 JOIN actor.org_unit_type y
95                                         ON x.ou_type = y.id AND y.depth = $2);
96 $$ LANGUAGE SQL STABLE;
97
98 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
99         SELECT  *
100           FROM  actor.org_unit_ancestors($1)
101                         UNION
102         SELECT  *
103           FROM  actor.org_unit_descendants($1);
104 $$ LANGUAGE SQL STABLE;
105
106 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
107         SELECT  * FROM actor.org_unit_full_path((actor.org_unit_ancestor_at_depth($1, $2)).id)
108 $$ LANGUAGE SQL STABLE;
109
110 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
111         SELECT  *
112           FROM  actor.org_unit_ancestors($1)
113                         UNION
114         SELECT  *
115           FROM  actor.org_unit_ancestors($2);
116 $$ LANGUAGE SQL STABLE;
117
118 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
119         SELECT  *
120           FROM  actor.org_unit_ancestors($1)
121                         INTERSECT
122         SELECT  *
123           FROM  actor.org_unit_ancestors($2);
124 $$ LANGUAGE SQL STABLE;
125
126 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
127         SELECT COUNT(id)::INT FROM (
128                 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
129                         EXCEPT
130                 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
131         ) z;
132 $$ LANGUAGE SQL STABLE;
133
134 CREATE AGGREGATE array_accum (
135         sfunc = array_append,
136         basetype = anyelement,
137         stype = anyarray,
138         initcond = '{}'
139 );
140