]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/020.schema.functions.sql
reporter stuff
[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_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
106         SELECT  *
107           FROM  actor.org_unit_ancestors($1)
108                         UNION
109         SELECT  *
110           FROM  actor.org_unit_descendants($1);
111 $$ LANGUAGE SQL STABLE;
112
113 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
114         SELECT  *
115           FROM  actor.org_unit_ancestors($1)
116                         UNION
117         SELECT  *
118           FROM  actor.org_unit_ancestors($2);
119 $$ LANGUAGE SQL STABLE;
120
121 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
122         SELECT  *
123           FROM  actor.org_unit_ancestors($1)
124                         INTERSECT
125         SELECT  *
126           FROM  actor.org_unit_ancestors($2);
127 $$ LANGUAGE SQL STABLE;
128
129 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
130         SELECT COUNT(id)::INT FROM (
131                 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
132                         EXCEPT
133                 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
134         ) z;
135 $$ LANGUAGE SQL STABLE;
136
137 CREATE AGGREGATE array_accum (
138         sfunc = array_append,
139         basetype = anyelement,
140         stype = anyarray,
141         initcond = '{}'
142 );