Project

General

Profile

1
--
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
SET search_path = functions, pg_catalog;
20

    
21
--
22
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
23
--
24

    
25
CREATE TYPE datatype AS ENUM (
26
    'str',
27
    'float'
28
);
29

    
30

    
31
--
32
-- Name: _alt(); Type: FUNCTION; Schema: functions; Owner: -
33
--
34

    
35
CREATE FUNCTION _alt() RETURNS trigger
36
    LANGUAGE plpgsql IMMUTABLE
37
    AS $$
38
BEGIN
39
    new.result := coalesce(new."0", new."1", new."2", new."3", new."4", new."5",
40
        new."6", new."7", new."8", new."9");
41
    RETURN new;
42
END;
43
$$;
44

    
45

    
46
--
47
-- Name: _label(); Type: FUNCTION; Schema: functions; Owner: -
48
--
49

    
50
CREATE FUNCTION _label() RETURNS trigger
51
    LANGUAGE plpgsql IMMUTABLE
52
    AS $$
53
BEGIN
54
    new.result := (SELECT new.label||': '||new.value);
55
    RETURN new;
56
END;
57
$$;
58

    
59

    
60
--
61
-- Name: _merge(); Type: FUNCTION; Schema: functions; Owner: -
62
--
63

    
64
CREATE FUNCTION _merge() RETURNS trigger
65
    LANGUAGE plpgsql IMMUTABLE
66
    AS $$
67
BEGIN
68
    new.result := (
69
        SELECT functions.join_strs('; ', value)
70
        FROM
71
        (
72
            SELECT *
73
            FROM
74
            (
75
                SELECT
76
                DISTINCT ON (value)
77
                *
78
                FROM
79
                (VALUES
80
                      (0, new."0")
81
                    , (1, new."1")
82
                    , (2, new."2")
83
                    , (3, new."3")
84
                    , (4, new."4")
85
                    , (5, new."5")
86
                    , (6, new."6")
87
                    , (7, new."7")
88
                    , (8, new."8")
89
                    , (9, new."9")
90
                )
91
                AS v (sort_order, value)
92
            )
93
            AS v
94
            ORDER BY sort_order
95
        )
96
        AS v
97
    );
98
    RETURN new;
99
END;
100
$$;
101

    
102

    
103
--
104
-- Name: _nullIf(); Type: FUNCTION; Schema: functions; Owner: -
105
--
106

    
107
CREATE FUNCTION "_nullIf"() RETURNS trigger
108
    LANGUAGE plpgsql IMMUTABLE
109
    AS $$
110
BEGIN
111
    IF new.result = 'str' THEN -- no cast needed
112
        new.result := (SELECT nullif(new.value, new."null"));
113
    -- Invalid value -> warning, but invalid null value -> error
114
    ELSIF new.result = 'float' THEN
115
        new.result := (SELECT nullif(functions."double precision"(new.value),
116
            new."null"::double precision));
117
    END IF;
118
    
119
    RETURN new;
120
END;
121
$$;
122

    
123

    
124
--
125
-- Name: boolean(text); Type: FUNCTION; Schema: functions; Owner: -
126
--
127

    
128
CREATE FUNCTION "boolean"(value text) RETURNS boolean
129
    LANGUAGE plpgsql IMMUTABLE STRICT
130
    AS $$
131
BEGIN
132
    BEGIN
133
        RETURN value::boolean;
134
    EXCEPTION
135
        WHEN data_exception THEN
136
            RAISE WARNING '%', SQLERRM;
137
            RETURN NULL;
138
    END;
139
    RETURN new;
140
END;
141
$$;
142

    
143

    
144
--
145
-- Name: double precision(text); Type: FUNCTION; Schema: functions; Owner: -
146
--
147

    
148
CREATE FUNCTION "double precision"(value text) RETURNS double precision
149
    LANGUAGE plpgsql IMMUTABLE STRICT
150
    AS $$
151
BEGIN
152
    BEGIN
153
        RETURN value::double precision;
154
    EXCEPTION
155
        WHEN data_exception THEN
156
            RAISE WARNING '%', SQLERRM;
157
            RETURN NULL;
158
    END;
159
    RETURN new;
160
END;
161
$$;
162

    
163

    
164
--
165
-- Name: ensure_not_null(text); Type: FUNCTION; Schema: functions; Owner: -
166
--
167

    
168
CREATE FUNCTION ensure_not_null(value text) RETURNS text
169
    LANGUAGE sql IMMUTABLE
170
    AS $_$
