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