Project

General

Profile

1
--
2
-- PostgreSQL database dump
3
--
4

    
5
SET statement_timeout = 0;
6
SET client_encoding = 'UTF8';
7
SET standard_conforming_strings = on;
8
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
--
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
SET search_path = functions, pg_catalog;
26

    
27
--
28
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
29
--
30

    
31
CREATE TYPE datatype AS ENUM (
32
    'str',
33
    'float'
34
);
35

    
36

    
37
--
38
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
39
--
40

    
41
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
    LANGUAGE sql IMMUTABLE
43
    AS $_$
44
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
45
$_$;
46

    
47

    
48
--
49
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
50
--
51

    
52
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
    LANGUAGE sql IMMUTABLE
54
    AS $_$
55
SELECT bool_and(value)
56
FROM
57
(VALUES
58
      ($1)
59
    , ($2)
60
    , ($3)
61
    , ($4)
62
    , ($5)
63
)
64
AS v (value)
65
$_$;
66

    
67

    
68
--
69
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
70
--
71

    
72
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

    
74

    
75
--
76
-- 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
-- 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
-- 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
-- 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
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
129
$_$;
130

    
131

    
132
--
133
-- 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
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
140
$_$;
141

    
142

    
143
--
144
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
145
--
146

    
147
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
148
    LANGUAGE sql IMMUTABLE
149
    AS $_$
150
SELECT coalesce($1 || ': ', '') || $2
151
$_$;
152

    
153

    
154
--
155
-- 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
    match text := map -> value;
163
BEGIN
164
    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
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
172
    ELSIF match = '*' THEN RETURN value;
173
    ELSE RETURN match;
174
    END IF;
175
END;
176
$$;
177

    
178

    
179
--
180
-- 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
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
187
$_$;
188

    
189

    
190
--
191
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
192
--
193

    
194
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
    LANGUAGE sql IMMUTABLE
196
    AS $_$
197
SELECT functions.join_strs(value, '; ')
198
FROM
199
(
200
    SELECT *
201
    FROM
202
    (
203
        SELECT
204
        DISTINCT ON (value)
205
        *
206
        FROM
207
        (VALUES
208
              (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
        )
219
        AS v (sort_order, value)
220
        WHERE value IS NOT NULL
221
    )
222
    AS v
223
    ORDER BY sort_order
224
)
225
AS v
226
$_$;
227

    
228

    
229
--
230
-- 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
-- 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
-- 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
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
287
$_$;
288

    
289

    
290
--
291
-- 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
-- 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
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
314
--
315

    
316
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
317
    LANGUAGE plpgsql IMMUTABLE
318
    AS $$
319
DECLARE
320
    type functions.datatype NOT NULL := type; -- add NOT NULL
321
BEGIN
322
    IF type = 'str' THEN RETURN nullif(value::text, "null");
323
    -- Invalid value is ignored, but invalid null value generates error
324
    ELSIF type = 'float' THEN
325
        DECLARE
326
            -- Outside the try block so that invalid null value generates error
327
            "null" double precision := "null"::double precision;
328
        BEGIN
329
            RETURN nullif(value::double precision, "null");
330
        EXCEPTION
331
            WHEN data_exception THEN RETURN value; -- ignore invalid value
332
        END;
333
    END IF;
334
END;
335
$$;
336

    
337

    
338
--
339
-- 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
-- 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
-- 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
-- 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
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
389
--
390

    
391
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
392
    LANGUAGE sql IMMUTABLE STRICT
393
    AS $_$
394
SELECT $1 || $3 || $2
395
$_$;
396

    
397

    
398
--
399
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
400
--
401

    
402
CREATE AGGREGATE join_strs(text, text) (
403
    SFUNC = join_strs_transform,
404
    STYPE = text
405
);
406

    
407

    
408
--
409
-- PostgreSQL database dump complete
410
--
411

    
(4-4/25)