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