]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/002.functions.aggregate.sql
Make DROP statement match new basetype for agg_tsvector
[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 BEGIN;
19
20 DROP AGGREGATE IF EXISTS array_accum(anyelement) CASCADE;
21 DROP AGGREGATE IF EXISTS public.first(anyelement) CASCADE;
22 DROP AGGREGATE IF EXISTS public.last(anyelement) CASCADE;
23 DROP AGGREGATE IF EXISTS public.agg_text(text) CASCADE;
24 DROP AGGREGATE IF EXISTS public.agg_tsvector(pg_catalog.tsvector) CASCADE;
25
26 CREATE AGGREGATE array_accum (
27         sfunc = array_append,
28         basetype = anyelement,
29         stype = anyarray,
30         initcond = '{}'
31 );
32
33 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
34         SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
35 $$ LANGUAGE SQL STABLE;
36
37 CREATE AGGREGATE public.first (
38         sfunc    = public.first_agg,
39         basetype = anyelement,
40         stype    = anyelement
41 );
42
43 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
44         SELECT $2;
45 $$ LANGUAGE SQL STABLE;
46
47 CREATE AGGREGATE public.last (
48         sfunc    = public.last_agg,
49         basetype = anyelement,
50         stype    = anyelement
51 );
52
53 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT 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_text (
64         sfunc    = public.text_concat,
65         basetype = text,
66         stype    = text
67 );
68
69 CREATE OR REPLACE FUNCTION tsvector_concat ( tsvector, tsvector ) RETURNS pg_catalog.tsvector AS $$
70 SELECT
71         CASE    WHEN $1 IS NULL
72                         THEN $2
73                 WHEN $2 IS NULL
74                         THEN $1
75                 ELSE $1 || ' ' || $2
76         END;
77 $$ LANGUAGE SQL STABLE;
78
79 CREATE AGGREGATE public.agg_tsvector (
80         sfunc    = tsvector_concat,
81         basetype = pg_catalog.tsvector,
82         stype    = pg_catalog.tsvector
83 );
84
85 CREATE OR REPLACE FUNCTION public.explode_array(anyarray) RETURNS SETOF anyelement AS $BODY$
86     SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
87 $BODY$
88 LANGUAGE 'sql' IMMUTABLE;
89
90 COMMIT;