comparison MySQL/Plugins/CreateInstance.sql @ 110:441a472bfd93

new extension implemented for MySQL: CreateInstance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Jan 2019 16:28:43 +0100
parents
children 740d9829f52e
comparison
equal deleted inserted replaced
109:3f31e3fa5114 110:441a472bfd93
1 CREATE PROCEDURE CreateInstance(
2 IN patient TEXT,
3 IN study TEXT,
4 IN series TEXT,
5 IN instance TEXT,
6 OUT isNewPatient BOOLEAN,
7 OUT isNewStudy BOOLEAN,
8 OUT isNewSeries BOOLEAN,
9 OUT isNewInstance BOOLEAN,
10 OUT patientKey BIGINT,
11 OUT studyKey BIGINT,
12 OUT seriesKey BIGINT,
13 OUT instanceKey BIGINT)
14 BEGIN
15 DECLARE recyclingSeq BIGINT@
16
17 SELECT internalId INTO instanceKey FROM Resources WHERE publicId = instance AND resourceType = 3@
18
19 IF NOT instanceKey IS NULL THEN
20 -- This instance already exists, stop here
21 SELECT 0 INTO isNewInstance@
22 ELSE
23 SELECT internalId INTO patientKey FROM Resources WHERE publicId = patient AND resourceType = 0@
24 SELECT internalId INTO studyKey FROM Resources WHERE publicId = study AND resourceType = 1@
25 SELECT internalId INTO seriesKey FROM Resources WHERE publicId = series AND resourceType = 2@
26
27 IF patientKey IS NULL THEN
28 -- Must create a new patient
29 IF NOT (studyKey IS NULL AND seriesKey IS NULL AND instanceKey IS NULL) THEN
30 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 1'@
31 END IF@
32
33 INSERT INTO Resources VALUES (DEFAULT, 0, patient, NULL)@
34 SELECT LAST_INSERT_ID() INTO patientKey@
35 SELECT 1 INTO isNewPatient@
36 ELSE
37 SELECT 0 INTO isNewPatient@
38 END IF@
39
40 IF patientKey IS NULL THEN
41 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 2'@
42 END IF@
43
44 IF studyKey IS NULL THEN
45 -- Must create a new study
46 IF NOT (seriesKey IS NULL AND instanceKey IS NULL) THEN
47 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 3'@
48 END IF@
49
50 INSERT INTO Resources VALUES (DEFAULT, 1, study, patientKey)@
51 SELECT LAST_INSERT_ID() INTO studyKey@
52 SELECT 1 INTO isNewStudy@
53 ELSE
54 SELECT 0 INTO isNewStudy@
55 END IF@
56
57 IF studyKey IS NULL THEN
58 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 4'@
59 END IF@
60
61 IF seriesKey IS NULL THEN
62 -- Must create a new series
63 IF NOT (instanceKey IS NULL) THEN
64 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 5'@
65 END IF@
66
67 INSERT INTO Resources VALUES (DEFAULT, 2, series, studyKey)@
68 SELECT LAST_INSERT_ID() INTO seriesKey@
69 SELECT 1 INTO isNewSeries@
70 ELSE
71 SELECT 0 INTO isNewSeries@
72 END IF@
73
74 IF (seriesKey IS NULL OR NOT instanceKey IS NULL) THEN
75 SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Broken invariant 6'@
76 END IF@
77
78 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey)@
79 SELECT LAST_INSERT_ID() INTO instanceKey@
80 SELECT 1 INTO isNewInstance@
81
82 -- Move the patient to the end of the recycling order
83 IF NOT isNewPatient THEN
84 SELECT seq FROM PatientRecyclingOrder WHERE patientId = patientKey INTO recyclingSeq@
85
86 IF NOT recyclingSeq IS NULL THEN
87 -- The patient is not protected
88 DELETE FROM PatientRecyclingOrder WHERE seq = recyclingSeq@
89 INSERT INTO PatientRecyclingOrder VALUES (DEFAULT, patientKey)@
90 END IF@
91 END IF@
92 END IF@
93 END;