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 |
4142
|
aaronmk
|
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
51 |
|
|
--
|
52 |
|
|
|
53 |
|
|
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
|
54 |
|
|
LANGUAGE sql IMMUTABLE
|
55 |
|
|
AS $_$
|
56 |
|
|
SELECT $1 = $2
|
57 |
|
|
$_$;
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
--
|
61 |
4147
|
aaronmk
|
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
62 |
|
|
--
|
63 |
|
|
|
64 |
|
|
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
65 |
|
|
LANGUAGE sql IMMUTABLE
|
66 |
|
|
AS $_$
|
67 |
|
|
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
|
68 |
|
|
$_$;
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
--
|
72 |
|
|
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
73 |
|
|
--
|
74 |
|
|
|
75 |
|
|
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
76 |
|
|
LANGUAGE sql IMMUTABLE
|
77 |
|
|
AS $_$
|
78 |
|
|
SELECT functions._if($1 != '', $2, $3)
|
79 |
|
|
$_$;
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
--
|
83 |
4325
|
aaronmk
|
-- Name: _join(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
84 |
|
|
--
|
85 |
|
|
|
86 |
|
|
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
|
87 |
|
|
LANGUAGE sql IMMUTABLE
|
88 |
|
|
AS $_$
|
89 |
5395
|
aaronmk
|
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], '; '), '')
|
90 |
4325
|
aaronmk
|
$_$;
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
--
|
94 |
5009
|
aaronmk
|
-- Name: _join_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
95 |
|
|
--
|
96 |
|
|
|
97 |
|
|
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
|
98 |
|
|
LANGUAGE sql IMMUTABLE
|
99 |
|
|
AS $_$
|
100 |
5395
|
aaronmk
|
SELECT nullif(array_to_string(ARRAY[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10], ' '), '')
|
101 |
5009
|
aaronmk
|
$_$;
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
--
|
105 |
4683
|
aaronmk
|
-- Name: _label(anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
106 |
3422
|
aaronmk
|
--
|
107 |
|
|
|
108 |
4683
|
aaronmk
|
CREATE FUNCTION _label(label anyelement, value anyelement) RETURNS anyelement
|
109 |
4682
|
aaronmk
|
LANGUAGE sql IMMUTABLE
|
110 |
|
|
AS $_$
|
111 |
|
|
SELECT coalesce($1 || ': ', '') || $2
|
112 |
|
|
$_$;
|
113 |
2596
|
aaronmk
|
|
114 |
|
|
|
115 |
|
|
--
|
116 |
5408
|
aaronmk
|
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
117 |
|
|
--
|
118 |
|
|
|
119 |
|
|
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
|
120 |
|
|
LANGUAGE sql IMMUTABLE
|
121 |
|
|
AS $_$
|
122 |
|
|
SELECT max(value)
|
123 |
|
|
FROM
|
124 |
|
|
(VALUES
|
125 |
|
|
($1)
|
126 |
|
|
, ($2)
|
127 |
|
|
, ($3)
|
128 |
|
|
, ($4)
|
129 |
|
|
, ($5)
|
130 |
|
|
, ($6)
|
131 |
|
|
, ($7)
|
132 |
|
|
, ($8)
|
133 |
|
|
, ($9)
|
134 |
|
|
, ($10)
|
135 |
|
|
)
|
136 |
|
|
AS v (value)
|
137 |
|
|
$_$;
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
--
|
141 |
4150
|
aaronmk
|
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
142 |
2940
|
aaronmk
|
--
|
143 |
|
|
|
144 |
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
|
145 |
2940
|
aaronmk
|
LANGUAGE sql IMMUTABLE
|
146 |
|
|
AS $_$
|
147 |
4053
|
aaronmk
|
SELECT functions.join_strs(value, '; ')
|
148 |
2940
|
aaronmk
|
FROM
|
149 |
|
|
(
|
150 |
|
|
SELECT *
|
151 |
|
|
FROM
|
152 |
|
|
(
|
153 |
|
|
SELECT
|
154 |
|
|
DISTINCT ON (value)
|
155 |
|
|
*
|
156 |
|
|
FROM
|
157 |
|
|
(VALUES
|
158 |
4012
|
aaronmk
|
(1, $1)
|
159 |
|
|
, (2, $2)
|
160 |
|
|
, (3, $3)
|
161 |
|
|
, (4, $4)
|
162 |
|
|
, (5, $5)
|
163 |
|
|
, (6, $6)
|
164 |
|
|
, (7, $7)
|
165 |
|
|
, (8, $8)
|
166 |
|
|
, (9, $9)
|
167 |
|
|
, (10, $10)
|
168 |
2940
|
aaronmk
|
)
|
169 |
|
|
AS v (sort_order, value)
|
170 |
4011
|
aaronmk
|
WHERE value IS NOT NULL
|
171 |
2940
|
aaronmk
|
)
|
172 |
|
|
AS v
|
173 |
|
|
ORDER BY sort_order
|
174 |
|
|
)
|
175 |
|
|
AS v
|
176 |
|
|
$_$;
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
--
|
180 |
5408
|
aaronmk
|
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: functions; Owner: -
|
181 |
|
|
--
|
182 |
|
|
|
183 |
|
|
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
|
184 |
|
|
LANGUAGE sql IMMUTABLE
|
185 |
|
|
AS $_$
|
186 |
|
|
SELECT min(value)
|
187 |
|
|
FROM
|
188 |
|
|
(VALUES
|
189 |
|
|
($1)
|
190 |
|
|
, ($2)
|
191 |
|
|
, ($3)
|
192 |
|
|
, ($4)
|
193 |
|
|
, ($5)
|
194 |
|
|
, ($6)
|
195 |
|
|
, ($7)
|
196 |
|
|
, ($8)
|
197 |
|
|
, ($9)
|
198 |
|
|
, ($10)
|
199 |
|
|
)
|
200 |
|
|
AS v (value)
|
201 |
|
|
$_$;
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
--
|
205 |
4475
|
aaronmk
|
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
|
206 |
2949
|
aaronmk
|
--
|
207 |
|
|
|
208 |
4475
|
aaronmk
|
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
|
209 |
2949
|
aaronmk
|
LANGUAGE plpgsql IMMUTABLE
|
210 |
|
|
AS $$
|
211 |
|
|
DECLARE
|
212 |
|
|
"null" text NOT NULL := "null"; -- add NOT NULL
|
213 |
|
|
type functions.datatype NOT NULL := type; -- add NOT NULL
|
214 |
|
|
BEGIN
|
215 |
4475
|
aaronmk
|
IF type = 'str' THEN RETURN nullif(value::text, "null");
|
216 |
2722
|
aaronmk
|
-- Invalid value is ignored, but invalid null value generates error
|
217 |
2949
|
aaronmk
|
ELSIF type = 'float' THEN
|
218 |
2722
|
aaronmk
|
DECLARE
|
219 |
|
|
-- Outside the try block so that invalid null value generates error
|
220 |
2949
|
aaronmk
|
"null" double precision := "null"::double precision;
|
221 |
2722
|
aaronmk
|
BEGIN
|
222 |
2949
|
aaronmk
|
RETURN nullif(value::double precision, "null");
|
223 |
2722
|
aaronmk
|
EXCEPTION
|
224 |
2949
|
aaronmk
|
WHEN data_exception THEN RETURN value; -- ignore invalid value
|
225 |
2722
|
aaronmk
|
END;
|
226 |
2610
|
aaronmk
|
END IF;
|
227 |
|
|
END;
|
228 |
|
|
$$;
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
--
|
232 |
4479
|
aaronmk
|
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: functions; Owner: -
|
233 |
|
|
--
|
234 |
|
|
|
235 |
|
|
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
|
236 |
|
|
LANGUAGE sql IMMUTABLE
|
237 |
|
|
AS $_$
|
238 |
|
|
SELECT functions."_nullIf"($1, $2, $3::functions.datatype)
|
239 |
|
|
$_$;
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
--
|
243 |
4052
|
aaronmk
|
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
|
244 |
4009
|
aaronmk
|
--
|
245 |
|
|
|
246 |
4053
|
aaronmk
|
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
|
247 |
4054
|
aaronmk
|
LANGUAGE sql IMMUTABLE STRICT
|
248 |
4009
|
aaronmk
|
AS $_$
|
249 |
4054
|
aaronmk
|
SELECT $1 || $3 || $2
|
250 |
2595
|
aaronmk
|
$_$;
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
--
|
254 |
|
|
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
|
255 |
|
|
--
|
256 |
|
|
|
257 |
|
|
CREATE AGGREGATE join_strs(text, text) (
|
258 |
4052
|
aaronmk
|
SFUNC = join_strs_transform,
|
259 |
4010
|
aaronmk
|
STYPE = text
|
260 |
2595
|
aaronmk
|
);
|
261 |
|
|
|
262 |
|
|
|
263 |
2107
|
aaronmk
|
--
|
264 |
2136
|
aaronmk
|
-- PostgreSQL database dump complete
|
265 |
|
|
--
|