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: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
88
--
89

    
90
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
91
    LANGUAGE sql IMMUTABLE
92
    AS $_$
93
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
94
$_$;
95

    
96

    
97
--
98
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
99
--
100

    
101
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
102
    LANGUAGE sql IMMUTABLE
103
    AS $_$
104
SELECT functions._if($1 != '', $2, $3)
105
$_$;
106

    
107

    
108
--
109
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
110
--
111

    
112
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
113
    LANGUAGE sql IMMUTABLE
114
    AS $_$
115
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
116
$_$;
117

    
118

    
119
--
120
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
121
--
122

    
123
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
124
    LANGUAGE sql IMMUTABLE
125
    AS $_$
126
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
127
$_$;
128

    
129

    
130
--
131
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
132
--
133

    
134
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
135
    LANGUAGE sql IMMUTABLE
136
    AS $_$
137
SELECT coalesce($1 || ': ', '') || $2
138
$_$;
139

    
140

    
141
--
142
-- Name: _map(hstore, text); Type: FUNCTION; Schema: functions; Owner: -
143
--
144

    
145
CREATE FUNCTION _map(map hstore, value text) RETURNS text
146
    LANGUAGE plpgsql IMMUTABLE
147
    AS $$
148
DECLARE
149
    match text := map -> value;
150
BEGIN
151
    IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
152
        match := map -> '*'; -- use default entry
153
        IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
154
        END IF;
155
    END IF;
156
    
157
    -- Interpret result
158
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
159
    ELSIF match = '*' THEN RETURN value;
160
    ELSE RETURN match;
161
    END IF;
162
END;
163
$$;
164

    
165

    
166
--
167
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
168
--
169

    
170
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
171
    LANGUAGE sql IMMUTABLE
172
    AS $_$
173
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
174
$_$;
175

    
176

    
177
--
178
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
179
--
180

    
181
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
182
    LANGUAGE sql IMMUTABLE
183
    AS $_$
184
SELECT functions.join_strs(value, '; ')
185
FROM
186
(
187
    SELECT *
188
    FROM
189
    (
190
        SELECT
191
        DISTINCT ON (value)
192
        *
193
        FROM
194
        (VALUES
195
              (1, $1)
196
            , (2, $2)
197
            , (3, $3)
198
            , (4, $4)
199
            , (5, $5)
200
            , (6, $6)
201
            , (7, $7)
202
            , (8, $8)
203
            , (9, $9)
204
            , (10, $10)
205
        )
206
        AS v (sort_order, value)
207
        WHERE value IS NOT NULL
208
    )
209
    AS v
210
    ORDER BY sort_order
211
)
212
AS v
213
$_$;
214

    
215

    
216
--
217
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: functions; Owner: -
218
--
219

    
220
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
221
    LANGUAGE sql IMMUTABLE
222
    AS $_$
223
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
224
$_$;
225

    
226

    
227
--
228
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
229
--
230

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

    
265

    
266
--
267
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
268
--
269

    
270
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
271
    LANGUAGE sql IMMUTABLE
272
    AS $_$
273
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
274
$_$;
275

    
276

    
277
--
278
-- Name: _not(boolean); Type: FUNCTION; Schema: functions; Owner: -
279
--
280

    
281
CREATE FUNCTION _not(value boolean) RETURNS boolean
282
    LANGUAGE sql IMMUTABLE STRICT
283
    AS $_$
284
SELECT NOT $1
285
$_$;
286

    
287

    
288
--
289
-- Name: _now(); Type: FUNCTION; Schema: functions; Owner: -
290
--
291

    
292
CREATE FUNCTION _now() RETURNS timestamp with time zone
293
    LANGUAGE sql STABLE
294
    AS $$
295
SELECT now()
296
$$;
297

    
298

    
299
--
300
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
301
--
302

    
303
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
304
    LANGUAGE plpgsql IMMUTABLE
305
    AS $$
306
DECLARE
307
    type functions.datatype NOT NULL := type; -- add NOT NULL
308
BEGIN
309
    IF type = 'str' THEN RETURN nullif(value::text, "null");
310
    -- Invalid value is ignored, but invalid null value generates error
311
    ELSIF type = 'float' THEN
312
        DECLARE
313
            -- Outside the try block so that invalid null value generates error
314
            "null" double precision := "null"::double precision;
315
        BEGIN
316
            RETURN nullif(value::double precision, "null");
317
        EXCEPTION
318
            WHEN data_exception THEN RETURN value; -- ignore invalid value
319
        END;
320
    END IF;
321
END;
322
$$;
323

    
324

    
325
--
326
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
327
--
328

    
329
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
330
    LANGUAGE sql IMMUTABLE
331
    AS $_$
332
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
333
$_$;
334

    
335

    
336
--
337
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
338
--
339

    
340
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
341
    LANGUAGE sql IMMUTABLE
342
    AS $_$
343
SELECT bool_or(value)
344
FROM
345
(VALUES
346
      ($1)
347
    , ($2)
348
    , ($3)
349
    , ($4)
350
    , ($5)
351
)
352
AS v (value)
353
$_$;
354

    
355

    
356
--
357
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: functions; Owner: -
358
--
359

    
360
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.';
361

    
362

    
363
--
364
-- Name: _split(text, text); Type: FUNCTION; Schema: functions; Owner: -
365
--
366

    
367
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
368
    LANGUAGE sql IMMUTABLE STRICT
369
    AS $_$
370
SELECT regexp_split_to_table($1, $2)
371
$_$;
372

    
373

    
374
--
375
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
376
--
377

    
378
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
379
    LANGUAGE sql IMMUTABLE STRICT
380
    AS $_$
381
SELECT $1 || $3 || $2
382
$_$;
383

    
384

    
385
--
386
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
387
--
388

    
389
CREATE AGGREGATE join_strs(text, text) (
390
    SFUNC = join_strs_transform,
391
    STYPE = text
392
);
393

    
394

    
395
--
396
-- PostgreSQL database dump complete
397
--
398

    
(4-4/25)