-
Notifications
You must be signed in to change notification settings - Fork 1
/
000-triggers.sql
2123 lines (1621 loc) · 86 KB
/
000-triggers.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 Triggers
* Adds insert, update and delete triggers to the meta system catalog
*
* 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
for f in select unnest(required_fields) as field_name loop
if (fields operator(public.->) 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
for f in select unnest(required_fields) as field_name loop
if (fields operator(public.->) 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 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 function meta.stmt_type_create(definition text) returns text as $$
select definition;
$$ language sql;
create function meta.stmt_type_drop(type_id meta.type_id) returns text as $$
select 'drop type ' ||
quote_ident((type_id).schema_name) || '.' ||
quote_ident((type_id).name) || ';';
$$ language sql;
create function meta.type_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['definition']);
execute meta.stmt_type_create(NEW.definition);
return NEW;
end;
$$ language plpgsql;
create function meta.type_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['definition']);
execute meta.stmt_type_drop(OLD.id);
execute meta.stmt_type_create(NEW.definition);
return NEW;
end;
$$ language plpgsql;
create function meta.type_delete() returns trigger as $$
begin
execute meta.stmt_type_drop(OLD.id);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.sequence
*****************************************************************************/
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 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 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 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_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.foreign_key
*****************************************************************************/
create or replace function meta.stmt_foreign_key_create(schema_name text, table_name text, constraint_name text, from_column_names text[], to_schema_name text, to_table_name text, to_column_names text[], match_option text, 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(quote_ident(u.name), ', ')
from unnest(from_column_names) u(name)
) || ') references ' || quote_ident(to_schema_name) || '.' || quote_ident(to_table_name) || '(' || (
select string_agg(quote_ident(u.name), ', ')
from unnest(to_column_names) u(name)
) || ') '
|| coalesce (' match ' || match_option, '')
|| coalesce (' on update ' || on_update, '')
|| coalesce (' 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 or replace function meta.foreign_key_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['schema_name', 'table_name', 'constraint_name', 'from_column_names', 'to_column_names']);
execute meta.stmt_foreign_key_create(
NEW.schema_name,
NEW.table_name,
NEW.constraint_name,
NEW.from_column_names,
NEW.to_schema_name,
NEW.to_table_name,
NEW.to_column_names,
NEW.match_option,
NEW.on_update,
NEW.on_delete
);
return NEW;
end;
$$ language plpgsql;
create or replace function meta.foreign_key_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['schema_name', 'table_name', 'constraint_name', 'from_column_names', 'to_schema_name', 'to_table_name', 'to_column_names']);
execute meta.stmt_foreign_key_drop(OLD.schema_name, OLD.table_name, OLD.constraint_name);
execute meta.stmt_foreign_key_create(
coalesce(NEW.schema_name, OLD.schema_name),
coalesce(NEW.table_name, OLD.table_name),
coalesce(NEW.constraint_name, OLD.constraint_name),
coalesce(NEW.from_column_names, OLD.from_column_names),
coalesce(NEW.to_schema_name, OLD.to_schema_name),
coalesce(NEW.to_table_name, OLD.to_table_name),
coalesce(NEW.to_column_names, OLD.to_column_names),
coalesce(NEW.match_option, OLD.match_option),
coalesce(NEW.on_update, OLD.on_update),
coalesce(NEW.on_delete, OLD.on_delete)
);
return NEW;
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.constraint_name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.function
*****************************************************************************/
create or replace function meta.stmt_function_create(schema_name text, function_name text, type_sig text[], parameters text[], return_type text, definition text, language text, returns_set boolean, volatility text, parallel text, security text) returns text as $$
declare
stmt text;
begin
-- "create function foo.bar"
stmt := 'create function ' || quote_ident(schema_name) || '.' || quote_ident(function_name);
-- "(a text, b uuid default null)"
if parameters is not null then
stmt := stmt || '(' || array_to_string(parameters, ',') || ') ';
else
stmt := stmt || '(' || array_to_string(type_sig, ',') || ') ';
end if;
-- stmt := stmt || 'returns ' || case when returns_set is not null and returns_set = 't' then 'setof ' else '' end || return_type || E' as $body$\n';
-- this is a change from meta.function_old to meta.function_pg (current). return_type now includes setof and returns_set is redundant. like?
stmt := stmt || 'returns ' || return_type || E' as $body$\n';
-- "select 1; $body$ language plpgsql;"
stmt := stmt || definition || E'\n$body$ language ' || quote_ident(language);
if volatility is not null and volatility != 'volatile' then
stmt := stmt || ' ' || volatility;
end if;
if parallel is not null and parallel != 'unsafe' then
stmt := stmt || ' ' || parallel;
end if;
if security is not null and security != 'invoker' then
stmt := stmt || ' security ' || security;
end if;
stmt := stmt || ';';
return stmt;
end;
$$ language plpgsql;
create function meta.stmt_function_drop(schema_name text, function_name text, type_sig text[]) returns text as $$
select 'drop function ' || quote_ident(schema_name) || '.' || quote_ident(function_name) || '(' ||
array_to_string(type_sig, ',') ||
');';
$$ language sql;
create function meta.function_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'return_type', 'definition', 'language']);
perform meta.require_one(public.hstore(NEW), array['parameters', 'type_sig']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
execute meta.stmt_function_create(coalesce(NEW.schema_name, (NEW.schema_id).name), NEW.name, NEW.type_sig, NEW.parameters, NEW.return_type, NEW.definition, NEW.language, NEW.returns_set, NEW.volatility, NEW.parallel, NEW.security);
return NEW;
end;
$$ language plpgsql;
create function meta.function_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'return_type', 'definition', 'language']);
perform meta.require_one(public.hstore(NEW), array['parameters', 'type_sig']);
perform meta.require_one(public.hstore(NEW), array['schema_id', 'schema_name']);
execute meta.stmt_function_drop(OLD.schema_name, OLD.name, OLD.type_sig);
execute meta.stmt_function_create(coalesce(NEW.schema_name, (NEW.schema_id).name), NEW.name, NEW.type_sig, NEW.parameters, NEW.return_type, NEW.definition, NEW.language, NEW.returns_set, NEW.volatility, NEW.parallel, NEW.security);
return NEW;
end;
$$ language plpgsql;
create function meta.function_delete() returns trigger as $$
begin
execute meta.stmt_function_drop(OLD.schema_name, OLD.name, OLD.parameters);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.trigger
*****************************************************************************/
create function meta.stmt_trigger_create(schema_name text, relation_name text, trigger_name text, function_schema_name text, function_name text, "when" text, "insert" boolean, "update" boolean, "delete" boolean, "truncate" boolean, "level" text) returns text as $$
select 'create trigger ' || quote_ident(trigger_name) || ' ' || "when" || ' ' ||
array_to_string(
array[]::text[]
|| case "insert" when true then 'insert'
else null
end
|| case "update" when true then 'update'
else null
end
|| case "delete" when true then 'delete'
else null
end
|| case "truncate" when true then 'truncate'
else null
end,
' or ') ||
' on ' || quote_ident(schema_name) || '.' || quote_ident(relation_name) ||
' for each ' || "level" || ' execute procedure ' ||
quote_ident(function_schema_name) || '.' || quote_ident(function_name) || '()';
$$ language sql;
create function meta.stmt_trigger_drop(schema_name text, relation_name text, trigger_name text) returns text as $$
select 'drop trigger ' || quote_ident(trigger_name) || ' on ' || quote_ident(schema_name) || '.' || quote_ident(relation_name);
$$ language sql;
create function meta.trigger_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'when', 'level']);
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_trigger_create(
coalesce(NEW.schema_name, ((NEW.relation_id).schema_name)),
coalesce(NEW.relation_name, (NEW.relation_id).name),
NEW.name,
((NEW.function_id).schema_name),
(NEW.function_id).name,
NEW."when", NEW."insert", NEW."update", NEW."delete", NEW."truncate", NEW."level"
);
return NEW;
end;
$$ language plpgsql;
create function meta.trigger_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name', 'when', 'level']);
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_trigger_drop(OLD.schema_name, OLD.relation_name, OLD.name);
execute meta.stmt_trigger_create(
coalesce(nullif(NEW.schema_name, OLD.schema_name), ((NEW.relation_id).schema_name)),
coalesce(nullif(NEW.relation_name, OLD.relation_name), (NEW.relation_id).name),
NEW.name,
((NEW.function_id).schema_name),
(NEW.function_id).name,
NEW."when", NEW."insert", NEW."update", NEW."delete", NEW."truncate", NEW."level"
);
return NEW;
end;
$$ language plpgsql;
create function meta.trigger_delete() returns trigger as $$
begin
execute meta.stmt_trigger_drop(OLD.schema_name, OLD.relation_name, OLD.name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.role
*****************************************************************************/
create function meta.stmt_role_create(role_name text, superuser boolean, inherit boolean, create_role boolean, create_db boolean, can_login boolean, replication boolean, connection_limit integer, password text, valid_until timestamp with time zone) returns text as $$
select 'create role ' || quote_ident(role_name) ||
case when superuser then ' with superuser '
else ' with nosuperuser '
end ||
case when inherit then ' inherit '
else ' noinherit '
end ||
case when create_role then ' createrole '
else ' nocreaterole '
end ||
case when create_db then ' createdb '
else ' nocreatedb '
end ||
case when can_login then ' login '
else ' nologin '
end ||
case when replication then ' replication '
else ' noreplication '
end ||
coalesce(' connection limit ' || connection_limit, '') || -- can't take quoted literal
coalesce(' password ' || quote_literal(password), '') ||
coalesce(' valid until ' || quote_literal(valid_until), '');
$$ language sql;
create function meta.stmt_role_rename(role_name text, new_role_name text) returns text as $$
select 'alter role ' || quote_ident(role_name) || ' rename to ' || quote_ident(new_role_name);
$$ language sql;
create function meta.stmt_role_alter(role_name text, superuser boolean, inherit boolean, create_role boolean, create_db boolean, can_login boolean, replication boolean, connection_limit integer, password text, valid_until timestamp with time zone) returns text as $$
select 'alter role ' || quote_ident(role_name) ||
case when superuser then ' with superuser '
else ' with nosuperuser '
end ||
case when inherit then ' inherit '
else ' noinherit '
end ||
case when create_role then ' createrole '
else ' nocreaterole '
end ||
case when create_db then ' createdb '
else ' nocreatedb '
end ||
case when can_login then ' login '
else ' nologin '
end ||
case when replication then ' replication '
else ' noreplication '
end ||
case when connection_limit is not null then ' connection limit ' || connection_limit -- can't take quoted literal
else ''
end ||
case when password is not null and password <> '********' then ' password ' || quote_literal(password)
else ''
end ||
case when valid_until is not null then ' valid until ' || quote_literal(valid_until)
else ''
end;
$$ language sql;
create function meta.stmt_role_reset(role_name text, config_param text) returns text as $$
select 'alter role ' || quote_ident(role_name) || ' reset ' || config_param;
$$ language sql;
create function meta.stmt_role_drop(role_name text) returns text as $$
select 'drop role ' || quote_ident(role_name);
$$ language sql;
create function meta.role_insert() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
execute meta.stmt_role_create(NEW.name, NEW.superuser, NEW.inherit, NEW.create_role, NEW.create_db, NEW.can_login, NEW.replication, NEW.connection_limit, NEW.password, NEW.valid_until);
return NEW;
end;
$$ language plpgsql;
create function meta.role_update() returns trigger as $$
begin
perform meta.require_all(public.hstore(NEW), array['name']);
if OLD.name != NEW.name then
execute meta.stmt_role_rename(OLD.name, NEW.name);
end if;
execute meta.stmt_role_alter(NEW.name, NEW.superuser, NEW.inherit, NEW.create_role, NEW.create_db, NEW.can_login, NEW.replication, NEW.connection_limit, NEW.password, NEW.valid_until);
if OLD.connection_limit is not null and NEW.connection_limit is null then
perform meta.stmt_role_reset(NEW.name, 'connection limit');
end if;
if OLD.password is not null and NEW.password is null then
perform meta.stmt_role_reset(NEW.name, 'password');
end if;
if OLD.valid_until is not null and NEW.valid_until is null then
perform meta.stmt_role_reset(NEW.name, 'valid until');
end if;
return NEW;
end;
$$ language plpgsql;
create function meta.role_delete() returns trigger as $$
begin
execute meta.stmt_role_drop(OLD.name);
return OLD;
end;
$$ language plpgsql;
create function meta.current_role_id() returns meta.role_id as $$
select id from meta.role where name=current_user;
$$ language sql;
/******************************************************************************
* meta.role_inheritance
*****************************************************************************/
create function meta.stmt_role_inheritance_create(role_name text, member_role_name text) returns text as $$
select 'grant ' || quote_ident(role_name) || ' to ' || quote_ident(member_role_name);
$$ language sql;
create function meta.stmt_role_inheritance_drop(role_name text, member_role_name text) returns text as $$
select 'revoke ' || quote_ident(role_name) || ' from ' || quote_ident(member_role_name);
$$ language sql;
create function meta.role_inheritance_insert() returns trigger as $$
begin
perform meta.require_one(public.hstore(NEW), array['role_name', 'role_id']);
perform meta.require_one(public.hstore(NEW), array['member_role_name', 'member_role_id']);
execute meta.stmt_role_inheritance_create(coalesce(NEW.role_name, (NEW.role_id).name), coalesce(NEW.member_role_name, (NEW.member_role_id).name));
return NEW;
end;
$$ language plpgsql;
create function meta.role_inheritance_update() returns trigger as $$
begin
perform meta.require_one(public.hstore(NEW), array['role_id', 'role_name']);
perform meta.require_one(public.hstore(NEW), array['member_role_id', 'member_role_name']);
execute meta.stmt_role_inheritance_drop((OLD.role_id).name, (OLD.member_role_id).name);
execute meta.stmt_role_inheritance_create(coalesce(NEW.role_name, (NEW.role_id).name), coalesce(NEW.member_role_name, (NEW.member_role_id).name));
return NEW;
end;
$$ language plpgsql;
create function meta.role_inheritance_delete() returns trigger as $$
begin
execute meta.stmt_role_inheritance_drop((OLD.role_id).name, (OLD.member_role_id).name);
return OLD;
end;
$$ language plpgsql;
/******************************************************************************
* meta.table_privilege
*****************************************************************************/
create function meta.stmt_table_privilege_create(schema_name text, table_name text, role_name text, type text) returns text as $$
-- TODO: create privilege_type so that "type" can be escaped here
select 'grant ' || type || ' on ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' to ' || quote_ident(role_name);
$$ language sql;
create function meta.stmt_table_privilege_drop(schema_name text, table_name text, role_name text, type text) returns text as $$
-- TODO: create privilege_type so that "type" can be escaped here
select 'revoke ' || type || ' on ' || quote_ident(schema_name) || '.' || quote_ident(table_name) || ' from ' || quote_ident(role_name);
$$ language sql;