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 = off;
8
SET check_function_bodies = false;
9
SET client_min_messages = warning;
10
SET escape_string_warning = off;
11

    
12
--
13
-- Name: functions; Type: SCHEMA; Schema: -; Owner: -
14
--
15

    
16
CREATE SCHEMA functions;
17

    
18

    
19
--
20
-- Name: SCHEMA functions; Type: COMMENT; Schema: -; Owner: -
21
--
22

    
23
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.';
24

    
25

    
26
SET search_path = functions, pg_catalog;
27

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

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

    
37

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

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

    
48

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

    
53
CREATE FUNCTION _and("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean) RETURNS boolean
54
    LANGUAGE sql IMMUTABLE
55
    AS $_$
56
SELECT bool_and(value)
57
FROM
58
(VALUES
59
      ($1)
60
    , ($2)
61
)
62
AS v (value)
63
$_$;
64

    
65

    
66
--
67
-- Name: FUNCTION _and("0" boolean, "1" boolean); Type: COMMENT; Schema: functions; Owner: -
68
--
69

    
70
COMMENT ON FUNCTION _and("0" boolean, "1" boolean) IS 'NULL values are ignored rather than propagated.';
71

    
72

    
73
--
74
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: functions; Owner: -
75
--
76

    
77
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
78
    LANGUAGE sql IMMUTABLE
79
    AS $_$
80
SELECT bool_and(value)
81
FROM
82
(VALUES
83
      ($1)
84
    , ($2)
85
    , ($3)
86
    , ($4)
87
    , ($5)
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: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
106
--
107

    
108
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
109
    LANGUAGE sql IMMUTABLE
110
    AS $_$
111
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
112
$_$;
113

    
114

    
115
--
116
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
117
--
118

    
119
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
120
    LANGUAGE sql IMMUTABLE
121
    AS $_$
122
SELECT functions._if($1 != '', $2, $3)
123
$_$;
124

    
125

    
126
--
127
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
128
--
129

    
130
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
131
    LANGUAGE sql IMMUTABLE
132
    AS $_$
133
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
134
$_$;
135

    
136

    
137
--
138
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
139
--
140

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

    
147

    
148
--
149
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
150
--
151

    
152
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
153
    LANGUAGE sql IMMUTABLE
154
    AS $_$
155
SELECT coalesce($1 || ': ', '') || $2
156
$_$;
157

    
158

    
159
--
160
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
161
--
162

    
163
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
164
    LANGUAGE sql IMMUTABLE
165
    AS $_$
166
SELECT max(value)
167
FROM
168
(VALUES
169
      ($1)
170
    , ($2)
171
    , ($3)
172
    , ($4)
173
    , ($5)
174
    , ($6)
175
    , ($7)
176
    , ($8)
177
    , ($9)
178
    , ($10)
179
)
180
AS v (value)
181
$_$;
182

    
183

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

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

    
222

    
223
--
224
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
225
--
226

    
227
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
228
    LANGUAGE sql IMMUTABLE
229
    AS $_$
230
SELECT min(value)
231
FROM
232
(VALUES
233
      ($1)
234
    , ($2)
235
    , ($3)
236
    , ($4)
237
    , ($5)
238
    , ($6)
239
    , ($7)
240
    , ($8)
241
    , ($9)
242
    , ($10)
243
)
244
AS v (value)
245
$_$;
246

    
247

    
248
--
249
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
250
--
251

    
252
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
253
    LANGUAGE plpgsql IMMUTABLE
254
    AS $$
255
DECLARE
256
    "null" text NOT NULL := "null"; -- add NOT NULL
257
    type functions.datatype NOT NULL := type; -- add NOT NULL
258
BEGIN
259
    IF type = 'str' THEN RETURN nullif(value::text, "null");
260
    -- Invalid value is ignored, but invalid null value generates error
261
    ELSIF type = 'float' THEN
262
        DECLARE
263
            -- Outside the try block so that invalid null value generates error
264
            "null" double precision := "null"::double precision;
265
        BEGIN
266
            RETURN nullif(value::double precision, "null");
267
        EXCEPTION
268
            WHEN data_exception THEN RETURN value; -- ignore invalid value
269
        END;
270
    END IF;
271
END;
272
$$;
273

    
274

    
275
--
276
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
277
--
278

    
279
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
280
    LANGUAGE sql IMMUTABLE
281
    AS $_$
282
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
283
$_$;
284

    
285

    
286
--
287
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
288
--
289

    
290
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
291
    LANGUAGE sql IMMUTABLE STRICT
292
    AS $_$
293
SELECT $1 || $3 || $2
294
$_$;
295

    
296

    
297
--
298
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
299
--
300

    
301
CREATE AGGREGATE join_strs(text, text) (
302
    SFUNC = join_strs_transform,
303
    STYPE = text
304
);
305

    
306

    
307
--
308
-- PostgreSQL database dump complete
309
--
310

    
(4-4/20)