comparison PostgreSQL/Plugins/CreateInstance.sql @ 90:e61587582cef db-changes

moved extension TagMostRecentPatient into stored procedure CreateInstance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Jan 2019 18:14:28 +0100
parents 8dd29af7c844
children b559af8fe6e0
comparison
equal deleted inserted replaced
89:3f917ed95daf 90:e61587582cef
9 OUT isNewInstance BIGINT, 9 OUT isNewInstance BIGINT,
10 OUT patientKey BIGINT, 10 OUT patientKey BIGINT,
11 OUT studyKey BIGINT, 11 OUT studyKey BIGINT,
12 OUT seriesKey BIGINT, 12 OUT seriesKey BIGINT,
13 OUT instanceKey BIGINT) AS $body$ 13 OUT instanceKey BIGINT) AS $body$
14
15 DECLARE
16 patientSeq BIGINT;
17 countRecycling BIGINT;
14 18
15 BEGIN 19 BEGIN
16 SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3; 20 SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3;
17 21
18 IF NOT (instanceKey IS NULL) THEN 22 IF NOT (instanceKey IS NULL) THEN
60 ASSERT NOT seriesKey IS NULL; 64 ASSERT NOT seriesKey IS NULL;
61 ASSERT instanceKey IS NULL; 65 ASSERT instanceKey IS NULL;
62 66
63 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey; 67 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey;
64 isNewInstance := 1; 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;
65 END IF; 82 END IF;
66 END; 83 END;
67 84
68 $body$ LANGUAGE plpgsql; 85 $body$ LANGUAGE plpgsql;