Project

General

Profile

1
--
2
-- PostgreSQL database dump
3
--
4

    
5
SET statement_timeout = 0;
6
SET lock_timeout = 0;
7
SET client_encoding = 'UTF8';
8
SET standard_conforming_strings = on;
9
SET check_function_bodies = false;
10
SET client_min_messages = warning;
11

    
12
--
13
-- Name: util; Type: SCHEMA; Schema: -; Owner: -
14
--
15

    
16
CREATE SCHEMA util;
17

    
18

    
19
--
20
-- Name: SCHEMA util; Type: COMMENT; Schema: -; Owner: -
21
--
22

    
23
COMMENT ON SCHEMA util IS '
24
IMPORTANT: Functions must always return NULL in place of '''' (the empty string). This ensures that empty strings do not find their way into VegBIEN.
25

    
26
NOTE: IMMUTABLE SQL-language functions should never be declared STRICT, because this prevents them from being inlined. inlining can create a significant speed improvement (7x+), by avoiding function calls and enabling additional constant folding.
27
';
28

    
29

    
30
SET search_path = util, pg_catalog;
31

    
32
--
33
-- Name: col_cast; Type: TYPE; Schema: util; Owner: -
34
--
35

    
36
CREATE TYPE col_cast AS (
37
	col_name text,
38
	type regtype
39
);
40

    
41

    
42
--
43
-- Name: col_ref; Type: TYPE; Schema: util; Owner: -
44
--
45

    
46
CREATE TYPE col_ref AS (
47
	table_ regclass,
48
	name text
49
);
50

    
51

    
52
--
53
-- Name: compass_dir; Type: TYPE; Schema: util; Owner: -
54
--
55

    
56
CREATE TYPE compass_dir AS ENUM (
57
    'N',
58
    'E',
59
    'S',
60
    'W'
61
);
62

    
63

    
64
--
65
-- Name: datatype; Type: TYPE; Schema: util; Owner: -
66
--
67

    
68
CREATE TYPE datatype AS ENUM (
69
    'str',
70
    'float'
71
);
72

    
73

    
74
--
75
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
76
--
77

    
78
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
79
    LANGUAGE sql IMMUTABLE
80
    AS $_$
81
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
82
$_$;
83

    
84

    
85
--
86
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
87
--
88

    
89
CREATE FUNCTION _and("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean, "2" boolean DEFAULT NULL::boolean, "3" boolean DEFAULT NULL::boolean, "4" boolean DEFAULT NULL::boolean) RETURNS boolean
90
    LANGUAGE sql IMMUTABLE
91
    AS $_$
92
SELECT bool_and(value)
93
FROM
94
(VALUES
95
      ($1)
96
    , ($2)
97
    , ($3)
98
    , ($4)
99
    , ($5)
100
)
101
AS v (value)
102
$_$;
103

    
104

    
105
--
106
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
107
--
108

    
109
COMMENT ON FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '
110
_and() ignores NULL values, while AND combines them with the other values to potentially convert true to NULL. AND should be used with required fields, and _and() with optional fields.
111
';
112

    
113

    
114
--
115
-- Name: _avg(double precision, double precision, double precision, double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
116
--
117

    
118
CREATE FUNCTION _avg("0" double precision DEFAULT NULL::double precision, "1" double precision DEFAULT NULL::double precision, "2" double precision DEFAULT NULL::double precision, "3" double precision DEFAULT NULL::double precision, "4" double precision DEFAULT NULL::double precision) RETURNS double precision
119
    LANGUAGE sql IMMUTABLE
120
    AS $_$
121
SELECT avg(value)
122
FROM
123
(VALUES
124
      ($1)
125
    , ($2)
126
    , ($3)
127
    , ($4)
128
    , ($5)
129
)
130
AS v (value)
131
$_$;
132

    
133

    
134
--
135
-- Name: _dms_to_dd(text); Type: FUNCTION; Schema: util; Owner: -
136
--
137

    
138
CREATE FUNCTION _dms_to_dd(value text DEFAULT NULL::text) RETURNS double precision
139
    LANGUAGE sql IMMUTABLE STRICT
140
    AS $_$
141
SELECT (g[1]||'1')::integer*util._dms_to_dd(deg := g[2]::double precision, min := g[3]::double precision, sec := g[4]::double precision, dir := g[5]::util.compass_dir)
142
FROM 
143
(
144
    SELECT regexp_matches($1, '^ *(-?)(\d{1,3}(?:\.\d*)?)(?:(?:deg|[°º])(?: *([\d.]+)(?:min|[''’]))?(?: *([\d.]+)(?:sec|["”]))?)? *([NESW])? *$')
145
    UNION ALL
146
    SELECT ARRAY[g[1], g[2], g[3]||'.'||g[4], NULL, g[5]]
147
    FROM regexp_matches($1, '^ *(-?)(\d{2,3})(\d{2})(\d{3}) *([NESW])? *$') matches (g) -- [D]DDMMmmm, where MMmmm = MM.mmm
148
)
149
matches (g)
150
$_$;
151

    
152

    
153
--
154
-- Name: _dms_to_dd(double precision, double precision, double precision, compass_dir); Type: FUNCTION; Schema: util; Owner: -
155
--
156

    
157
CREATE FUNCTION _dms_to_dd(deg double precision DEFAULT NULL::double precision, min double precision DEFAULT NULL::double precision, sec double precision DEFAULT NULL::double precision, dir compass_dir DEFAULT NULL::compass_dir) RETURNS double precision
158
    LANGUAGE sql IMMUTABLE
159
    AS $_$
160
SELECT sum(value)*COALESCE(util._map('N=>1,E=>1,S=>-1,W=>-1', $4::text)::integer, 1)
161
FROM
162
(VALUES
163
      ($1)
164
    , ($2/60)
165
    , ($3/60/60)
166
)
167
AS v (value)
168
$_$;
169

    
170

    
171
--
172
-- Name: _dms_to_dd(text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
173
--
174

    
175
CREATE FUNCTION _dms_to_dd(deg text DEFAULT NULL::text, min text DEFAULT NULL::text, sec text DEFAULT NULL::text, dir text DEFAULT NULL::text) RETURNS double precision
176
    LANGUAGE sql IMMUTABLE
177
    AS $_$
178
SELECT util._dms_to_dd($1::double precision, $2::double precision, $3::double precision, $4::util.compass_dir)
179
$_$;
180

    
181

    
182
--
183
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
184
--
185

    
186
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
187
    LANGUAGE sql IMMUTABLE
188
    AS $_$
189
SELECT $1 = $2
190
$_$;
191

    
192

    
193
--
194
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: util; Owner: -
195
--
196

    
197
CREATE FUNCTION _fix_date(value date DEFAULT NULL::date, threshold date DEFAULT NULL::date) RETURNS date
198
    LANGUAGE sql IMMUTABLE
199
    AS $_$
200
-- Fix dates after threshold date
201
-- This fixes e.g. 2-digit years before 1970 misinterpreted as 20xx
202
SELECT (CASE WHEN $1 > COALESCE($2, now()) THEN ($1 - '100 years'::interval)::date ELSE $1 END)
203
$_$;
204

    
205

    
206
--
207
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
208
--
209

    
210
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
211
    LANGUAGE sql IMMUTABLE
212
    AS $_$
213
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
214
$_$;
215

    
216

    
217
--
218
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
219
--
220

    
221
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
222
    LANGUAGE sql IMMUTABLE
223
    AS $_$
224
SELECT util._if($1 != '', $2, $3)
225
$_$;
226

    
227

    
228
--
229
-- Name: _join(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
230
--
231

    
232
CREATE FUNCTION _join("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
233
    LANGUAGE sql IMMUTABLE
234
    AS $_$
235
SELECT NULLIF(concat_ws('; ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
236
$_$;
237

    
238

    
239
--
240
-- Name: _join_words(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
241
--
242

    
243
CREATE FUNCTION _join_words("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
244
    LANGUAGE sql IMMUTABLE
245
    AS $_$
246
SELECT NULLIF(concat_ws(' ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
247
$_$;
248

    
249

    
250
--
251
-- Name: _label(text, text); Type: FUNCTION; Schema: util; Owner: -
252
--
253

    
254
CREATE FUNCTION _label(label text, value text) RETURNS text
255
    LANGUAGE sql IMMUTABLE
256
    AS $_$
257
SELECT coalesce($1 || ': ', '') || $2
258
$_$;
259

    
260

    
261
--
262
-- Name: _lowercase(text); Type: FUNCTION; Schema: util; Owner: -
263
--
264

    
265
CREATE FUNCTION _lowercase(value text) RETURNS text
266
    LANGUAGE sql IMMUTABLE
267
    AS $_$
268
SELECT lower($1)
269
$_$;
270

    
271

    
272
--
273
-- Name: _map(hstore, anyelement); Type: FUNCTION; Schema: util; Owner: -
274
--
275

    
276
CREATE FUNCTION _map(map hstore, value anyelement) RETURNS anyelement
277
    LANGUAGE plpgsql IMMUTABLE STRICT
278
    AS $$
279
DECLARE
280
    result value%TYPE := util._map(map, value::text)::unknown;
281
BEGIN
282
    RETURN result;
283
END;
284
$$;
285

    
286

    
287
--
288
-- Name: _map(hstore, text); Type: FUNCTION; Schema: util; Owner: -
289
--
290

    
291
CREATE FUNCTION _map(map hstore, value text) RETURNS text
292
    LANGUAGE plpgsql IMMUTABLE STRICT
293
    AS $$
294
DECLARE
295
    match text := map -> value;
296
BEGIN
297
    IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
298
        match := map -> '*'; -- use default entry
299
        IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
300
        END IF;
301
    END IF;
302
    
303
    -- Interpret result
304
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
305
    ELSIF match = '*' THEN RETURN value;
306
    ELSE RETURN match;
307
    END IF;
308
END;
309
$$;
310

    
311

    
312
--
313
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
314
--
315

    
316
CREATE FUNCTION _max("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
317
    LANGUAGE sql IMMUTABLE
318
    AS $_$
319
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
320
$_$;
321

    
322

    
323
--
324
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
325
--
326

    
327
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
328
    LANGUAGE sql IMMUTABLE
329
    AS $_$
330
SELECT util.join_strs(value, '; ')
331
FROM
332
(
333
    SELECT *
334
    FROM
335
    (
336
        SELECT
337
        DISTINCT ON (value)
338
        *
339
        FROM
340
        (VALUES
341
              (1, $1)
342
            , (2, $2)
343
            , (3, $3)
344
            , (4, $4)
345
            , (5, $5)
346
            , (6, $6)
347
            , (7, $7)
348
            , (8, $8)
349
            , (9, $9)
350
            , (10, $10)
351
        )
352
        AS v (sort_order, value)
353
        WHERE value IS NOT NULL
354
    )
355
    AS v
356
    ORDER BY sort_order
357
)
358
AS v
359
$_$;
360

    
361

    
362
--
363
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
364
--
365

    
366
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
367
    LANGUAGE sql IMMUTABLE
368
    AS $_$
369
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
370
$_$;
371

    
372

    
373
--
374
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
375
--
376

    
377
CREATE FUNCTION _merge_words("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
378
    LANGUAGE sql IMMUTABLE
379
    AS $_$
380
SELECT util.join_strs(value, ' ')
381
FROM
382
(
383
    SELECT *
384
    FROM
385
    (
386
        SELECT
387
        DISTINCT ON (value)
388
        *
389
        FROM
390
        (VALUES
391
              (1, $1)
392
            , (2, $2)
393
            , (3, $3)
394
            , (4, $4)
395
            , (5, $5)
396
            , (6, $6)
397
            , (7, $7)
398
            , (8, $8)
399
            , (9, $9)
400
            , (10, $10)
401
        )
402
        AS v (sort_order, value)
403
        WHERE value IS NOT NULL
404
    )
405
    AS v
406
    ORDER BY sort_order
407
)
408
AS v
409
$_$;
410

    
411

    
412
--
413
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
414
--
415

    
416
CREATE FUNCTION _min("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
417
    LANGUAGE sql IMMUTABLE
418
    AS $_$
419
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
420
$_$;
421

    
422

    
423
--
424
-- Name: _not(boolean); Type: FUNCTION; Schema: util; Owner: -
425
--
426

    
427
CREATE FUNCTION _not(value boolean) RETURNS boolean
428
    LANGUAGE sql IMMUTABLE
429
    AS $_$
430
SELECT NOT $1
431
$_$;
432

    
433

    
434
--
435
-- Name: _now(); Type: FUNCTION; Schema: util; Owner: -
436
--
437

    
438
CREATE FUNCTION _now() RETURNS timestamp with time zone
439
    LANGUAGE sql STABLE
440
    AS $$
441
SELECT now()
442
$$;
443

    
444

    
445
--
446
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: util; Owner: -
447
--
448

    
449
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
450
    LANGUAGE sql IMMUTABLE
451
    AS $_$
452
SELECT util."_nullIf"($1, $2, $3::util.datatype)
453
$_$;
454

    
455

    
456
--
457
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: util; Owner: -
458
--
459

    
460
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
461
    LANGUAGE plpgsql IMMUTABLE
462
    AS $$
463
DECLARE
464
    type util.datatype NOT NULL := type; -- add NOT NULL
465
BEGIN
466
    IF type = 'str' THEN RETURN nullif(value::text, "null");
467
    -- Invalid value is ignored, but invalid null value generates error
468
    ELSIF type = 'float' THEN
469
        DECLARE
470
            -- Outside the try block so that invalid null value generates error
471
            "null" double precision := "null"::double precision;
472
        BEGIN
473
            RETURN nullif(value::double precision, "null");
474
        EXCEPTION
475
            WHEN data_exception THEN RETURN value; -- ignore invalid value
476
        END;
477
    END IF;
478
END;
479
$$;
480

    
481

    
482
--
483
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
484
--
485

    
486
CREATE FUNCTION _or("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean, "2" boolean DEFAULT NULL::boolean, "3" boolean DEFAULT NULL::boolean, "4" boolean DEFAULT NULL::boolean) RETURNS boolean
487
    LANGUAGE sql IMMUTABLE
488
    AS $_$
489
SELECT bool_or(value)
490
FROM
491
(VALUES
492
      ($1)
493
    , ($2)
494
    , ($3)
495
    , ($4)
496
    , ($5)
497
)
498
AS v (value)
499
$_$;
500

    
501

    
502
--
503
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
504
--
505

    
506
COMMENT ON FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '
507
_or() ignores NULL values, while OR combines them with the other values to potentially convert false to NULL. OR should be used with required fields, and _or() with optional fields.
508
';
509

    
510

    
511
--
512
-- Name: _range(double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
513
--
514

    
515
CREATE FUNCTION _range("from" double precision DEFAULT NULL::double precision, "to" double precision DEFAULT NULL::double precision) RETURNS double precision
516
    LANGUAGE sql IMMUTABLE
517
    AS $_$
518
SELECT $2 - $1
519
$_$;
520

    
521

    
522
--
523
-- Name: _split(text, text); Type: FUNCTION; Schema: util; Owner: -
524
--
525

    
526
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
527
    LANGUAGE sql IMMUTABLE
528
    AS $_$
529
SELECT regexp_split_to_table($1, $2)
530
$_$;
531

    
532

    
533
--
534
-- Name: added_cols(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
535
--
536

    
537
CREATE FUNCTION added_cols(table_ regclass, names regclass) RETURNS SETOF text
538
    LANGUAGE sql STABLE STRICT
539
    AS $_$
540
SELECT util.derived_cols($1, $2)
541
UNION
542
SELECT util.eval2set($$
543
SELECT col
544
FROM util.col_names($$||quote_nullable($1)||$$::regclass) f (col)
545
JOIN $$||$2||$$ ON "to" = col
546
WHERE "from" LIKE ':%'
547
$$, NULL::text)
548
$_$;
549

    
550

    
551
--
552
-- Name: FUNCTION added_cols(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
553
--
554

    
555
COMMENT ON FUNCTION added_cols(table_ regclass, names regclass) IS '
556
gets table_''s added columns (all the columns not in the original data)
557
';
558

    
559

    
560
--
561
-- Name: all_same_final(anyarray); Type: FUNCTION; Schema: util; Owner: -
562
--
563

    
564
CREATE FUNCTION all_same_final(state anyarray) RETURNS boolean
565
    LANGUAGE sql IMMUTABLE
566
    AS $_$
567
SELECT $1 IS NULL/*no rows*/ OR util.not_empty($1)/*not no_match_sentinel*/
568
$_$;
569

    
570

    
571
--
572
-- Name: all_same_transform(anyarray, anyelement); Type: FUNCTION; Schema: util; Owner: -
573
--
574

    
575
CREATE FUNCTION all_same_transform(state anyarray, value anyelement) RETURNS anyarray
576
    LANGUAGE plpgsql IMMUTABLE
577
    AS $$
578
DECLARE
579
	value_cmp         state%TYPE = ARRAY[value];
580
	state             state%TYPE = COALESCE(state, value_cmp);
581
	no_match_sentinel state%TYPE = value_cmp[1:0]/*=ARRAY[]::state%TYPE*/;
582
BEGIN
583
	RETURN (CASE WHEN value_cmp IS NOT DISTINCT FROM state THEN state ELSE no_match_sentinel END);
584
END;
585
$$;
586

    
587

    
588
--
589
-- Name: analyze_(regclass); Type: FUNCTION; Schema: util; Owner: -
590
--
591

    
592
CREATE FUNCTION analyze_(table_ regclass) RETURNS void
593
    LANGUAGE sql
594
    AS $_$
595
SELECT util.eval($$ANALYZE VERBOSE $$||$1)
596
$_$;
597

    
598

    
599
--
600
-- Name: array_fill(anyelement, integer); Type: FUNCTION; Schema: util; Owner: -
601
--
602

    
603
CREATE FUNCTION array_fill(value anyelement, length integer) RETURNS anyarray
604
    LANGUAGE sql IMMUTABLE
605
    AS $_$
606
SELECT pg_catalog.array_fill($1, ARRAY[$2])
607
$_$;
608

    
609

    
610
--
611
-- Name: array_length(anyarray); Type: FUNCTION; Schema: util; Owner: -
612
--
613

    
614
CREATE FUNCTION array_length("array" anyarray) RETURNS integer
615
    LANGUAGE sql IMMUTABLE
616
    AS $_$
617
SELECT util.array_length($1, 1)
618
$_$;
619

    
620

    
621
--
622
-- Name: array_length(anyarray, integer); Type: FUNCTION; Schema: util; Owner: -
623
--
624

    
625
CREATE FUNCTION array_length("array" anyarray, dimension integer) RETURNS integer
626
    LANGUAGE sql IMMUTABLE
627
    AS $_$
628
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE COALESCE(pg_catalog.array_length($1, $2), 0) END
629
$_$;
630

    
631

    
632
--
633
-- Name: FUNCTION array_length("array" anyarray, dimension integer); Type: COMMENT; Schema: util; Owner: -
634
--
635

    
636
COMMENT ON FUNCTION array_length("array" anyarray, dimension integer) IS '
637
returns 0 instead of NULL for empty arrays
638
';
639

    
640

    
641
--
642
-- Name: cluster_index(regclass); Type: FUNCTION; Schema: util; Owner: -
643
--
644

    
645
CREATE FUNCTION cluster_index(table_ regclass) RETURNS regclass
646
    LANGUAGE sql STABLE STRICT
647
    AS $_$
648
SELECT indexrelid FROM pg_index WHERE indrelid = $1 AND indisclustered
649
$_$;
650

    
651

    
652
--
653
-- Name: cluster_once(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
654
--
655

    
656
CREATE FUNCTION cluster_once(table_ regclass, index regclass) RETURNS void
657
    LANGUAGE plpgsql STRICT
658
    AS $_$
659
BEGIN
660
    -- not yet clustered (ARRAY[] compares NULLs literally)
661
    IF ARRAY[util.cluster_index(table_)] != ARRAY[index] THEN
662
        EXECUTE $$CLUSTER $$||table_||$$ USING $$||index;
663
    END IF;
664
END;
665
$_$;
666

    
667

    
668
--
669
-- Name: FUNCTION cluster_once(table_ regclass, index regclass); Type: COMMENT; Schema: util; Owner: -
670
--
671

    
672
COMMENT ON FUNCTION cluster_once(table_ regclass, index regclass) IS '
673
idempotent
674
';
675

    
676

    
677
--
678
-- Name: col__min(col_ref); Type: FUNCTION; Schema: util; Owner: -
679
--
680

    
681
CREATE FUNCTION col__min(col col_ref) RETURNS integer
682
    LANGUAGE sql STABLE
683
    AS $_$
684
SELECT util.eval2val($$
685
SELECT $$||quote_ident($1.name)||$$
686
FROM $$||$1.table_||$$
687
ORDER BY $$||quote_ident($1.name)||$$ ASC
688
LIMIT 1
689
$$, NULL::integer)
690
$_$;
691

    
692

    
693
--
694
-- Name: col_comment(col_ref); Type: FUNCTION; Schema: util; Owner: -
695
--
696

    
697
CREATE FUNCTION col_comment(col col_ref) RETURNS text
698
    LANGUAGE plpgsql STABLE STRICT
699
    AS $$
700
DECLARE
701
	comment text;
702
BEGIN
703
	SELECT description
704
	FROM pg_attribute
705
	LEFT JOIN pg_description ON objoid = attrelid
706
		AND classoid = 'pg_class'::regclass AND objsubid = attnum
707
	WHERE attrelid = col.table_ AND attname = col.name
708
	INTO STRICT comment
709
	;
710
	RETURN comment;
711
EXCEPTION
712
	WHEN no_data_found THEN PERFORM util.raise_undefined_column(col);
713
END;
714
$$;
715

    
716

    
717
--
718
-- Name: col_default_sql(col_ref); Type: FUNCTION; Schema: util; Owner: -
719
--
720

    
721
CREATE FUNCTION col_default_sql(col col_ref) RETURNS text
722
    LANGUAGE plpgsql STABLE STRICT
723
    AS $$
724
DECLARE
725
	default_sql text;
726
BEGIN
727
	SELECT adsrc
728
	FROM pg_attribute
729
	LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum
730
	WHERE attrelid = col.table_ AND attname = col.name
731
	INTO STRICT default_sql
732
	;
733
	RETURN default_sql;
734
EXCEPTION
735
	WHEN no_data_found THEN PERFORM util.raise_undefined_column(col);
736
END;
737
$$;
738

    
739

    
740
--
741
-- Name: col_default_value(col_ref, anyelement); Type: FUNCTION; Schema: util; Owner: -
742
--
743

    
744
CREATE FUNCTION col_default_value(col col_ref, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
745
    LANGUAGE sql STABLE
746
    AS $_$
747
SELECT util.eval_expr_passthru(util.col_default_sql($1), $2)
748
$_$;
749

    
750

    
751
--
752
-- Name: FUNCTION col_default_value(col col_ref, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
753
--
754

    
755
COMMENT ON FUNCTION col_default_value(col col_ref, ret_type_null anyelement) IS '
756
ret_type_null: NULL::ret_type
757
';
758

    
759

    
760
--
761
-- Name: col_exists(col_ref); Type: FUNCTION; Schema: util; Owner: -
762
--
763

    
764
CREATE FUNCTION col_exists(col col_ref) RETURNS boolean
765
    LANGUAGE plpgsql STRICT
766
    AS $$
767
BEGIN
768
    PERFORM util.col_type(col);
769
    RETURN true;
770
EXCEPTION
771
    WHEN undefined_column THEN RETURN false;
772
END;
773
$$;
774

    
775

    
776
--
777
-- Name: col_global_names(regtype); Type: FUNCTION; Schema: util; Owner: -
778
--
779

    
780
CREATE FUNCTION col_global_names(type regtype, OUT name text, OUT global_name text) RETURNS SETOF record
781
    LANGUAGE plpgsql STABLE STRICT
782
    AS $$
783
DECLARE
784
    prefix text := util.name(type)||'.';
785
BEGIN
786
    RETURN QUERY
787
        SELECT name_, (CASE WHEN util.contains(search_for:='.', in_str:=name_) THEN '' ELSE prefix END)||name_
788
        FROM util.col_names(type) f (name_);
789
END;
790
$$;
791

    
792

    
793
--
794
-- Name: col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
795
--
796

    
797
CREATE FUNCTION col_names(table_ regclass) RETURNS SETOF text
798
    LANGUAGE sql STABLE STRICT
799
    AS $_$
800
SELECT attname::text
801
FROM pg_attribute
802
WHERE attrelid = $1 AND attnum >= 1 AND NOT attisdropped
803
ORDER BY attnum
804
$_$;
805

    
806

    
807
--
808
-- Name: col_names(regtype); Type: FUNCTION; Schema: util; Owner: -
809
--
810

    
811
CREATE FUNCTION col_names(type regtype) RETURNS SETOF text
812
    LANGUAGE plpgsql STABLE STRICT
813
    AS $_$
814
BEGIN
815
    RETURN QUERY EXECUTE $$SELECT skeys(hstore(NULL::$$||type||$$))$$;
816
END;
817
$_$;
818

    
819

    
820
--
821
-- Name: col_type(col_ref); Type: FUNCTION; Schema: util; Owner: -
822
--
823

    
824
CREATE FUNCTION col_type(col col_ref) RETURNS regtype
825
    LANGUAGE plpgsql STABLE STRICT
826
    AS $$
827
DECLARE
828
    type regtype;
829
BEGIN
830
    SELECT atttypid FROM pg_attribute
831
    WHERE attrelid = col.table_ AND attname = col.name
832
    INTO STRICT type
833
    ;
834
    RETURN type;
835
EXCEPTION
836
    WHEN no_data_found THEN
837
        RAISE undefined_column USING MESSAGE =
838
            concat('undefined column: ', col.name);
839
END;
840
$$;
841

    
842

    
843
--
844
-- Name: comment(oid); Type: FUNCTION; Schema: util; Owner: -
845
--
846

    
847
CREATE FUNCTION comment(element oid) RETURNS text
848
    LANGUAGE sql STABLE STRICT
849
    AS $_$
850
SELECT description FROM pg_description WHERE objoid = $1
851
$_$;
852

    
853

    
854
--
855
-- Name: concat_esc(text, text); Type: FUNCTION; Schema: util; Owner: -
856
--
857

    
858
CREATE FUNCTION concat_esc("left" text, "right" text) RETURNS text
859
    LANGUAGE sql IMMUTABLE
860
    AS $_$
861
SELECT util.esc_name__append($2, $1)
862
$_$;
863

    
864

    
865
--
866
-- Name: contains(text, text); Type: FUNCTION; Schema: util; Owner: -
867
--
868

    
869
CREATE FUNCTION contains(search_for text, in_str text) RETURNS boolean
870
    LANGUAGE sql IMMUTABLE
871
    AS $_$
872
SELECT position($1 in $2) > 0 /*1-based offset*/
873
$_$;
874

    
875

    
876
--
877
-- Name: copy_struct(regclass, text); Type: FUNCTION; Schema: util; Owner: -
878
--
879

    
880
CREATE FUNCTION copy_struct(from_ regclass, to_ text) RETURNS void
881
    LANGUAGE sql
882
    AS $_$
883
SELECT util.eval($$CREATE TABLE $$||$2||$$ (LIKE $$||$1||$$ INCLUDING ALL)$$)
884
$_$;
885

    
886

    
887
--
888
-- Name: create_if_not_exists(text); Type: FUNCTION; Schema: util; Owner: -
889
--
890

    
891
CREATE FUNCTION create_if_not_exists(sql text) RETURNS void
892
    LANGUAGE plpgsql STRICT
893
    AS $$
894
BEGIN
895
    PERFORM util.eval(sql);
896
EXCEPTION
897
    WHEN duplicate_table  THEN NULL;
898
    WHEN duplicate_object THEN NULL; -- e.g. constraint
899
    WHEN duplicate_column THEN NULL;
900
    WHEN invalid_table_definition THEN
901
        IF SQLERRM LIKE 'multiple primary keys for table % are not allowed' THEN NULL;
902
        ELSE RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM; -- rethrow
903
        END IF;
904
END;
905
$$;
906

    
907

    
908
--
909
-- Name: FUNCTION create_if_not_exists(sql text); Type: COMMENT; Schema: util; Owner: -
910
--
911

    
912
COMMENT ON FUNCTION create_if_not_exists(sql text) IS '
913
idempotent
914
';
915

    
916

    
917
--
918
-- Name: debug_print_sql(text); Type: FUNCTION; Schema: util; Owner: -
919
--
920

    
921
CREATE FUNCTION debug_print_sql(sql text) RETURNS void
922
    LANGUAGE sql IMMUTABLE
923
    AS $_$
924
/* newline before so the query starts at the beginning of the line.
925
newline after to visually separate queries from one another. */
926
SELECT util.raise_notice($$
927
$$||$1||$$
928
$$)
929
$_$;
930

    
931

    
932
--
933
-- Name: derived_cols(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
934
--
935

    
936
CREATE FUNCTION derived_cols(table_ regclass, names regclass) RETURNS SETOF text
937
    LANGUAGE sql STABLE STRICT
938
    AS $_$
939
SELECT util.eval2set($$
940
SELECT col
941
FROM util.col_names($$||quote_nullable($1)||$$::regclass) f (col)
942
LEFT JOIN $$||$2||$$ ON "to" = col
943
WHERE "from" IS NULL
944
$$, NULL::text)
945
$_$;
946

    
947

    
948
--
949
-- Name: FUNCTION derived_cols(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
950
--
951

    
952
COMMENT ON FUNCTION derived_cols(table_ regclass, names regclass) IS '
953
gets table_''s derived columns (all the columns not in the names table)
954
';
955

    
956

    
957
--
958
-- Name: diff(regclass, regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
959
--
960

    
961
CREATE FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
962
    LANGUAGE sql STABLE
963
    AS $_$
964
SELECT * FROM util.diff($1::text, $2::text, $3,
965
	util.has_single_row($1) AND util.has_single_row($2))
966
$_$;
967

    
968

    
969
--
970
-- Name: FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
971
--
972

    
973
COMMENT ON FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
974
col_type_null (*required*): NULL::shared_base_type
975
usage:
976
SELECT * FROM util.diff(''"left_table"''::regclass, ''"right_table"''::regclass, NULL::shared_base_type)
977
';
978

    
979

    
980
--
981
-- Name: diff(text, text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
982
--
983

    
984
CREATE FUNCTION diff(left_ text, right_ text, col_type_null anyelement, single_row boolean DEFAULT false, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
985
    LANGUAGE sql STABLE
986
    AS $_$
987
SELECT * FROM
988
util.eval2col_pair($$
989
/* need to explicitly cast each side to the return type because this does not
990
happen automatically even when an implicit cast is available */
991
SELECT left_::$$||pg_typeof($3)||$$, right_::$$||pg_typeof($3)||$$
992
FROM $$||$1||$$ left_
993
$$||util._if($4, 'CROSS'::text, 'FULL')||$$ JOIN $$||$2||$$ right_
994
$$||util._if($4, ''::text, $$ON left_ = right_
995
$$)||
996
$$WHERE left_ IS DISTINCT FROM right_
997
ORDER BY left_, right_
998
$$, $3)
999
$_$;
1000

    
1001

    
1002
--
1003
-- Name: FUNCTION diff(left_ text, right_ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
1004
--
1005

    
1006
COMMENT ON FUNCTION diff(left_ text, right_ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
1007
col_type_null (*required*): NULL::col_type
1008
single_row: whether the tables consist of a single row, which should be
1009
	displayed side-by-side
1010

    
1011
to run EXPLAIN on the FULL JOIN query:
1012
# run this function
1013
# look for a NOTICE containing the expanded query that it ran
1014
# run EXPLAIN on this expanded query
1015
';
1016

    
1017

    
1018
--
1019
-- Name: do_optionally_ignore(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1020
--
1021

    
1022
CREATE FUNCTION do_optionally_ignore(sql text, ignore boolean) RETURNS void
1023
    LANGUAGE sql STRICT
1024
    AS $_$
1025
SELECT CASE WHEN $2 THEN util.try_create($1) ELSE util.create_if_not_exists($1) END
1026
$_$;
1027

    
1028

    
1029
--
1030
-- Name: FUNCTION do_optionally_ignore(sql text, ignore boolean); Type: COMMENT; Schema: util; Owner: -
1031
--
1032

    
1033
COMMENT ON FUNCTION do_optionally_ignore(sql text, ignore boolean) IS '
1034
idempotent
1035
';
1036

    
1037

    
1038
--
1039
-- Name: drop_column(col_ref, boolean); Type: FUNCTION; Schema: util; Owner: -
1040
--
1041

    
1042
CREATE FUNCTION drop_column(col col_ref, force boolean DEFAULT false) RETURNS void
1043
    LANGUAGE sql STRICT
1044
    AS $_$
1045
SELECT util.eval($$ALTER TABLE $$||$1.table_||$$ DROP COLUMN IF EXISTS $$||
1046
quote_ident($1.name)||util._if($2, $$ CASCADE$$, ''::text))
1047
$_$;
1048

    
1049

    
1050
--
1051
-- Name: FUNCTION drop_column(col col_ref, force boolean); Type: COMMENT; Schema: util; Owner: -
1052
--
1053

    
1054
COMMENT ON FUNCTION drop_column(col col_ref, force boolean) IS '
1055
idempotent
1056
';
1057

    
1058

    
1059
--
1060
-- Name: drop_relation(regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
1061
--
1062

    
1063
CREATE FUNCTION drop_relation(relation regclass, force boolean DEFAULT false) RETURNS void
1064
    LANGUAGE sql
1065
    AS $_$
1066
/* use util.qual_name() instead of ::text so that the schema qualifier is always
1067
included in the debug SQL */
1068
SELECT util.drop_relation(util.relation_type($1), util.qual_name($1), $2)
1069
$_$;
1070

    
1071

    
1072
--
1073
-- Name: drop_relation(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
1074
--
1075

    
1076
CREATE FUNCTION drop_relation(type text, relation_esc text, force boolean DEFAULT false) RETURNS void
1077
    LANGUAGE sql
1078
    AS $_$
1079
SELECT util.eval($$DROP $$||$1||$$ IF EXISTS $$||$2
1080
||util._if($3, $$ CASCADE$$, ''::text))
1081
$_$;
1082

    
1083

    
1084
--
1085
-- Name: FUNCTION drop_relation(type text, relation_esc text, force boolean); Type: COMMENT; Schema: util; Owner: -
1086
--
1087

    
1088
COMMENT ON FUNCTION drop_relation(type text, relation_esc text, force boolean) IS '
1089
idempotent
1090
';
1091

    
1092

    
1093
--
1094
-- Name: drop_table(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1095
--
1096

    
1097
CREATE FUNCTION drop_table(table_ text, force boolean DEFAULT false) RETURNS void
1098
    LANGUAGE sql STRICT
1099
    AS $_$
1100
SELECT util.drop_relation('TABLE', $1, $2)
1101
$_$;
1102

    
1103

    
1104
--
1105
-- Name: FUNCTION drop_table(table_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1106
--
1107

    
1108
COMMENT ON FUNCTION drop_table(table_ text, force boolean) IS '
1109
idempotent
1110
';
1111

    
1112

    
1113
--
1114
-- Name: drop_view(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1115
--
1116

    
1117
CREATE FUNCTION drop_view(view_ text, force boolean DEFAULT false) RETURNS void
1118
    LANGUAGE sql STRICT
1119
    AS $_$
1120
SELECT util.drop_relation('VIEW', $1, $2)
1121
$_$;
1122

    
1123

    
1124
--
1125
-- Name: FUNCTION drop_view(view_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1126
--
1127

    
1128
COMMENT ON FUNCTION drop_view(view_ text, force boolean) IS '
1129
idempotent
1130
';
1131

    
1132

    
1133
--
1134
-- Name: empty_array(anyelement); Type: FUNCTION; Schema: util; Owner: -
1135
--
1136

    
1137
CREATE FUNCTION empty_array(elem_type_null anyelement DEFAULT NULL::text) RETURNS anyarray
1138
    LANGUAGE sql IMMUTABLE
1139
    AS $_$
1140
SELECT util.array_fill($1, 0)
1141
$_$;
1142

    
1143

    
1144
--
1145
-- Name: FUNCTION empty_array(elem_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1146
--
1147

    
1148
COMMENT ON FUNCTION empty_array(elem_type_null anyelement) IS '
1149
constructs proper empty 1-dimensional array whose dimensions are not NULL ( ''{}''::text[] does not do this)
1150
';
1151

    
1152

    
1153
--
1154
-- Name: ensure_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1155
--
1156

    
1157
CREATE FUNCTION ensure_prefix(prefix text, str text) RETURNS text
1158
    LANGUAGE sql IMMUTABLE
1159
    AS $_$
1160
SELECT (CASE WHEN util.has_prefix($1, $2) THEN $2 ELSE $1||$2 END)
1161
$_$;
1162

    
1163

    
1164
--
1165
-- Name: esc_name__append(text, text); Type: FUNCTION; Schema: util; Owner: -
1166
--
1167

    
1168
CREATE FUNCTION esc_name__append(suffix text, esc_name text) RETURNS text
1169
    LANGUAGE sql IMMUTABLE
1170
    AS $_$
1171
SELECT regexp_replace($2, '("?)$', $1||'\1')
1172
$_$;
1173

    
1174

    
1175
--
1176
-- Name: eval(text); Type: FUNCTION; Schema: util; Owner: -
1177
--
1178

    
1179
CREATE FUNCTION eval(sql text) RETURNS void
1180
    LANGUAGE plpgsql STRICT
1181
    AS $$
1182
BEGIN
1183
	PERFORM util.debug_print_sql(sql);
1184
	EXECUTE sql;
1185
END;
1186
$$;
1187

    
1188

    
1189
--
1190
-- Name: eval2col_pair(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1191
--
1192

    
1193
CREATE FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
1194
    LANGUAGE plpgsql
1195
    AS $$
1196
BEGIN
1197
	PERFORM util.debug_print_sql(sql);
1198
	RETURN QUERY EXECUTE sql;
1199
END;
1200
$$;
1201

    
1202

    
1203
--
1204
-- Name: FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
1205
--
1206

    
1207
COMMENT ON FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
1208
col_type_null (*required*): NULL::col_type
1209
';
1210

    
1211

    
1212
--
1213
-- Name: eval2records(text); Type: FUNCTION; Schema: util; Owner: -
1214
--
1215

    
1216
CREATE FUNCTION eval2records(sql text) RETURNS SETOF record
1217
    LANGUAGE plpgsql
1218
    AS $$
1219
BEGIN
1220
	PERFORM util.debug_print_sql(sql);
1221
	RETURN QUERY EXECUTE sql;
1222
END;
1223
$$;
1224

    
1225

    
1226
--
1227
-- Name: eval2set(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1228
--
1229

    
1230
CREATE FUNCTION eval2set(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS SETOF anyelement
1231
    LANGUAGE plpgsql
1232
    AS $$
1233
BEGIN
1234
	PERFORM util.debug_print_sql(sql);
1235
	RETURN QUERY EXECUTE sql;
1236
END;
1237
$$;
1238

    
1239

    
1240
--
1241
-- Name: FUNCTION eval2set(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1242
--
1243

    
1244
COMMENT ON FUNCTION eval2set(sql text, ret_type_null anyelement) IS '
1245
ret_type_null: NULL::ret_type
1246
';
1247

    
1248

    
1249
--
1250
-- Name: eval2val(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1251
--
1252

    
1253
CREATE FUNCTION eval2val(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1254
    LANGUAGE plpgsql
1255
    AS $$
1256
DECLARE
1257
	ret_val ret_type_null%TYPE;
1258
BEGIN
1259
	PERFORM util.debug_print_sql(sql);
1260
	EXECUTE sql INTO STRICT ret_val;
1261
	RETURN ret_val;
1262
END;
1263
$$;
1264

    
1265

    
1266
--
1267
-- Name: FUNCTION eval2val(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1268
--
1269

    
1270
COMMENT ON FUNCTION eval2val(sql text, ret_type_null anyelement) IS '
1271
ret_type_null: NULL::ret_type
1272
';
1273

    
1274

    
1275
--
1276
-- Name: eval_expr(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1277
--
1278

    
1279
CREATE FUNCTION eval_expr(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1280
    LANGUAGE sql
1281
    AS $_$
1282
SELECT util.eval2val($$SELECT $$||$1, $2)
1283
$_$;
1284

    
1285

    
1286
--
1287
-- Name: FUNCTION eval_expr(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1288
--
1289

    
1290
COMMENT ON FUNCTION eval_expr(sql text, ret_type_null anyelement) IS '
1291
ret_type_null: NULL::ret_type
1292
';
1293

    
1294

    
1295
--
1296
-- Name: eval_expr_passthru(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1297
--
1298

    
1299
CREATE FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1300
    LANGUAGE sql
1301
    AS $_$
1302
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE util.eval_expr($1, $2) END
1303
$_$;
1304

    
1305

    
1306
--
1307
-- Name: FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1308
--
1309

    
1310
COMMENT ON FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement) IS '
1311
sql: can be NULL, which will be passed through
1312
ret_type_null: NULL::ret_type
1313
';
1314

    
1315

    
1316
--
1317
-- Name: existing_cols(regclass, text[]); Type: FUNCTION; Schema: util; Owner: -
1318
--
1319

    
1320
CREATE FUNCTION existing_cols(table_ regclass, VARIADIC col_names text[]) RETURNS SETOF text
1321
    LANGUAGE sql STABLE STRICT
1322
    AS $_$
1323
SELECT col_name
1324
FROM unnest($2) s (col_name)
1325
WHERE util.col_exists(($1, col_name))
1326
$_$;
1327

    
1328

    
1329
--
1330
-- Name: explain(text); Type: FUNCTION; Schema: util; Owner: -
1331
--
1332

    
1333
CREATE FUNCTION explain(sql text) RETURNS SETOF text
1334
    LANGUAGE sql
1335
    AS $_$
1336
SELECT util.eval2set($$EXPLAIN $$||$1)
1337
$_$;
1338

    
1339

    
1340
--
1341
-- Name: explain2notice(text); Type: FUNCTION; Schema: util; Owner: -
1342
--
1343

    
1344
CREATE FUNCTION explain2notice(sql text) RETURNS void
1345
    LANGUAGE plpgsql
1346
    AS $_$
1347
BEGIN
1348
	RAISE NOTICE '%', $$EXPLAIN:
1349
$$||util.explain2str(sql);
1350
END;
1351
$_$;
1352

    
1353

    
1354
--
1355
-- Name: explain2str(text); Type: FUNCTION; Schema: util; Owner: -
1356
--
1357

    
1358
CREATE FUNCTION explain2str(sql text) RETURNS text
1359
    LANGUAGE sql
1360
    AS $_$
1361
SELECT util.join_strs(explain, $$
1362
$$) FROM util.explain($1)
1363
$_$;
1364

    
1365

    
1366
SET default_tablespace = '';
1367

    
1368
SET default_with_oids = false;
1369

    
1370
--
1371
-- Name: explain; Type: TABLE; Schema: util; Owner: -; Tablespace: 
1372
--
1373

    
1374
CREATE TABLE explain (
1375
    line text NOT NULL
1376
);
1377

    
1378

    
1379
--
1380
-- Name: explain2table(text, regclass); Type: FUNCTION; Schema: util; Owner: -
1381
--
1382

    
1383
CREATE FUNCTION explain2table(sql text, table_ regclass DEFAULT 'explain'::regclass) RETURNS void
1384
    LANGUAGE sql
1385
    AS $_$
1386
SELECT util.eval($$INSERT INTO $$||$2||$$ SELECT util.explain(
1387
$$||quote_nullable($1)||$$
1388
)$$)
1389
$_$;
1390

    
1391

    
1392
--
1393
-- Name: FUNCTION explain2table(sql text, table_ regclass); Type: COMMENT; Schema: util; Owner: -
1394
--
1395

    
1396
COMMENT ON FUNCTION explain2table(sql text, table_ regclass) IS '
1397
usage:
1398
PERFORM util.explain2table($$
1399
query
1400
$$);
1401
';
1402

    
1403

    
1404
--
1405
-- Name: fix_array(anyarray); Type: FUNCTION; Schema: util; Owner: -
1406
--
1407

    
1408
CREATE FUNCTION fix_array("array" anyarray) RETURNS anyarray
1409
    LANGUAGE sql IMMUTABLE
1410
    AS $_$
1411
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE (
1412
	CASE WHEN pg_catalog.array_ndims($1) IS NULL THEN util.empty_array($1[1]) ELSE $1 END
1413
) END
1414
$_$;
1415

    
1416

    
1417
--
1418
-- Name: FUNCTION fix_array("array" anyarray); Type: COMMENT; Schema: util; Owner: -
1419
--
1420

    
1421
COMMENT ON FUNCTION fix_array("array" anyarray) IS '
1422
ensures that an array will always have proper non-NULL dimensions
1423
';
1424

    
1425

    
1426
--
1427
-- Name: force_recreate(text, text[]); Type: FUNCTION; Schema: util; Owner: -
1428
--
1429

    
1430
CREATE FUNCTION force_recreate(cmd text, users text[] DEFAULT NULL::text[]) RETURNS void
1431
    LANGUAGE plpgsql
1432
    AS $_$
1433
DECLARE
1434
	PG_EXCEPTION_DETAIL text;
1435
	recreate_users_cmd text = util.save_drop_views(users);
1436
BEGIN
1437
	PERFORM util.eval(cmd);
1438
	PERFORM util.eval(recreate_users_cmd);
1439
EXCEPTION
1440
WHEN dependent_objects_still_exist THEN
1441
	IF users IS NOT NULL THEN RAISE; END IF; -- save_drop_views() didn't fix it
1442
	GET STACKED DIAGNOSTICS PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL;
1443
	users = array(SELECT * FROM util.regexp_matches_group(
1444
		PG_EXCEPTION_DETAIL, '(?m)^view (.*) depends on table .*$'));
1445
	IF util.is_empty(users) THEN RAISE; END IF;
1446
	PERFORM util.force_recreate(cmd, users);
1447
END;
1448
$_$;
1449

    
1450

    
1451
--
1452
-- Name: FUNCTION force_recreate(cmd text, users text[]); Type: COMMENT; Schema: util; Owner: -
1453
--
1454

    
1455
COMMENT ON FUNCTION force_recreate(cmd text, users text[]) IS '
1456
idempotent
1457

    
1458
users: not necessary to provide this because it will be autopopulated
1459
';
1460

    
1461

    
1462
--
1463
-- Name: force_update_view(text, text); Type: FUNCTION; Schema: util; Owner: -
1464
--
1465

    
1466
CREATE FUNCTION force_update_view(view_ text, query text) RETURNS void
1467
    LANGUAGE plpgsql STRICT
1468
    AS $_$
1469
DECLARE
1470
	mk_view text = $$CREATE OR REPLACE VIEW $$||view_||$$ AS
1471
$$||query;
1472
BEGIN
1473
	EXECUTE mk_view;
1474
EXCEPTION
1475
WHEN invalid_table_definition THEN
1476
	IF SQLERRM = 'cannot drop columns from view'
1477
	OR SQLERRM LIKE 'cannot change name of view column "%" to "%"'
1478
	THEN
1479
		EXECUTE $$DROP VIEW $$||view_||$$ CASCADE$$;
1480
		EXECUTE mk_view;
1481
	ELSE RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
1482
	END IF;
1483
END;
1484
$_$;
1485

    
1486

    
1487
--
1488
-- Name: FUNCTION force_update_view(view_ text, query text); Type: COMMENT; Schema: util; Owner: -
1489
--
1490

    
1491
COMMENT ON FUNCTION force_update_view(view_ text, query text) IS '
1492
idempotent
1493
';
1494

    
1495

    
1496
--
1497
-- Name: grants_users(); Type: FUNCTION; Schema: util; Owner: -
1498
--
1499

    
1500
CREATE FUNCTION grants_users() RETURNS SETOF text
1501
    LANGUAGE sql IMMUTABLE
1502
    AS $$
1503
VALUES ('bien_read'), ('public_')
1504
$$;
1505

    
1506

    
1507
--
1508
-- Name: has_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1509
--
1510

    
1511
CREATE FUNCTION has_prefix(prefix text, str text) RETURNS boolean
1512
    LANGUAGE sql IMMUTABLE
1513
    AS $_$
1514
SELECT substring($2 for length($1)) = $1
1515
$_$;
1516

    
1517

    
1518
--
1519
-- Name: has_single_row(regclass); Type: FUNCTION; Schema: util; Owner: -
1520
--
1521

    
1522
CREATE FUNCTION has_single_row(table_ regclass) RETURNS boolean
1523
    LANGUAGE sql STABLE
1524
    AS $_$
1525
SELECT util.eval2val($$SELECT COUNT(*) = 1 FROM $$||$1, NULL::boolean)
1526
$_$;
1527

    
1528

    
1529
--
1530
-- Name: hstore(text[], text); Type: FUNCTION; Schema: util; Owner: -
1531
--
1532

    
1533
CREATE FUNCTION hstore(keys text[], value text) RETURNS hstore
1534
    LANGUAGE sql IMMUTABLE
1535
    AS $_$
1536
SELECT hstore(util.fix_array($1), util.array_fill($2, util.array_length($1)))
1537
$_$;
1538

    
1539

    
1540
--
1541
-- Name: FUNCTION hstore(keys text[], value text); Type: COMMENT; Schema: util; Owner: -
1542
--
1543

    
1544
COMMENT ON FUNCTION hstore(keys text[], value text) IS '
1545
avoids repeating the same value for each key
1546
';
1547

    
1548

    
1549
--
1550
-- Name: ifnull(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
1551
--
1552

    
1553
CREATE FUNCTION ifnull(value anyelement, null_ anyelement) RETURNS anyelement
1554
    LANGUAGE sql IMMUTABLE
1555
    AS $_$
1556
SELECT COALESCE($1, $2)
1557
$_$;
1558

    
1559

    
1560
--
1561
-- Name: FUNCTION ifnull(value anyelement, null_ anyelement); Type: COMMENT; Schema: util; Owner: -
1562
--
1563

    
1564
COMMENT ON FUNCTION ifnull(value anyelement, null_ anyelement) IS '
1565
equivalent to MySQL''s IFNULL() (Postgres auto-lowercases the name)
1566
';
1567

    
1568

    
1569
--
1570
-- Name: inherit(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
1571
--
1572

    
1573
CREATE FUNCTION inherit(derived regclass, base regclass) RETURNS void
1574
    LANGUAGE sql
1575
    AS $_$
1576
SELECT util.eval($$ALTER TABLE $$||$1||$$ INHERIT $$||$2)
1577
$_$;
1578

    
1579

    
1580
--
1581
-- Name: is_constant(col_ref); Type: FUNCTION; Schema: util; Owner: -
1582
--
1583

    
1584
CREATE FUNCTION is_constant(col col_ref) RETURNS boolean
1585
    LANGUAGE sql STABLE STRICT
1586
    AS $_$
1587
SELECT COALESCE(util.col_comment($1) LIKE 'constant%', false)
1588
$_$;
1589

    
1590

    
1591
--
1592
-- Name: is_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
1593
--
1594

    
1595
CREATE FUNCTION is_empty("array" anyarray) RETURNS boolean
1596
    LANGUAGE sql IMMUTABLE
1597
    AS $_$
1598
SELECT util.array_length($1) = 0
1599
$_$;
1600

    
1601

    
1602
--
1603
-- Name: is_more_complete_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
1604
--
1605

    
1606
CREATE FUNCTION is_more_complete_than("left" anyelement, "right" anyelement) RETURNS boolean
1607
    LANGUAGE sql IMMUTABLE
1608
    AS $_$
1609
SELECT $1 IS NOT DISTINCT FROM $2 OR ($1 IS NOT NULL AND $2 IS NULL)
1610
$_$;
1611

    
1612

    
1613
--
1614
-- Name: is_populated_more_often_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
1615
--
1616

    
1617
CREATE FUNCTION is_populated_more_often_than("left" anyelement, "right" anyelement) RETURNS boolean
1618
    LANGUAGE sql IMMUTABLE
1619
    AS $_$
1620
SELECT $1 IS NOT NULL >= $2 IS NOT NULL -- true > false
1621
$_$;
1622

    
1623

    
1624
--
1625
-- Name: is_table(regclass); Type: FUNCTION; Schema: util; Owner: -
1626
--
1627

    
1628
CREATE FUNCTION is_table(relation regclass) RETURNS boolean
1629
    LANGUAGE sql STABLE
1630
    AS $_$
1631
SELECT relkind = 'r' FROM pg_class WHERE oid = $1
1632
$_$;
1633

    
1634

    
1635
--
1636
-- Name: is_view(regclass); Type: FUNCTION; Schema: util; Owner: -
1637
--
1638

    
1639
CREATE FUNCTION is_view(relation regclass) RETURNS boolean
1640
    LANGUAGE sql STABLE
1641
    AS $_$
1642
SELECT relkind = 'v' FROM pg_class WHERE oid = $1
1643
$_$;
1644

    
1645

    
1646
--
1647
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: util; Owner: -
1648
--
1649

    
1650
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
1651
    LANGUAGE sql IMMUTABLE STRICT
1652
    AS $_$
1653
SELECT $1 || $3 || $2
1654
$_$;
1655

    
1656

    
1657
--
1658
-- Name: limit2row_num(integer, integer, integer); Type: FUNCTION; Schema: util; Owner: -
1659
--
1660

    
1661
CREATE FUNCTION limit2row_num(limit_ integer, offset_ integer DEFAULT NULL::integer, min_row_num integer DEFAULT 1) RETURNS integer
1662
    LANGUAGE sql IMMUTABLE
1663
    AS $_$
1664
SELECT COALESCE(util.offset2row_num($2, $3) + $1 - 1, 2147483647)
1665
$_$;
1666

    
1667

    
1668
--
1669
-- Name: ltrim_nl(text); Type: FUNCTION; Schema: util; Owner: -
1670
--
1671

    
1672
CREATE FUNCTION ltrim_nl(str text) RETURNS text
1673
    LANGUAGE sql IMMUTABLE
1674
    AS $_$
1675
SELECT ltrim($1, $$
1676
$$)
1677
$_$;
1678

    
1679

    
1680
--
1681
-- Name: map_filter_insert(); Type: FUNCTION; Schema: util; Owner: -
1682
--
1683

    
1684
CREATE FUNCTION map_filter_insert() RETURNS trigger
1685
    LANGUAGE plpgsql
1686
    AS $$
1687
BEGIN
1688
	IF new."from" LIKE ':%' THEN RETURN NULL; END IF; -- exclude metadata values
1689
	RETURN new;
1690
END;
1691
$$;
1692

    
1693

    
1694
--
1695
-- Name: map_get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1696
--
1697

    
1698
CREATE FUNCTION map_get(map regclass, key text) RETURNS text
1699
    LANGUAGE plpgsql STABLE STRICT
1700
    AS $_$
1701
DECLARE
1702
    value text;
1703
BEGIN
1704
    EXECUTE $$SELECT "to" FROM $$||map||$$ WHERE "from" = $1$$
1705
        INTO value USING key;
1706
    RETURN value;
1707
END;
1708
$_$;
1709

    
1710

    
1711
--
1712
-- Name: map_nulls(text[], anyelement); Type: FUNCTION; Schema: util; Owner: -
1713
--
1714

    
1715
CREATE FUNCTION map_nulls(nulls text[], value anyelement) RETURNS anyelement
1716
    LANGUAGE sql IMMUTABLE
1717
    AS $_$
1718
SELECT util._map(util.nulls_map($1), $2)
1719
$_$;
1720

    
1721

    
1722
--
1723
-- Name: FUNCTION map_nulls(nulls text[], value anyelement); Type: COMMENT; Schema: util; Owner: -
1724
--
1725

    
1726
COMMENT ON FUNCTION map_nulls(nulls text[], value anyelement) IS '
1727
due to dynamic inlining[1], this is just as fast as util._map() which it wraps[2].
1728

    
1729
[1] inlining of function calls, which is different from constant folding
1730
[2] _map()''s profiling query
1731
SELECT util._map(''"1"=>NULL, "2"=>NULL, "3"=>NULL, *=>*'', v) FROM unnest(array_fill(1, array[100000])) f (v)
1732
and map_nulls()''s profiling query
1733
SELECT util.map_nulls(array[1, 2, 3]::text[], v) FROM unnest(array_fill(1, array[100000])) f (v)
1734
both take ~920 ms.
1735
also, /inputs/REMIB/Specimen/postprocess.sql > country takes the same amount of time (56000 ms) to build with map_nulls() as with a literal hstore.
1736
';
1737

    
1738

    
1739
--
1740
-- Name: map_values(regclass); Type: FUNCTION; Schema: util; Owner: -
1741
--
1742

    
1743
CREATE FUNCTION map_values(map regclass) RETURNS SETOF text
1744
    LANGUAGE plpgsql STABLE STRICT
1745
    AS $_$
1746
BEGIN
1747
    RETURN QUERY EXECUTE $$SELECT "to" FROM $$||map;
1748
END;
1749
$_$;
1750

    
1751

    
1752
--
1753
-- Name: materialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
1754
--
1755

    
1756
CREATE FUNCTION materialize_query(table_esc text, sql text) RETURNS void
1757
    LANGUAGE sql
1758
    AS $_$
1759
SELECT util.create_if_not_exists($$CREATE TABLE $$||$1||$$ AS
1760
$$||util.ltrim_nl($2));
1761
-- make sure the created table has the correct estimated row count
1762
SELECT util.analyze_($1);
1763
$_$;
1764

    
1765

    
1766
--
1767
-- Name: FUNCTION materialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
1768
--
1769

    
1770
COMMENT ON FUNCTION materialize_query(table_esc text, sql text) IS '
1771
idempotent
1772
';
1773

    
1774

    
1775
--
1776
-- Name: materialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
1777
--
1778

    
1779
CREATE FUNCTION materialize_view(table_esc text, view_ regclass) RETURNS void
1780
    LANGUAGE sql
1781
    AS $_$
1782
SELECT util.materialize_query($1, $$SELECT * FROM $$||$2)
1783
$_$;
1784

    
1785

    
1786
--
1787
-- Name: FUNCTION materialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
1788
--
1789

    
1790
COMMENT ON FUNCTION materialize_view(table_esc text, view_ regclass) IS '
1791
idempotent
1792
';
1793

    
1794

    
1795
--
1796
-- Name: mk_const_col(col_ref, anyelement); Type: FUNCTION; Schema: util; Owner: -
1797
--
1798

    
1799
CREATE FUNCTION mk_const_col(col col_ref, value anyelement) RETURNS void
1800
    LANGUAGE sql STRICT
1801
    AS $_$
1802
SELECT util.create_if_not_exists($$
1803
ALTER TABLE $$||$1.table_||$$ ADD COLUMN $$
1804
||quote_ident($1.name)||$$ $$||pg_typeof($2)||util.type_qual($2)||$$ DEFAULT $$
1805
||quote_literal($2)||$$;
1806
COMMENT ON COLUMN $$||$1.table_||$$.$$||quote_ident($1.name)||$$ IS '
1807
constant
1808
';
1809
$$)
1810
$_$;
1811

    
1812

    
1813
--
1814
-- Name: FUNCTION mk_const_col(col col_ref, value anyelement); Type: COMMENT; Schema: util; Owner: -
1815
--
1816

    
1817
COMMENT ON FUNCTION mk_const_col(col col_ref, value anyelement) IS '
1818
idempotent
1819
';
1820

    
1821

    
1822
--
1823
-- Name: mk_derived_col(col_ref, text, boolean); Type: FUNCTION; Schema: util; Owner: -
1824
--
1825

    
1826
CREATE FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean DEFAULT false) RETURNS void
1827
    LANGUAGE plpgsql STRICT
1828
    AS $_$
1829
DECLARE
1830
    type regtype = util.typeof(expr, col.table_::text::regtype);
1831
    col_name_sql text = quote_ident(col.name);
1832
BEGIN
1833
    PERFORM util.create_if_not_exists((CASE WHEN overwrite THEN '' ELSE $$
1834
ALTER TABLE $$||col.table_||$$ ADD   COLUMN $$||col_name_sql||$$      $$||type||$$;$$ END)||$$
1835
ALTER TABLE $$||col.table_||$$ ALTER COLUMN $$||col_name_sql||$$ TYPE $$||type||$$ USING
1836
$$||expr||$$;
1837
$$);
1838
END;
1839
$_$;
1840

    
1841

    
1842
--
1843
-- Name: FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean); Type: COMMENT; Schema: util; Owner: -
1844
--
1845

    
1846
COMMENT ON FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean) IS '
1847
idempotent
1848
';
1849

    
1850

    
1851
--
1852
-- Name: mk_map_table(text); Type: FUNCTION; Schema: util; Owner: -
1853
--
1854

    
1855
CREATE FUNCTION mk_map_table(table_ text) RETURNS void
1856
    LANGUAGE sql STRICT
1857
    AS $_$
1858
SELECT util.create_if_not_exists($$
1859
CREATE TABLE $$||$1||$$
1860
(
1861
    LIKE util.map INCLUDING ALL
1862
);
1863

    
1864
CREATE TRIGGER map_filter_insert
1865
  BEFORE INSERT
1866
  ON $$||$1||$$
1867
  FOR EACH ROW
1868
  EXECUTE PROCEDURE util.map_filter_insert();
1869
$$)
1870
$_$;
1871

    
1872

    
1873
--
1874
-- Name: mk_search_path(text[]); Type: FUNCTION; Schema: util; Owner: -
1875
--
1876

    
1877
CREATE FUNCTION mk_search_path(VARIADIC schemas text[]) RETURNS text
1878
    LANGUAGE sql IMMUTABLE
1879
    AS $_$
1880
SELECT string_agg(quote_ident(unnest), ', ') FROM unnest($1)
1881
$_$;
1882

    
1883

    
1884
--
1885
-- Name: mk_set_search_path(text[]); Type: FUNCTION; Schema: util; Owner: -
1886
--
1887

    
1888
CREATE FUNCTION mk_set_search_path(VARIADIC schemas text[]) RETURNS text
1889
    LANGUAGE sql IMMUTABLE
1890
    AS $_$
1891
SELECT util.mk_set_search_path(util.mk_search_path(VARIADIC $1))
1892
$_$;
1893

    
1894

    
1895
--
1896
-- Name: FUNCTION mk_set_search_path(VARIADIC schemas text[]); Type: COMMENT; Schema: util; Owner: -
1897
--
1898

    
1899
COMMENT ON FUNCTION mk_set_search_path(VARIADIC schemas text[]) IS '
1900
usage:
1901
for *1* schema arg:
1902
SELECT util.mk_set_search_path(''schema'', NULL) AS search_path;
1903
	-- 2 params are needed to use the correct variant of mk_set_search_path()
1904
';
1905

    
1906

    
1907
--
1908
-- Name: mk_set_search_path(text); Type: FUNCTION; Schema: util; Owner: -
1909
--
1910

    
1911
CREATE FUNCTION mk_set_search_path(search_path text) RETURNS text
1912
    LANGUAGE sql IMMUTABLE
1913
    AS $_$
1914
SELECT $$SET LOCAL search_path TO $$||$1
1915
$_$;
1916

    
1917

    
1918
--
1919
-- Name: mk_source_col(regclass); Type: FUNCTION; Schema: util; Owner: -
1920
--
1921

    
1922
CREATE FUNCTION mk_source_col(table_ regclass) RETURNS void
1923
    LANGUAGE sql STRICT
1924
    AS $_$
1925
SELECT util.mk_const_col(($1, 'source'), util.schema($1))
1926
$_$;
1927

    
1928

    
1929
--
1930
-- Name: FUNCTION mk_source_col(table_ regclass); Type: COMMENT; Schema: util; Owner: -
1931
--
1932

    
1933
COMMENT ON FUNCTION mk_source_col(table_ regclass) IS '
1934
idempotent
1935
';
1936

    
1937

    
1938
--
1939
-- Name: mk_subset_by_row_num_func(regclass); Type: FUNCTION; Schema: util; Owner: -
1940
--
1941

    
1942
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass) RETURNS void
1943
    LANGUAGE plpgsql STRICT
1944
    AS $_$
1945
DECLARE
1946
	view_qual_name text = util.qual_name(view_);
1947
BEGIN
1948
	EXECUTE $$
1949
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
1950
  RETURNS SETOF $$||view_||$$ AS
1951
$BODY1$
1952
SELECT * FROM $$||view_qual_name||$$
1953
ORDER BY sort_col
1954
LIMIT $1 OFFSET $2
1955
$BODY1$
1956
  LANGUAGE sql STABLE
1957
  COST 100
1958
  ROWS 1000
1959
$$;
1960
	
1961
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
1962
END;
1963
$_$;
1964

    
1965

    
1966
--
1967
-- Name: mk_subset_by_row_num_func(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1968
--
1969

    
1970
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass, row_num_col text) RETURNS void
1971
    LANGUAGE plpgsql STRICT
1972
    AS $_$
1973
DECLARE
1974
	view_qual_name text = util.qual_name(view_);
1975
	row_num__min__fn text = util.esc_name__append('__row_num__min', view_qual_name);
1976
BEGIN
1977
	EXECUTE $$
1978
CREATE OR REPLACE FUNCTION $$||row_num__min__fn||$$()
1979
  RETURNS integer AS
1980
$BODY1$
1981
SELECT $$||quote_ident(row_num_col)||$$
1982
FROM $$||view_qual_name||$$
1983
ORDER BY $$||quote_ident(row_num_col)||$$ ASC
1984
LIMIT 1
1985
$BODY1$
1986
  LANGUAGE sql STABLE
1987
  COST 100;
1988
$$;
1989
	
1990
	EXECUTE $$
1991
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
1992
  RETURNS SETOF $$||view_||$$ AS
1993
$BODY1$
1994
SELECT * FROM $$||view_qual_name||$$
1995
WHERE $$||quote_ident(row_num_col)||$$ BETWEEN
1996
	util.offset2row_num(    $2, $$||row_num__min__fn||$$())
1997
AND util.limit2row_num ($1, $2, $$||row_num__min__fn||$$())
1998
ORDER BY $$||quote_ident(row_num_col)||$$
1999
$BODY1$
2000
  LANGUAGE sql STABLE
2001
  COST 100
2002
  ROWS 1000
2003
$$;
2004
	
2005
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
2006
END;
2007
$_$;
2008

    
2009

    
2010
--
2011
-- Name: mk_subset_by_row_num_no_sort_func(regclass); Type: FUNCTION; Schema: util; Owner: -
2012
--
2013

    
2014
CREATE FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) RETURNS void
2015
    LANGUAGE plpgsql STRICT
2016
    AS $_$
2017
DECLARE
2018
	view_qual_name text = util.qual_name(view_);
2019
BEGIN
2020
	EXECUTE $$
2021
CREATE OR REPLACE FUNCTION $$||view_||$$(no_sort boolean, limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2022
  RETURNS SETOF $$||view_||$$
2023
  SET enable_sort TO 'off'
2024
  AS
2025
$BODY1$
2026
SELECT * FROM $$||view_qual_name||$$($2, $3)
2027
$BODY1$
2028
  LANGUAGE sql STABLE
2029
  COST 100
2030
  ROWS 1000
2031
;
2032
COMMENT ON FUNCTION $$||view_||$$(no_sort boolean, limit_ integer, offset_ integer) IS '
2033
Use this for limit values greater than ~100,000 to avoid unwanted slow sorts.
2034
If you want to run EXPLAIN and get expanded output, use the regular subset
2035
function instead. (When a config param is set on a function, EXPLAIN produces
2036
just a function scan.)
2037
';
2038
$$;
2039
END;
2040
$_$;
2041

    
2042

    
2043
--
2044
-- Name: FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass); Type: COMMENT; Schema: util; Owner: -
2045
--
2046

    
2047
COMMENT ON FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) IS '
2048
creates subset function which turns off enable_sort
2049
';
2050

    
2051

    
2052
--
2053
-- Name: mk_use_own_schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
2054
--
2055

    
2056
CREATE FUNCTION mk_use_own_schema(schema_anchor anyelement) RETURNS text
2057
    LANGUAGE sql IMMUTABLE
2058
    AS $_$
2059
SELECT util.mk_set_search_path(util.schema_esc($1))
2060
$_$;
2061

    
2062

    
2063
--
2064
-- Name: name(regclass); Type: FUNCTION; Schema: util; Owner: -
2065
--
2066

    
2067
CREATE FUNCTION name(table_ regclass) RETURNS text
2068
    LANGUAGE sql STABLE
2069
    AS $_$
2070
SELECT relname::text FROM pg_class WHERE oid = $1
2071
$_$;
2072

    
2073

    
2074
--
2075
-- Name: name(regtype); Type: FUNCTION; Schema: util; Owner: -
2076
--
2077

    
2078
CREATE FUNCTION name(type regtype) RETURNS text
2079
    LANGUAGE sql STABLE STRICT
2080
    AS $_$
2081
SELECT typname::text FROM pg_type WHERE oid = $1
2082
$_$;
2083

    
2084

    
2085
--
2086
-- Name: name_was_truncated(text, integer); Type: FUNCTION; Schema: util; Owner: -
2087
--
2088

    
2089
CREATE FUNCTION name_was_truncated(name_ text, max_prefix_len integer DEFAULT 0) RETURNS boolean
2090
    LANGUAGE sql IMMUTABLE
2091
    AS $_$
2092
SELECT octet_length($1) >= util.namedatalen() - $2
2093
$_$;
2094

    
2095

    
2096
--
2097
-- Name: namedatalen(); Type: FUNCTION; Schema: util; Owner: -
2098
--
2099

    
2100
CREATE FUNCTION namedatalen() RETURNS integer
2101
    LANGUAGE sql IMMUTABLE
2102
    AS $$
2103
SELECT octet_length(repeat('_', 1024/*>63*/)::name::text)
2104
$$;
2105

    
2106

    
2107
--
2108
-- Name: not_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
2109
--
2110

    
2111
CREATE FUNCTION not_empty(value anyarray) RETURNS boolean
2112
    LANGUAGE sql IMMUTABLE
2113
    AS $_$
2114
SELECT $1 IS NOT NULL AND util.array_length($1) > 0
2115
$_$;
2116

    
2117

    
2118
--
2119
-- Name: not_null(anyelement); Type: FUNCTION; Schema: util; Owner: -
2120
--
2121

    
2122
CREATE FUNCTION not_null(value anyelement) RETURNS boolean
2123
    LANGUAGE sql IMMUTABLE
2124
    AS $_$
2125
SELECT $1 IS NOT NULL
2126
$_$;
2127

    
2128

    
2129
--
2130
-- Name: nulls_map(text[]); Type: FUNCTION; Schema: util; Owner: -
2131
--
2132

    
2133
CREATE FUNCTION nulls_map(nulls text[]) RETURNS hstore
2134
    LANGUAGE sql IMMUTABLE
2135
    AS $_$
2136
SELECT util.hstore($1, NULL) || '*=>*'
2137
$_$;
2138

    
2139

    
2140
--
2141
-- Name: FUNCTION nulls_map(nulls text[]); Type: COMMENT; Schema: util; Owner: -
2142
--
2143

    
2144
COMMENT ON FUNCTION nulls_map(nulls text[]) IS '
2145
for use with _map()
2146
';
2147

    
2148

    
2149
--
2150
-- Name: offset2row_num(integer, integer); Type: FUNCTION; Schema: util; Owner: -
2151
--
2152

    
2153
CREATE FUNCTION offset2row_num(offset_ integer, min_row_num integer DEFAULT 1) RETURNS integer
2154
    LANGUAGE sql IMMUTABLE
2155
    AS $_$
2156
SELECT $2 + COALESCE($1, 0)
2157
$_$;
2158

    
2159

    
2160
--
2161
-- Name: qual_name(text[]); Type: FUNCTION; Schema: util; Owner: -
2162
--
2163

    
2164
CREATE FUNCTION qual_name(VARIADIC elems text[]) RETURNS text
2165
    LANGUAGE sql IMMUTABLE
2166
    AS $_$
2167
SELECT string_agg(quote_ident(unnest), '.') FROM unnest($1)
2168
$_$;
2169

    
2170

    
2171
--
2172
-- Name: qual_name(regclass); Type: FUNCTION; Schema: util; Owner: -
2173
--
2174

    
2175
CREATE FUNCTION qual_name(table_ regclass) RETURNS text
2176
    LANGUAGE sql STABLE STRICT
2177
    SET search_path TO pg_temp
2178
    AS $_$
2179
SELECT $1::text
2180
$_$;
2181

    
2182

    
2183
--
2184
-- Name: qual_name(regtype); Type: FUNCTION; Schema: util; Owner: -
2185
--
2186

    
2187
CREATE FUNCTION qual_name(type regtype) RETURNS text
2188
    LANGUAGE sql STABLE STRICT
2189
    SET search_path TO pg_temp
2190
    AS $_$
2191
SELECT $1::text
2192
$_$;
2193

    
2194

    
2195
--
2196
-- Name: FUNCTION qual_name(type regtype); Type: COMMENT; Schema: util; Owner: -
2197
--
2198

    
2199
COMMENT ON FUNCTION qual_name(type regtype) IS '
2200
a type''s schema-qualified name
2201
';
2202

    
2203

    
2204
--
2205
-- Name: qual_name(unknown); Type: FUNCTION; Schema: util; Owner: -
2206
--
2207

    
2208
CREATE FUNCTION qual_name(type unknown) RETURNS text
2209
    LANGUAGE sql STABLE STRICT
2210
    AS $_$
2211
SELECT util.qual_name($1::text::regtype)
2212
$_$;
2213

    
2214

    
2215
--
2216
-- Name: raise_error_notice(text); Type: FUNCTION; Schema: util; Owner: -
2217
--
2218

    
2219
CREATE FUNCTION raise_error_notice(msg text) RETURNS void
2220
    LANGUAGE sql IMMUTABLE STRICT
2221
    AS $_$
2222
SELECT util.raise_notice('ERROR:  '||$1)
2223
$_$;
2224

    
2225

    
2226
--
2227
-- Name: raise_notice(text); Type: FUNCTION; Schema: util; Owner: -
2228
--
2229

    
2230
CREATE FUNCTION raise_notice(msg text) RETURNS void
2231
    LANGUAGE plpgsql IMMUTABLE STRICT
2232
    AS $$
2233
BEGIN
2234
	RAISE NOTICE '%', msg;
2235
END;
2236
$$;
2237

    
2238

    
2239
--
2240
-- Name: raise_undefined_column(col_ref); Type: FUNCTION; Schema: util; Owner: -
2241
--
2242

    
2243
CREATE FUNCTION raise_undefined_column(col col_ref) RETURNS text
2244
    LANGUAGE plpgsql IMMUTABLE STRICT
2245
    AS $$
2246
BEGIN
2247
	RAISE undefined_column USING MESSAGE = concat('undefined column: ', col.name);
2248
END;
2249
$$;
2250

    
2251

    
2252
--
2253
-- Name: regexp_matches_group(text, text, integer); Type: FUNCTION; Schema: util; Owner: -
2254
--
2255

    
2256
CREATE FUNCTION regexp_matches_group(str text, re text, group_ integer DEFAULT 1) RETURNS SETOF text
2257
    LANGUAGE sql IMMUTABLE
2258
    AS $_$
2259
SELECT regexp_matches[$3] FROM regexp_matches($1, $2, 'g')
2260
$_$;
2261

    
2262

    
2263
--
2264
-- Name: regexp_quote(text); Type: FUNCTION; Schema: util; Owner: -
2265
--
2266

    
2267
CREATE FUNCTION regexp_quote(str text) RETURNS text
2268
    LANGUAGE sql IMMUTABLE
2269
    AS $_$
2270
SELECT regexp_replace($1, '\W', /*\char*/'\\\&', 'g')
2271
$_$;
2272

    
2273

    
2274
--
2275
-- Name: relation_type(regclass); Type: FUNCTION; Schema: util; Owner: -
2276
--
2277

    
2278
CREATE FUNCTION relation_type(relation regclass) RETURNS text
2279
    LANGUAGE sql STABLE
2280
    AS $_$
2281
SELECT util.relation_type(util.relation_type_char($1))
2282
$_$;
2283

    
2284

    
2285
--
2286
-- Name: relation_type("char"); Type: FUNCTION; Schema: util; Owner: -
2287
--
2288

    
2289
CREATE FUNCTION relation_type(relation_type_char "char") RETURNS text
2290
    LANGUAGE sql IMMUTABLE
2291
    AS $_$
2292
SELECT 'r=>TABLE, v=>VIEW'::hstore -> $1
2293
$_$;
2294

    
2295

    
2296
--
2297
-- Name: relation_type_char(regclass); Type: FUNCTION; Schema: util; Owner: -
2298
--
2299

    
2300
CREATE FUNCTION relation_type_char(relation regclass) RETURNS "char"
2301
    LANGUAGE sql STABLE
2302
    AS $_$
2303
SELECT relkind FROM pg_class WHERE oid = $1
2304
$_$;
2305

    
2306

    
2307
--
2308
-- Name: remake_diff_table(text, regclass, regclass, text); Type: FUNCTION; Schema: util; Owner: -
2309
--
2310

    
2311
CREATE FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) RETURNS void
2312
    LANGUAGE sql
2313
    AS $_$
2314
/* can't have in_table/out_table inherit from *each other*, because inheritance
2315
also causes the rows of the parent table to be included in the child table.
2316
instead, they need to inherit from a common, empty table. */
2317
SELECT util.drop_table($4, force := true);
2318
SELECT util.copy_struct($2, $4);
2319
SELECT util.inherit($2, $4);
2320
SELECT util.inherit($3, $4);
2321

    
2322
SELECT util.rematerialize_query($1, $$
2323
SELECT * FROM util.diff(
2324
  $$||quote_nullable($2)||$$::regclass
2325
, $$||quote_nullable($3)||$$::regclass
2326
, NULL::$$||$4||$$)
2327
$$);
2328

    
2329
/* the table unfortunately cannot be *materialized* in human-readable form,
2330
because this would create column name collisions between the two sides */
2331
SELECT util.set_comment($1, '
2332
to view this table in human-readable form (with each side''s tuple column
2333
expanded to its component fields):
2334
SELECT (left_).*, (right_).* FROM '||$1||';
2335
');
2336
$_$;
2337

    
2338

    
2339
--
2340
-- Name: FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text); Type: COMMENT; Schema: util; Owner: -
2341
--
2342

    
2343
COMMENT ON FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) IS '
2344
type_table (*required*): table to create as the shared base type
2345
';
2346

    
2347

    
2348
--
2349
-- Name: rematerialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
2350
--
2351

    
2352
CREATE FUNCTION rematerialize_query(table_esc text, sql text) RETURNS void
2353
    LANGUAGE sql
2354
    AS $_$
2355
SELECT util.drop_table($1);
2356
SELECT util.materialize_query($1, $2);
2357
$_$;
2358

    
2359

    
2360
--
2361
-- Name: FUNCTION rematerialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
2362
--
2363

    
2364
COMMENT ON FUNCTION rematerialize_query(table_esc text, sql text) IS '
2365
idempotent, but repeats action each time
2366
';
2367

    
2368

    
2369
--
2370
-- Name: rematerialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
2371
--
2372

    
2373
CREATE FUNCTION rematerialize_view(table_esc text, view_ regclass) RETURNS void
2374
    LANGUAGE sql
2375
    AS $_$
2376
SELECT util.drop_table($1);
2377
SELECT util.materialize_view($1, $2);
2378
$_$;
2379

    
2380

    
2381
--
2382
-- Name: FUNCTION rematerialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
2383
--
2384

    
2385
COMMENT ON FUNCTION rematerialize_view(table_esc text, view_ regclass) IS '
2386
idempotent, but repeats action each time
2387
';
2388

    
2389

    
2390
--
2391
-- Name: rename_cols(regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
2392
--
2393

    
2394
CREATE FUNCTION rename_cols(table_ regclass, renames anyelement) RETURNS void
2395
    LANGUAGE sql STRICT
2396
    AS $_$
2397
SELECT util.try_create($$ALTER TABLE $$||$1||$$ RENAME $$
2398
||quote_ident(name)||$$ TO $$||quote_ident($2 -> name))
2399
FROM util.col_names($1::text::regtype) f (name);
2400
SELECT NULL::void; -- don't fold away functions called in previous query
2401
$_$;
2402

    
2403

    
2404
--
2405
-- Name: FUNCTION rename_cols(table_ regclass, renames anyelement); Type: COMMENT; Schema: util; Owner: -
2406
--
2407

    
2408
COMMENT ON FUNCTION rename_cols(table_ regclass, renames anyelement) IS '
2409
idempotent
2410
';
2411

    
2412

    
2413
--
2414
-- Name: rename_relation(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2415
--
2416

    
2417
CREATE FUNCTION rename_relation(from_ regclass, to_ text) RETURNS void
2418
    LANGUAGE sql
2419
    AS $_$
2420
/* use util.qual_name() instead of ::text so that the schema qualifier is always
2421
included in the debug SQL */
2422
SELECT util.rename_relation(util.qual_name($1), $2)
2423
$_$;
2424

    
2425

    
2426
--
2427
-- Name: rename_relation(text, text); Type: FUNCTION; Schema: util; Owner: -
2428
--
2429

    
2430
CREATE FUNCTION rename_relation(from_esc text, to_name text) RETURNS void
2431
    LANGUAGE sql
2432
    AS $_$
2433
/* 'ALTER TABLE can be used with views too'
2434
(http://www.postgresql.org/docs/9.3/static/sql-alterview.html) */
2435
SELECT util.eval($$ALTER TABLE IF EXISTS $$||$1||$$ RENAME TO $$
2436
||quote_ident($2))
2437
$_$;
2438

    
2439

    
2440
--
2441
-- Name: FUNCTION rename_relation(from_esc text, to_name text); Type: COMMENT; Schema: util; Owner: -
2442
--
2443

    
2444
COMMENT ON FUNCTION rename_relation(from_esc text, to_name text) IS '
2445
idempotent
2446
';
2447

    
2448

    
2449
--
2450
-- Name: replace_suffix(text, text, text, integer); Type: FUNCTION; Schema: util; Owner: -
2451
--
2452

    
2453
CREATE FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer DEFAULT 0) RETURNS text
2454
    LANGUAGE sql IMMUTABLE
2455
    AS $_$
2456
SELECT regexp_replace($1, util.truncated_prefixed_name_regexp($2, $4), '\1'||$3)
2457
$_$;
2458

    
2459

    
2460
--
2461
-- Name: FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer); Type: COMMENT; Schema: util; Owner: -
2462
--
2463

    
2464
COMMENT ON FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer) IS '
2465
max_prefix_len: when str may have been truncated (eg. as a table name) due to the prepending of a prefix, support prefixes up to this length 
2466
';
2467

    
2468

    
2469
--
2470
-- Name: reset_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
2471
--
2472

    
2473
CREATE FUNCTION reset_col_names(table_ regclass, names regclass) RETURNS void
2474
    LANGUAGE sql STRICT
2475
    AS $_$
2476
SELECT util.eval($$DELETE FROM $$||$2||$$ WHERE "from" LIKE ':%'$$);
2477
SELECT util.mk_derived_col(($2, 'to'), $$"from"$$, overwrite := true);
2478
SELECT util.set_col_names($1, $2);
2479
$_$;
2480

    
2481

    
2482
--
2483
-- Name: FUNCTION reset_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
2484
--
2485

    
2486
COMMENT ON FUNCTION reset_col_names(table_ regclass, names regclass) IS '
2487
idempotent.
2488
alters the names table, so it will need to be repopulated after running this function.
2489
';
2490

    
2491

    
2492
--
2493
-- Name: reset_map_table(text); Type: FUNCTION; Schema: util; Owner: -
2494
--
2495

    
2496
CREATE FUNCTION reset_map_table(table_ text) RETURNS void
2497
    LANGUAGE sql STRICT
2498
    AS $_$
2499
SELECT util.drop_table($1);
2500
SELECT util.mk_map_table($1);
2501
$_$;
2502

    
2503

    
2504
--
2505
-- Name: rtrim_n(text, integer); Type: FUNCTION; Schema: util; Owner: -
2506
--
2507

    
2508
CREATE FUNCTION rtrim_n(str text, count integer) RETURNS text
2509
    LANGUAGE sql IMMUTABLE
2510
    AS $_$
2511
SELECT (CASE WHEN $2 <= 0 THEN $1 ELSE left($1, -$2) END)
2512
$_$;
2513

    
2514

    
2515
--
2516
-- Name: save_drop_view(text); Type: FUNCTION; Schema: util; Owner: -
2517
--
2518

    
2519
CREATE FUNCTION save_drop_view(view_ text) RETURNS text
2520
    LANGUAGE plpgsql STRICT
2521
    AS $_$
2522
DECLARE
2523
	result text = NULL;
2524
BEGIN
2525
	BEGIN
2526
		result = util.show_create_view(view_);
2527
		PERFORM util.eval($$DROP VIEW $$||view_);
2528
	EXCEPTION
2529
		WHEN undefined_table THEN NULL;
2530
	END;
2531
	RETURN result;
2532
END;
2533
$_$;
2534

    
2535

    
2536
--
2537
-- Name: save_drop_views(text[]); Type: FUNCTION; Schema: util; Owner: -
2538
--
2539

    
2540
CREATE FUNCTION save_drop_views(views text[]) RETURNS text
2541
    LANGUAGE sql
2542
    AS $_$
2543
SELECT string_agg(util.save_drop_view(unnest), '') FROM unnest($1)
2544
$_$;
2545

    
2546

    
2547
--
2548
-- Name: schema(oid); Type: FUNCTION; Schema: util; Owner: -
2549
--
2550

    
2551
CREATE FUNCTION schema(pg_namespace_oid oid) RETURNS text
2552
    LANGUAGE sql STABLE
2553
    AS $_$
2554
SELECT nspname::text FROM pg_namespace WHERE pg_namespace.oid = $1
2555
$_$;
2556

    
2557

    
2558
--
2559
-- Name: schema(regclass); Type: FUNCTION; Schema: util; Owner: -
2560
--
2561

    
2562
CREATE FUNCTION schema(table_ regclass) RETURNS text
2563
    LANGUAGE sql STABLE
2564
    AS $_$
2565
SELECT util.schema(relnamespace) FROM pg_class WHERE oid = $1
2566
$_$;
2567

    
2568

    
2569
--
2570
-- Name: schema(regtype); Type: FUNCTION; Schema: util; Owner: -
2571
--
2572

    
2573
CREATE FUNCTION schema(type regtype) RETURNS text
2574
    LANGUAGE sql STABLE
2575
    AS $_$
2576
SELECT util.schema(typnamespace) FROM pg_type WHERE oid = $1
2577
$_$;
2578

    
2579

    
2580
--
2581
-- Name: schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
2582
--
2583

    
2584
CREATE FUNCTION schema(type_null anyelement) RETURNS text
2585
    LANGUAGE sql STABLE
2586
    AS $_$
2587
SELECT util.schema(pg_typeof($1))
2588
$_$;
2589

    
2590

    
2591
--
2592
-- Name: schema_bundle_get_schemas(text); Type: FUNCTION; Schema: util; Owner: -
2593
--
2594

    
2595
CREATE FUNCTION schema_bundle_get_schemas(schema_bundle text) RETURNS SETOF text
2596
    LANGUAGE sql STABLE
2597
    AS $_$
2598
SELECT nspname::text FROM pg_namespace WHERE nspname ~ ('^'||$1||'(?=\y|_)')
2599
$_$;
2600

    
2601

    
2602
--
2603
-- Name: FUNCTION schema_bundle_get_schemas(schema_bundle text); Type: COMMENT; Schema: util; Owner: -
2604
--
2605

    
2606
COMMENT ON FUNCTION schema_bundle_get_schemas(schema_bundle text) IS '
2607
a schema bundle is a group of schemas with a common prefix
2608
';
2609

    
2610

    
2611
--
2612
-- Name: schema_bundle_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
2613
--
2614

    
2615
CREATE FUNCTION schema_bundle_rename(old text, new text) RETURNS void
2616
    LANGUAGE sql
2617
    AS $_$
2618
SELECT util.schema_rename(old_schema,
2619
	overlay(old_schema placing new from 1 for length(old))) -- replace prefix
2620
FROM util.schema_bundle_get_schemas($1) f (old_schema);
2621
SELECT NULL::void; -- don't fold away functions called in previous query
2622
$_$;
2623

    
2624

    
2625
--
2626
-- Name: schema_bundle_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
2627
--
2628

    
2629
CREATE FUNCTION schema_bundle_replace(replace text, with_ text) RETURNS void
2630
    LANGUAGE plpgsql
2631
    AS $$
2632
BEGIN
2633
	-- don't schema_bundle_rm() the schema_bundle to keep!
2634
	IF replace = with_ THEN RETURN; END IF;
2635
	
2636
	PERFORM util.schema_bundle_rm(replace);
2637
	PERFORM util.schema_bundle_rename(with_, replace);
2638
END;
2639
$$;
2640

    
2641

    
2642
--
2643
-- Name: schema_bundle_rm(text); Type: FUNCTION; Schema: util; Owner: -
2644
--
2645

    
2646
CREATE FUNCTION schema_bundle_rm(schema_bundle text) RETURNS void
2647
    LANGUAGE sql
2648
    AS $_$
2649
SELECT util.schema_rm(schema)
2650
FROM util.schema_bundle_get_schemas($1) f (schema);
2651
SELECT NULL::void; -- don't fold away functions called in previous query
2652
$_$;
2653

    
2654

    
2655
--
2656
-- Name: schema_esc(anyelement); Type: FUNCTION; Schema: util; Owner: -
2657
--
2658

    
2659
CREATE FUNCTION schema_esc(type_null anyelement) RETURNS text
2660
    LANGUAGE sql STABLE
2661
    AS $_$
2662
SELECT quote_ident(util.schema($1))
2663
$_$;
2664

    
2665

    
2666
--
2667
-- Name: schema_matches(text, text); Type: FUNCTION; Schema: util; Owner: -
2668
--
2669

    
2670
CREATE FUNCTION schema_matches(schema text, schema_regexp text) RETURNS boolean
2671
    LANGUAGE sql IMMUTABLE
2672
    AS $_$
2673
SELECT $1 ~ $2 AND /*in userspace*/$1 !~ '^(?:information_schema|pg_.*)$'
2674
$_$;
2675

    
2676

    
2677
--
2678
-- Name: schema_oid(text); Type: FUNCTION; Schema: util; Owner: -
2679
--
2680

    
2681
CREATE FUNCTION schema_oid(schema text) RETURNS oid
2682
    LANGUAGE sql STABLE
2683
    AS $_$
2684
SELECT oid FROM pg_namespace WHERE nspname = $1
2685
$_$;
2686

    
2687

    
2688
--
2689
-- Name: schema_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
2690
--
2691

    
2692
CREATE FUNCTION schema_rename(old text, new text) RETURNS void
2693
    LANGUAGE sql
2694
    AS $_$
2695
SELECT util.eval($$ALTER SCHEMA $$||quote_ident($1)||$$ RENAME TO $$||quote_ident($2));
2696
$_$;
2697

    
2698

    
2699
--
2700
-- Name: schema_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
2701
--
2702

    
2703
CREATE FUNCTION schema_replace(replace text, with_ text) RETURNS void
2704
    LANGUAGE plpgsql
2705
    AS $$
2706
BEGIN
2707
	-- don't schema_rm() the schema to keep!
2708
	IF replace = with_ THEN RETURN; END IF;
2709
	
2710
	PERFORM util.schema_rm(replace);
2711
	PERFORM util.schema_rename(with_, replace);
2712
END;
2713
$$;
2714

    
2715

    
2716
--
2717
-- Name: schema_rm(text); Type: FUNCTION; Schema: util; Owner: -
2718
--
2719

    
2720
CREATE FUNCTION schema_rm(schema text) RETURNS void
2721
    LANGUAGE sql
2722
    AS $_$
2723
SELECT util.eval($$DROP SCHEMA IF EXISTS $$||quote_ident($1)||$$ CASCADE$$);
2724
$_$;
2725

    
2726

    
2727
--
2728
-- Name: search_path_append(text); Type: FUNCTION; Schema: util; Owner: -
2729
--
2730

    
2731
CREATE FUNCTION search_path_append(schemas text) RETURNS void
2732
    LANGUAGE sql STRICT
2733
    AS $_$
2734
SELECT util.eval(
2735
$$SET search_path TO $$||current_setting('search_path')||$$, $$||$1);
2736
$_$;
2737

    
2738

    
2739
--
2740
-- Name: set_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
2741
--
2742

    
2743
CREATE FUNCTION set_col_names(table_ regclass, names regclass) RETURNS void
2744
    LANGUAGE plpgsql STRICT
2745
    AS $_$
2746
DECLARE
2747
    old text[] = ARRAY(SELECT util.col_names(table_));
2748
    new text[] = ARRAY(SELECT util.map_values(names));
2749
BEGIN
2750
    old = old[1:array_length(new, 1)]; -- truncate to same length
2751
    PERFORM util.eval($$ALTER TABLE $$||$1||$$ RENAME $$||quote_ident(key)
2752
||$$ TO $$||quote_ident(value))
2753
    FROM each(hstore(old, new))
2754
    WHERE value != key -- not same name
2755
    ;
2756
END;
2757
$_$;
2758

    
2759

    
2760
--
2761
-- Name: FUNCTION set_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
2762
--
2763

    
2764
COMMENT ON FUNCTION set_col_names(table_ regclass, names regclass) IS '
2765
idempotent
2766
';
2767

    
2768

    
2769
--
2770
-- Name: set_col_names_with_metadata(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
2771
--
2772

    
2773
CREATE FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) RETURNS void
2774
    LANGUAGE plpgsql STRICT
2775
    AS $_$
2776
DECLARE
2777
	row_ util.map;
2778
BEGIN
2779
	-- rename any metadata cols rather than re-adding them with new names
2780
	BEGIN
2781
		PERFORM util.set_col_names(table_, names);
2782
	EXCEPTION
2783
		WHEN array_subscript_error THEN -- selective suppress
2784
			IF SQLERRM LIKE 'arrays must have same bounds' THEN NULL;
2785
				-- metadata cols not yet added
2786
			ELSE RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM; -- rethrow
2787
			END IF;
2788
	END;
2789
	
2790
	FOR row_ IN EXECUTE $$SELECT * FROM $$||names||$$ WHERE "from" LIKE ':%'$$
2791
	LOOP
2792
		PERFORM util.mk_const_col((table_, row_."to"),
2793
			substring(row_."from" from 2));
2794
	END LOOP;
2795
	
2796
	PERFORM util.set_col_names(table_, names);
2797
END;
2798
$_$;
2799

    
2800

    
2801
--
2802
-- Name: FUNCTION set_col_names_with_metadata(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
2803
--
2804

    
2805
COMMENT ON FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) IS '
2806
idempotent.
2807
the metadata mappings must be *last* in the names table.
2808
';
2809

    
2810

    
2811
--
2812
-- Name: set_col_types(regclass, col_cast[]); Type: FUNCTION; Schema: util; Owner: -
2813
--
2814

    
2815
CREATE FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) RETURNS void
2816
    LANGUAGE plpgsql STRICT
2817
    AS $_$
2818
DECLARE
2819
    sql text = $$ALTER TABLE $$||table_||$$
2820
$$||NULLIF(array_to_string(ARRAY(
2821
    SELECT
2822
    $$ALTER COLUMN $$||col_name_sql||$$ TYPE $$||target_type
2823
    ||$$ USING $$||col_name_sql||$$::$$||target_type
2824
    FROM
2825
    (
2826
        SELECT
2827
          quote_ident(col_name) AS col_name_sql
2828
        , util.col_type((table_, col_name)) AS curr_type
2829
        , type AS target_type
2830
        FROM unnest(col_casts)
2831
    ) s
2832
    WHERE curr_type != target_type
2833
), '
2834
, '), '');
2835
BEGIN
2836
    PERFORM util.debug_print_sql(sql);
2837
    EXECUTE COALESCE(sql, '');
2838
END;
2839
$_$;
2840

    
2841

    
2842
--
2843
-- Name: FUNCTION set_col_types(table_ regclass, col_casts col_cast[]); Type: COMMENT; Schema: util; Owner: -
2844
--
2845

    
2846
COMMENT ON FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) IS '
2847
idempotent
2848
';
2849

    
2850

    
2851
--
2852
-- Name: set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2853
--
2854

    
2855
CREATE FUNCTION set_comment(table_ regclass, comment text) RETURNS void
2856
    LANGUAGE sql STRICT
2857
    AS $_$
2858
SELECT util.eval($$COMMENT ON TABLE $$||$1||$$ IS $$||quote_nullable($2))
2859
$_$;
2860

    
2861

    
2862
--
2863
-- Name: show_create_view(regclass); Type: FUNCTION; Schema: util; Owner: -
2864
--
2865

    
2866
CREATE FUNCTION show_create_view(view_ regclass) RETURNS text
2867
    LANGUAGE sql STABLE
2868
    AS $_$
2869
SELECT $$CREATE OR REPLACE VIEW $$||$1||$$ AS $$||pg_get_viewdef($1)||$$;
2870
$$||util.show_grants_for($1)
2871
$_$;
2872

    
2873

    
2874
--
2875
-- Name: show_grants_for(regclass); Type: FUNCTION; Schema: util; Owner: -
2876
--
2877

    
2878
CREATE FUNCTION show_grants_for(table_ regclass) RETURNS text
2879
    LANGUAGE sql STABLE
2880
    AS $_$
2881
SELECT string_agg(cmd, '')
2882
FROM
2883
(
2884
	SELECT (CASE WHEN has_table_privilege(user_, $1, 'SELECT') THEN
2885
$$GRANT SELECT ON TABLE $$||$1||$$ TO $$||quote_ident(user_)||$$;
2886
$$ ELSE '' END) AS cmd
2887
	FROM util.grants_users() f (user_)
2888
) s
2889
$_$;
2890

    
2891

    
2892
--
2893
-- Name: show_relations_like(text, text, character[]); Type: FUNCTION; Schema: util; Owner: -
2894
--
2895

    
2896
CREATE FUNCTION show_relations_like(name_regexp text, schema_regexp text DEFAULT ''::text, types character[] DEFAULT ARRAY['r'::text, 'v'::text]) RETURNS SETOF regclass
2897
    LANGUAGE sql STABLE
2898
    AS $_$
2899
SELECT oid FROM pg_class
2900
WHERE relkind = ANY($3) AND relname ~ $1
2901
AND util.schema_matches(util.schema(relnamespace), $2)
2902
ORDER BY relname
2903
$_$;
2904

    
2905

    
2906
--
2907
-- Name: show_views_like(text, text); Type: FUNCTION; Schema: util; Owner: -
2908
--
2909

    
2910
CREATE FUNCTION show_views_like(name_regexp text, schema text) RETURNS SETOF regclass
2911
    LANGUAGE sql STABLE
2912
    AS $_$
2913
SELECT view_
2914
FROM util.show_relations_like($1, types := ARRAY['v']) view_
2915
WHERE util.schema(view_) = $2
2916
$_$;
2917

    
2918

    
2919
--
2920
-- Name: table2hstore(regclass); Type: FUNCTION; Schema: util; Owner: -
2921
--
2922

    
2923
CREATE FUNCTION table2hstore(table_ regclass) RETURNS hstore
2924
    LANGUAGE plpgsql STABLE STRICT
2925
    AS $_$
2926
DECLARE
2927
    hstore hstore;
2928
BEGIN
2929
    EXECUTE $$SELECT hstore(ARRAY(SELECT unnest(ARRAY["from", "to"]) FROM $$||
2930
        table_||$$))$$ INTO STRICT hstore;
2931
    RETURN hstore;
2932
END;
2933
$_$;
2934

    
2935

    
2936
--
2937
-- Name: table_flag__get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2938
--
2939

    
2940
CREATE FUNCTION table_flag__get(table_ regclass, flag text) RETURNS boolean
2941
    LANGUAGE sql STABLE STRICT
2942
    AS $_$
2943
SELECT COUNT(*) > 0 FROM pg_constraint
2944
WHERE conrelid = $1 AND contype = 'c' AND conname = $2
2945
$_$;
2946

    
2947

    
2948
--
2949
-- Name: FUNCTION table_flag__get(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
2950
--
2951

    
2952
COMMENT ON FUNCTION table_flag__get(table_ regclass, flag text) IS '
2953
gets whether a status flag is set by the presence of a table constraint
2954
';
2955

    
2956

    
2957
--
2958
-- Name: table_flag__set(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2959
--
2960

    
2961
CREATE FUNCTION table_flag__set(table_ regclass, flag text) RETURNS void
2962
    LANGUAGE sql STRICT
2963
    AS $_$
2964
SELECT util.create_if_not_exists($$ALTER TABLE $$||$1||$$ ADD CONSTRAINT $$
2965
||quote_ident($2)||$$ CHECK (true)$$)
2966
$_$;
2967

    
2968

    
2969
--
2970
-- Name: FUNCTION table_flag__set(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
2971
--
2972

    
2973
COMMENT ON FUNCTION table_flag__set(table_ regclass, flag text) IS '
2974
stores a status flag by the presence of a table constraint.
2975
idempotent.
2976
';
2977

    
2978

    
2979
--
2980
-- Name: table_nulls_mapped__get(regclass); Type: FUNCTION; Schema: util; Owner: -
2981
--
2982

    
2983
CREATE FUNCTION table_nulls_mapped__get(table_ regclass) RETURNS boolean
2984
    LANGUAGE sql STABLE STRICT
2985
    AS $_$
2986
SELECT util.table_flag__get($1, 'nulls_mapped')
2987
$_$;
2988

    
2989

    
2990
--
2991
-- Name: FUNCTION table_nulls_mapped__get(table_ regclass); Type: COMMENT; Schema: util; Owner: -
2992
--
2993

    
2994
COMMENT ON FUNCTION table_nulls_mapped__get(table_ regclass) IS '
2995
gets whether a table''s NULL-equivalent strings have been replaced with NULL
2996
';
2997

    
2998

    
2999
--
3000
-- Name: table_nulls_mapped__set(regclass); Type: FUNCTION; Schema: util; Owner: -
3001
--
3002

    
3003
CREATE FUNCTION table_nulls_mapped__set(table_ regclass) RETURNS void
3004
    LANGUAGE sql STRICT
3005
    AS $_$
3006
SELECT util.table_flag__set($1, 'nulls_mapped')
3007
$_$;
3008

    
3009

    
3010
--
3011
-- Name: FUNCTION table_nulls_mapped__set(table_ regclass); Type: COMMENT; Schema: util; Owner: -
3012
--
3013

    
3014
COMMENT ON FUNCTION table_nulls_mapped__set(table_ regclass) IS '
3015
sets that a table''s NULL-equivalent strings have been replaced with NULL.
3016
idempotent.
3017
';
3018

    
3019

    
3020
--
3021
-- Name: to_global_col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
3022
--
3023

    
3024
CREATE FUNCTION to_global_col_names(table_ regclass) RETURNS void
3025
    LANGUAGE plpgsql STRICT
3026
    AS $_$
3027
DECLARE
3028
    row record;
3029
BEGIN
3030
    FOR row IN SELECT * FROM util.col_global_names(table_::text::regtype)
3031
    LOOP
3032
        IF row.global_name != row.name THEN
3033
            EXECUTE $$ALTER TABLE $$||table_||$$ RENAME $$
3034
                ||quote_ident(row.name)||$$ TO $$||quote_ident(row.global_name);
3035
        END IF;
3036
    END LOOP;
3037
END;
3038
$_$;
3039

    
3040

    
3041
--
3042
-- Name: FUNCTION to_global_col_names(table_ regclass); Type: COMMENT; Schema: util; Owner: -
3043
--
3044

    
3045
COMMENT ON FUNCTION to_global_col_names(table_ regclass) IS '
3046
idempotent
3047
';
3048

    
3049

    
3050
--
3051
-- Name: trim(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3052
--
3053

    
3054
CREATE FUNCTION "trim"(table_ regclass, names regclass) RETURNS void
3055
    LANGUAGE sql STRICT
3056
    AS $_$
3057
SELECT util.drop_column(($1, col)) FROM util.added_cols($1, $2) f (col);
3058
SELECT NULL::void; -- don't fold away functions called in previous query
3059
$_$;
3060

    
3061

    
3062
--
3063
-- Name: FUNCTION "trim"(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3064
--
3065

    
3066
COMMENT ON FUNCTION "trim"(table_ regclass, names regclass) IS '
3067
trims table_ to include only columns in the original data.
3068
idempotent.
3069
';
3070

    
3071

    
3072
--
3073
-- Name: truncate(regclass); Type: FUNCTION; Schema: util; Owner: -
3074
--
3075

    
3076
CREATE FUNCTION truncate(table_ regclass) RETURNS void
3077
    LANGUAGE plpgsql STRICT
3078
    AS $_$
3079
BEGIN
3080
    EXECUTE $$TRUNCATE $$||table_||$$ CASCADE$$;
3081
END;
3082
$_$;
3083

    
3084

    
3085
--
3086
-- Name: FUNCTION truncate(table_ regclass); Type: COMMENT; Schema: util; Owner: -
3087
--
3088

    
3089
COMMENT ON FUNCTION truncate(table_ regclass) IS '
3090
idempotent
3091
';
3092

    
3093

    
3094
--
3095
-- Name: truncated_prefixed_name_regexp(text, integer); Type: FUNCTION; Schema: util; Owner: -
3096
--
3097

    
3098
CREATE FUNCTION truncated_prefixed_name_regexp(name text, max_prefix_len integer) RETURNS text
3099
    LANGUAGE sql IMMUTABLE
3100
    AS $_$
3101
SELECT '^(.*)'||util._if(util.name_was_truncated($1, $2),
3102
util.regexp_quote(util.rtrim_n($1, $2))||'.*', util.regexp_quote($1)) ||'$'
3103
$_$;
3104

    
3105

    
3106
--
3107
-- Name: try_create(text); Type: FUNCTION; Schema: util; Owner: -
3108
--
3109

    
3110
CREATE FUNCTION try_create(sql text) RETURNS void
3111
    LANGUAGE plpgsql STRICT
3112
    AS $$
3113
BEGIN
3114
    PERFORM util.eval(sql);
3115
EXCEPTION
3116
    WHEN wrong_object_type THEN NULL; -- trying to alter a view's columns
3117
    WHEN undefined_column THEN NULL;
3118
    WHEN duplicate_column THEN NULL;
3119
END;
3120
$$;
3121

    
3122

    
3123
--
3124
-- Name: FUNCTION try_create(sql text); Type: COMMENT; Schema: util; Owner: -
3125
--
3126

    
3127
COMMENT ON FUNCTION try_create(sql text) IS '
3128
idempotent
3129
';
3130

    
3131

    
3132
--
3133
-- Name: try_mk_derived_col(col_ref, text); Type: FUNCTION; Schema: util; Owner: -
3134
--
3135

    
3136
CREATE FUNCTION try_mk_derived_col(col col_ref, expr text) RETURNS void
3137
    LANGUAGE sql STRICT
3138
    AS $_$
3139
SELECT util.try_create($$SELECT util.mk_derived_col($$||quote_literal($1)||$$, $$||quote_literal($2)||$$)$$)
3140
$_$;
3141

    
3142

    
3143
--
3144
-- Name: FUNCTION try_mk_derived_col(col col_ref, expr text); Type: COMMENT; Schema: util; Owner: -
3145
--
3146

    
3147
COMMENT ON FUNCTION try_mk_derived_col(col col_ref, expr text) IS '
3148
idempotent
3149
';
3150

    
3151

    
3152
--
3153
-- Name: type_qual(anyelement); Type: FUNCTION; Schema: util; Owner: -
3154
--
3155

    
3156
CREATE FUNCTION type_qual(value anyelement) RETURNS text
3157
    LANGUAGE sql IMMUTABLE
3158
    AS $_$
3159
SELECT CASE WHEN $1 IS NULL THEN '' ELSE $$ NOT NULL$$ END
3160
$_$;
3161

    
3162

    
3163
--
3164
-- Name: FUNCTION type_qual(value anyelement); Type: COMMENT; Schema: util; Owner: -
3165
--
3166

    
3167
COMMENT ON FUNCTION type_qual(value anyelement) IS '
3168
a type''s NOT NULL qualifier
3169
';
3170

    
3171

    
3172
--
3173
-- Name: typeof(text, regtype); Type: FUNCTION; Schema: util; Owner: -
3174
--
3175

    
3176
CREATE FUNCTION typeof(expr text, table_ regtype DEFAULT NULL::regtype) RETURNS regtype
3177
    LANGUAGE plpgsql STABLE
3178
    AS $_$
3179
DECLARE
3180
    type regtype;
3181
BEGIN
3182
    EXECUTE $$SELECT pg_typeof($$||expr||$$)$$||
3183
COALESCE($$ FROM (SELECT (NULL::$$||table_||$$).*) _s$$, '') INTO STRICT type;
3184
    RETURN type;
3185
END;
3186
$_$;
3187

    
3188

    
3189
--
3190
-- Name: all_same(anyelement); Type: AGGREGATE; Schema: util; Owner: -
3191
--
3192

    
3193
CREATE AGGREGATE all_same(anyelement) (
3194
    SFUNC = all_same_transform,
3195
    STYPE = anyarray,
3196
    FINALFUNC = all_same_final
3197
);
3198

    
3199

    
3200
--
3201
-- Name: AGGREGATE all_same(anyelement); Type: COMMENT; Schema: util; Owner: -
3202
--
3203

    
3204
COMMENT ON AGGREGATE all_same(anyelement) IS '
3205
includes NULLs in comparison
3206
';
3207

    
3208

    
3209
--
3210
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: util; Owner: -
3211
--
3212

    
3213
CREATE AGGREGATE join_strs(text, text) (
3214
    SFUNC = join_strs_transform,
3215
    STYPE = text
3216
);
3217

    
3218

    
3219
--
3220
-- Name: ->; Type: OPERATOR; Schema: util; Owner: -
3221
--
3222

    
3223
CREATE OPERATOR -> (
3224
    PROCEDURE = map_get,
3225
    LEFTARG = regclass,
3226
    RIGHTARG = text
3227
);
3228

    
3229

    
3230
--
3231
-- Name: =>; Type: OPERATOR; Schema: util; Owner: -
3232
--
3233

    
3234
CREATE OPERATOR => (
3235
    PROCEDURE = hstore,
3236
    LEFTARG = text[],
3237
    RIGHTARG = text
3238
);
3239

    
3240

    
3241
--
3242
-- Name: OPERATOR => (text[], text); Type: COMMENT; Schema: util; Owner: -
3243
--
3244

    
3245
COMMENT ON OPERATOR => (text[], text) IS '
3246
usage: array[''key1'', ...]::text[] => ''value''
3247
';
3248

    
3249

    
3250
--
3251
-- Name: ?*>=; Type: OPERATOR; Schema: util; Owner: -
3252
--
3253

    
3254
CREATE OPERATOR ?*>= (
3255
    PROCEDURE = is_populated_more_often_than,
3256
    LEFTARG = anyelement,
3257
    RIGHTARG = anyelement
3258
);
3259

    
3260

    
3261
--
3262
-- Name: ?>=; Type: OPERATOR; Schema: util; Owner: -
3263
--
3264

    
3265
CREATE OPERATOR ?>= (
3266
    PROCEDURE = is_more_complete_than,
3267
    LEFTARG = anyelement,
3268
    RIGHTARG = anyelement
3269
);
3270

    
3271

    
3272
--
3273
-- Name: ||%; Type: OPERATOR; Schema: util; Owner: -
3274
--
3275

    
3276
CREATE OPERATOR ||% (
3277
    PROCEDURE = concat_esc,
3278
    LEFTARG = text,
3279
    RIGHTARG = text
3280
);
3281

    
3282

    
3283
--
3284
-- Name: OPERATOR ||% (text, text); Type: COMMENT; Schema: util; Owner: -
3285
--
3286

    
3287
COMMENT ON OPERATOR ||% (text, text) IS '
3288
% indicates an identifier, as in Perl hashes and one of the x86 assembler syntaxes for registers
3289
';
3290

    
3291

    
3292
--
3293
-- Name: map; Type: TABLE; Schema: util; Owner: -; Tablespace: 
3294
--
3295

    
3296
CREATE TABLE map (
3297
    "from" text NOT NULL,
3298
    "to" text,
3299
    filter text,
3300
    notes text
3301
);
3302

    
3303

    
3304
--
3305
-- Data for Name: explain; Type: TABLE DATA; Schema: util; Owner: -
3306
--
3307

    
3308

    
3309

    
3310
--
3311
-- Data for Name: map; Type: TABLE DATA; Schema: util; Owner: -
3312
--
3313

    
3314

    
3315

    
3316
--
3317
-- Name: map__unique__from; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace: 
3318
--
3319

    
3320
ALTER TABLE ONLY map
3321
    ADD CONSTRAINT map__unique__from UNIQUE ("from");
3322

    
3323

    
3324
--
3325
-- Name: map__unique__to; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace: 
3326
--
3327

    
3328
ALTER TABLE ONLY map
3329
    ADD CONSTRAINT map__unique__to UNIQUE ("to");
3330

    
3331

    
3332
--
3333
-- Name: map_filter_insert; Type: TRIGGER; Schema: util; Owner: -
3334
--
3335

    
3336
CREATE TRIGGER map_filter_insert BEFORE INSERT ON map FOR EACH ROW EXECUTE PROCEDURE map_filter_insert();
3337

    
3338

    
3339
--
3340
-- PostgreSQL database dump complete
3341
--
3342

    
(19-19/29)