]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/002.functions.aggregate.sql
Add limit_count and offset_count columns to query.stored_query table.
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 002.functions.aggregate.sql
1 /*
2  * Copyright (C) 2004-2008  Georgia Public Library Service
3  * Copyright (C) 2008  Equinox Software, Inc.
4  * Mike Rylander <miker@esilibrary.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 -- Allow these to fail gracelessly outside the transaction
19 -- because PostgreSQL 8.1 does not support IF EXISTS
20 DROP AGGREGATE array_accum(anyelement) CASCADE;
21 DROP AGGREGATE public.first(anyelement) CASCADE;
22 DROP AGGREGATE public.last(anyelement) CASCADE;
23 DROP AGGREGATE public.agg_text(text) CASCADE;
24 DROP AGGREGATE public.agg_tsvector(tsvector) CASCADE;
25
26 BEGIN;
27
28 CREATE AGGREGATE array_accum (
29         sfunc = array_append,
30         basetype = anyelement,
31         stype = anyarray,
32         initcond = '{}'
33 );
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 OR REPLACE FUNCTION public.explode_array(anyarray) RETURNS SETOF anyelement AS $BODY$
88     SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
89 $BODY$
90 LANGUAGE 'sql' IMMUTABLE;
91
92 COMMIT;