-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
387 lines (333 loc) · 13.9 KB
/
Copy pathschema.sql
File metadata and controls
387 lines (333 loc) · 13.9 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
-- the main table in the db to which everything is tied
CREATE TABLE "conditions" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL UNIQUE,
"type" TEXT NOT NULL,
"description" TEXT NOT NULL
);
-- this table stores the organisations that might choose to use this db. Through this table scaling should be possible
CREATE TABLE "organisations" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL UNIQUE,
"location" TEXT NOT NULL,
"type" TEXT NOT NULL CHECK("type" IN('Emergency medical service', 'Private provider', 'Hopital provider', 'Fire Rescue', 'Other'))
);
-- table to store protocols
CREATE TABLE "protocols" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL,
"resource_link" TEXT NOT NULL,
"date_added" NUMERIC NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- junction table to connect protocols to organisations and conditions
CREATE TABLE "protocols_connector" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"protocol_id" INTEGER NOT NULL,
"organisation_id" INTEGER NOT NULL,
"condition_id" INTEGER NOT NULL,
"obsolete" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY ("protocol_id") REFERENCES "protocols"("id"),
FOREIGN KEY ("organisation_id") REFERENCES "organisations"("id"),
FOREIGN KEY ("condition_id") REFERENCES "conditions"("id")
);
-- a table to store single pieces of equipment, devices and gadgets.
CREATE TABLE "equipment" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL UNIQUE,
"type" TEXT NOT NULL CHECK ("type" IN ('transport', 'extrication', 'diagnostics', 'therapeutical', 'other', 'diagnostics/therapeutical')),
"description" TEXT NOT NULL,
"how_to_use" TEXT NOT NULL
);
-- a junction table connecting equipment to conditions and organisations
CREATE TABLE "equipment_connector" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"equipment_id" INTEGER NOT NULL,
"organisation_id" INTEGER NOT NULL,
"condition_id" INTEGER NOT NULL,
"obsolete" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY ("equipment_id") REFERENCES "equipment"("id"),
FOREIGN KEY ("organisation_id") REFERENCES "organisations"("id"),
FOREIGN KEY ("condition_id") REFERENCES "conditions"("id")
);
-- a table storing basic information about medications
CREATE TABLE "medications" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL UNIQUE,
"active_ingredient" TEXT NOT NULL,
"packaging_in_use" TEXT NOT NULL
);
-- this table stores dosages of specific medications which are meant for specific conditions and serves as a connector as well
CREATE TABLE "dosages" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"condition_id" INTEGER NOT NULL,
"organisation_id" INTEGER NOT NULL,
"medication_id" INTEGER NOT NULL,
"dosage" TEXT NOT NULL,
"description" TEXT NOT NULL,
"cave" TEXT NOT NULL,
"paediatric" INTEGER NOT NULL DEFAULT 0,
"obsolete" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY ("condition_id") REFERENCES "conditions"("id"),
FOREIGN KEY ("organisation_id") REFERENCES "organisations"("id"),
FOREIGN KEY ("medication_id") REFERENCES "medications"("id")
);
-- this table serves as a log
CREATE TABLE "logs" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"user_id" TEXT DEFAULT NULL,
"action" TEXT CHECK("action" IN('delete', 'attempted_delete', 'soft_delete', 'update', 'insert', 'recovery')),
"on_table" TEXT NOT NULL,
"table_specific_id" INTEGER NOT NULL,
"datetime" NUMERIC DEFAULT CURRENT_TIMESTAMP
);
-- no triggers were created for inserting, updating or deleting from the views since I would expect an application handling this database to fulfill that functionality.
-- these three views serve as the main infromation chanel for the user and through them, soft delete is supported
CREATE VIEW "current_medications" AS
SELECT "dosages"."id" AS "dosages_id", "conditions"."name" AS "condition_name", "dosages"."organisation_id" AS "organisation_id", "medications"."name", "dosages"."dosage", "dosages"."description", "dosages"."cave" FROM "medications"
JOIN "dosages" ON "medications"."id" = "dosages"."medication_id"
JOIN "conditions" ON "dosages"."condition_id" = "conditions"."id"
WHERE "dosages"."obsolete" = 0;
CREATE VIEW "current_equipment" AS
SELECT "equipment"."id" AS "equipment_id", "conditions"."name" AS "condition_name", "equipment_connector"."organisation_id" AS "organisation_id", "equipment"."name", "equipment"."description", "equipment"."how_to_use" FROM "equipment"
JOIN "equipment_connector" ON "equipment"."id" = "equipment_connector"."equipment_id"
JOIN "conditions" ON "equipment_connector"."condition_id" = "conditions"."id"
WHERE "equipment_connector"."obsolete" = 0;
CREATE VIEW "current_protocols" AS
SELECT "protocols"."id" AS "protocol_id", "conditions"."name" AS "condition_name", "protocols_connector"."organisation_id" AS "organisation_id", "protocols"."name", "protocols"."description", "protocols"."resource_link" FROM "protocols"
JOIN "protocols_connector" ON "protocols"."id" = "protocols_connector"."protocol_id"
JOIN "conditions" ON "protocols_connector"."condition_id" = "conditions"."id"
WHERE "protocols_connector"."obsolete" = 0;
-- indexes on names since there could be a ot of SELECT's based on names
CREATE INDEX "medication_names"
ON "medications" ("name");
CREATE INDEX "organisation_names"
ON "organisations" ("name");
CREATE INDEX "condition_names"
ON "conditions" ("name");
CREATE INDEX "protocol_names"
ON "protocols" ("name");
CREATE INDEX "equipment_names"
ON "equipment" ("name");
-- triggers for underlying tables preventing users from changing them directly, mainly preventing hard deletes
CREATE TRIGGER "prevent_delete_medications"
BEFORE DELETE ON "medications"
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('attempted_delete', 'medications', OLD."id");
SELECT RAISE(FAIL, 'Deletes from this table are not allowed!');
END;
CREATE TRIGGER "prevent_delete_conditions"
BEFORE DELETE ON "conditions"
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('attempted_delete', 'conditions', OLD."id");
SELECT RAISE(FAIL, 'Deletes from this table are not allowed!');
END;
-- These following triggers serve to enable soft delete and create a logs row.
CREATE TRIGGER "soft_delete_protocols"
BEFORE DELETE ON "protocols"
BEGIN
UPDATE "protocols_connector" SET "obsolete" = 1
WHERE "protocol_id" = OLD."id";
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('soft_delete', 'protocols', OLD."id");
SELECT RAISE(FAIL, 'Soft delete performed');
END;
CREATE TRIGGER "soft_delete_equipment"
BEFORE DELETE ON "equipment"
BEGIN
UPDATE "equipment_connector" SET "obsolete" = 1
WHERE "equipment_id" = OLD."id";
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('soft_delete', 'equipment', OLD."id");
SELECT RAISE(FAIL, 'Soft delete performed');
END;
CREATE TRIGGER "soft_delete_dosages"
BEFORE DELETE ON "dosages"
BEGIN
UPDATE "dosages" SET "obsolete" = 1
WHERE "id" = OLD."id";
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('soft_delete', 'dosages', OLD."id");
SELECT RAISE(FAIL, 'Soft delete performed');
END;
-- triggers for logging purposes only
CREATE TRIGGER "conditions_update_log"
AFTER UPDATE ON "conditions"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'conditions', NEW."id");
END;
CREATE TRIGGER "organisations_update_log"
AFTER UPDATE ON "organisations"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'organisations', NEW."id");
END;
CREATE TRIGGER "protocols_update_log"
AFTER UPDATE ON "protocols"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'protocols', NEW."id");
END;
CREATE TRIGGER "equipment_update_log"
AFTER UPDATE ON "equipment"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'equipment', NEW."id");
END;
CREATE TRIGGER "medications_update_log"
AFTER UPDATE ON "medications"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'medications', NEW."id");
END;
CREATE TRIGGER "dosages_update_log"
AFTER UPDATE ON "dosages"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'dosages', NEW."id");
END;
-- insert logs
CREATE TRIGGER "conditions_insert_log"
AFTER INSERT ON "conditions"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'conditions', NEW."id");
END;
CREATE TRIGGER "organisations_insert_log"
AFTER INSERT ON "organisations"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'organisations', NEW."id");
END;
CREATE TRIGGER "protocols_insert_log"
AFTER INSERT ON "protocols"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'protocols', NEW."id");
END;
CREATE TRIGGER "equipment_insert_log"
AFTER INSERT ON "equipment"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'equipment', NEW."id");
END;
CREATE TRIGGER "medications_insert_log"
AFTER INSERT ON "medications"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'medications', NEW."id");
END;
CREATE TRIGGER "dosages_insert_log"
AFTER INSERT ON "dosages"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'dosages', NEW."id");
END;
-- junction table update, insert and delete triggers for logging.
CREATE TRIGGER "equipment_connector_insert_log"
AFTER INSERT ON "equipment_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'equipment_connector', NEW."id");
END;
CREATE TRIGGER "equipment_connector_update_log"
AFTER UPDATE ON "equipment_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'equipment_connector', NEW."id");
END;
CREATE TRIGGER "equipment_connector_delete_log"
AFTER DELETE ON "equipment_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('delete', 'equipment_connector', OLD."id");
END;
CREATE TRIGGER "protocols_connector_insert_log"
AFTER INSERT ON "protocols_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('insert', 'protocols_connector', NEW."id");
END;
CREATE TRIGGER "protocols_connector_update_log"
AFTER UPDATE ON "protocols_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('update', 'protocols_connector', NEW."id");
END;
CREATE TRIGGER "protocols_connector_delete_log"
AFTER DELETE ON "protocols_connector"
FOR EACH ROW
BEGIN
INSERT INTO "logs" ("action", "on_table", "table_specific_id")
VALUES ('delete', 'protocols_connector', OLD."id");
END;
-- insert triggers that recognize if something already exists in the database and was only soft deleted - particularly protocols and equipment
CREATE TRIGGER "insert_equipment_connector_when_exists"
BEFORE INSERT ON "equipment_connector"
FOR EACH ROW
WHEN EXISTS(
SELECT "equipment_id", "organisation_id", "condition_id", "obsolete" FROM "equipment_connector"
WHERE "equipment_id" = NEW."equipment_id" AND "organisation_id" = NEW."organisation_id"
AND "condition_id" = NEW."condition_id" AND "obsolete" = 1
)
BEGIN
UPDATE "equipment_connector" SET "obsolete" = 0
WHERE "equipment_id" = NEW."equipment_id"
AND "condition_id" = NEW."condition_id"
AND "organisation_id" = NEW."organisation_id";
SELECT RAISE(FAIL, 'Recovered from soft deleted row.');
END;
CREATE TRIGGER "insert_protocols_connector_when_exists"
BEFORE INSERT ON "protocols_connector"
FOR EACH ROW
WHEN EXISTS(
SELECT "protocol_id", "organisation_id", "condition_id", "obsolete" FROM "protocols_connector"
WHERE "protocol_id" = NEW."protocol_id" AND "organisation_id" = NEW."organisation_id"
AND "condition_id" = NEW."condition_id" AND "obsolete" = 1
)
BEGIN
UPDATE "protocols_connector" SET "obsolete" = 0
WHERE "protocol_id" = NEW."protocol_id"
AND "condition_id" = NEW."condition_id"
AND "organisation_id" = NEW."organisation_id";
SELECT RAISE(FAIL, 'Recovered from soft deleted row.');
END;
-- this trigger is a bit different as it checks the contents of the new dosage being inputed. If there is any difference in the dosage, it should be inserted in a normal way becase the
-- specific dosage might have changed and the user chose to delete and insert instead of update.
CREATE TRIGGER "insert_dosages_when_exists"
BEFORE INSERT ON "dosages"
FOR EACH ROW
WHEN EXISTS (
SELECT "medication_id", "organisation_id", "condition_id", "dosage","description", "cave", "obsolete" FROM "dosages"
WHERE "medication_id" = NEW."medication_id" AND "organisation_id" = NEW."organisation_id"
AND "condition_id" = NEW."condition_id" AND "obsolete" = 1
AND "dosage" = NEW."dosage" AND "description" = NEW."description"
AND "cave" = NEW."cave"
)
BEGIN
UPDATE "dosages" SET "obsolete" = 0
WHERE "medication_id" = NEW."medication_id"
AND "organisation_id" = NEW."organisation_id"
AND "condition_id" = NEW."condition_id";
SELECT RAISE (FAIL, 'Recovered from soft deleted row.');
END;