Project

General

Profile

1 2094 aaronmk
--
2
-- PostgreSQL database dump
3
--
4
5
SET statement_timeout = 0;
6
SET client_encoding = 'UTF8';
7 6213 aaronmk
SET standard_conforming_strings = on;
8 2094 aaronmk
SET check_function_bodies = false;
9
SET client_min_messages = warning;
10
11
--
12
-- Name: functions; Type: SCHEMA; Schema: -; Owner: -
13
--
14
15
CREATE SCHEMA functions;
16
17
18 4982 aaronmk
--
19
-- Name: SCHEMA functions; Type: COMMENT; Schema: -; Owner: -
20
--
21
22
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.';
23
24
25 2107 aaronmk
SET search_path = functions, pg_catalog;
26
27 2094 aaronmk
--
28 2610 aaronmk
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
29
--
30
31
CREATE TYPE datatype AS ENUM (
32
    'str',
33
    'float'
34
);
35
36
37
--
38 4501 aaronmk
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
39 2596 aaronmk
--
40
41 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
42 3422 aaronmk
    LANGUAGE sql IMMUTABLE
43
    AS $_$
44 4501 aaronmk
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
45 3422 aaronmk
$_$;
46
47
48
--
49 5956 aaronmk
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
50 5937 aaronmk
--
51
52 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
53 5937 aaronmk
    LANGUAGE sql IMMUTABLE
54
    AS $_$
55
SELECT bool_and(value)
56
FROM
57
(VALUES
58
      ($1)
59
    , ($2)
60 5956 aaronmk
    , ($3)
61
    , ($4)
62
    , ($5)
63 5937 aaronmk
)
64
AS v (value)
65
$_$;
66
67
68
--
69 5956 aaronmk
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
70 5937 aaronmk
--
71
72 6437 aaronmk
COMMENT ON FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '_and() ignores NULL values, while AND combines them with the other values to potentially convert true to NULL. AND should be used with required fields, and _and() with optional fields.';
73 5937 aaronmk
74
75
--
76 4142 aaronmk
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
77
--
78
79
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
80
    LANGUAGE sql IMMUTABLE
81
    AS $_$
82
SELECT $1 = $2
83
$_$;
84
85
86
--
87 7396 aaronmk
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: functions; Owner: -
88
--
89
90
CREATE FUNCTION _fix_date(value date DEFAULT NULL::date, threshold date DEFAULT NULL::date) RETURNS date
91
    LANGUAGE sql IMMUTABLE
92
    AS $_$
93
-- Fix dates after threshold date
94
-- This fixes e.g. 2-digit years before 1970 misinterpreted as 20xx
95
SELECT (CASE WHEN $1 > COALESCE($2, now()) THEN ($1 - '100 years'::interval)::date ELSE $1 END)
96
$_$;
97
98
99
--
100 4147 aaronmk
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
101
--
102
103
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
104
    LANGUAGE sql IMMUTABLE
105
    AS $_$
106
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
107
$_$;
108
109
110
--
111
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
112
--
113
114
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
115
    LANGUAGE sql IMMUTABLE
116
    AS $_$
117
SELECT functions._if($1 != '', $2, $3)
118
$_$;
119
120
121
--
122 4325 aaronmk
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
123
--
124
125
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
126
    LANGUAGE sql IMMUTABLE
127
    AS $_$
128 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
129 4325 aaronmk
$_$;
130
131
132
--
133 5009 aaronmk
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
134
--
135
136
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
137
    LANGUAGE sql IMMUTABLE
138
    AS $_$
139 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
140 5009 aaronmk
$_$;
141
142
143
--
144 4683 aaronmk
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
145 3422 aaronmk
--
146
147 4683 aaronmk
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
148 4682 aaronmk
    LANGUAGE sql IMMUTABLE
149
    AS $_$
150
SELECT coalesce($1 || ': ', '') || $2
151
$_$;
152 2596 aaronmk
153
154
--
155 6222 aaronmk
-- Name: _map(hstore, text); Type: FUNCTION; Schema: functions; Owner: -
156
--
157
158
CREATE FUNCTION _map(map hstore, value text) RETURNS text
159
    LANGUAGE plpgsql IMMUTABLE
160
    AS $$
161
DECLARE
162 6271 aaronmk
    match text := map -> value;
163 6222 aaronmk
BEGIN
164 6271 aaronmk
    IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
165
        match := map -> '*'; -- use default entry
166
        IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
167
        END IF;
168
    END IF;
169
170
    -- Interpret result
171 6243 aaronmk
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
172
    ELSIF match = '*' THEN RETURN value;
173
    ELSE RETURN match;
174 6222 aaronmk
    END IF;
175
END;
176
$$;
177
178
179
--
180 5408 aaronmk
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
181
--
182
183
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
184
    LANGUAGE sql IMMUTABLE
185
    AS $_$
186 7289 aaronmk
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
187 5408 aaronmk
$_$;
188
189
190
--
191 4150 aaronmk
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
192 2940 aaronmk
--
193
194 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
195 2940 aaronmk
    LANGUAGE sql IMMUTABLE
196
    AS $_$
