Mercurial > hg > orthanc-databases
comparison PostgreSQL/Plugins/CreateInstance.sql @ 97:66fd0d587773
integration db-changes->mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Jan 2019 14:38:12 +0100 |
parents | e61587582cef |
children | b559af8fe6e0 |
comparison
equal
deleted
inserted
replaced
91:1bd538a5a783 | 97:66fd0d587773 |
---|---|
1 CREATE FUNCTION CreateInstance( | |
2 IN patient TEXT, | |
3 IN study TEXT, | |
4 IN series TEXT, | |
5 IN instance TEXT, | |
6 OUT isNewPatient BIGINT, | |
7 OUT isNewStudy BIGINT, | |
8 OUT isNewSeries BIGINT, | |
9 OUT isNewInstance BIGINT, | |
10 OUT patientKey BIGINT, | |
11 OUT studyKey BIGINT, | |
12 OUT seriesKey BIGINT, | |
13 OUT instanceKey BIGINT) AS $body$ | |
14 | |
15 DECLARE | |
16 patientSeq BIGINT; | |
17 countRecycling BIGINT; | |
18 | |
19 BEGIN | |
20 SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3; | |
21 | |
22 IF NOT (instanceKey IS NULL) THEN | |
23 -- This instance already exists, stop here | |
24 isNewInstance := 0; | |
25 ELSE | |
26 SELECT internalId FROM Resources INTO patientKey WHERE publicId = patient AND resourceType = 0; | |
27 SELECT internalId FROM Resources INTO studyKey WHERE publicId = study AND resourceType = 1; | |
28 SELECT internalId FROM Resources INTO seriesKey WHERE publicId = series AND resourceType = 2; | |
29 | |
30 IF patientKey IS NULL THEN | |
31 -- Must create a new patient | |
32 ASSERT studyKey IS NULL; | |
33 ASSERT seriesKey IS NULL; | |
34 ASSERT instanceKey IS NULL; | |
35 INSERT INTO Resources VALUES (DEFAULT, 0, patient, NULL) RETURNING internalId INTO patientKey; | |
36 isNewPatient := 1; | |
37 ELSE | |
38 isNewPatient := 0; | |
39 END IF; | |
40 | |
41 ASSERT NOT patientKey IS NULL; | |
42 | |
43 IF studyKey IS NULL THEN | |
44 -- Must create a new study | |
45 ASSERT seriesKey IS NULL; | |
46 ASSERT instanceKey IS NULL; | |
47 INSERT INTO Resources VALUES (DEFAULT, 1, study, patientKey) RETURNING internalId INTO studyKey; | |
48 isNewStudy := 1; | |
49 ELSE | |
50 isNewStudy := 0; | |
51 END IF; | |
52 | |
53 ASSERT NOT studyKey IS NULL; | |
54 | |
55 IF seriesKey IS NULL THEN | |
56 -- Must create a new series | |
57 ASSERT instanceKey IS NULL; | |
58 INSERT INTO Resources VALUES (DEFAULT, 2, series, studyKey) RETURNING internalId INTO seriesKey; | |
59 isNewSeries := 1; | |
60 ELSE | |
61 isNewSeries := 0; | |
62 END IF; | |
63 | |
64 ASSERT NOT seriesKey IS NULL; | |
65 ASSERT instanceKey IS NULL; | |
66 | |
67 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey; | |
68 isNewInstance := 1; | |
69 | |
70 -- Move the patient to the end of the recycling order | |
71 SELECT seq FROM PatientRecyclingOrder WHERE patientId = patientKey INTO patientSeq; | |
72 | |
73 IF NOT (patientSeq IS NULL) THEN | |
74 -- The patient is not protected | |
75 SELECT COUNT(*) FROM (SELECT * FROM PatientRecyclingOrder WHERE seq >= patientSeq LIMIT 2) AS tmp INTO countRecycling; | |
76 IF countRecycling = 2 THEN | |
77 -- The patient was not at the end of the recycling order | |
78 DELETE FROM PatientRecyclingOrder WHERE seq = patientSeq; | |
79 INSERT INTO PatientRecyclingOrder VALUES(DEFAULT, patientKey); | |
80 END IF; | |
81 END IF; | |
82 END IF; | |
83 END; | |
84 | |
85 $body$ LANGUAGE plpgsql; |