]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
059aa79c227e43a2fdc0f060991b77a42fc14ded
[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
57
58 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
59         SELECT  a.*
60           FROM  connectby('actor.org_unit','id','parent_ou','name',
61                                 (SELECT x.id
62                                    FROM actor.org_unit_ancestors($1) x
63                                         JOIN actor.org_unit_type y ON x.ou_type = y.id
64                                   WHERE y.depth = $2)
65                 ,'100','.')
66                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
67                 JOIN actor.org_unit a ON a.id = t.keyid
68           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
69 $$ LANGUAGE SQL STABLE;
70
71 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS '
72         SELECT  *
73           FROM  actor.org_unit_ancestors($1)
74                         UNION
75         SELECT  *
76           FROM  actor.org_unit_descendants($1);
77 ' LANGUAGE SQL STABLE;
78
79
80 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS '
81         SELECT COUNT(id)::INT FROM (
82                 select * from (SELECT id FROM  actor.org_unit_ancestors($1) UNION SELECT  id FROM  actor.org_unit_ancestors($2)) x
83                         EXCEPT
84                 select * from (SELECT id FROM  actor.org_unit_ancestors($1) INTERSECT SELECT  id FROM  actor.org_unit_ancestors($2)) y) z;
85 ' LANGUAGE SQL STABLE;
86
87