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: _dms_to_dd(double precision, double precision, double precision); Type: FUNCTION; Schema: functions; Owner: -
77
--
78

    
79
CREATE FUNCTION _dms_to_dd(deg double precision DEFAULT NULL::double precision, min double precision DEFAULT NULL::double precision, sec double precision DEFAULT NULL::double precision) RETURNS double precision
80
    LANGUAGE sql IMMUTABLE
81
    AS $_$
82
SELECT sum(value)
83
FROM
84
(VALUES
85
      ($1)
86
    , ($2/60)
87
    , ($3/60/60)
88
)
89
AS v (value)
90
$_$;
91

    
92

    
93
--
94
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
95
--
96

    
97
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
98
    LANGUAGE sql IMMUTABLE
99
    AS $_$
100
SELECT $1 = $2
101
$_$;
102

    
103

    
104
--
105
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: functions; Owner: -
106
--
107

    
108
CREATE FUNCTION _fix_date(value date DEFAULT NULL::date, threshold date DEFAULT NULL::date) RETURNS date
109
    LANGUAGE sql IMMUTABLE
110
    AS $_$
111
-- Fix dates after threshold date
112
-- This fixes e.g. 2-digit years before 1970 misinterpreted as 20xx
113
SELECT (CASE WHEN $1 > COALESCE($2, now()) THEN ($1 - '100 years'::interval)::date ELSE $1 END)
114
$_$;
115

    
116

    
117
--
118
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
119
--
120

    
121
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
122
    LANGUAGE sql IMMUTABLE
123
    AS $_$
124
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
125
$_$;
126

    
127

    
128
--
129
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
130
--
131

    
132
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
133
    LANGUAGE sql IMMUTABLE
134
    AS $_$
135
SELECT functions._if($1 != '', $2, $3)
136
$_$;
137

    
138

    
139
--
140
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
141
--
142

    
143
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
144
    LANGUAGE sql IMMUTABLE
145
    AS $_$
146
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
147
$_$;
148

    
149

    
150
--
151
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
152
--
153

    
154
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
155
    LANGUAGE sql IMMUTABLE
156
    AS $_$
157
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
158
$_$;
159

    
160

    
161
--
162
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
163
--
164

    
165
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
166
    LANGUAGE sql IMMUTABLE
167
    AS $_$
168
SELECT coalesce($1 || ': ', '') || $2
169
$_$;
170

    
171

    
172
--
173
-- Name: _map(hstore, text); Type: FUNCTION; Schema: functions; Owner: -
174
--
175

    
176
CREATE FUNCTION _map(map hstore, value text) RETURNS text
177
    LANGUAGE plpgsql IMMUTABLE
178
    AS $$
179
DECLARE
180
    match text := map -> value;
181
BEGIN
182
    IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
183
        match := map -> '*'; -- use default entry
184
        IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
185
        END IF;
186
    END IF;
187
    
188
    -- Interpret result
189
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
190
    ELSIF match = '*' THEN RETURN value;
191
    ELSE RETURN match;
192
    END IF;
193
END;
194
$$;
195

    
196

    
197
--
198
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
199
--
200

    
201
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
202
    LANGUAGE sql IMMUTABLE
203
    AS $_$
204
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
205
$_$;
206

    
207

    
208
--
209
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
210
--
211

    
212
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
213
    LANGUAGE sql IMMUTABLE
214
    AS $_$
215
SELECT functions.join_strs(value, '; ')
216
FROM
217
(
218
    SELECT *
219
    FROM
220
    (
221
        SELECT
222
        DISTINCT ON (value)
223
        *
224
        FROM
225
        (VALUES
226
              (1, $1)
227
            , (2, $2)
228
            , (3, $3)
229
            , (4, $4)
230
            , (5, $5)
231
            , (6, $6)
232
            , (7, $7)
233
            , (8, $8)
234
            , (9, $9)
235
            , (10, $10)
236
        )
237
        AS v (sort_order, value)
238
        WHERE value IS NOT NULL
239
    )
240
    AS v
241
    ORDER BY sort_order
242
)
243
AS v
244
$_$;
245

    
246

    
247
--
248
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: functions; Owner: -
249
--
250

    
251
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
252
    LANGUAGE sql IMMUTABLE
