-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteInvestigation.sql
76 lines (58 loc) · 2.28 KB
/
deleteInvestigation.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
-- FUNCTION: public.delete_investigation(integer, integer)
-- DROP FUNCTION public.delete_investigation(integer, integer);
CREATE OR REPLACE FUNCTION public.delete_investigation(
delete_epi_number integer)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
declare
investigations_in_group int4;
group_id_to_inspect uuid;
begin
DELETE FROM public.investigated_patient_symptoms
where investigation_id = delete_epi_number;
DELETE FROM public.exposure
where investigation_id = delete_epi_number;
DELETE FROM public.contacted_person
where contact_event in (select id from public.contact_event where investigation_id = delete_epi_number);
DELETE FROM public.investigated_patient_background_diseases
WHERE investigated_patient_id in (select id from public.investigated_patient where covid_patient = delete_epi_number);
DELETE FROM public.investigation_settings
WHERE epidemiology_number = delete_epi_number;
DELETE FROM public.involved_contact
WHERE investigation_id = delete_epi_number;
DELETE FROM public.contact_event
where investigation_id = delete_epi_number;
-- deleting persons from involved contacts
DELETE FROM public.person
WHERE id in (select person_id from public.involved_contact where investigation_id = delete_epi_number);
-- deleting persons that are related to contact_event
DELETE FROM public.person
where id in (select person_info from public.contacted_person where contact_event in (select id from public.contact_event where investigation_id = delete_epi_number));
SELECT INTO group_id_to_inspect
group_id
FROM investigation
WHERE epidemiology_number = delete_epi_number;
DELETE FROM public.investigation
WHERE epidemiology_number = delete_epi_number;
DELETE FROM public.investigated_patient
where covid_patient = delete_epi_number;
SELECT INTO investigations_in_group
COUNT(*)
FROM investigation
WHERE group_id = group_id_to_inspect;
-- if in the future the minimum for group will be increased
IF investigations_in_group < 2 THEN
-- in order to avoid unnecessary scan
IF investigations_in_group > 0 THEN
UPDATE investigation
SET group_id = null
WHERE group_id = group_id_to_inspect;
END IF;
DELETE FROM public.investigation_group
WHERE id = group_id_to_inspect;
END IF;
end;
$BODY$;