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 |
3422
|
aaronmk
|
-- Name: __alt(); Type: FUNCTION; Schema: functions; Owner: -
|
33 |
2566
|
aaronmk
|
--
|
34 |
|
|
|
35 |
3422
|
aaronmk
|
CREATE FUNCTION __alt() RETURNS trigger
|
36 |
2566
|
aaronmk
|
LANGUAGE plpgsql IMMUTABLE
|
37 |
|
|
AS $$
|
38 |
|
|
BEGIN
|
39 |
2944
|
aaronmk
|
new.result := functions._alt(new."0", new."1", new."2", new."3", new."4",
|
40 |
|
|
new."5", new."6", new."7", new."8", new."9");
|
41 |
2566
|
aaronmk
|
RETURN new;
|
42 |
|
|
END;
|
43 |
|
|
$$;
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
--
|
47 |
3422
|
aaronmk
|
-- Name: __label(); Type: FUNCTION; Schema: functions; Owner: -
|
48 |
2939
|
aaronmk
|
--
|
49 |
|
|
|
50 |
3422
|
aaronmk
|
CREATE FUNCTION __label() RETURNS trigger
|
51 |
|
|
LANGUAGE plpgsql IMMUTABLE
|
52 |
|
|
AS $$
|
53 |
|
|
BEGIN
|
54 |
|
|
new.result := functions._label(new.label, new.value);
|
55 |
|
|
RETURN new;
|
56 |
|
|
END;
|
57 |
|
|
$$;
|
58 |
2939
|
aaronmk
|
|
59 |
|
|
|
60 |
|
|
--
|
61 |
3422
|
aaronmk
|
-- Name: __merge(); Type: FUNCTION; Schema: functions; Owner: -
|
62 |
2606
|
aaronmk
|
--
|
63 |
|
|
|
64 |
3422
|
aaronmk
|
CREATE FUNCTION __merge() RETURNS trigger
|
65 |
2606
|
aaronmk
|
LANGUAGE plpgsql IMMUTABLE
|
66 |
|
|
AS $$
|
67 |
|
|
BEGIN
|
68 |
3422
|
aaronmk
|
new.result := functions._merge(new."0", new."1", new."2", new."3", new."4",
|
69 |
|
|
new."5", new."6", new."7", new."8", new."9");
|
70 |
2606
|
aaronmk
|
RETURN new;
|
71 |
|
|
END;
|
72 |
|
|
$$;
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
--
|
76 |
3422
|
aaronmk
|
-- Name: __nullIf(); Type: FUNCTION; Schema: functions; Owner: -
|
77 |
2943
|
aaronmk
|
--
|
78 |
|
|
|
79 |
3422
|
aaronmk
|
CREATE FUNCTION "__nullIf"() RETURNS trigger
|
80 |
2950
|
aaronmk
|
LANGUAGE plpgsql IMMUTABLE
|
81 |
|
|
AS $$
|
82 |
|
|
BEGIN
|
83 |
3422
|
aaronmk
|
new.result := functions."_nullIf"(new.value, new."null", new.type);
|
84 |
|
|
RETURN new;
|
85 |
2950
|
aaronmk
|
END;
|
86 |
|
|
$$;
|
87 |
2943
|
aaronmk
|
|
88 |
|
|
|
89 |
|
|
--
|
90 |
3422
|
aaronmk
|
-- Name: _alt(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
|
91 |
2596
|
aaronmk
|
--
|
92 |
|
|
|
93 |
3422
|
aaronmk
|
CREATE FUNCTION _alt("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
|
94 |
|
|
LANGUAGE sql IMMUTABLE
|
95 |
|
|
AS $_$
|
96 |
|
|
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
97 |
|
|
$_$;
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
--
|
101 |
|
|
-- Name: _label(text, text); Type: FUNCTION; Schema: functions; Owner: -
|
102 |
|
|
--
|
103 |
|
|
|
104 |
|
|
CREATE FUNCTION _label(label text, value text) RETURNS text
|
105 |
2596
|
aaronmk
|
LANGUAGE plpgsql IMMUTABLE
|
106 |
|
|
AS $$
|
107 |
3422
|
aaronmk
|
DECLARE
|
108 |
|
|
label text NOT NULL := label; -- add NOT NULL
|
109 |
2596
|
aaronmk
|
BEGIN
|
110 |
3422
|
aaronmk
|
RETURN label||': '||value;
|
111 |
2596
|
aaronmk
|
END;
|
112 |
|
|
$$;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
--
|
116 |
2940
|
aaronmk
|
-- Name: _merge(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
|
117 |
|
|
--
|
118 |
|
|
|
119 |
|
|
CREATE FUNCTION _merge("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
|
120 |
|
|
LANGUAGE sql IMMUTABLE
|
121 |
|
|
AS $_$
|
122 |
|
|
SELECT functions.join_strs('; ', value)
|
123 |
|
|
FROM
|
124 |
|
|
(
|
125 |
|
|
SELECT *
|
126 |
|
|
FROM
|
127 |
|
|
(
|
128 |
|
|
SELECT
|
129 |
|
|
DISTINCT ON (value)
|
130 |
|
|
*
|
131 |
|
|
FROM
|
132 |
|
|
(VALUES
|
133 |
|
|
(0, $1)
|
134 |
|
|
, (1, $2)
|
135 |
|
|
, (2, $3)
|
136 |
|
|
, (3, $4)
|
137 |
|
|
, (4, $5)
|
138 |
|
|
, (5, $6)
|
139 |
|
|
, (6, $7)
|
140 |
|
|
, (7, $8)
|
141 |
|
|
, (8, $9)
|
142 |
|
|
, (9, $10)
|
143 |
|
|
)
|
144 |
|
|
AS v (sort_order, value)
|
145 |
|
|
)
|
146 |
|
|
AS v
|
147 |
|
|
ORDER BY sort_order
|
148 |
|
|
)
|
149 |
|
|
AS v
|
150 |
|
|
$_$;
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
--
|
154 |
2949
|
aaronmk
|
-- Name: _nullIf(text, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
|
155 |
|
|
--
|
156 |
|
|
|
157 |
|
|
CREATE FUNCTION "_nullIf"(value text, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS text
|
158 |
|
|
LANGUAGE plpgsql IMMUTABLE
|
159 |
|
|
AS $$
|
160 |
|
|
DECLARE
|
161 |
|
|
"null" text NOT NULL := "null"; -- add NOT NULL
|
162 |
|
|
type functions.datatype NOT NULL := type; -- add NOT NULL
|
163 |
|
|
BEGIN
|
164 |
|
|
IF type = 'str' THEN RETURN nullif(value, "null"); -- no cast needed
|
165 |
2722
|
aaronmk
|
-- Invalid value is ignored, but invalid null value generates error
|
166 |
2949
|
aaronmk
|
ELSIF type = 'float' THEN
|
167 |
2722
|
aaronmk
|
DECLARE
|
168 |
|
|
-- Outside the try block so that invalid null value generates error
|
169 |
2949
|
aaronmk
|
"null" double precision := "null"::double precision;
|
170 |
2722
|
aaronmk
|
BEGIN
|
171 |
2949
|
aaronmk
|
RETURN nullif(value::double precision, "null");
|
172 |
2722
|
aaronmk
|
EXCEPTION
|
173 |
2949
|
aaronmk
|
WHEN data_exception THEN RETURN value; -- ignore invalid value
|
174 |
2722
|
aaronmk
|
END;
|
175 |
2610
|
aaronmk
|
END IF;
|
176 |
|
|
END;
|
177 |
|
|
$$;
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
--
|
181 |
2595
|
aaronmk
|
-- Name: join_strs_(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
|
182 |
|
|
--
|
183 |
|
|
|
184 |
|
|
CREATE FUNCTION join_strs_(state text, delim text, value text) RETURNS text
|
185 |
|
|
LANGUAGE sql IMMUTABLE
|
186 |
|
|
AS $_$
|
187 |
|
|
SELECT $1 || (CASE
|
188 |
|
|
WHEN $1 = '' OR $3 IS NULL OR $3 = ''
|
189 |
|
|
THEN ''
|
190 |
|
|
ELSE $2
|
191 |
|
|
END) || coalesce($3, '');
|
192 |
|
|
$_$;
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
--
|
196 |
|
|
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
|
197 |
|
|
--
|
198 |
|
|
|
199 |
|
|
CREATE AGGREGATE join_strs(text, text) (
|
200 |
|
|
SFUNC = join_strs_,
|
201 |
|
|
STYPE = text,
|
202 |
|
|
INITCOND = ''
|
203 |
|
|
);
|
204 |
|
|
|
205 |
|
|
|
206 |
2566
|
aaronmk
|
SET default_tablespace = '';
|
207 |
|
|
|
208 |
|
|
SET default_with_oids = false;
|
209 |
|
|
|
210 |
2107
|
aaronmk
|
--
|
211 |
2566
|
aaronmk
|
-- Name: _alt; Type: TABLE; Schema: functions; Owner: -; Tablespace:
|
212 |
|
|
--
|
213 |
|
|
|
214 |
|
|
CREATE TABLE _alt (
|
215 |
|
|
result text,
|
216 |
|
|
not_null_col boolean DEFAULT true NOT NULL,
|
217 |
|
|
"0" text,
|
218 |
|
|
"1" text,
|
219 |
|
|
"2" text,
|
220 |
|
|
"3" text,
|
221 |
|
|
"4" text,
|
222 |
|
|
"5" text,
|
223 |
|
|
"6" text,
|
224 |
|
|
"7" text,
|
225 |
|
|
"8" text,
|
226 |
|
|
"9" text
|
227 |
|
|
);
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
--
|
231 |
2606
|
aaronmk
|
-- Name: _label; Type: TABLE; Schema: functions; Owner: -; Tablespace:
|
232 |
|
|
--
|
233 |
|
|
|
234 |
|
|
CREATE TABLE _label (
|
235 |
|
|
result text,
|
236 |
|
|
not_null_col boolean DEFAULT true NOT NULL,
|
237 |
2862
|
aaronmk
|
label text NOT NULL,
|
238 |
2606
|
aaronmk
|
value text
|
239 |
|
|
);
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
--
|
243 |
2596
|
aaronmk
|
-- Name: _merge; Type: TABLE; Schema: functions; Owner: -; Tablespace:
|
244 |
|
|
--
|
245 |
|
|
|
246 |
|
|
CREATE TABLE _merge (
|
247 |
|
|
result text,
|
248 |
|
|
not_null_col boolean DEFAULT true NOT NULL,
|
249 |
|
|
"0" text,
|
250 |
|
|
"1" text,
|
251 |
|
|
"2" text,
|
252 |
|
|
"3" text,
|
253 |
|
|
"4" text,
|
254 |
|
|
"5" text,
|
255 |
|
|
"6" text,
|
256 |
|
|
"7" text,
|
257 |
|
|
"8" text,
|
258 |
|
|
"9" text
|
259 |
|
|
);
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
--
|
263 |
2662
|
aaronmk
|
-- Name: _nullIf; Type: TABLE; Schema: functions; Owner: -; Tablespace:
|
264 |
|
|
--
|
265 |
|
|
|
266 |
|
|
CREATE TABLE "_nullIf" (
|
267 |
|
|
result text,
|
268 |
|
|
not_null_col boolean DEFAULT true NOT NULL,
|
269 |
2864
|
aaronmk
|
"null" text NOT NULL,
|
270 |
2662
|
aaronmk
|
type datatype DEFAULT 'str'::datatype NOT NULL,
|
271 |
|
|
value text
|
272 |
|
|
);
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
--
|
276 |
2566
|
aaronmk
|
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
|
277 |
|
|
--
|
278 |
|
|
|
279 |
2838
|
aaronmk
|
CREATE UNIQUE INDEX _alt_unique ON _alt USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));
|
280 |
2566
|
aaronmk
|
|
281 |
|
|
|
282 |
|
|
--
|
283 |
2606
|
aaronmk
|
-- Name: _label_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
|
284 |
|
|
--
|
285 |
|
|
|
286 |
2863
|
aaronmk
|
CREATE UNIQUE INDEX _label_unique ON _label USING btree ((COALESCE(label, '\\N'::text)), (COALESCE(value, '\\N'::text)));
|
287 |
2606
|
aaronmk
|
|
288 |
|
|
|
289 |
|
|
--
|
290 |
2596
|
aaronmk
|
-- Name: _merge_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
|
291 |
|
|
--
|
292 |
|
|
|
293 |
2839
|
aaronmk
|
CREATE UNIQUE INDEX _merge_unique ON _merge USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));
|
294 |
2596
|
aaronmk
|
|
295 |
|
|
|
296 |
|
|
--
|
297 |
2610
|
aaronmk
|
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
|
298 |
|
|
--
|
299 |
|
|
|
300 |
2865
|
aaronmk
|
CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree ("null", type, (COALESCE(value, '\\N'::text)));
|
301 |
2610
|
aaronmk
|
|
302 |
|
|
|
303 |
|
|
--
|
304 |
2566
|
aaronmk
|
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
|
305 |
|
|
--
|
306 |
|
|
|
307 |
3422
|
aaronmk
|
CREATE TRIGGER _alt BEFORE INSERT OR UPDATE ON _alt FOR EACH ROW EXECUTE PROCEDURE __alt();
|
308 |
2566
|
aaronmk
|
|
309 |
|
|
|
310 |
|
|
--
|
311 |
2606
|
aaronmk
|
-- Name: _label; Type: TRIGGER; Schema: functions; Owner: -
|
312 |
|
|
--
|
313 |
|
|
|
314 |
3422
|
aaronmk
|
CREATE TRIGGER _label BEFORE INSERT OR UPDATE ON _label FOR EACH ROW EXECUTE PROCEDURE __label();
|
315 |
2606
|
aaronmk
|
|
316 |
|
|
|
317 |
|
|
--
|
318 |
2596
|
aaronmk
|
-- Name: _merge; Type: TRIGGER; Schema: functions; Owner: -
|
319 |
|
|
--
|
320 |
|
|
|
321 |
3422
|
aaronmk
|
CREATE TRIGGER _merge BEFORE INSERT OR UPDATE ON _merge FOR EACH ROW EXECUTE PROCEDURE __merge();
|
322 |
2596
|
aaronmk
|
|
323 |
|
|
|
324 |
|
|
--
|
325 |
2610
|
aaronmk
|
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
|
326 |
|
|
--
|
327 |
|
|
|
328 |
3422
|
aaronmk
|
CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "__nullIf"();
|
329 |
2610
|
aaronmk
|
|
330 |
|
|
|
331 |
|
|
--
|
332 |
2136
|
aaronmk
|
-- PostgreSQL database dump complete
|
333 |
|
|
--
|