197 4053 aaronmk
SELECT functions.join_strs(value, '; ')
198 2940 aaronmk
FROM
199
(
200
    SELECT *
201
    FROM
202
    (
203
        SELECT
204
        DISTINCT ON (value)
205
        *
206
        FROM
207
        (VALUES
208 4012 aaronmk
              (1, $1)
209
            , (2, $2)
210
            , (3, $3)
211
            , (4, $4)
212
            , (5, $5)
213
            , (6, $6)
214
            , (7, $7)
215
            , (8, $8)
216
            , (9, $9)
217
            , (10, $10)
218 2940 aaronmk
        )
219
        AS v (sort_order, value)
220 4011 aaronmk
        WHERE value IS NOT NULL
221 2940 aaronmk
    )
222
    AS v
223
    ORDER BY sort_order
224
)
225
AS v
226
$_$;
227
228
229
--
230 7140 aaronmk
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: functions; Owner: -
231
--
232
233
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
234
    LANGUAGE sql IMMUTABLE
235
    AS $_$
236
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
237
$_$;
238
239
240
--
241 6354 aaronmk
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
242
--
243
244
CREATE FUNCTION _merge_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
245
    LANGUAGE sql IMMUTABLE
246
    AS $_$
247
SELECT functions.join_strs(value, ' ')
248
FROM
249
(
250
    SELECT *
251
    FROM
252
    (
253
        SELECT
254
        DISTINCT ON (value)
255
        *
256
        FROM
257
        (VALUES
258
              (1, $1)
259
            , (2, $2)
260
            , (3, $3)
261
            , (4, $4)
262
            , (5, $5)
263
            , (6, $6)
264
            , (7, $7)
265
            , (8, $8)
266
            , (9, $9)
267
            , (10, $10)
268
        )
269
        AS v (sort_order, value)
270
        WHERE value IS NOT NULL
271
    )
272
    AS v
273
    ORDER BY sort_order
274
)
275
AS v
276
$_$;
277
278
279
--
280 5408 aaronmk
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
281
--
282
283
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
284
    LANGUAGE sql IMMUTABLE
285
    AS $_$
286 7289 aaronmk
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
287 5408 aaronmk
$_$;
288
289
290
--
291 6316 aaronmk
-- Name: _not(boolean); Type: FUNCTION; Schema: functions; Owner: -
292
--
293
294
CREATE FUNCTION _not(value boolean) RETURNS boolean
295
    LANGUAGE sql IMMUTABLE STRICT
296
    AS $_$
297
SELECT NOT $1
298
$_$;
299
300
301
--
302 7104 aaronmk
-- Name: _now(); Type: FUNCTION; Schema: functions; Owner: -
303
--
304
305
CREATE FUNCTION _now() RETURNS timestamp with time zone
306
    LANGUAGE sql STABLE
307
    AS $$
308
SELECT now()
309
$$;
310
311
312
--
313 4475 aaronmk
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
314 2949 aaronmk
--
315
316 4475 aaronmk
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
317 2949 aaronmk
    LANGUAGE plpgsql IMMUTABLE
318
    AS $$
319
DECLARE
320
    type functions.datatype NOT NULL := type; -- add NOT NULL
321
BEGIN
322 4475 aaronmk
    IF type = 'str' THEN RETURN nullif(value::text, "null");
323 2722 aaronmk
    -- Invalid value is ignored, but invalid null value generates error
324 2949 aaronmk
    ELSIF type = 'float' THEN
325 2722 aaronmk
        DECLARE
326
            -- Outside the try block so that invalid null value generates error
327 2949 aaronmk
            "null" double precision := "null"::double precision;
328 2722 aaronmk
        BEGIN
329 2949 aaronmk
            RETURN nullif(value::double precision, "null");
330 2722 aaronmk
        EXCEPTION
331 2949 aaronmk
            WHEN data_exception THEN RETURN value; -- ignore invalid value
332 2722 aaronmk
        END;
333 2610 aaronmk
    END IF;
334
END;
335
$$;
336
337
338
--
339 4479 aaronmk
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
340
--
341
342
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
343
    LANGUAGE sql IMMUTABLE
344
    AS $_$
345
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
346
$_$;
347
348
349
--
350 6355 aaronmk
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
351
--
352
353
CREATE FUNCTION _or("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
354
    LANGUAGE sql IMMUTABLE
355
    AS $_$
356
SELECT bool_or(value)
357
FROM
358
(VALUES
359
      ($1)
360
    , ($2)
361
    , ($3)
362
    , ($4)
363
    , ($5)
364
)
365
AS v (value)
366
$_$;
367
368
369
--
370 6437 aaronmk
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
371
--
372
373
COMMENT ON FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '_or() ignores NULL values, while OR combines them with the other values to potentially convert false to NULL. OR should be used with required fields, and _or() with optional fields.';
374
375
376
--
377 6793 aaronmk
-- Name: _split(text, text); Type: FUNCTION; Schema: functions; Owner: -
378
--
379
380
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
381
    LANGUAGE sql IMMUTABLE STRICT
382
    AS $_$
383
SELECT regexp_split_to_table($1, $2)
384
$_$;
385
386
387
--
388 4052 aaronmk
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
389 4009 aaronmk
--
390
391 4053 aaronmk
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
392 4054 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
393 4009 aaronmk
    AS $_$
394 4054 aaronmk
SELECT $1 || $3 || $2
395 2595 aaronmk
$_$;
396
397
398
--
399
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
400
--
401
402
CREATE AGGREGATE join_strs(text, text) (
403 4052 aaronmk
    SFUNC = join_strs_transform,
404 4010 aaronmk
    STYPE = text
405 2595 aaronmk
);
406
407
408 2107 aaronmk
--
409 2136 aaronmk
-- PostgreSQL database dump complete
410
--