forked from aquametalabs/aquameta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
001-catalog.sql
2889 lines (2278 loc) · 116 KB
/
001-catalog.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Meta Catalog
* A writable system catalog for PostgreSQL
*
* Copyriright (c) 2019 - Aquameta - http://aquameta.org/
******************************************************************************/
/******************************************************************************
* utility functions
*****************************************************************************/
create function meta.require_all(fields public.hstore, required_fields text[]) returns void as $$
declare
f record;
begin
-- hstore needs this
set local search_path=public,meta;
for f in select unnest(required_fields) as field_name loop
if (fields->f.field_name) is null then
raise exception '% is a required field.', f.field_name;
end if;
end loop;
end;
$$ language plpgsql;
create function meta.require_one(fields public.hstore, required_fields text[]) returns void as $$
declare
f record;
begin
-- hstore needs this
set local search_path=public,meta;
for f in select unnest(required_fields) as field_name loop
if (fields->f.field_name) is not null then
return;
end if;
end loop;
raise exception 'One of the fields % is required.', required_fields;
end;
$$ language plpgsql;
/******************************************************************************
* meta.schema
*****************************************************************************/
create view meta.schema as
select row(schema_name)::meta.schema_id as id,
schema_name::text as name
from information_schema.schemata;
create function meta.stmt_schema_create(name text) returns text as $$
select 'create schema ' || quote_ident(name)
$$ language sql;
create function meta.stmt_schema_rename(old_name text, new_name text) returns text as $$
select 'alter schema ' || quote_ident(old_name) || ' rename to ' || quote_ident(new_name);
$$ language sql;
create function meta.stmt_schema_drop(name text) returns text as $$
select 'drop schema ' || quote_ident(name);
$$ language sql;
create function meta.schema_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
execute meta.stmt_schema_create(NEW.name);
NEW.id := row(NEW.name)::meta.schema_id;
return NEW;
end;
$$ language plpgsql;
create function meta.schema_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
if OLD.name is distinct from NEW.name then
execute meta.stmt_schema_rename(OLD.name, NEW.name);
end if;
return NEW;
end;
$$ language plpgsql;
create function meta.schema_delete() returns trigger as $$
begin
execute meta.stmt_schema_drop(OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.type
*****************************************************************************/
create view meta.type as
select
meta.type_id(n.nspname, pg_catalog.format_type(t.oid, NULL)) as id,
n.nspname::text as "schema_name",
pg_catalog.format_type(t.oid, NULL)::text as "name",
case when c.relkind = 'c' then true else false end as "composite",
pg_catalog.obj_description(t.oid, 'pg_type') as "description"
from pg_catalog.pg_type t
left join pg_catalog.pg_namespace n on n.oid = t.typnamespace
left join pg_catalog.pg_class c on c.oid = t.typrelid
where (t.typrelid = 0 or c.relkind = 'c')
and not exists(select 1 from pg_catalog.pg_type el where el.oid = t.typelem and el.typarray = t.oid)
and pg_catalog.pg_type_is_visible(t.oid)
order by 1, 2;
/******************************************************************************
* meta.cast
*****************************************************************************/
create view meta.cast as
--TODO
SELECT meta.cast_id(ts.typname, pg_catalog.format_type(castsource, NULL),tt.typname, pg_catalog.format_type(casttarget, NULL)) as id,
pg_catalog.format_type(castsource, NULL) AS "source type",
pg_catalog.format_type(casttarget, NULL) AS "target type",
(CASE WHEN castfunc = 0 THEN '(binary coercible)'
ELSE p.proname
END)::text as "function",
CASE WHEN c.castcontext = 'e' THEN 'no'
WHEN c.castcontext = 'a' THEN 'in assignment'
ELSE 'yes'
END as "implicit?" FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p
ON c.castfunc = p.oid
LEFT JOIN pg_catalog.pg_type ts
ON c.castsource = ts.oid
LEFT JOIN pg_catalog.pg_namespace ns
ON ns.oid = ts.typnamespace
LEFT JOIN pg_catalog.pg_type tt
ON c.casttarget = tt.oid
LEFT JOIN pg_catalog.pg_namespace nt
ON nt.oid = tt.typnamespace
WHERE ( (true AND pg_catalog.pg_type_is_visible(ts.oid)
) OR (true AND pg_catalog.pg_type_is_visible(tt.oid)
) )
ORDER BY 1, 2;
/******************************************************************************
* meta.operator
*****************************************************************************/
create view meta.operator as
SELECT meta.operator_id(n.nspname, o.oprname, trns.nspname, tr.typname, trns.nspname, tr.typname) as id,
n.nspname::text as schema_name,
o.oprname::text as name,
CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS "Left arg type",
CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS "Right arg type",
pg_catalog.format_type(o.oprresult, NULL) AS "Result type",
coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),
pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS "Description"
FROM pg_catalog.pg_operator o
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
JOIN pg_catalog.pg_type tl ON o.oprleft = tl.oid
JOIN pg_catalog.pg_namespace tlns on tl.typnamespace = tlns.oid
JOIN pg_catalog.pg_type tr ON o.oprleft = tr.oid
JOIN pg_catalog.pg_namespace trns on tr.typnamespace = trns.oid
WHERE n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_operator_is_visible(o.oid)
ORDER BY 1, 2, 3, 4;
/******************************************************************************
* meta.sequence
*****************************************************************************/
create view meta.sequence as
select meta.sequence_id(sequence_schema, sequence_name) as id,
meta.schema_id(sequence_schema) as schema_id,
sequence_schema::text as schema_name,
sequence_name::text as name,
start_value::bigint,
minimum_value::bigint,
maximum_value::bigint,
increment::bigint,
cycle_option = 'YES' as cycle
from information_schema.sequences;
create function meta.stmt_sequence_create(
schema_name text,
name text,
start_value bigint,
minimum_value bigint,
maximum_value bigint,
increment bigint,
cycle boolean
) returns text as $$
select 'create sequence ' || quote_ident(schema_name) || '.' || quote_ident(name)
|| coalesce(' increment ' || increment, '')
|| coalesce(' minvalue ' || minimum_value, ' no minvalue ')
|| coalesce(' maxvalue ' || maximum_value, ' no maxvalue ')
|| coalesce(' start ' || start_value, '')
|| case cycle when true then ' cycle '
else ' no cycle '
end;
$$ language sql;
create function meta.stmt_sequence_set_schema(
schema_name text,
name text,
new_schema_name text
) returns text as $$
select 'alter sequence ' || quote_ident(schema_name) || '.' || quote_ident(name)
|| ' set schema ' || quote_ident(new_schema_name);
$$ language sql immutable;
create function meta.stmt_sequence_rename(
schema_name text,
name text,
new_name text
) returns text as $$
select 'alter sequence ' || quote_ident(schema_name) || '.' || quote_ident(name)
|| ' rename to ' || quote_ident(new_name);
$$ language sql immutable;
create function meta.stmt_sequence_alter(
schema_name text,
name text,
start_value bigint,
minimum_value bigint,
maximum_value bigint,
increment bigint,
cycle boolean
) returns text as $$
select 'alter sequence ' || quote_ident(schema_name) || '.' || quote_ident(name)
|| coalesce(' increment ' || increment, '')
|| coalesce(' minvalue ' || minimum_value, ' no minvalue ')
|| coalesce(' maxvalue ' || maximum_value, ' no maxvalue ')
|| coalesce(' start ' || start_value, '')
|| case cycle when true then ' cycle '
else ' no cycle '
end;
$$ language sql;
create function meta.stmt_sequence_drop(schema_name text, name text) returns text as $$
select 'drop sequence ' || quote_ident(schema_name) || '.' || quote_ident(name);
$$ language sql;
create function meta.sequence_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
execute meta.stmt_sequence_create(
coalesce(NEW.schema_name, (NEW.schema_id).name),
NEW.name,
NEW.start_value,
NEW.minimum_value,
NEW.maximum_value,
NEW.increment,
NEW.cycle
);
NEW.id := meta.sequence_id(
coalesce(NEW.schema_name, (NEW.schema_id).name),
NEW.name
);
return NEW;
end;
$$ language plpgsql;
create function meta.sequence_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
if NEW.schema_id != OLD.schema_id or OLD.schema_name != NEW.schema_name then
execute meta.stmt_sequence_set_schema(OLD.schema_name, OLD.name, coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name));
end if;
if NEW.name != OLD.name then
execute meta.stmt_sequence_rename(coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name), OLD.name, NEW.name);
end if;
execute meta.stmt_sequence_alter(
coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name),
NEW.name,
NEW.start_value,
NEW.minimum_value,
NEW.maximum_value,
NEW.increment,
NEW.cycle
);
return NEW;
end;
$$ language plpgsql;
create function meta.sequence_delete() returns trigger as $$
begin
execute meta.stmt_sequence_drop(OLD.schema_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.table
*****************************************************************************/
create view meta.table as
select row(row(schemaname), tablename)::meta.relation_id as id,
row(schemaname)::meta.schema_id as schema_id,
schemaname::text as schema_name,
tablename::text as name,
rowsecurity as rowsecurity
/*
-- going from pg_catalog.pg_tables instead, so we can get rowsecurity
from information_schema.tables
where table_type = 'BASE TABLE';
*/
from pg_catalog.pg_tables;
create function meta.stmt_table_create(schema_name text, table_name text) returns text as $$
select 'create table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || '()'
$$ language sql;
create function meta.stmt_table_set_schema(schema_name text, table_name text, new_schema_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' set schema ' || quote_ident(new_schema_name);
$$ language sql;
create function meta.stmt_table_enable_rowsecurity(schema_name text, table_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' enable row level security'
$$ language sql;
create function meta.stmt_table_disable_rowsecurity(schema_name text, table_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' disable row level security'
$$ language sql;
create function meta.stmt_table_rename(schema_name text, table_name text, new_table_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' rename to ' || quote_ident(new_table_name);
$$ language sql;
create function meta.stmt_table_drop(schema_name text, table_name text) returns text as $$
select 'drop table ' || quote_ident(schema_name) || '.' || quote_ident(table_name);
$$ language sql;
create function meta.table_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
execute meta.stmt_table_create(coalesce(NEW.schema_name, (NEW.schema_id).name), NEW.name);
if NEW.rowsecurity = true then
execute meta.stmt_table_enable_rowsecurity(NEW.schema_name, NEW.name);
end if;
NEW.id := row(row(coalesce(NEW.schema_name, (NEW.schema_id).name)), NEW.name)::meta.relation_id;
return NEW;
end;
$$ language plpgsql;
create function meta.table_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
if NEW.schema_id != OLD.schema_id or OLD.schema_name != NEW.schema_name then
execute meta.stmt_table_set_schema(OLD.schema_name, OLD.name, coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name));
end if;
if NEW.name != OLD.name then
execute meta.stmt_table_rename(coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name), OLD.name, NEW.name);
end if;
if NEW.rowsecurity != OLD.rowsecurity then
if NEW.rowsecurity = true then
execute meta.stmt_table_enable_rowsecurity(NEW.schema_name, NEW.name);
else
execute meta.stmt_table_disable_rowsecurity(NEW.schema_name, NEW.name);
end if;
end if;
return NEW;
end;
$$ language plpgsql;
create function meta.table_delete() returns trigger as $$
begin
execute meta.stmt_table_drop(OLD.schema_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.view
*****************************************************************************/
create view meta.view as
select row(row(table_schema), table_name)::meta.relation_id as id,
row(table_schema)::meta.schema_id as schema_id,
table_schema::text as schema_name,
table_name::text as name,
view_definition::text as query
from information_schema.views v;
create function meta.stmt_view_create(schema_name text, view_name text, query text) returns text as $$
select 'create view ' || quote_ident(schema_name) || '.' || quote_ident(view_name) || ' as ' || query;
$$ language sql;
create function meta.stmt_view_set_schema(schema_name text, view_name text, new_schema_name text) returns text as $$
select 'alter view ' || quote_ident(schema_name) || '.' || quote_ident(view_name) || ' set schema ' || quote_ident(new_schema_name);
$$ language sql;
create function meta.stmt_view_rename(schema_name text, view_name text, new_name text) returns text as $$
select 'alter view ' || quote_ident(schema_name) || '.' || quote_ident(view_name) || ' rename to ' || quote_ident(new_name);
$$ language sql;
create function meta.stmt_view_drop(schema_name text, view_name text) returns text as $$
select 'drop view ' || quote_ident(schema_name) || '.' || quote_ident(view_name);
$$ language sql;
create function meta.view_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'query']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
execute meta.stmt_view_create(coalesce(NEW.schema_name, (NEW.schema_id).name), NEW.name, NEW.query);
return NEW;
end;
$$ language plpgsql;
create function meta.view_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'query']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
if NEW.schema_id != OLD.schema_id or NEW.schema_name != OLD.schema_name then
execute meta.stmt_view_set_schema(OLD.schema_name, OLD.name, coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name));
end if;
if NEW.name != OLD.name then
execute meta.stmt_view_rename(coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name), OLD.name, NEW.name);
end if;
if NEW.query != OLD.query then
execute meta.stmt_view_drop(coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name), NEW.name);
execute meta.stmt_view_create(coalesce(nullif(NEW.schema_name, OLD.schema_name), (NEW.schema_id).name), NEW.name, NEW.query);
end if;
return NEW;
end;
$$ language plpgsql;
create function meta.view_delete() returns trigger as $$
begin
execute meta.stmt_view_drop(OLD.schema_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.column
*****************************************************************************/
create view meta.relation_column as
select meta.column_id(c.table_schema, c.table_name, c.column_name) as id,
meta.relation_id(c.table_schema, c.table_name) as relation_id,
c.table_schema::text as schema_name,
c.table_name::text as relation_name,
c.column_name::text as name,
c.ordinal_position::integer as position,
quote_ident(c.udt_schema) || '.' || quote_ident(c.udt_name) as type_name,
meta.type_id (c.udt_schema, c.udt_name) as "type_id",
(c.is_nullable = 'YES') as nullable,
c.column_default::text as "default",
k.column_name is not null or (c.table_schema = 'meta' and c.column_name = 'id') as primary_key
from information_schema.columns c
left join information_schema.table_constraints t
on t.table_catalog = c.table_catalog and
t.table_schema = c.table_schema and
t.table_name = c.table_name and
t.constraint_type = 'PRIMARY KEY'
left join information_schema.key_column_usage k
on k.constraint_catalog = t.constraint_catalog and
k.constraint_schema = t.constraint_schema and
k.constraint_name = t.constraint_name and
k.column_name = c.column_name;
create view meta.column as
-- select c.id, c.relation_id as table_id, c.schema_name, c.relation_name, c.name, c.position, c.type_name, c.type_id, c.nullable, c.column_default, c.primary_key
select c.*
from meta.table t
join meta.relation_column c on c.relation_id = t.id;
create function meta.stmt_column_create(schema_name text, relation_name text, column_name text, type_name text, nullable boolean, "default" text, primary_key boolean) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' add column ' || quote_ident(column_name) || ' ' || type_name ||
case when nullable then ''
else ' not null'
end ||
case when "default" is not null and column_name != 'id' then (' default ' || "default" || '::' || type_name)
else ''
end ||
case when primary_key then ' primary key'
else ''
end;
$$ language sql;
create function meta.stmt_column_rename(schema_name text, relation_name text, column_name text, new_column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' rename column ' || quote_ident(column_name) || ' to ' || quote_ident(new_column_name);
$$ language sql;
create function meta.stmt_column_add_primary_key(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' add primary key (' || quote_ident(column_name) || ')';
$$ language sql;
create function meta.stmt_column_drop_primary_key(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' drop constraint ' || quote_ident(column_name) || '_pkey';
$$ language sql;
create function meta.stmt_column_set_not_null(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' alter column ' || quote_ident(column_name) || ' set not null';
$$ language sql;
create function meta.stmt_column_drop_not_null(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' alter column ' || quote_ident(column_name) || ' drop not null';
$$ language sql;
create function meta.stmt_column_set_default(schema_name text, relation_name text, column_name text, "default" text, type_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' alter column ' || quote_ident(column_name) || ' set default ' || "default" || '::' || type_name;
$$ language sql;
create function meta.stmt_column_drop_default(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' alter column ' || quote_ident(column_name) || ' drop default ';
$$ language sql;
create function meta.stmt_column_set_type(schema_name text, relation_name text, column_name text, type_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' alter column ' || quote_ident(column_name) || ' type ' || type_name;
$$ language sql;
create function meta.stmt_column_drop(schema_name text, relation_name text, column_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' drop column ' || quote_ident(column_name);
$$ language sql;
create function meta.column_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'type_name']);
perform meta.require_one(public.hstore(NEW), array['relation_id', 'schema_name']);
perform meta.require_one(public.hstore(NEW), array['relation_id', 'relation_name']);
execute meta.stmt_column_create(coalesce(NEW.schema_name, ((NEW.relation_id).schema_id).name), coalesce(NEW.relation_name, (NEW.relation_id).name), NEW.name, NEW.type_name, NEW.nullable, NEW."default", NEW.primary_key);
return NEW;
end;
$$ language plpgsql;
create function meta.column_update() returns trigger as $$
declare
schema_name text;
relation_name text;
begin
perform meta.require_all(public.hstore(NEW), array['name', 'type_name', 'nullable']);
perform meta.require_one(public.hstore(NEW), array['relation_id', 'schema_name']);
perform meta.require_one(public.hstore(NEW), array['relation_id', 'relation_name']);
if NEW.relation_id is not null and OLD.relation_id != NEW.relation_id or
NEW.schema_name is not null and OLD.schema_name != NEW.schema_name or
NEW.relation_name is not null and OLD.relation_name != NEW.relation_name then
raise exception 'Moving a column to another table is not yet supported.';
end if;
schema_name := OLD.schema_name;
relation_name := OLD.relation_name;
if NEW.name != OLD.name then
execute meta.stmt_column_rename(schema_name, relation_name, OLD.name, NEW.name);
end if;
if NEW.type_name != OLD.type_name then
execute meta.stmt_column_set_type(schema_name, relation_name, NEW.name, NEW.type_name);
end if;
if NEW.nullable != OLD.nullable then
if NEW.nullable then
execute meta.stmt_column_drop_not_null(schema_name, relation_name, NEW.name);
else
execute meta.stmt_column_set_not_null(schema_name, relation_name, NEW.name);
end if;
end if;
if NEW."default" is distinct from OLD."default" then
if NEW."default" is null then
execute meta.stmt_column_drop_default(schema_name, relation_name, NEW.name);
else
execute meta.stmt_column_set_default(schema_name, relation_name, NEW.name, NEW."default", NEW."type_name");
end if;
end if;
if NEW.primary_key != OLD.primary_key then
if NEW.primary_key then
execute meta.stmt_column_add_primary_key(schema_name, relation_name, NEW.name);
else
execute meta.stmt_column_drop_primary_key(schema_name, relation_name, NEW.name);
end if;
end if;
return NEW;
end;
$$ language plpgsql;
create function meta.column_delete() returns trigger as $$
begin
execute meta.stmt_column_drop(OLD.schema_name, OLD.relation_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.relation
*****************************************************************************/
create view meta.relation as
select row(row(t.table_schema), t.table_name)::meta.relation_id as id,
row(t.table_schema)::meta.schema_id as schema_id,
t.table_schema::text as schema_name,
t.table_name::text as name,
t.table_type::text as "type",
nullif(array_agg(c.id), array[null]::meta.column_id[]) as primary_key_column_ids,
nullif(array_agg(c.name::text), array[null]::text[]) as primary_key_column_names
from information_schema.tables t
left join meta.relation_column c
on c.relation_id = row(row(t.table_schema), t.table_name)::meta.relation_id and c.primary_key
group by t.table_schema, t.table_name, t.table_type;
/******************************************************************************
* meta.foreign_key
*****************************************************************************/
create view meta.foreign_key as
select row(row(row(tc.table_schema), tc.table_name), tc.constraint_name)::meta.foreign_key_id as id,
row(row(tc.table_schema), tc.table_name)::meta.relation_id as table_id,
tc.table_schema::text as schema_name,
tc.table_name::text as table_name,
tc.constraint_name::text as name,
array_agg(row(row(row(kcu.table_schema), kcu.table_name), kcu.column_name)::meta.column_id) as from_column_ids,
array_agg(row(row(row(ccu.table_schema), ccu.table_name), ccu.column_name)::meta.column_id) as to_column_ids,
update_rule::text as on_update,
delete_rule::text as on_delete
from information_schema.table_constraints tc
inner join information_schema.referential_constraints rc
on rc.constraint_catalog = tc.constraint_catalog and
rc.constraint_schema = tc.constraint_schema and
rc.constraint_name = tc.constraint_name
inner join information_schema.constraint_column_usage ccu
on ccu.constraint_catalog = tc.constraint_catalog and
ccu.constraint_schema = tc.constraint_schema and
ccu.constraint_name = tc.constraint_name
inner join information_schema.key_column_usage kcu
on kcu.constraint_catalog = tc.constraint_catalog and
kcu.constraint_schema = tc.constraint_schema and
kcu.constraint_name = tc.constraint_name
where constraint_type = 'FOREIGN KEY'
group by tc.table_schema, tc.table_name, tc.constraint_name, update_rule, delete_rule;
create function meta.stmt_foreign_key_create(schema_name text, table_name text, constraint_name text, from_column_ids meta.column_id[], to_column_ids meta.column_id[], on_update text, on_delete text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' add constraint ' || quote_ident(constraint_name) ||
' foreign key (' || (
select string_agg(name, ', ')
from meta."column"
where id = any(from_column_ids)
) || ') references ' || (((to_column_ids[1]).relation_id).schema_id).name || '.' || ((to_column_ids[1]).relation_id).name || (
select '(' || string_agg(c.name, ', ') || ')'
from meta."column" c
where c.id = any(to_column_ids)
) || ' on update ' || on_update
|| ' on delete ' || on_delete;
$$ language sql;
create function meta.stmt_foreign_key_drop(schema_name text, table_name text, constraint_name text) returns text as $$
select 'alter table ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' drop constraint ' || quote_ident(constraint_name);
$$ language sql;
create function meta.foreign_key_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'from_column_ids', 'to_column_ids', 'on_update', 'on_delete']);
perform meta.require_one(public.hstore(NEW), array['table_id', 'schema_name']);
perform meta.require_one(public.hstore(NEW), array['table_id', 'table_name']);
execute meta.stmt_foreign_key_create(
coalesce(NEW.schema_name, ((NEW.table_id).schema_id).name),
coalesce(NEW.table_name, (NEW.table_id).name),
NEW.name, NEW.from_column_ids, NEW.to_column_ids, NEW.on_update, NEW.on_delete
);
return NEW;
exception
when null_value_not_allowed then
raise exception 'A provided column_id was not found in meta.column.';
end;
$$ language plpgsql;
create function meta.foreign_key_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'from_column_ids', 'to_column_ids', 'on_update', 'on_delete']);
perform meta.require_one(public.hstore(NEW), array['table_id', 'schema_name']);
perform meta.require_one(public.hstore(NEW), array['table_id', 'table_name']);
execute meta.stmt_foreign_key_drop(OLD.schema_name, OLD.table_name, OLD.name);
execute meta.stmt_foreign_key_create(
coalesce(NEW.schema_name, ((NEW.table_id).schema_id).name),
coalesce(NEW.table_name, (NEW.table_id).name),
NEW.name, NEW.from_column_ids, NEW.to_column_ids, NEW.on_update, NEW.on_delete
);
return NEW;
exception
when null_value_not_allowed then
raise exception 'A provided column_id was not found in meta.column.';
end;
$$ language plpgsql;
create function meta.foreign_key_delete() returns trigger as $$
begin
execute meta.stmt_foreign_key_drop(OLD.schema_name, OLD.table_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.row_id
*****************************************************************************/
-- not sure this is a good idea.
/******************************************************************************
* meta.field_id
*****************************************************************************/
-- not sure this is a good idea.
/******************************************************************************
* meta.function_definition
*
* A view that contains the function_id and it's definition statement unparsed
* and without any kind of metadata -- Built because meta.function was having
* some problems, namely that argument names were not present, and are required
* to recreate the function on an INSERT. meta.function needs an entire
* rewrite and potentially rethink, and ramifications on
* endpoint.rows_select_function would be far-reaching and likely highly
* disruptive. Long term, function handling in both meta and endpoint need a
* complete rewrite. However, for bundle IO on meta.function rows, this might
* actually be the simplest solution anyway.
*****************************************************************************/
create or replace view meta.function_definition as
select
meta.function_id( pronamespace::pg_catalog.regnamespace::text, proname::text, regexp_split_to_array(pg_catalog.pg_get_function_arguments(p.oid),', ')) as id,
pg_catalog.pg_get_functiondef_no_searchpath(p.oid) as definition
from pg_catalog.pg_proc p
where prokind != 'a'; -- why?? otherwise I get "ERROR: "sum" is an aggregate function"
create function meta.stmt_function_definition_create(definition text) returns text as $$
select definition;
$$ language sql;
create function meta.stmt_function_definition_drop(function_id meta.function_id) returns text as $$
select 'drop function ' || quote_ident((function_id::meta.schema_id).name) || '.' || quote_ident(function_id.name) || '(' ||
array_to_string(function_id.parameters, ',') ||
');';
$$ language sql;
create function meta.function_definition_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['definition']);
execute meta.stmt_function_definition_create(NEW.definition);
return NEW;
end;
$$ language plpgsql;
create function meta.function_definition_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['definition']);
execute meta.stmt_function_definition_drop(OLD.id);
execute meta.stmt_function_definition_create(NEW.definition);
return NEW;
end;
$$ language plpgsql;
create function meta.function_definition_delete() returns trigger as $$
begin
execute meta.stmt_function_definition_drop(OLD.id);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.function
*****************************************************************************/
create view meta.function as
select id,
schema_id,
schema_name,
name,
parameters,
definition,
return_type,
return_type_id,
language,
substring(pg_get_function_result((quote_ident(schema_name) || '.' || quote_ident(name) || '(' ||
array_to_string(
coalesce(
nullif(
array_agg(coalesce(lower(nullif(p_in.parameter_mode, 'IN')) || ' ', '')
|| coalesce(nullif(nullif(p_in.data_type, 'ARRAY'), 'USER-DEFINED'), p_in.udt_schema || '.' || p_in.udt_name)
order by p_in.ordinal_position),
array[null]
),
array[]::text[]
),
', '
)
|| ')')::regprocedure) from 1 for 6) = 'SETOF '
-- FIXME: this circumvents information_schema and uses
-- pg_catalog because pg_proc.proretset is not used in info_schema,
-- so it doesn't have enough information to determine whether this
-- record returns a setof. not enough info? and limit 1 is a
-- hack. this whole function needs a rewrite, so working around
-- it for now.
or (select proretset = 't' from pg_proc join pg_namespace on pg_proc.pronamespace = pg_namespace.oid where proname = q.name and nspname = q.schema_name limit 1)
as returns_set
from (
select meta.function_id(
r.routine_schema::text,
r.routine_name::text,
coalesce(
nullif(
array_agg( -- Array of types of the 'IN' parameters to this function
coalesce( nullif( nullif(p.data_type, 'ARRAY'), 'USER-DEFINED'), p.udt_schema || '.' || p.udt_name)
order by p.ordinal_position),
array[null]
),
array[]::text[]
)
) as id,
meta.schema_id(r.routine_schema) as schema_id,
r.routine_schema as schema_name,
r.routine_name as name,
r.specific_catalog,
r.specific_schema,
r.specific_name,
coalesce(
nullif(
array_agg( -- Array of types of the 'IN' parameters to this function
coalesce( nullif( nullif(p.data_type, 'ARRAY'), 'USER-DEFINED'), p.udt_schema || '.' || p.udt_name)
order by p.ordinal_position),
array[null]
),
array[]::text[]
) as parameters,
r.routine_definition::text as definition,
coalesce(nullif(r.data_type, 'USER-DEFINED'), r.type_udt_schema || '.' || r.type_udt_name) as return_type,
meta.type_id(r.type_udt_schema, r.type_udt_name) as return_type_id,
lower(r.external_language)::information_schema.character_data::text as language
from information_schema.routines r
left join information_schema.parameters p
on p.specific_catalog = r.specific_catalog and
p.specific_schema = r.specific_schema and
p.specific_name = r.specific_name
where r.routine_type = 'FUNCTION' and
r.routine_name not in ('pg_identify_object', 'pg_sequence_parameters') and
p.ordinal_position > 0 and
p.parameter_mode like 'IN%' -- Includes IN and INOUT
group by r.routine_catalog,
r.routine_schema,
r.routine_name,
r.routine_definition,
r.data_type,
r.type_udt_schema,
r.type_udt_name,
r.external_language,
r.specific_catalog,
r.specific_schema,
r.specific_name,
p.specific_catalog,
p.specific_schema,
p.specific_name
) q
left join information_schema.parameters p_in
on p_in.specific_catalog = q.specific_catalog and
p_in.specific_schema = q.specific_schema and
p_in.specific_name = q.specific_name
where
p_in.ordinal_position > 0 and
p_in.parameter_mode = 'IN'
group by id,
schema_id,
schema_name,
name,
parameters,
definition,
return_type,
return_type_id,
language;
create view meta.function_parameter as
select q.schema_id,
q.schema_name,
q.function_id,
q.function_name,
par.parameter_name as name,
meta.type_id(par.udt_schema, par.udt_name) as type_id,
quote_ident(par.udt_schema) || '.' || quote_ident(par.udt_name) as type_name,
par.parameter_mode::text as "mode",
par.ordinal_position::integer as position,