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 2566 aaronmk
-- Name: _alt(); Type: FUNCTION; Schema: functions; Owner: -
33
--
34
35
CREATE FUNCTION _alt() RETURNS trigger
36
    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 2939 aaronmk
-- Name: _alt(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
48
--
49
50
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
51
    LANGUAGE sql IMMUTABLE
52
    AS $_$
53
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
54
$_$;
55
56
57
--
58 2606 aaronmk
-- Name: _label(); Type: FUNCTION; Schema: functions; Owner: -
59
--
60
61
CREATE FUNCTION _label() RETURNS trigger
62
    LANGUAGE plpgsql IMMUTABLE
63
    AS $$
64
BEGIN
65 2944 aaronmk
    new.result := functions._label(new.label, new.value);
66 2606 aaronmk
    RETURN new;
67
END;
68
$$;
69
70
71
--
72 2943 aaronmk
-- Name: _label(text, text); Type: FUNCTION; Schema: functions; Owner: -
73
--
74
75
CREATE FUNCTION _label(label text, value text) RETURNS text
76 2950 aaronmk
    LANGUAGE plpgsql IMMUTABLE
77
    AS $$
78
DECLARE
79
    label text NOT NULL := label; -- add NOT NULL
80
BEGIN
81
    RETURN label||': '||value;
82
END;
83
$$;
84 2943 aaronmk
85
86
--
87 2596 aaronmk
-- Name: _merge(); Type: FUNCTION; Schema: functions; Owner: -
88
--
89
90
CREATE FUNCTION _merge() RETURNS trigger
91
    LANGUAGE plpgsql IMMUTABLE
92
    AS $$
93
BEGIN
94 2944 aaronmk
    new.result := functions._merge(new."0", new."1", new."2", new."3", new."4",
95
        new."5", new."6", new."7", new."8", new."9");
96 2596 aaronmk
    RETURN new;
97
END;
98
$$;
99
100
101
--
102 2940 aaronmk
-- Name: _merge(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
103
--
104
105
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
106
    LANGUAGE sql IMMUTABLE
107
    AS $_$
108
SELECT functions.join_strs('; ', value)
109
FROM
110
(
111
    SELECT *
112
    FROM
113
    (
114
        SELECT
115
        DISTINCT ON (value)
116
        *
117
        FROM
118
        (VALUES
119
              (0, $1)
120
            , (1, $2)
121
            , (2, $3)
122
            , (3, $4)
123
            , (4, $5)
124
            , (5, $6)
125
            , (6, $7)
126
            , (7, $8)
127
            , (8, $9)
128
            , (9, $10)
129
        )
130
        AS v (sort_order, value)
131
    )
132
    AS v
133
    ORDER BY sort_order
134
)
135
AS v
136
$_$;
137
138
139
--
140 2610 aaronmk
-- Name: _nullIf(); Type: FUNCTION; Schema: functions; Owner: -
141
--
142
143
CREATE FUNCTION "_nullIf"() RETURNS trigger
144
    LANGUAGE plpgsql IMMUTABLE
145
    AS $$
146
BEGIN
147 2949 aaronmk
    new.result := functions."_nullIf"(new.value, new."null", new.type);
148
    RETURN new;
149
END;
150
$$;
151
152
153
--
154
-- 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 2636 aaronmk
-- Name: _alt_0; Type: INDEX; Schema: functions; Owner: -; Tablespace:
277
--
278
279 2838 aaronmk
CREATE INDEX _alt_0 ON _alt USING btree ((COALESCE("0", '\\N'::text)));
280 2636 aaronmk
281
282
--
283
-- Name: _alt_1; Type: INDEX; Schema: functions; Owner: -; Tablespace:
284
--
285
286 2838 aaronmk
CREATE INDEX _alt_1 ON _alt USING btree ((COALESCE("1", '\\N'::text)));
287 2636 aaronmk
288
289
--
290
-- Name: _alt_2; Type: INDEX; Schema: functions; Owner: -; Tablespace:
291
--
292
293 2838 aaronmk
CREATE INDEX _alt_2 ON _alt USING btree ((COALESCE("2", '\\N'::text)));
294 2636 aaronmk
295
296
--
297
-- Name: _alt_3; Type: INDEX; Schema: functions; Owner: -; Tablespace:
298
--
299
300 2838 aaronmk
CREATE INDEX _alt_3 ON _alt USING btree ((COALESCE("3", '\\N'::text)));
301 2636 aaronmk
302
303
--
304
-- Name: _alt_4; Type: INDEX; Schema: functions; Owner: -; Tablespace:
305
--
306
307 2838 aaronmk
CREATE INDEX _alt_4 ON _alt USING btree ((COALESCE("4", '\\N'::text)));
308 2636 aaronmk
309
310
--
311
-- Name: _alt_5; Type: INDEX; Schema: functions; Owner: -; Tablespace:
312
--
313
314 2838 aaronmk
CREATE INDEX _alt_5 ON _alt USING btree ((COALESCE("5", '\\N'::text)));
315 2636 aaronmk
316
317
--
318
-- Name: _alt_6; Type: INDEX; Schema: functions; Owner: -; Tablespace:
319
--
320
321 2838 aaronmk
CREATE INDEX _alt_6 ON _alt USING btree ((COALESCE("6", '\\N'::text)));
322 2636 aaronmk
323
324
--
325
-- Name: _alt_7; Type: INDEX; Schema: functions; Owner: -; Tablespace:
326
--
327
328 2838 aaronmk
CREATE INDEX _alt_7 ON _alt USING btree ((COALESCE("7", '\\N'::text)));
329 2636 aaronmk
330
331
--
332
-- Name: _alt_8; Type: INDEX; Schema: functions; Owner: -; Tablespace:
333
--
334
335 2838 aaronmk
CREATE INDEX _alt_8 ON _alt USING btree ((COALESCE("8", '\\N'::text)));
336 2636 aaronmk
337
338
--
339
-- Name: _alt_9; Type: INDEX; Schema: functions; Owner: -; Tablespace:
340
--
341
342 2838 aaronmk
CREATE INDEX _alt_9 ON _alt USING btree ((COALESCE("9", '\\N'::text)));
343 2636 aaronmk
344
345
--
346 2566 aaronmk
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
347
--
348
349 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)));
350 2566 aaronmk
351
352
--
353 2636 aaronmk
-- Name: _label_label; Type: INDEX; Schema: functions; Owner: -; Tablespace:
354
--
355
356 2863 aaronmk
CREATE INDEX _label_label ON _label USING btree ((COALESCE(label, '\\N'::text)));
357 2636 aaronmk
358
359
--
360 2606 aaronmk
-- Name: _label_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
361
--
362
363 2863 aaronmk
CREATE UNIQUE INDEX _label_unique ON _label USING btree ((COALESCE(label, '\\N'::text)), (COALESCE(value, '\\N'::text)));
364 2606 aaronmk
365
366
--
367 2636 aaronmk
-- Name: _label_value; Type: INDEX; Schema: functions; Owner: -; Tablespace:
368
--
369
370
CREATE INDEX _label_value ON _label USING btree (value);
371
372
373
--
374
-- Name: _merge_0; Type: INDEX; Schema: functions; Owner: -; Tablespace:
375
--
376
377 2839 aaronmk
CREATE INDEX _merge_0 ON _merge USING btree ((COALESCE("0", '\\N'::text)));
378 2636 aaronmk
379
380
--
381
-- Name: _merge_1; Type: INDEX; Schema: functions; Owner: -; Tablespace:
382
--
383
384 2839 aaronmk
CREATE INDEX _merge_1 ON _merge USING btree ((COALESCE("1", '\\N'::text)));
385 2636 aaronmk
386
387
--
388
-- Name: _merge_2; Type: INDEX; Schema: functions; Owner: -; Tablespace:
389
--
390
391 2839 aaronmk
CREATE INDEX _merge_2 ON _merge USING btree ((COALESCE("2", '\\N'::text)));
392 2636 aaronmk
393
394
--
395
-- Name: _merge_3; Type: INDEX; Schema: functions; Owner: -; Tablespace:
396
--
397
398 2839 aaronmk
CREATE INDEX _merge_3 ON _merge USING btree ((COALESCE("3", '\\N'::text)));
399 2636 aaronmk
400
401
--
402
-- Name: _merge_4; Type: INDEX; Schema: functions; Owner: -; Tablespace:
403
--
404
405 2839 aaronmk
CREATE INDEX _merge_4 ON _merge USING btree ((COALESCE("4", '\\N'::text)));
406 2636 aaronmk
407
408
--
409
-- Name: _merge_5; Type: INDEX; Schema: functions; Owner: -; Tablespace:
410
--
411
412 2839 aaronmk
CREATE INDEX _merge_5 ON _merge USING btree ((COALESCE("5", '\\N'::text)));
413 2636 aaronmk
414
415
--
416
-- Name: _merge_6; Type: INDEX; Schema: functions; Owner: -; Tablespace:
417
--
418
419 2839 aaronmk
CREATE INDEX _merge_6 ON _merge USING btree ((COALESCE("6", '\\N'::text)));
420 2636 aaronmk
421
422
--
423
-- Name: _merge_7; Type: INDEX; Schema: functions; Owner: -; Tablespace:
424
--
425
426 2839 aaronmk
CREATE INDEX _merge_7 ON _merge USING btree ((COALESCE("7", '\\N'::text)));
427 2636 aaronmk
428
429
--
430
-- Name: _merge_8; Type: INDEX; Schema: functions; Owner: -; Tablespace:
431
--
432
433 2839 aaronmk
CREATE INDEX _merge_8 ON _merge USING btree ((COALESCE("8", '\\N'::text)));
434 2636 aaronmk
435
436
--
437
-- Name: _merge_9; Type: INDEX; Schema: functions; Owner: -; Tablespace:
438
--
439
440 2839 aaronmk
CREATE INDEX _merge_9 ON _merge USING btree ((COALESCE("9", '\\N'::text)));
441 2636 aaronmk
442
443
--
444 2596 aaronmk
-- Name: _merge_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
445
--
446
447 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)));
448 2596 aaronmk
449
450
--
451 2636 aaronmk
-- Name: _nullIf_null; Type: INDEX; Schema: functions; Owner: -; Tablespace:
452
--
453
454 2662 aaronmk
CREATE INDEX "_nullIf_null" ON "_nullIf" USING btree ("null");
455 2636 aaronmk
456
457
--
458
-- Name: _nullIf_type; Type: INDEX; Schema: functions; Owner: -; Tablespace:
459
--
460
461 2662 aaronmk
CREATE INDEX "_nullIf_type" ON "_nullIf" USING btree (type);
462 2636 aaronmk
463
464
--
465 2610 aaronmk
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace:
466
--
467
468 2865 aaronmk
CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree ("null", type, (COALESCE(value, '\\N'::text)));
469 2610 aaronmk
470
471
--
472 2636 aaronmk
-- Name: _nullIf_value; Type: INDEX; Schema: functions; Owner: -; Tablespace:
473
--
474
475 2662 aaronmk
CREATE INDEX "_nullIf_value" ON "_nullIf" USING btree (value);
476 2636 aaronmk
477
478
--
479 2566 aaronmk
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
480
--
481
482 2939 aaronmk
CREATE TRIGGER _alt BEFORE INSERT OR UPDATE ON _alt FOR EACH ROW EXECUTE PROCEDURE functions._alt();
483 2566 aaronmk
484
485
--
486 2606 aaronmk
-- Name: _label; Type: TRIGGER; Schema: functions; Owner: -
487
--
488
489
CREATE TRIGGER _label BEFORE INSERT OR UPDATE ON _label FOR EACH ROW EXECUTE PROCEDURE _label();
490
491
492
--
493 2596 aaronmk
-- Name: _merge; Type: TRIGGER; Schema: functions; Owner: -
494
--
495
496 2940 aaronmk
CREATE TRIGGER _merge BEFORE INSERT OR UPDATE ON _merge FOR EACH ROW EXECUTE PROCEDURE functions._merge();
497 2596 aaronmk
498
499
--
500 2610 aaronmk
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
501
--
502
503 2662 aaronmk
CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "_nullIf"();
504 2610 aaronmk
505
506
--
507 2136 aaronmk
-- PostgreSQL database dump complete
508
--