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: compass_dir; Type: TYPE; Schema: functions; Owner: -
29
--
30

    
31
CREATE TYPE compass_dir AS ENUM (
32
    'N',
33
    'E',
34
    'S',
35
    'W'
36
);
37

    
38

    
39
--
40
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
41
--
42

    
43
CREATE TYPE datatype AS ENUM (
44
    'str',
45
    'float'
46
);
47

    
48

    
49
--
50
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
51
--
52

    
53
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
54
    LANGUAGE sql IMMUTABLE
55
    AS $_$
56
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
57
$_$;
58

    
59

    
60
--
61
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
62
--
63

    
64
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
65
    LANGUAGE sql IMMUTABLE
66
    AS $_$
67
SELECT bool_and(value)
68
FROM
69
(VALUES
70
      ($1)
71
    , ($2)
72
    , ($3)
73
    , ($4)
74
    , ($5)
75
)
76
AS v (value)
77
$_$;
78

    
79

    
80
--
81
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
82
--
83

    
84
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.';
85

    
86

    
87
--
88
-- Name: _dms_to_dd(text); Type: FUNCTION; Schema: functions; Owner: -
89
--
90

    
91
CREATE FUNCTION _dms_to_dd(value text DEFAULT NULL::text) RETURNS double precision
92
    LANGUAGE sql IMMUTABLE STRICT
93
    AS $_$
94
SELECT (g[1]||'1')::integer*functions._dms_to_dd(deg := g[2]::double precision, min := g[3]::double precision, sec := g[4]::double precision, dir := g[5]::functions.compass_dir)
95
FROM regexp_matches($1, '^ *(-?)([\d.]+)(?:(?:deg|[°º])(?: *([\d.]+)(?:min|[''’]))?(?: *([\d.]+)(?:sec|["”]))?)? *([NESW])? *$') matches (g)
96
$_$;
97

    
98

    
99
--
100
-- Name: _dms_to_dd(double precision, double precision, double precision, compass_dir); Type: FUNCTION; Schema: functions; Owner: -
101
--
102

    
103
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, dir compass_dir DEFAULT NULL::compass_dir) RETURNS double precision
104
    LANGUAGE sql IMMUTABLE
105
    AS $_$
106
SELECT sum(value)*COALESCE(functions._map('N=>1,E=>1,S=>-1,W=>-1', $4::text)::integer, 1)
107
FROM
108
(VALUES
109
      ($1)
110
    , ($2/60)
111
    , ($3/60/60)
112
)
113
AS v (value)
114
$_$;
115

    
116

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

    
121
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
122
    LANGUAGE sql IMMUTABLE
123
    AS $_$
124
SELECT $1 = $2
125
$_$;
126

    
127

    
128
--
129
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: functions; Owner: -
130
--
131

    
132
CREATE FUNCTION _fix_date(value date DEFAULT NULL::date, threshold date DEFAULT NULL::date) RETURNS date
133
    LANGUAGE sql IMMUTABLE
134
    AS $_$
135
-- Fix dates after threshold date
136
-- This fixes e.g. 2-digit years before 1970 misinterpreted as 20xx
137
SELECT (CASE WHEN $1 > COALESCE($2, now()) THEN ($1 - '100 years'::interval)::date ELSE $1 END)
138
$_$;
139

    
140

    
141
--
142
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
143
--
144

    
145
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
146
    LANGUAGE sql IMMUTABLE
147
    AS $_$
148
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
149
$_$;
150

    
151

    
152
--
153
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
154
--
155

    
156
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
157
    LANGUAGE sql IMMUTABLE
158
    AS $_$
159
SELECT functions._if($1 != '', $2, $3)
160
$_$;
161

    
162

    
163
--
164
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
165
--
166

    
167
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
168
    LANGUAGE sql IMMUTABLE
169
    AS $_$
170
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
171
$_$;
172

    
173

    
174
--
175
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
176
--
177

    
178
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
179
    LANGUAGE sql IMMUTABLE
180
    AS $_$
181
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
182
$_$;
183

    
184

    
185
--
186
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
187
--
188

    
189
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
190
    LANGUAGE sql IMMUTABLE
191
    AS $_$
192
SELECT coalesce($1 || ': ', '') || $2
193
$_$;
194

    
195

    
196
--
197
-- Name: _map(hstore, text); Type: FUNCTION; Schema: functions; Owner: -
198
--
199

    
200
CREATE FUNCTION _map(map hstore, value text) RETURNS text
201
    LANGUAGE plpgsql IMMUTABLE
202
    AS $$
203
DECLARE
204
    match text := map -> value;
205
BEGIN
206
    IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
207
        match := map -> '*'; -- use default entry
208
        IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
209
        END IF;
210
    END IF;
211
    
212
    -- Interpret result
213
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
214
    ELSIF match = '*' THEN RETURN value;
215
    ELSE RETURN match;
216
    END IF;
217
END;
218
$$;
219

    
220

    
221
--
222
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
223
--
224

    
225
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
226
    LANGUAGE sql IMMUTABLE
227
    AS $_$
