Project

General

Profile

1 2094 aaronmk
--
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 4982 aaronmk
--
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 2107 aaronmk
SET search_path = functions, pg_catalog;
27
28 2094 aaronmk
--
29 2610 aaronmk
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
30
--
31
32
CREATE TYPE datatype AS ENUM (
33
    'str',
34
    'float'
35
);
36
37
38
--
39 4501 aaronmk
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
40 2596 aaronmk
--
41
42 4501 aaronmk
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 3422 aaronmk
    LANGUAGE sql IMMUTABLE
44
    AS $_$
45 4501 aaronmk
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
46 3422 aaronmk
$_$;
47
48
49
--
50 4783 aaronmk
-- Name: _cm_to_m(double precision); Type: FUNCTION; Schema: functions; Owner: -
51
--
52
53
CREATE FUNCTION _cm_to_m(value double precision) RETURNS double precision
54
    LANGUAGE sql IMMUTABLE STRICT
55
    AS $_$
56
SELECT $1/100.
57
$_$;
58
59
60
--
61
-- Name: _cm_to_m(text); Type: FUNCTION; Schema: functions; Owner: -
62
--
63
64
CREATE FUNCTION _cm_to_m(value text) RETURNS double precision
65
    LANGUAGE sql IMMUTABLE STRICT
66
    AS $_$
67
SELECT functions._cm_to_m($1::double precision)
68
$_$;
69
70
71
--
72 4142 aaronmk
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
73
--
74
75
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
76
    LANGUAGE sql IMMUTABLE
77
    AS $_$
78
SELECT $1 = $2
79
$_$;
80
81
82
--
83 4789 aaronmk
-- Name: _fraction_to_percent(double precision); Type: FUNCTION; Schema: functions; Owner: -
84 4760 aaronmk
--
85
86 4789 aaronmk
CREATE FUNCTION _fraction_to_percent(value double precision) RETURNS double precision
87 4760 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
88
    AS $_$
89
SELECT $1*100.
90
$_$;
91
92
93
--
94 5559 aaronmk
-- Name: _ft_to_m(double precision); Type: FUNCTION; Schema: functions; Owner: -
95
--
96
97
CREATE FUNCTION _ft_to_m(value double precision) RETURNS double precision
98
    LANGUAGE sql IMMUTABLE STRICT
99
    AS $_$
100
SELECT functions._in_to_m($1*12)
101
$_$;
102
103
104
--
105
-- Name: _ft_to_m(text); Type: FUNCTION; Schema: functions; Owner: -
106
--
107
108
CREATE FUNCTION _ft_to_m(value text) RETURNS double precision
109
    LANGUAGE sql IMMUTABLE STRICT
110
    AS $_$
111
SELECT functions._ft_to_m($1::double precision)
112
$_$;
113
114
115
--
116 4764 aaronmk
-- Name: _ha_to_m2(double precision); Type: FUNCTION; Schema: functions; Owner: -
117
--
118
119
CREATE FUNCTION _ha_to_m2(value double precision) RETURNS double precision
120
    LANGUAGE sql IMMUTABLE STRICT
121
    AS $_$
122
SELECT $1*10000.
123
$_$;
124
125
126
--
127 4776 aaronmk
-- Name: _ha_to_m2(text); Type: FUNCTION; Schema: functions; Owner: -
128
--
129
130
CREATE FUNCTION _ha_to_m2(value text) RETURNS double precision
131
    LANGUAGE sql IMMUTABLE STRICT
132
    AS $_$
133
SELECT functions._ha_to_m2($1::double precision)
134
$_$;
135
136
137
--
138 4147 aaronmk
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
139
--
140
141
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
142
    LANGUAGE sql IMMUTABLE
143
    AS $_$
144
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
145
$_$;
146
147
148
--
149
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
150
--
151
152
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
153
    LANGUAGE sql IMMUTABLE
154
    AS $_$
155
SELECT functions._if($1 != '', $2, $3)
156
$_$;
157
158
159
--
160 4863 aaronmk
-- Name: _in_to_m(double precision); Type: FUNCTION; Schema: functions; Owner: -
161
--
162
163
CREATE FUNCTION _in_to_m(value double precision) RETURNS double precision
164
    LANGUAGE sql IMMUTABLE STRICT
165
    AS $_$
166
SELECT $1*2.54/100.
167
$_$;
168
169
170
--
171
-- Name: _in_to_m(text); Type: FUNCTION; Schema: functions; Owner: -
172
--
173
174
CREATE FUNCTION _in_to_m(value text) RETURNS double precision
175
    LANGUAGE sql IMMUTABLE STRICT
176
    AS $_$
177
SELECT functions._in_to_m($1::double precision)
178
$_$;
179
180
181
--
182 4325 aaronmk
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
183
--
184
185
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
186
    LANGUAGE sql IMMUTABLE
187
    AS $_$
188 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
189 4325 aaronmk
$_$;
190
191
192
--
193 5009 aaronmk
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
194
--
195
196
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
197
    LANGUAGE sql IMMUTABLE
198
    AS $_$
199 5395 aaronmk
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
200 5009 aaronmk
$_$;
201
202
203
--
204 4683 aaronmk
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
205 3422 aaronmk
--
206
207 4683 aaronmk
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
208 4682 aaronmk
    LANGUAGE sql IMMUTABLE
209
    AS $_$
