-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprod_upserts.sql
More file actions
3629 lines (3407 loc) · 147 KB
/
prod_upserts.sql
File metadata and controls
3629 lines (3407 loc) · 147 KB
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
--======================================================================
/*
Name: prod_upsert
Parameters: none
Returns: NA
Author: G. Cattabriga
Date: 2020.09.15
Description: contain the upsert functions used in ESCALATE
Notes:
*/
--======================================================================
/*
Name: upsert_tag_type()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.20
Description: trigger proc that deletes, inserts or updates tag_type record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_tag_type (type, description) values ('TESTDEV', 'tags used to help identify development cycle phase');
insert into vw_tag_type (type) values ('TESTDEV');
update vw_tag_type set description = 'tags used to help identify development cycle phase; e.g. SPEC, TEST, DEV' where tag_type_uuid = (select tag_type_uuid from vw_tag_type where (type = 'TESTDEV'));
update vw_tag_type set type = 'TESTDEV1', description = 'tags used to help identify development cycle phase; e.g. SPEC, TEST, DEV' where tag_type_uuid = (select tag_type_uuid from vw_tag_type where (type = 'TESTDEV'));
delete from vw_tag_type where tag_type_uuid = (select tag_type_uuid from vw_tag_type where (type = 'TESTDEV'));
delete from vw_tag_type where tag_type_uuid = (select tag_type_uuid from vw_tag_type where (type = 'TESTDEV1'));
*/
CREATE OR REPLACE FUNCTION upsert_tag_type ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the tag_type record
DELETE FROM tag_type
WHERE tag_type_uuid = OLD.tag_type_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.tag_type_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
tag_type
SET
type = NEW.type,
description = NEW.description,
mod_date = now()
WHERE
tag_type.tag_type_uuid = NEW.tag_type_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO tag_type (type, description)
VALUES(NEW.type, NEW.description) returning tag_type_uuid into NEW.tag_type_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_tag()
Parameters: trigger proc that deletes, inserts or updates tag record based on TG_OP (trigger operation)
Returns: void
Author: G. Cattabriga
Date: 2020.06.22
Description: trigger proc that deletes, inserts or updates tag record based on TG_OP (trigger operation)
Notes: will not be able to delete a tag if any connected records in tag_x exist
Example: -- insert new tag (tag_uuid = NULL, ref_tag_uuid = NULL)
insert into vw_tag (display_text, description, actor_uuid, tag_type_uuid)
values ('invalid', 'invalid experiment', (select actor_uuid from vw_actor where person_last_name = 'Alves'), null);
update vw_tag set description = 'invalid experiment with stuff added',
tag_type_uuid = (select tag_type_uuid from vw_tag_type where type = 'experiment')
where tag_uuid = (select tag_uuid from vw_tag where (display_text = 'invalid'));
delete from vw_tag where tag_uuid = (select tag_uuid from vw_tag where (display_text = 'invalid' and type = 'experiment'));
*/
CREATE OR REPLACE FUNCTION upsert_tag ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
DELETE FROM tag
WHERE tag_uuid = OLD.tag_uuid;
-- delete any associated notes using the tag_uuid
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.tag_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
tag
SET
display_text = NEW.display_text,
description = NEW.description,
tag_type_uuid = NEW.tag_type_uuid,
actor_uuid = NEW.actor_uuid,
mod_date = now()
WHERE
tag.tag_uuid = NEW.tag_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO tag (display_text, description, tag_type_uuid, actor_uuid)
VALUES(NEW.display_text, NEW.description, NEW.tag_type_uuid, NEW.actor_uuid) returning tag_uuid into NEW.tag_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_tag_assign()
Parameters: trigger proc that deletes, inserts or updates tag_x record based on TG_OP (trigger operation)
Returns: void
Author: G. Cattabriga
Date: 2020.06.22
Description: trigger proc that deletes, inserts or updates tag_x record based on TG_OP (trigger operation)
Notes: requires both ref_tag_uuid and tag_uuid
Example: -- insert new tag_assign (ref_tag)
insert into vw_tag_assign (tag_uuid, ref_tag_uuid) values ((select tag_uuid from vw_tag
where (display_text = 'inactive' and vw_tag.type = 'actor')), (select actor_uuid from vw_actor where person_last_name = 'Alves') );
delete from vw_tag_assign where tag_uuid = (select tag_uuid from vw_tag
where (display_text = 'inactive' and vw_tag.type = 'actor') and ref_tag_uuid = (select actor_uuid from vw_actor where person_last_name = 'Alves') );
*/
CREATE OR REPLACE FUNCTION upsert_tag_assign ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
DELETE FROM tag_x
WHERE (tag_uuid = OLD.tag_uuid)
and(ref_tag_uuid = OLD.ref_tag_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO tag_x (ref_tag_uuid, tag_uuid)
VALUES(NEW.ref_tag_uuid, NEW.tag_uuid);
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_udf_def()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.22
Description: trigger proc that deletes, inserts or updates udf_def record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_udf_def (description, val_type_uuid)
values ('user defined 1', (select type_def_uuid from vw_type_def where category = 'data' and description = 'text'));
update vw_udf_def set unit = 'test-unit' where
udf_def_uuid = (select udf_def_uuid from vw_udf_def where (description = 'user defined 1'));
delete from vw_udf_def where udf_def_uuid = (select udf_def_uuid from udf_def where (description = 'user defined 1'));
*/
CREATE OR REPLACE FUNCTION upsert_udf_def ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the udf_def record
DELETE FROM udf_def
WHERE udf_def_uuid = OLD.udf_def_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- then delete any associated note record
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.udf_def_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
udf_def
SET
val_type_uuid = NEW.val_type_uuid,
description = NEW.description,
unit = NEW.unit,
mod_date = now()
WHERE
udf_def.udf_def_uuid = NEW.udf_def_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO udf_def (description, val_type_uuid, unit)
VALUES(NEW.description, NEW.val_type_uuid, NEW.unit) returning udf_def_uuid into NEW.udf_def_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_udf()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.09.07
Description: trigger proc that deletes, inserts or updates udf record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_udf (ref_udf_uuid, udf_def_uuid, udf_val_val) values
((select actor_uuid from vw_actor where description = 'HC'),
(select udf_def_uuid from vw_udf_def where description = 'user defined 1')
, 'some text: a, b, c, d');
update vw_udf set udf_val_val = 'some more text: a, b, c, d, e, f' where
udf_def_uuid = (select udf_def_uuid from vw_udf_def where (description = 'user defined 1'));
delete from vw_udf where udf_def_uuid = (select udf_def_uuid from udf_def where (description = 'user defined 1'));
*/
CREATE OR REPLACE FUNCTION upsert_udf ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the udf_x record
DELETE FROM udf_x
WHERE udf_uuid = OLD.udf_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM udf
WHERE udf_uuid = OLD.udf_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- then delete any associated note record
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.udf_def_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
udf
SET
udf_val = (select put_val (NEW.udf_val_type_uuid, NEW.udf_val_val, NEW.udf_val_unit)),
mod_date = now()
WHERE
udf.udf_uuid = NEW.udf_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
IF NEW.udf_def_uuid is null THEN
return null;
END IF;
INSERT INTO udf (udf_def_uuid, udf_val)
VALUES(NEW.udf_def_uuid,
(select put_val ((select val_type_uuid from vw_udf_def where udf_def_uuid = NEW.udf_def_uuid),
NEW.udf_val_val,
(select unit from vw_udf_def where udf_def_uuid = NEW.udf_def_uuid)))) returning udf_uuid into NEW.udf_uuid;
INSERT INTO udf_x (ref_udf_uuid, udf_uuid)
VALUES (NEW.ref_udf_uuid, NEW.udf_uuid);
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_status()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.20
Description: trigger proc that deletes, inserts or updates status record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_status (description) values ('testtest');
update vw_status set description = 'testtest status' where status_uuid = (select status_uuid from vw_status where (description = 'testtest'));
delete from vw_status where status_uuid = (select status_uuid from vw_status where (description = 'testtest status'));
*/
CREATE OR REPLACE FUNCTION upsert_status ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the status record
DELETE FROM status
WHERE status_uuid = OLD.status_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.status_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
status
SET
description = NEW.description,
mod_date = now()
WHERE
status.status_uuid = NEW.status_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO status (description)
VALUES(NEW.description) returning status_uuid into NEW.status_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_type_def()
Parameters: type_def_category (enum type) and description required on insert
Returns: void
Author: G. Cattabriga
Date: 2020.09.01
Description: trigger proc that deletes, inserts or updates note and note_x (for the object ref_note_uuid) based on TG_OP (trigger operation)
Notes: must have ref_note_uuid in order to return appropriate notes for that entity
Example: insert into vw_type_def (category, description) values ('data', 'bool');
insert into vw_type_def (category, description) values ('file', 'pdf');
update vw_type_def set description = 'svg' where type_def_uuid = (select type_def_uuid from
vw_type_def where category = 'file' and description = 'pdf');
delete from vw_type_def where type_def_uuid = (select type_def_uuid from vw_type_def where category = 'data' and description = 'bool');
delete from vw_type_def where type_def_uuid = (select type_def_uuid from vw_type_def where category = 'file' and description = 'svg');
*/
CREATE OR REPLACE FUNCTION upsert_type_def ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
DELETE FROM type_def
WHERE type_def_uuid = OLD.type_def_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
type_def
SET
category = NEW.category,
description = NEW.description,
mod_date = now()
WHERE
type_def.type_def_uuid = NEW.type_def_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO type_def (category, description)
VALUES(NEW.category, NEW.description)
RETURNING type_def_uuid INTO NEW.type_def_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_note()
Parameters: ref_note_uuid required on insert
Returns: void
Author: G. Cattabriga
Date: 2020.06.26
Description: trigger proc that deletes, inserts or updates note and note_x (for the object ref_note_uuid) based on TG_OP (trigger operation)
Notes: must have ref_note_uuid in order to return appropriate notes for that entity
Example: insert into vw_note (notetext, actor_uuid, ref_note_uuid) values ('test note', (select actor_uuid from vw_actor where person_last_name = 'Cattabriga'), (select actor_uuid from vw_actor where person_last_name = 'Cattabriga'));
insert into vw_note (notetext, actor_uuid) values ('test note', (select actor_uuid from vw_actor where person_last_name = 'Cattabriga'));
update vw_note set notetext = 'test note with additional text...' where note_uuid = (select note_uuid from vw_note where (notetext = 'test note'));
delete from vw_note where note_uuid = (select note_uuid from vw_note where (notetext = 'test note with additional text...'));
--- delete all notes associated with a given entity
insert into vw_note (notetext, actor_uuid, ref_note_uuid) values ('test note 1', (select actor_uuid from vw_actor where person_last_name = 'Alves'), (select actor_uuid from vw_actor where person_last_name = 'Alves'));
insert into vw_note (notetext, actor_uuid, ref_note_uuid) values ('test note 2', (select actor_uuid from vw_actor where person_last_name = 'Alves'), (select actor_uuid from vw_actor where person_last_name = 'Alves'));
insert into vw_note (notetext, actor_uuid, ref_note_uuid) values ('test note 2', (select actor_uuid from vw_actor where person_last_name = 'Alves'), (select actor_uuid from vw_actor where person_last_name = 'Alves'));
delete from vw_note where note_uuid in (select note_uuid from vw_note where actor_uuid = (select actor_uuid from vw_actor where person_last_name = 'Alves'));
*/
CREATE OR REPLACE FUNCTION upsert_note ()
RETURNS TRIGGER
AS $$
DECLARE
_note_uuid uuid;
BEGIN
IF(TG_OP = 'DELETE') THEN
DELETE FROM note_x
WHERE note_x_uuid = OLD.note_x_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM note
WHERE note_uuid = OLD.note_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
note
SET
notetext = NEW.notetext,
actor_uuid = NEW.actor_uuid,
mod_date = now()
WHERE
note.note_uuid = NEW.note_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
IF NEW.ref_note_uuid IS NULL THEN
RETURN NULL;
END IF;
INSERT INTO note (notetext, actor_uuid)
VALUES(NEW.notetext, NEW.actor_uuid)
RETURNING
note_uuid INTO _note_uuid;
INSERT INTO note_x (ref_note_uuid, note_uuid)
VALUES(NEW.ref_note_uuid, _note_uuid) returning _note_uuid into NEW.note_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_edocument()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.08.12
Description: trigger proc that deletes, inserts or updates edocument record based on TG_OP (trigger operation)
Notes:
Example: -- just insert the document, with no association to an entity
insert into vw_edocument (title, description, filename, source, edocument, doc_type_uuid, doc_ver,
actor_uuid, status_uuid, ref_edocument_uuid)
values ('Test document 1', 'This is a test document', null, null, 'a bunch of text cast as a blob'::bytea, (select type_def_uuid from vw_type_def where category = 'file' and description = 'text'), null,
(select actor_uuid from vw_actor where description = 'Gary Cattabriga'), (select status_uuid from vw_status where description = 'active'),
null);
-- now associate the edocument to an actor
update vw_edocument set ref_edocument_uuid = (select actor_uuid from vw_actor where description = 'Gary Cattabriga') where
edocument_uuid = (select edocument_uuid from vw_edocument where title = 'Test document 1');
delete from vw_edocument where edocument_uuid = (select edocument_uuid from vw_edocument where title = 'Test document 1');
*/
CREATE OR REPLACE FUNCTION upsert_edocument ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the document_x record if exists
DELETE FROM edocument_x
WHERE edocument_x_uuid = OLD.edocument_x_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM edocument
WHERE edocument_uuid = OLD.edocument_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM vw_note
WHERE ref_note_uuid = OLD.edocument_uuid;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
edocument
SET
title = NEW.title,
description = NEW.description,
filename = NEW.filename,
source = NEW.source,
edocument = NEW.edocument,
doc_type_uuid = NEW.doc_type_uuid,
doc_ver = NEW.doc_ver,
actor_uuid = NEW.actor_uuid,
status_uuid = NEW.status_uuid,
mod_date = now()
WHERE
edocument.edocument_uuid = NEW.edocument_uuid;
INSERT INTO edocument_x (ref_edocument_uuid, edocument_uuid)
VALUES(NEW.ref_edocument_uuid, NEW.edocument_uuid) ON CONFLICT DO NOTHING;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO edocument (title, description, filename, source, edocument, doc_type_uuid, doc_ver, actor_uuid, status_uuid)
VALUES(NEW.title, NEW.description, NEW.filename, NEW.source, NEW.edocument, NEW.doc_type_uuid, NEW.doc_ver,
NEW.actor_uuid, NEW.status_uuid)
RETURNING edocument_uuid INTO NEW.edocument_uuid;
IF NEW.ref_edocument_uuid IS NOT NULL THEN
INSERT INTO edocument_x (ref_edocument_uuid, edocument_uuid)
VALUES(NEW.ref_edocument_uuid, NEW.edocument_uuid);
END IF;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
--======================================================================
--======================================================================
/*
Name: upsert_actor ()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.07.15
Description: trigger proc that deletes, inserts or updates actor
Notes: there is going to be a lot of dependencies on actor, so a 'delete' will need a lot of cleanup first; easier to just change status to 'inactive' or something like that
Example: insert into vw_person (last_name, first_name, middle_name, address1, address2, city, state_province, zip, country, phone, email, title, suffix, organization_uuid)
values ('Tester','Lester','Fester','1313 Mockingbird Ln',null,'Munsterville','NY',null,null,null,null,null,null,null) returning *;
delete from vw_person where person_uuid = (select person_uuid from vw_person
where (last_name = 'Tester' and first_name = 'Lester'));
insert into vw_actor (person_uuid, description, status_uuid)
values ((select person_uuid from vw_person where (last_name = 'Tester' and first_name = 'Lester')), 'Lester the Actor',
(select status_uuid from vw_status where description = 'active')) returning *;
-- now add a note
insert into vw_note (notetext, actor_uuid, ref_note_uuid)
values ('test note for Lester the Actor', (select actor_uuid from vw_actor where person_last_name = 'Tester'),
(select actor_uuid from vw_actor where person_last_name = 'Tester'));
-- assign a tag
insert into vw_tag_assign (tag_uuid, ref_tag_uuid)
values ((select tag_uuid from vw_tag where (display_text = 'do_not_use' and type = 'actor')),
(select actor_uuid from vw_actor where person_last_name = 'Tester'));
-- assign a udf
insert into vw_udf (ref_udf_uuid, udf_def_uuid, udf_val_val) values
((select actor_uuid from vw_actor where person_last_name = 'Tester'),
(select udf_def_uuid from vw_udf_def where description = 'batch count'),
'123 -> batch no. test');
-- update the description for the actor
update vw_actor set description = 'new description for Lester the Actor'
where person_uuid = (select person_uuid from vw_person where (last_name = 'Tester' and first_name = 'Lester'));
update vw_actor set organization_uuid = (select organization_uuid from vw_organization where full_name = 'Haverford College')
where person_uuid = (select person_uuid from person where (last_name = 'Tester' and first_name = 'Lester'));
delete from vw_actor where actor_uuid in (select actor_uuid from vw_actor where description = 'Lester the Actor');
delete from vw_note where note_uuid in (select note_uuid from vw_note where actor_uuid = (select actor_uuid from vw_actor where person_last_name = 'Tester'));
delete from vw_actor where person_uuid = (select person_uuid from vw_person where (last_name = 'Tester' and first_name = 'Lester'));
*/
CREATE OR REPLACE FUNCTION upsert_actor ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete all the actor_pref records
DELETE FROM vw_actor_pref
WHERE actor_uuid = OLD.actor_uuid;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.actor_uuid);
-- then delete the actor record
DELETE FROM actor
WHERE actor_uuid = OLD.actor_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- then delete the associated [sub]actor (org, person, systemtool)
IF OLD.organization_uuid is not NULL THEN
DELETE FROM vw_organization WHERE organization_uuid = OLD.organization_uuid;
ELSIF OLD.person_uuid is not NULL THEN
DELETE FROM vw_person WHERE person_uuid = OLD.person_uuid;
ELSIF OLD.person_uuid is not NULL THEN
DELETE FROM vw_systemtool WHERE systemtool_uuid = OLD.systemtool_uuid;
END IF;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
actor
SET
organization_uuid = NEW.organization_uuid,
person_uuid = NEW.person_uuid,
systemtool_uuid = NEW.systemtool_uuid,
description = NEW.description,
status_uuid = NEW.status_uuid,
mod_date = now()
WHERE
actor.actor_uuid = NEW.actor_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
IF (NEW.organization_uuid is NULL and NEW.person_uuid is NULL and NEW.systemtool_uuid is NULL) THEN
RETURN NULL;
ELSE
INSERT INTO actor (organization_uuid, person_uuid, systemtool_uuid, description, status_uuid)
VALUES(NEW.organization_uuid, NEW.person_uuid, NEW.systemtool_uuid, NEW.description, NEW.status_uuid) returning actor_uuid into NEW.actor_uuid;
RETURN NEW;
END IF;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_actor_pref()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.07.20
Description: trigger proc that deletes, inserts or updates actor_pref record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_actor_pref (actor_uuid, pkey, pvalue) values ((select actor_uuid from vw_actor where person_last_name = 'Tester'), 'test_key', 'test_value');
update vw_actor_pref set pvalue = 'new_new_test_value' where actor_pref_uuid = (select actor_pref_uuid from vw_actor_pref where actor_uuid = (select actor_uuid from vw_actor where description = 'Lester Fester Tester') and pkey = 'test_key');
delete from vw_actor_pref where actor_pref_uuid = (select actor_pref_uuid from vw_actor_pref where actor_uuid = (select actor_uuid from vw_actor where description = 'Lester Fester Tester'));
*/
CREATE OR REPLACE FUNCTION upsert_actor_pref ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the actor_pref record
DELETE FROM actor_pref
WHERE actor_pref_uuid = OLD.actor_pref_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.actor_pref_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
actor_pref
SET
pkey = NEW.pkey,
pvalue = NEW.pvalue,
mod_date = now()
WHERE
actor_pref.actor_pref_uuid = NEW.actor_pref_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO actor_pref (actor_uuid, pkey, pvalue)
VALUES(NEW.actor_uuid, NEW.pkey, NEW.pvalue) returning actor_pref_uuid into NEW.actor_pref_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_organization ()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.16
Description: trigger proc that deletes, inserts or updates organization record based on TG_OP (trigger operation)
Notes: added functionality to insert a NEW organization into a NEW actor
Example: -- note: this insert also inserts record into actor
insert into vw_organization (description, full_name, short_name, address1, address2, city, state_province, zip, country, website_url, phone, parent_uuid) values ('some description here','IBM','IBM','1001 IBM Lane',null,'Some City','NY',null,null,null,null,null);
update vw_organization set description = 'some [new] description here', city = 'Some [new] City', zip = '00000' where full_name = 'IBM';
update vw_organization set parent_uuid = (select organization_uuid from organization where organization.full_name = 'Haverford College') where full_name = 'IBM';
-- if related actor exists, will not be able to delete
delete from vw_organization where full_name = 'IBM';
delete from vw_actor where organization_uuid = (select organization_uuid from vw_organization where full_name = 'IBM');
*/
CREATE OR REPLACE FUNCTION upsert_organization ()
RETURNS TRIGGER
AS $$
DECLARE
_org_description varchar;
_org_uuid uuid;
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the organization record
DELETE FROM organization
WHERE organization_uuid = OLD.organization_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.organization_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
organization
SET
description = NEW.description,
full_name = NEW.full_name,
short_name = NEW.short_name,
address1 = NEW.address1,
address2 = NEW.address2,
city = NEW.city,
state_province = NEW.state_province,
zip = NEW.zip,
country = NEW.country,
website_url = NEW.website_url,
phone = NEW.phone,
parent_uuid = NEW.parent_uuid,
mod_date = now()
WHERE
organization.full_name = NEW.full_name;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO organization (description, full_name, short_name, address1, address2, city, state_province, zip, country, website_url, phone, parent_uuid) VALUES(NEW.description, NEW.full_name, NEW.short_name, NEW.address1, NEW.address2, NEW.city, NEW.state_province, NEW.zip, NEW.country, NEW.website_url, NEW.phone, NEW.parent_uuid) returning organization_uuid, short_name into _org_uuid, _org_description;
insert into vw_actor (organization_uuid, description, status_uuid) values (_org_uuid, _org_description, (select status_uuid from vw_status where description = 'active')) returning _org_uuid into NEW.organization_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_person ()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.17
Description: trigger proc that deletes, inserts or updates person record based on TG_OP (trigger operation)
Notes: added functionality to insert a NEW organization into a NEW actor
Example: -- note: this insert also inserts record into actor
insert into vw_person (last_name, first_name, middle_name, address1, address2, city, state_province, zip, country, phone, email, title, suffix, organization_uuid)
values ('Tester','Lester','Fester','1313 Mockingbird Ln',null,'Munsterville','NY',null,null,null,null,null,null,null) returning *;
update vw_person set title = 'Mr', city = 'Some [new] City', zip = '99999', email = 'TesterL@scarythings.xxx' where person_uuid =
(select person_uuid from person where (last_name = 'Tester' and first_name = 'Lester')) returning *;
update vw_person set organization_uuid = (select organization_uuid from organization where organization.full_name = 'Haverford College') where (last_name = 'Tester' and first_name = 'Lester') returning *;
delete from vw_person where person_uuid = (select person_uuid from person where (last_name = 'Tester' and first_name = 'Lester'));
delete from vw_actor where person_uuid = (select person_uuid from vw_person where (last_name = 'Tester' and first_name = 'Lester'));
*/
CREATE OR REPLACE FUNCTION upsert_person ()
RETURNS TRIGGER
AS $$
DECLARE
_person_first_name varchar;
_person_last_name varchar;
_person_middle_name varchar;
_person_uuid uuid;
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the person record
DELETE FROM person
WHERE person_uuid = OLD.person_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.person_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
person
SET
last_name = NEW.last_name,
first_name = NEW.first_name,
middle_name = NEW.middle_name,
address1 = NEW.address1,
address2 = NEW.address2,
city = NEW.city,
state_province = NEW.state_province,
zip = NEW.zip,
country = NEW.country,
phone = NEW.phone,
email = NEW.email,
title = NEW.title,
suffix = NEW.suffix,
organization_uuid = NEW.organization_uuid,
mod_date = now()
WHERE
person.person_uuid = NEW.person_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO person (last_name, first_name, middle_name, address1, address2, city, state_province, zip, country, phone, email, title, suffix, organization_uuid) VALUES(NEW.last_name, NEW.first_name, NEW.middle_name, NEW.address1, NEW.address2, NEW.city, NEW.state_province, NEW.zip, NEW.country, NEW.phone, NEW.email, NEW.title, NEW.suffix, NEW.organization_uuid) returning person_uuid, first_name, middle_name, last_name into _person_uuid, _person_first_name, _person_middle_name, _person_last_name;
insert into vw_actor (person_uuid, description, status_uuid) values (_person_uuid, trim(concat(_person_first_name,' ', _person_last_name)), (select status_uuid from vw_status where description = 'active')) returning _person_uuid into NEW.person_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_systemtool()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.17
Description: trigger proc that deletes, inserts or updates systemtool record based on TG_OP (trigger operation)
Notes: added functionality to insert a NEW organization into a NEW actor
Example: -- note: this insert also inserts record into actor
insert into vw_systemtool (systemtool_name, description, systemtool_type_uuid, vendor_organization_uuid, model, serial, ver) values ('MRROBOT', 'MR Robot to you',(select systemtool_type_uuid from vw_systemtool_type where description = 'API'),(select organization_uuid from vw_organization where full_name = 'ChemAxon'),'super duper', null, '1.0') returning *;
update vw_systemtool set serial = 'ABC-1234' where systemtool_uuid = (select systemtool_uuid from vw_systemtool where (systemtool_name = 'MRROBOT'));
update vw_systemtool set ver = '1.1' where systemtool_uuid = (select systemtool_uuid from vw_systemtool where systemtool_name = 'MRROBOT');
update vw_systemtool set ver = '1.2' where systemtool_uuid = (select systemtool_uuid from vw_systemtool where systemtool_name = 'MRROBOT' and ver = '1.1');
delete from actor where systemtool_uuid in (select systemtool_uuid from systemtool where systemtool_name = 'MRROBOT');
delete from vw_systemtool where systemtool_uuid in (select systemtool_uuid from vw_systemtool where systemtool_name = 'MRROBOT');
delete from vw_systemtool where systemtool_uuid = (select systemtool_uuid from vw_systemtool where systemtool_name = 'MRROBOT' and ver = '1.1');
*/
CREATE OR REPLACE FUNCTION upsert_systemtool ()
RETURNS TRIGGER
AS $$
DECLARE
_systemtool_description varchar;
_systemtool_uuid uuid;
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the systemtool record
DELETE FROM systemtool
WHERE systemtool_uuid = OLD.systemtool_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.systemtool_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
-- check to see if it's a version change of the tool. if so, then create a new record
if(OLD.ver != NEW.ver) THEN
INSERT INTO systemtool (systemtool_name, description, systemtool_type_uuid, vendor_organization_uuid, model, serial, ver) VALUES(NEW.systemtool_name, NEW.description, NEW.systemtool_type_uuid, NEW.vendor_organization_uuid, NEW.model, NEW.serial, NEW.ver) returning systemtool_uuid, description into _systemtool_uuid, _systemtool_description;
insert into vw_actor (systemtool_uuid, description, status_uuid) values (_systemtool_uuid, _systemtool_description, (select status_uuid from vw_status where description = 'active'));
RETURN NEW;
ELSE
UPDATE
systemtool
SET
systemtool_name = NEW.systemtool_name,
description = NEW.description,
systemtool_type_uuid = NEW.systemtool_type_uuid,
vendor_organization_uuid = NEW.vendor_organization_uuid,
model = NEW.model,
serial = NEW.serial,
ver = NEW.ver,
mod_date = now()
WHERE
systemtool.systemtool_uuid = NEW.systemtool_uuid;
RETURN NEW;
END IF;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO systemtool (systemtool_name, description, systemtool_type_uuid, vendor_organization_uuid, model, serial, ver) VALUES(NEW.systemtool_name, NEW.description, NEW.systemtool_type_uuid, NEW.vendor_organization_uuid, NEW.model, NEW.serial, NEW.ver) returning systemtool_uuid, description into _systemtool_uuid, _systemtool_description;
insert into vw_actor (systemtool_uuid, description, status_uuid) values (_systemtool_uuid, _systemtool_description, (select status_uuid from vw_status where description = 'active')) returning _systemtool_uuid into NEW.systemtool_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_systemtool_type()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.06.17
Description: trigger proc that deletes, inserts or updates systemtool_type record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_systemtool_type (description) values ('TEST Systemtool Type');
delete from vw_systemtool_type where systemtool_type_uuid = (select systemtool_type_uuid from vw_systemtool_type where (description = 'TEST Systemtool Type'));
*/
CREATE OR REPLACE FUNCTION upsert_systemtool_type ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the systemtool_type record
DELETE FROM systemtool_type
WHERE systemtool_type_uuid = OLD.systemtool_type_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.systemtool_type_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
systemtool_type
SET
description = NEW.description,
mod_date = now()
WHERE
systemtool_type.systemtool_type_uuid = NEW.systemtool_type_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO systemtool_type (description)
VALUES(NEW.description) returning systemtool_type_uuid into NEW.systemtool_type_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_measure_type()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.11.01
Description: trigger proc that deletes, inserts or updates measure_type record based on TG_OP (trigger operation)
Notes:
Example: insert into vw_measure_type (description, actor_uuid, status_uuid) values
('TEST measure type',
(select actor_uuid from vw_actor where org_short_name = 'HC'),
null);
update vw_measure_type set
status_uuid = (select status_uuid from vw_status where description = 'active') where (description = 'TEST measure type');
delete from vw_measure_type where measure_type_uuid = (select measure_type_uuid from vw_measure_type where (description = 'TEST measure type'));
*/
CREATE OR REPLACE FUNCTION upsert_measure_type ()
RETURNS TRIGGER
AS $$
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the measure_type record
DELETE FROM measure_type
WHERE measure_type_uuid = OLD.measure_type_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.measure_type_uuid);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
UPDATE
measure_type
SET
description = NEW.description,
actor_uuid = NEW.actor_uuid,
status_uuid = NEW.status_uuid,
mod_date = now()
WHERE
measure_type.measure_type_uuid = NEW.measure_type_uuid;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO measure_type (description, actor_uuid, status_uuid)
VALUES(NEW.description, NEW.actor_uuid, NEW.status_uuid) returning measure_type_uuid into NEW.measure_type_uuid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
/*
Name: upsert_measure()
Parameters:
Returns: void
Author: G. Cattabriga
Date: 2020.11.01
Description: trigger proc that deletes, inserts or updates measure record based on TG_OP (trigger operation)
Notes: this must be associated with an entity (i.e. ref_measure_uuid cannot be NULL)
Example: insert into vw_measure (measure_def_uuid, measure_type_uuid, ref_measure_uuid, description, measure_value, actor_uuid, status_uuid) values
(
null,
null,
(select material_uuid from vw_material where description = 'Formic Acid'),
'TEST measure',
(select put_val(
(select get_type_def ('data', 'num')),
'3.1415926535',
'slice')),
(select actor_uuid from vw_actor where org_short_name = 'HC'),
null);
update vw_measure set
status_uuid = (select status_uuid from vw_status where description = 'active') where (description = 'TEST measure');
delete from vw_measure where measure_uuid = (select measure_uuid from vw_measure where description = 'TEST measure');
*/
CREATE OR REPLACE FUNCTION upsert_measure ()
RETURNS TRIGGER
AS $$
DECLARE
_measure_type_uuid uuid;
_measure_value val;
BEGIN
IF(TG_OP = 'DELETE') THEN
-- first delete the measure_x record
DELETE FROM measure_x
WHERE measure_uuid = OLD.measure_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
DELETE FROM measure
WHERE measure_uuid = OLD.measure_uuid;
IF NOT FOUND THEN
RETURN NULL;
END IF;
-- delete any assigned records
PERFORM delete_assigned_recs (OLD.measure_uuid);
RETURN OLD;