]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
misc cleanup
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 020.schema.functions.sql
1 CREATE OR REPLACE FUNCTION asset.call_number_dewey( TEXT ) RETURNS TEXT AS $$
2         my $txt = shift;
3         $txt =~ s/^\s+//o;
4         $txt =~ s/[\[\]\{\}\(\)`'"#<>\*\?\-\+\$\\]+//o;
5         $txt =~ s/\s+$//o;
6         if (/(\d{3}(?:\.\d+)?)/o) {
7                 return $1;
8         } else {
9                 return (split /\s+/, $txt)[0];
10         }
11 $$ LANGUAGE 'plperl' STRICT IMMUTABLE;
12
13 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
14 SELECT
15         CASE    WHEN $1 IS NULL
16                         THEN $2
17                 WHEN $2 IS NULL
18                         THEN $1
19                 ELSE $1 || ' ' || $2
20         END;
21 $$ LANGUAGE SQL STABLE;
22
23 CREATE AGGREGATE public.agg_text (
24         sfunc    = public.text_concat,
25         basetype = text,
26         stype    = text
27 );
28
29 CREATE OR REPLACE FUNCTION public.tsvector_concat ( tsvector, tsvector ) RETURNS tsvector AS $$
30 SELECT
31         CASE    WHEN $1 IS NULL
32                         THEN $2
33                 WHEN $2 IS NULL
34                         THEN $1
35                 ELSE $1 || ' ' || $2
36         END;
37 $$ LANGUAGE SQL STABLE;
38
39 CREATE AGGREGATE public.agg_tsvector (
40         sfunc    = public.tsvector_concat,
41         basetype = tsvector,
42         stype    = tsvector
43 );
44
45 CREATE FUNCTION tableoid2name ( oid ) RETURNS TEXT AS $$
46         BEGIN
47                 RETURN $1::regclass;
48         END;
49 $$ language 'plpgsql';
50
51
52 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT ) RETURNS SETOF actor.org_unit AS $$
53         SELECT  a.*
54           FROM  connectby('actor.org_unit','id','parent_ou','name',$1,'100','.')
55                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
56                 JOIN actor.org_unit a ON a.id = t.keyid
57           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
58 $$ LANGUAGE SQL STABLE;
59
60 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors ( INT ) RETURNS SETOF actor.org_unit AS $$
61         SELECT  a.*
62           FROM  connectby('actor.org_unit','parent_ou','id','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_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
69         SELECT  a.*
70           FROM  connectby('actor.org_unit','id','parent_ou','name',
71                                 (SELECT x.id
72                                    FROM actor.org_unit_ancestors($1) x
73                                         JOIN actor.org_unit_type y ON x.ou_type = y.id
74                                   WHERE y.depth = $2)
75                 ,'100','.')
76                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
77                 JOIN actor.org_unit a ON a.id = t.keyid
78           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
79 $$ LANGUAGE SQL STABLE;
80
81 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
82         SELECT  *
83           FROM  actor.org_unit_ancestors($1)
84                         UNION
85         SELECT  *
86           FROM  actor.org_unit_descendants($1);
87 $$ LANGUAGE SQL STABLE;
88
89 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
90         SELECT  *
91           FROM  actor.org_unit_ancestors($1)
92                         UNION
93         SELECT  *
94           FROM  actor.org_unit_ancestors($2);
95 $$ LANGUAGE SQL STABLE;
96
97 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
98         SELECT  *
99           FROM  actor.org_unit_ancestors($1)
100                         INTERSECT
101         SELECT  *
102           FROM  actor.org_unit_ancestors($2);
103 $$ LANGUAGE SQL STABLE;
104
105 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
106         SELECT COUNT(id)::INT FROM (
107                 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
108                         EXCEPT
109                 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
110         ) z;
111 $$ LANGUAGE SQL STABLE;
112
113 CREATE AGGREGATE array_accum (
114         sfunc = array_append,
115         basetype = anyelement,
116         stype = anyarray,
117         initcond = '{}'
118 );