Project

General

Profile

1 2094 aaronmk
--
2
-- PostgreSQL database dump
3
--
4
5
SET statement_timeout = 0;
6
SET client_encoding = 'UTF8';
7
SET standard_conforming_strings = off;
8
SET check_function_bodies = false;
9
SET client_min_messages = warning;
10
SET escape_string_warning = off;
11
12
--
13
-- Name: functions; Type: SCHEMA; Schema: -; Owner: -
14
--
15
16
CREATE SCHEMA functions;
17
18
19 4982 aaronmk
--
20
-- Name: SCHEMA functions; Type: COMMENT; Schema: -; Owner: -
21
--
22
23
COMMENT ON SCHEMA functions IS 'IMPORTANT: Functions must always return NULL in place of '''' (the empty string). This ensures that empty strings do not find their way into VegBIEN.';
24
25
26 2107 aaronmk
SET search_path = functions, pg_catalog;
27
28 2094 aaronmk
--
29 2610 aaronmk
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
30
--
31
32
CREATE TYPE datatype AS ENUM (
33
    'str',
34
    'float'
35
);
36
37
38
--
39 4501 aaronmk
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
40 2596 aaronmk
--
41
42 4501 aaronmk
CREATE FUNCTION _alt("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown, "10" anyelement DEFAULT NULL::unknown, "11" anyelement DEFAULT NULL::unknown, "12" anyelement DEFAULT NULL::unknown) RETURNS anyelement
43 3422 aaronmk
    LANGUAGE sql IMMUTABLE
44
    AS $_$
45 4501 aaronmk
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
46 3422 aaronmk
$_$;
47
48
49
--
50 5956 aaronmk
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
51 5937 aaronmk
--
52
53 5956 aaronmk
CREATE FUNCTION _and("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean, "2" boolean DEFAULT NULL::boolean, "3" boolean DEFAULT NULL::boolean, "4" boolean DEFAULT NULL::boolean) RETURNS boolean
54 5937 aaronmk
    LANGUAGE sql IMMUTABLE
55
    AS $_$
56
SELECT bool_and(value)
57
FROM
58
(VALUES
59
      ($1)
60
    , ($2)
61 5956 aaronmk
    , ($3)
62
    , ($4)
63
    , ($5)
64 5937 aaronmk
)
65
AS v (value)
66
$_$;
67
68
69
--
70 5956 aaronmk
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
71 5937 aaronmk
--
72
73 5956 aaronmk
COMMENT ON FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS 'NULL values are ignored rather than propagated.';
74 5937 aaronmk
75
76
--
77 4142 aaronmk
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
78
--
79
80
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
81
    LANGUAGE sql IMMUTABLE
82
    AS $_$
83
SELECT $1 = $2
84
$_$;
85
86
87
--
88 4147 aaronmk
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
89
--
90
91
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
92
    LANGUAGE sql IMMUTABLE
93
    AS $_$
94
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
95
$_$;
96
97
98
--
99
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
100
--
101
102
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
103
    LANGUAGE sql IMMUTABLE
104
    AS $_$
105
SELECT functions._if($1 != '', $2, $3)
106
$_$;
107
108
109
--
110 4325 aaronmk
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
111
--
112
113
CREATE FUNCTION _join("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
114
    LANGUAGE sql IMMUTABLE
115
    AS $_$
116 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
117 4325 aaronmk
$_$;
118
119
120
--
121 5009 aaronmk
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
122
--
123
124
CREATE FUNCTION _join_words("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
125
    LANGUAGE sql IMMUTABLE
126
    AS $_$
127 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
128 5009 aaronmk
$_$;
129
130
131
--
132 4683 aaronmk
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
133 3422 aaronmk
--
134
135 4683 aaronmk
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
136 4682 aaronmk
    LANGUAGE sql IMMUTABLE
137
    AS $_$
138
SELECT coalesce($1 || ': ', '') || $2
139
$_$;
140 2596 aaronmk
141
142
--
143 5408 aaronmk
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
144
--
145
146
CREATE FUNCTION _max("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
147
    LANGUAGE sql IMMUTABLE
148
    AS $_$
149
SELECT max(value)
150
FROM
151
(VALUES
152
      ($1)
153
    , ($2)
154
    , ($3)
155
    , ($4)
156
    , ($5)
157
    , ($6)
158
    , ($7)
159
    , ($8)
160
    , ($9)
161
    , ($10)
162
)
163
AS v (value)
164
$_$;
165
166
167
--
168 4150 aaronmk
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
169 2940 aaronmk
--
170
171 4150 aaronmk
CREATE FUNCTION _merge("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
172 2940 aaronmk
    LANGUAGE sql IMMUTABLE
173
    AS $_$
174 4053 aaronmk
SELECT functions.join_strs(value, '; ')
175 2940 aaronmk
FROM
176
(
177
    SELECT *
178
    FROM
179
    (
180
        SELECT
181
        DISTINCT ON (value)
182
        *
183
        FROM
184
        (VALUES
185 4012 aaronmk
              (1, $1)
186
            , (2, $2)
187
            , (3, $3)
188
            , (4, $4)
189
            , (5, $5)
190
            , (6, $6)
191
            , (7, $7)
192
            , (8, $8)
193
            , (9, $9)
194
            , (10, $10)
195 2940 aaronmk
        )
196
        AS v (sort_order, value)
197 4011 aaronmk
        WHERE value IS NOT NULL
198 2940 aaronmk
    )
199
    AS v
200
    ORDER BY sort_order
201
)
202
AS v
203
$_$;
204
205
206
--
207 5408 aaronmk
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
208
--
209
210
CREATE FUNCTION _min("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
211
    LANGUAGE sql IMMUTABLE
212
    AS $_$
213
SELECT min(value)
214
FROM
215
(VALUES
216
      ($1)
217
    , ($2)
218
    , ($3)
219
    , ($4)
220
    , ($5)
221
    , ($6)
222
    , ($7)
223
    , ($8)
224
    , ($9)
225
    , ($10)
226
)
227
AS v (value)
228
$_$;
229
230
231
--
232 4475 aaronmk
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
233 2949 aaronmk
--
234
235 4475 aaronmk
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
236 2949 aaronmk
    LANGUAGE plpgsql IMMUTABLE
237
    AS $$
238
DECLARE
239
    "null" text NOT NULL := "null"; -- add NOT NULL
240
    type functions.datatype NOT NULL := type; -- add NOT NULL
241
BEGIN
242 4475 aaronmk
    IF type = 'str' THEN RETURN nullif(value::text, "null");
243 2722 aaronmk
    -- Invalid value is ignored, but invalid null value generates error
244 2949 aaronmk
    ELSIF type = 'float' THEN
245 2722 aaronmk
        DECLARE
246
            -- Outside the try block so that invalid null value generates error
247 2949 aaronmk
            "null" double precision := "null"::double precision;
248 2722 aaronmk
        BEGIN
249 2949 aaronmk
            RETURN nullif(value::double precision, "null");
250 2722 aaronmk
        EXCEPTION
251 2949 aaronmk
            WHEN data_exception THEN RETURN value; -- ignore invalid value
252 2722 aaronmk
        END;
253 2610 aaronmk
    END IF;
254
END;
255
$$;
256
257
258
--
259 4479 aaronmk
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
260
--
261
262
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
263
    LANGUAGE sql IMMUTABLE
264
    AS $_$
265
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
266
$_$;
267
268
269
--
270 4052 aaronmk
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
271 4009 aaronmk
--
272
273 4053 aaronmk
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
274 4054 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
275 4009 aaronmk
    AS $_$
276 4054 aaronmk
SELECT $1 || $3 || $2
277 2595 aaronmk
$_$;
278
279
280
--
281
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
282
--
283
284
CREATE AGGREGATE join_strs(text, text) (
285 4052 aaronmk
    SFUNC = join_strs_transform,
286 4010 aaronmk
    STYPE = text
287 2595 aaronmk
);
288
289
290 2107 aaronmk
--
291 2136 aaronmk
-- PostgreSQL database dump complete
292
--