210
SELECT coalesce($1 || ': ', '') || $2
211
$_$;
212 2596 aaronmk
213
214
--
215 4773 aaronmk
-- Name: _m2_to_ha(double precision); Type: FUNCTION; Schema: functions; Owner: -
216
--
217
218
CREATE FUNCTION _m2_to_ha(value double precision) RETURNS double precision
219
    LANGUAGE sql IMMUTABLE STRICT
220
    AS $_$
221
SELECT $1/10000.
222
$_$;
223
224
225
--
226 5408 aaronmk
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
227
--
228
229
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
230
    LANGUAGE sql IMMUTABLE
231
    AS $_$
232
SELECT max(value)
233
FROM
234
(VALUES
235
      ($1)
236
    , ($2)
237
    , ($3)
238
    , ($4)
239
    , ($5)
240
    , ($6)
241
    , ($7)
242
    , ($8)
243
    , ($9)
244
    , ($10)
245
)
246
AS v (value)
247
$_$;
248
249
250
--
251 4150 aaronmk
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
252 2940 aaronmk
--
253
254 4150 aaronmk
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
255 2940 aaronmk
    LANGUAGE sql IMMUTABLE
256
    AS $_$
257 4053 aaronmk
SELECT functions.join_strs(value, '; ')
258 2940 aaronmk
FROM
259
(
260
    SELECT *
261
    FROM
262
    (
263
        SELECT
264
        DISTINCT ON (value)
265
        *
266
        FROM
267
        (VALUES
268 4012 aaronmk
              (1, $1)
269
            , (2, $2)
270
            , (3, $3)
271
            , (4, $4)
272
            , (5, $5)
273
            , (6, $6)
274
            , (7, $7)
275
            , (8, $8)
276
            , (9, $9)
277
            , (10, $10)
278 2940 aaronmk
        )
279
        AS v (sort_order, value)
280 4011 aaronmk
        WHERE value IS NOT NULL
281 2940 aaronmk
    )
282
    AS v
283
    ORDER BY sort_order
284
)
285
AS v
286
$_$;
287
288
289
--
290 5408 aaronmk
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
291
--
292
293
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
294
    LANGUAGE sql IMMUTABLE
295
    AS $_$
296
SELECT min(value)
297
FROM
298
(VALUES
299
      ($1)
300
    , ($2)
301
    , ($3)
302
    , ($4)
303
    , ($5)
304
    , ($6)
305
    , ($7)
306
    , ($8)
307
    , ($9)
308
    , ($10)
309
)
310
AS v (value)
311
$_$;
312
313
314
--
315 4475 aaronmk
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
316 2949 aaronmk
--
317
318 4475 aaronmk
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
319 2949 aaronmk
    LANGUAGE plpgsql IMMUTABLE
320
    AS $$
321
DECLARE
322
    "null" text NOT NULL := "null"; -- add NOT NULL
323
    type functions.datatype NOT NULL := type; -- add NOT NULL
324
BEGIN
325 4475 aaronmk
    IF type = 'str' THEN RETURN nullif(value::text, "null");
326 2722 aaronmk
    -- Invalid value is ignored, but invalid null value generates error
327 2949 aaronmk
    ELSIF type = 'float' THEN
328 2722 aaronmk
        DECLARE
329
            -- Outside the try block so that invalid null value generates error
330 2949 aaronmk
            "null" double precision := "null"::double precision;
331 2722 aaronmk
        BEGIN
332 2949 aaronmk
            RETURN nullif(value::double precision, "null");
333 2722 aaronmk
        EXCEPTION
334 2949 aaronmk
            WHEN data_exception THEN RETURN value; -- ignore invalid value
335 2722 aaronmk
        END;
336 2610 aaronmk
    END IF;
337
END;
338
$$;
339
340
341
--
342 4479 aaronmk
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
343
--
344
345
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
346
    LANGUAGE sql IMMUTABLE
347
    AS $_$
348
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
349
$_$;
350
351
352
--
353 4789 aaronmk
-- Name: _percent_to_fraction(double precision); Type: FUNCTION; Schema: functions; Owner: -
354 4760 aaronmk
--
355
356 4789 aaronmk
CREATE FUNCTION _percent_to_fraction(value double precision) RETURNS double precision
357 4760 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
358
    AS $_$
359
SELECT $1/100.
360
$_$;
361
362
363
--
364 4789 aaronmk
-- Name: _percent_to_fraction(text); Type: FUNCTION; Schema: functions; Owner: -
365 4776 aaronmk
--
366
367 4789 aaronmk
CREATE FUNCTION _percent_to_fraction(value text) RETURNS double precision
368 4776 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
369
    AS $_$
370 4789 aaronmk
SELECT functions._percent_to_fraction($1::double precision)
371 4776 aaronmk
$_$;
372
373
374
--
375 4052 aaronmk
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
376 4009 aaronmk
--
377
378 4053 aaronmk
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
379 4054 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
380 4009 aaronmk
    AS $_$
381 4054 aaronmk
SELECT $1 || $3 || $2
382 2595 aaronmk
$_$;
383
384
385
--
386
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
387
--
388
389
CREATE AGGREGATE join_strs(text, text) (
390 4052 aaronmk
    SFUNC = join_strs_transform,
391 4010 aaronmk
    STYPE = text
392 2595 aaronmk
);
393
394
395 2107 aaronmk
--
396 2136 aaronmk
-- PostgreSQL database dump complete
397
--