253
    AS $_$
254
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
255
$_$;
256

    
257

    
258
--
259
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
260
--
261

    
262
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
263
    LANGUAGE sql IMMUTABLE
264
    AS $_$
265
SELECT functions.join_strs(value, ' ')
266
FROM
267
(
268
    SELECT *
269
    FROM
270
    (
271
        SELECT
272
        DISTINCT ON (value)
273
        *
274
        FROM
275
        (VALUES
276
              (1, $1)
277
            , (2, $2)
278
            , (3, $3)
279
            , (4, $4)
280
            , (5, $5)
281
            , (6, $6)
282
            , (7, $7)
283
            , (8, $8)
284
            , (9, $9)
285
            , (10, $10)
286
        )
287
        AS v (sort_order, value)
288
        WHERE value IS NOT NULL
289
    )
290
    AS v
291
    ORDER BY sort_order
292
)
293
AS v
294
$_$;
295

    
296

    
297
--
298
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
299
--
300

    
301
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
302
    LANGUAGE sql IMMUTABLE
303
    AS $_$
304
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
305
$_$;
306

    
307

    
308
--
309
-- Name: _not(boolean); Type: FUNCTION; Schema: functions; Owner: -
310
--
311

    
312
CREATE FUNCTION _not(value boolean) RETURNS boolean
313
    LANGUAGE sql IMMUTABLE STRICT
314
    AS $_$
315
SELECT NOT $1
316
$_$;
317

    
318

    
319
--
320
-- Name: _now(); Type: FUNCTION; Schema: functions; Owner: -
321
--
322

    
323
CREATE FUNCTION _now() RETURNS timestamp with time zone
324
    LANGUAGE sql STABLE
325
    AS $$
326
SELECT now()
327
$$;
328

    
329

    
330
--
331
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
332
--
333

    
334
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
335
    LANGUAGE plpgsql IMMUTABLE
336
    AS $$
337
DECLARE
338
    type functions.datatype NOT NULL := type; -- add NOT NULL
339
BEGIN
340
    IF type = 'str' THEN RETURN nullif(value::text, "null");
341
    -- Invalid value is ignored, but invalid null value generates error
342
    ELSIF type = 'float' THEN
343
        DECLARE
344
            -- Outside the try block so that invalid null value generates error
345
            "null" double precision := "null"::double precision;
346
        BEGIN
347
            RETURN nullif(value::double precision, "null");
348
        EXCEPTION
349
            WHEN data_exception THEN RETURN value; -- ignore invalid value
350
        END;
351
    END IF;
352
END;
353
$$;
354

    
355

    
356
--
357
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
358
--
359

    
360
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
361
    LANGUAGE sql IMMUTABLE
362
    AS $_$
363
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
364
$_$;
365

    
366

    
367
--
368
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
369
--
370

    
371
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
372
    LANGUAGE sql IMMUTABLE
373
    AS $_$
374
SELECT bool_or(value)
375
FROM
376
(VALUES
377
      ($1)
378
    , ($2)
379
    , ($3)
380
    , ($4)
381
    , ($5)
382
)
383
AS v (value)
384
$_$;
385

    
386

    
387
--
388
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
389
--
390

    
391
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.';
392

    
393

    
394
--
395
-- Name: _split(text, text); Type: FUNCTION; Schema: functions; Owner: -
396
--
397

    
398
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
399
    LANGUAGE sql IMMUTABLE STRICT
400
    AS $_$
401
SELECT regexp_split_to_table($1, $2)
402
$_$;
403

    
404

    
405
--
406
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
407
--
408

    
409
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
410
    LANGUAGE sql IMMUTABLE STRICT
411
    AS $_$
412
SELECT $1 || $3 || $2
413
$_$;
414

    
415

    
416
--
417
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
418
--
419

    
420
CREATE AGGREGATE join_strs(text, text) (
421
    SFUNC = join_strs_transform,
422
    STYPE = text
423
);
424

    
425

    
426
--
427
-- PostgreSQL database dump complete
428
--
429

    
(4-4/25)