]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/002.functions.aggregate.sql
LP2061136 - Stamping 1405 DB upgrade script
[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 public.first(anyelement) CASCADE;
21 DROP AGGREGATE IF EXISTS public.last(anyelement) CASCADE;
22 DROP AGGREGATE IF EXISTS public.agg_text(text) CASCADE;
23
24 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
25         SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
26 $$ LANGUAGE SQL STABLE;
27
28 CREATE AGGREGATE public.first (
29         sfunc    = public.first_agg,
30         basetype = anyelement,
31         stype    = anyelement
32 );
33
34 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
35         SELECT $2;
36 $$ LANGUAGE SQL STABLE;
37
38 CREATE AGGREGATE public.last (
39         sfunc    = public.last_agg,
40         basetype = anyelement,
41         stype    = anyelement
42 );
43
44 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
45 SELECT
46         CASE    WHEN $1 IS NULL
47                         THEN $2
48                 WHEN $2 IS NULL
49                         THEN $1
50                 ELSE $1 || ' ' || $2
51         END;
52 $$ LANGUAGE SQL STABLE;
53
54 CREATE AGGREGATE public.agg_text (
55         sfunc    = public.text_concat,
56         basetype = text,
57         stype    = text
58 );
59
60 COMMIT;