171
SELECT COALESCE($1, E'\\N');
172
$_$;
173

    
174

    
175
--
176
-- Name: join_strs_(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
177
--
178

    
179
CREATE FUNCTION join_strs_(state text, delim text, value text) RETURNS text
180
    LANGUAGE sql IMMUTABLE
181
    AS $_$
182
SELECT $1 || (CASE
183
WHEN $1 = '' OR $3 IS NULL OR $3 = ''
184
THEN ''
185
ELSE $2
186
END) || coalesce($3, '');
187
$_$;
188

    
189

    
190
--
191
-- Name: timestamp with time zone(text); Type: FUNCTION; Schema: functions; Owner: -
192
--
193

    
194
CREATE FUNCTION "timestamp with time zone"(value text) RETURNS timestamp with time zone
195
    LANGUAGE plpgsql IMMUTABLE STRICT
196
    AS $$
197
BEGIN
198
    BEGIN
199
        RETURN value::timestamp with time zone;
200
    EXCEPTION
201
        WHEN data_exception THEN
202
            RAISE WARNING '%', SQLERRM;
203
            RETURN NULL;
204
    END;
205
    RETURN new;
206
END;
207
$$;
208

    
209

    
210
--
211
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
212
--
213

    
214
CREATE AGGREGATE join_strs(text, text) (
215
    SFUNC = join_strs_,
216
    STYPE = text,
217
    INITCOND = ''
218
);
219

    
220

    
221
SET default_tablespace = '';
222

    
223
SET default_with_oids = false;
224

    
225
--
226
-- Name: _alt; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
227
--
228

    
229
CREATE TABLE _alt (
230
    result text,
231
    not_null_col boolean DEFAULT true NOT NULL,
232
    "0" text,
233
    "1" text,
234
    "2" text,
235
    "3" text,
236
    "4" text,
237
    "5" text,
238
    "6" text,
239
    "7" text,
240
    "8" text,
241
    "9" text
242
);
243

    
244

    
245
--
246
-- Name: _label; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
247
--
248

    
249
CREATE TABLE _label (
250
    result text,
251
    not_null_col boolean DEFAULT true NOT NULL,
252
    label text,
253
    value text
254
);
255

    
256

    
257
--
258
-- Name: _merge; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
259
--
260

    
261
CREATE TABLE _merge (
262
    result text,
263
    not_null_col boolean DEFAULT true NOT NULL,
264
    "0" text,
265
    "1" text,
266
    "2" text,
267
    "3" text,
268
    "4" text,
269
    "5" text,
270
    "6" text,
271
    "7" text,
272
    "8" text,
273
    "9" text
274
);
275

    
276

    
277
--
278
-- Name: _nullIf; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
279
--
280

    
281
CREATE TABLE "_nullIf" (
282
    result text,
283
    not_null_col boolean DEFAULT true NOT NULL,
284
    "null" text,
285
    type datatype DEFAULT 'str'::datatype NOT NULL,
286
    value text
287
);
288

    
289

    
290
--
291
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
292
--
293

    
294
CREATE UNIQUE INDEX _alt_unique ON _alt USING btree (ensure_not_null("0"), ensure_not_null("1"), ensure_not_null("2"), ensure_not_null("3"), ensure_not_null("4"), ensure_not_null("5"), ensure_not_null("6"), ensure_not_null("7"), ensure_not_null("8"), ensure_not_null("9"));
295

    
296

    
297
--
298
-- Name: _label_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
299
--
300

    
301
CREATE UNIQUE INDEX _label_unique ON _label USING btree (ensure_not_null(label), ensure_not_null(value));
302

    
303

    
304
--
305
-- Name: _merge_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
306
--
307

    
308
CREATE UNIQUE INDEX _merge_unique ON _merge USING btree (ensure_not_null("0"), ensure_not_null("1"), ensure_not_null("2"), ensure_not_null("3"), ensure_not_null("4"), ensure_not_null("5"), ensure_not_null("6"), ensure_not_null("7"), ensure_not_null("8"), ensure_not_null("9"));
309

    
310

    
311
--
312
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
313
--
314

    
315
CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree (ensure_not_null("null"), type, ensure_not_null(value));
316

    
317

    
318
--
319
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
320
--
321

    
322
CREATE TRIGGER _alt BEFORE INSERT OR UPDATE ON _alt FOR EACH ROW EXECUTE PROCEDURE _alt();
323

    
324

    
325
--
326
-- Name: _label; Type: TRIGGER; Schema: functions; Owner: -
327
--
328

    
329
CREATE TRIGGER _label BEFORE INSERT OR UPDATE ON _label FOR EACH ROW EXECUTE PROCEDURE _label();
330

    
331

    
332
--
333
-- Name: _merge; Type: TRIGGER; Schema: functions; Owner: -
334
--
335

    
336
CREATE TRIGGER _merge BEFORE INSERT OR UPDATE ON _merge FOR EACH ROW EXECUTE PROCEDURE _merge();
337

    
338

    
339
--
340
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
341
--
342

    
343
CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "_nullIf"();
344

    
345

    
346
--
347
-- PostgreSQL database dump complete
348
--
349

    
(4-4/20)