]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
adding storage method open-ils.storage.asset.merge_record_assets to call stored proc...
[Evergreen.git] / Open-ILS / src / sql / Pg / 020.schema.functions.sql
1 CREATE OR REPLACE FUNCTION public.non_filing_normalize ( TEXT, "char" ) RETURNS TEXT AS $$
2         SELECT  SUBSTRING(
3                         REGEXP_REPLACE(
4                                 REGEXP_REPLACE(
5                                         $1,
6                                         E'\W*$',
7                                         ''
8                                 ),
9                                 '  ',
10                                 ' '
11                         ),
12                         CASE
13                                 WHEN $2::INT NOT BETWEEN 48 AND 57 THEN 1
14                                 ELSE $2::TEXT::INT + 1
15                         END
16                 );
17 $$ LANGUAGE SQL STRICT IMMUTABLE;
18
19 CREATE OR REPLACE FUNCTION public.call_number_dewey( TEXT ) RETURNS TEXT AS $$
20         my $txt = shift;
21         $txt =~ s/^\s+//o;
22         $txt =~ s/[\[\]\{\}\(\)`'"#<>\*\?\-\+\$\\]+//o;
23         $txt =~ s/\s+$//o;
24         if (/(\d{3}(?:\.\d+)?)/o) {
25                 return $1;
26         } else {
27                 return (split /\s+/, $txt)[0];
28         }
29 $$ LANGUAGE 'plperl' STRICT IMMUTABLE;
30
31 CREATE OR REPLACE FUNCTION public.call_number_dewey( TEXT, INT ) RETURNS TEXT AS $$
32         SELECT SUBSTRING(call_number_dewey($1) FROM 1 FOR $2);
33 $$ LANGUAGE SQL STRICT IMMUTABLE;
34
35 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
36         SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
37 $$ LANGUAGE SQL STABLE;
38
39 CREATE AGGREGATE public.first (
40         sfunc    = public.first_agg,
41         basetype = anyelement,
42         stype    = anyelement
43 );
44
45 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
46         SELECT $2;
47 $$ LANGUAGE SQL STABLE;
48
49 CREATE AGGREGATE public.last (
50         sfunc    = public.last_agg,
51         basetype = anyelement,
52         stype    = anyelement
53 );
54
55 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
56 SELECT
57         CASE    WHEN $1 IS NULL
58                         THEN $2
59                 WHEN $2 IS NULL
60                         THEN $1
61                 ELSE $1 || ' ' || $2
62         END;
63 $$ LANGUAGE SQL STABLE;
64
65 CREATE AGGREGATE public.agg_text (
66         sfunc    = public.text_concat,
67         basetype = text,
68         stype    = text
69 );
70
71 CREATE OR REPLACE FUNCTION public.tsvector_concat ( tsvector, tsvector ) RETURNS tsvector AS $$
72 SELECT
73         CASE    WHEN $1 IS NULL
74                         THEN $2
75                 WHEN $2 IS NULL
76                         THEN $1
77                 ELSE $1 || ' ' || $2
78         END;
79 $$ LANGUAGE SQL STABLE;
80
81 CREATE AGGREGATE public.agg_tsvector (
82         sfunc    = public.tsvector_concat,
83         basetype = tsvector,
84         stype    = tsvector
85 );
86
87 CREATE FUNCTION tableoid2name ( oid ) RETURNS TEXT AS $$
88         BEGIN
89                 RETURN $1::regclass;
90         END;
91 $$ language 'plpgsql';
92
93
94 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT ) RETURNS SETOF actor.org_unit AS $$
95         SELECT  a.*
96           FROM  connectby('actor.org_unit','id','parent_ou','name',$1,'100','.')
97                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
98                 JOIN actor.org_unit a ON a.id = t.keyid
99           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
100 $$ LANGUAGE SQL STABLE;
101
102 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors ( INT ) RETURNS SETOF actor.org_unit AS $$
103         SELECT  a.*
104           FROM  connectby('actor.org_unit','parent_ou','id','name',$1,'100','.')
105                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
106                 JOIN actor.org_unit a ON a.id = t.keyid
107           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
108 $$ LANGUAGE SQL STABLE;
109
110 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
111         SELECT  a.*
112           FROM  connectby('actor.org_unit','id','parent_ou','name',
113                                 (SELECT x.id
114                                    FROM actor.org_unit_ancestors($1) x
115                                         JOIN actor.org_unit_type y ON x.ou_type = y.id
116                                   WHERE y.depth = $2)
117                 ,'100','.')
118                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
119                 JOIN actor.org_unit a ON a.id = t.keyid
120           ORDER BY  CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
121 $$ LANGUAGE SQL STABLE;
122
123 CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_at_depth ( INT,INT ) RETURNS actor.org_unit AS $$
124         SELECT  a.*
125           FROM  actor.org_unit a
126           WHERE id = ( SELECT FIRST(x.id)
127                          FROM   actor.org_unit_ancestors($1) x
128                                 JOIN actor.org_unit_type y
129                                         ON x.ou_type = y.id AND y.depth = $2);
130 $$ LANGUAGE SQL STABLE;
131
132 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
133         SELECT  *
134           FROM  actor.org_unit_ancestors($1)
135                         UNION
136         SELECT  *
137           FROM  actor.org_unit_descendants($1);
138 $$ LANGUAGE SQL STABLE;
139
140 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
141         SELECT  * FROM actor.org_unit_full_path((actor.org_unit_ancestor_at_depth($1, $2)).id)
142 $$ LANGUAGE SQL STABLE;
143
144 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
145         SELECT  *
146           FROM  actor.org_unit_ancestors($1)
147                         UNION
148         SELECT  *
149           FROM  actor.org_unit_ancestors($2);
150 $$ LANGUAGE SQL STABLE;
151
152 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
153         SELECT  *
154           FROM  actor.org_unit_ancestors($1)
155                         INTERSECT
156         SELECT  *
157           FROM  actor.org_unit_ancestors($2);
158 $$ LANGUAGE SQL STABLE;
159
160 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
161         SELECT COUNT(id)::INT FROM (
162                 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
163                         EXCEPT
164                 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
165         ) z;
166 $$ LANGUAGE SQL STABLE;
167
168 CREATE AGGREGATE array_accum (
169         sfunc = array_append,
170         basetype = anyelement,
171         stype = anyarray,
172         initcond = '{}'
173 );
174