228
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
229
$_$;
230

    
231

    
232
--
233
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
234
--
235

    
236
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
237
    LANGUAGE sql IMMUTABLE
238
    AS $_$
239
SELECT functions.join_strs(value, '; ')
240
FROM
241
(
242
    SELECT *
243
    FROM
244
    (
245
        SELECT
246
        DISTINCT ON (value)
247
        *
248
        FROM
249
        (VALUES
250
              (1, $1)
251
            , (2, $2)
252
            , (3, $3)
253
            , (4, $4)
254
            , (5, $5)
255
            , (6, $6)
256
            , (7, $7)
257
            , (8, $8)
258
            , (9, $9)
259
            , (10, $10)
260
        )
261
        AS v (sort_order, value)
262
        WHERE value IS NOT NULL
263
    )
264
    AS v
265
    ORDER BY sort_order
266
)
267
AS v
268
$_$;
269

    
270

    
271
--
272
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: functions; Owner: -
273
--
274

    
275
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
276
    LANGUAGE sql IMMUTABLE
277
    AS $_$
278
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
279
$_$;
280

    
281

    
282
--
283
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
284
--
285

    
286
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
287
    LANGUAGE sql IMMUTABLE
288
    AS $_$
289
SELECT functions.join_strs(value, ' ')
290
FROM
291
(
292
    SELECT *
293
    FROM
294
    (
295
        SELECT
296
        DISTINCT ON (value)
297
        *
298
        FROM
299
        (VALUES
300
              (1, $1)
301
            , (2, $2)
302
            , (3, $3)
303
            , (4, $4)
304
            , (5, $5)
305
            , (6, $6)
306
            , (7, $7)
307
            , (8, $8)
308
            , (9, $9)
309
            , (10, $10)
310
        )
311
        AS v (sort_order, value)
312
        WHERE value IS NOT NULL
313
    )
314
    AS v
315
    ORDER BY sort_order
316
)
317
AS v
318
$_$;
319

    
320

    
321
--
322
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
323
--
324

    
325
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
326
    LANGUAGE sql IMMUTABLE
327
    AS $_$
328
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
329
$_$;
330

    
331

    
332
--
333
-- Name: _not(boolean); Type: FUNCTION; Schema: functions; Owner: -
334
--
335

    
336
CREATE FUNCTION _not(value boolean) RETURNS boolean
337
    LANGUAGE sql IMMUTABLE STRICT
338
    AS $_$
339
SELECT NOT $1
340
$_$;
341

    
342

    
343
--
344
-- Name: _now(); Type: FUNCTION; Schema: functions; Owner: -
345
--
346

    
347
CREATE FUNCTION _now() RETURNS timestamp with time zone
348
    LANGUAGE sql STABLE
349
    AS $$
350
SELECT now()
351
$$;
352

    
353

    
354
--
355
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
356
--
357

    
358
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
359
    LANGUAGE plpgsql IMMUTABLE
360
    AS $$
361
DECLARE
362
    type functions.datatype NOT NULL := type; -- add NOT NULL
363
BEGIN
364
    IF type = 'str' THEN RETURN nullif(value::text, "null");
365
    -- Invalid value is ignored, but invalid null value generates error
366
    ELSIF type = 'float' THEN
367
        DECLARE
368
            -- Outside the try block so that invalid null value generates error
369
            "null" double precision := "null"::double precision;
370
        BEGIN
371
            RETURN nullif(value::double precision, "null");
372
        EXCEPTION
373
            WHEN data_exception THEN RETURN value; -- ignore invalid value
374
        END;
375
    END IF;
376
END;
377
$$;
378

    
379

    
380
--
381
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
382
--
383

    
384
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
385
    LANGUAGE sql IMMUTABLE
386
    AS $_$
387
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
388
$_$;
389

    
390

    
391
--
392
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
393
--
394

    
395
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
396
    LANGUAGE sql IMMUTABLE
397
    AS $_$
398
SELECT bool_or(value)
399
FROM
400
(VALUES
401
      ($1)
402
    , ($2)
403
    , ($3)
404
    , ($4)
405
    , ($5)
406
)
407
AS v (value)
408
$_$;
409

    
410

    
411
--
412
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
413
--
414

    
415
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.';
416

    
417

    
418
--
419
-- Name: _split(text, text); Type: FUNCTION; Schema: functions; Owner: -
420
--
421

    
422
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
423
    LANGUAGE sql IMMUTABLE STRICT
424
    AS $_$
425
SELECT regexp_split_to_table($1, $2)
426
$_$;
427

    
428

    
429
--
430
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
431
--
432

    
433
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
434
    LANGUAGE sql IMMUTABLE STRICT
435
    AS $_$
436
SELECT $1 || $3 || $2
437
$_$;
438

    
439

    
440
--
441
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
442
--
443

    
444
CREATE AGGREGATE join_strs(text, text) (
445
    SFUNC = join_strs_transform,
446
    STYPE = text
447
);
448

    
449

    
450
--
451
-- PostgreSQL database dump complete
452
--
453

    
(4-4/25)