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