File: clin-episode.sql

package info (click to toggle)
gnumed-server 16.17-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 42,064 kB
  • sloc: sql: 1,190,603; python: 11,850; sh: 1,082; makefile: 19
file content (112 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (7)
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
-- ==============================================================
-- GNUmed database schema change script
--
-- Source database version: v2
-- Target database version: v3
--
-- What it does:
-- - modify clin.episode
--
-- License: GPL v2 or later
-- Author: Karsten Hilbert/Syan Tan
-- 
-- ==============================================================
-- $Id: clin-episode.sql,v 1.3 2006-12-11 17:02:46 ncq Exp $
-- $Revision: 1.3 $

-- --------------------------------------------------------------
\set ON_ERROR_STOP 1

-- --------------------------------------------------------------
\unset ON_ERROR_STOP
drop index clin.idx_episode_valid_issue;
drop index clin.idx_episode_with_issue;
drop index clin.idx_episode_without_issue;
drop index clin.idx_episode_modified_by;
\set ON_ERROR_STOP 1

create index idx_episode_with_issue on clin.episode(fk_health_issue) where fk_health_issue is not null;
comment on index clin.idx_episode_with_issue is
	'index episodes with associated health issue by their issue';

create index idx_episode_without_issue on clin.episode(fk_health_issue) where fk_health_issue is null;
comment on index clin.idx_episode_without_issue is
	'index episodes without associated health issue';

create index idx_episode_modified_by on clin.episode(modified_by);

-- --------------------------------------------------------------
\unset ON_ERROR_STOP
drop function trf_announce_episode_mod() cascade;
\set ON_ERROR_STOP 1

create function audit.trf_announce_episode_mod()
	returns trigger
	language 'plpgsql'
	as '
declare
	patient_pk integer;
begin
	-- get patient ID
	if TG_OP = ''DELETE'' then
		-- if no patient in episode
		if OLD.fk_patient is null then
			-- get it from attached health issue
			select into patient_pk fk_patient
				from clin.health_issue
				where pk = OLD.fk_health_issue;
		else
			patient_pk := OLD.fk_patient;
		end if;
	else
		-- if no patient in episode
		if NEW.fk_patient is null then
			-- get it from attached health issue
			select into patient_pk fk_patient
				from clin.health_issue
				where pk = NEW.fk_health_issue;
		else
			patient_pk := NEW.fk_patient;
		end if;
	end if;
	-- execute() the NOTIFY
	execute ''notify "episode_change_db:'' || patient_pk || ''"'';
	return NULL;
end;
';

create trigger tr_episode_mod
	after insert or delete or update
	on clin.episode
	for each row
		execute procedure audit.trf_announce_episode_mod()
;

-- --------------------------------------------------------------
-- don't forget appropriate grants
--grant select on forgot_to_edit_grants to group "gm-doctors";

-- --------------------------------------------------------------
select public.log_script_insertion('$RCSfile: clin-episode.sql,v $', '$Revision: 1.3 $');

-- ==============================================================
-- $Log: clin-episode.sql,v $
-- Revision 1.3  2006-12-11 17:02:46  ncq
-- - index on modified_by
--
-- Revision 1.2  2006/11/24 09:21:36  ncq
-- - fix notification trigger col name use
--
-- Revision 1.1  2006/09/25 10:55:01  ncq
-- - added here
--
-- Revision 1.3  2006/09/18 17:32:53  ncq
-- - make more fool-proof
--
-- Revision 1.2  2006/09/16 21:47:37  ncq
-- - improvements
--
-- Revision 1.1  2006/09/16 14:02:36  ncq
-- - use this as a template for change scripts
--
--