]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/002.functions.aggregate.sql
Lp 1730726: Fix a number of PgTap tests for PostgreSQL 10.
[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
25 CREATE AGGREGATE array_accum (
26         sfunc = array_append,
27         basetype = anyelement,
28         stype = anyarray,
29         initcond = '{}'
30 );
31
32 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
33         SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
34 $$ LANGUAGE SQL STABLE;
35
36 CREATE AGGREGATE public.first (
37         sfunc    = public.first_agg,
38         basetype = anyelement,
39         stype    = anyelement
40 );
41
42 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
43         SELECT $2;
44 $$ LANGUAGE SQL STABLE;
45
46 CREATE AGGREGATE public.last (
47         sfunc    = public.last_agg,
48         basetype = anyelement,
49         stype    = anyelement
50 );
51
52 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
53 SELECT
54         CASE    WHEN $1 IS NULL
55                         THEN $2
56                 WHEN $2 IS NULL
57                         THEN $1
58                 ELSE $1 || ' ' || $2
59         END;
60 $$ LANGUAGE SQL STABLE;
61
62 CREATE AGGREGATE public.agg_text (
63         sfunc    = public.text_concat,
64         basetype = text,
65         stype    = text
66 );
67
68 COMMIT;