comparison PostgreSQL/Plugins/CreateInstance.sql @ 106:b559af8fe6e0

Remove "ASSERT" in SQL for compatibility with older releases of PostgreSQL
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Jan 2019 15:35:52 +0100
parents e61587582cef
children 4d0bacbd0fba
comparison
equal deleted inserted replaced
105:eefbcee7bd78 106:b559af8fe6e0
27 SELECT internalId FROM Resources INTO studyKey WHERE publicId = study AND resourceType = 1; 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; 28 SELECT internalId FROM Resources INTO seriesKey WHERE publicId = series AND resourceType = 2;
29 29
30 IF patientKey IS NULL THEN 30 IF patientKey IS NULL THEN
31 -- Must create a new patient 31 -- Must create a new patient
32 ASSERT studyKey IS NULL; 32 IF NOT (studyKey IS NULL AND seriesKey IS NULL AND instanceKey IS NULL) THEN
33 ASSERT seriesKey IS NULL; 33 RAISE EXCEPTION 'Broken invariant';
34 ASSERT instanceKey IS NULL; 34 END IF;
35
35 INSERT INTO Resources VALUES (DEFAULT, 0, patient, NULL) RETURNING internalId INTO patientKey; 36 INSERT INTO Resources VALUES (DEFAULT, 0, patient, NULL) RETURNING internalId INTO patientKey;
36 isNewPatient := 1; 37 isNewPatient := 1;
37 ELSE 38 ELSE
38 isNewPatient := 0; 39 isNewPatient := 0;
39 END IF; 40 END IF;
40 41
41 ASSERT NOT patientKey IS NULL; 42 IF (patientKey IS NULL) THEN
43 RAISE EXCEPTION 'Broken invariant';
44 END IF;
42 45
43 IF studyKey IS NULL THEN 46 IF studyKey IS NULL THEN
44 -- Must create a new study 47 -- Must create a new study
45 ASSERT seriesKey IS NULL; 48 IF NOT (seriesKey IS NULL AND instanceKey IS NULL) THEN
46 ASSERT instanceKey IS NULL; 49 RAISE EXCEPTION 'Broken invariant';
50 END IF;
51
47 INSERT INTO Resources VALUES (DEFAULT, 1, study, patientKey) RETURNING internalId INTO studyKey; 52 INSERT INTO Resources VALUES (DEFAULT, 1, study, patientKey) RETURNING internalId INTO studyKey;
48 isNewStudy := 1; 53 isNewStudy := 1;
49 ELSE 54 ELSE
50 isNewStudy := 0; 55 isNewStudy := 0;
51 END IF; 56 END IF;
52 57
53 ASSERT NOT studyKey IS NULL; 58 IF (studyKey IS NULL) THEN
54 59 RAISE EXCEPTION 'Broken invariant';
60 END IF;
61
55 IF seriesKey IS NULL THEN 62 IF seriesKey IS NULL THEN
56 -- Must create a new series 63 -- Must create a new series
57 ASSERT instanceKey IS NULL; 64 IF NOT (instanceKey IS NULL) THEN
65 RAISE EXCEPTION 'Broken invariant';
66 END IF;
67
58 INSERT INTO Resources VALUES (DEFAULT, 2, series, studyKey) RETURNING internalId INTO seriesKey; 68 INSERT INTO Resources VALUES (DEFAULT, 2, series, studyKey) RETURNING internalId INTO seriesKey;
59 isNewSeries := 1; 69 isNewSeries := 1;
60 ELSE 70 ELSE
61 isNewSeries := 0; 71 isNewSeries := 0;
62 END IF; 72 END IF;
63 73
64 ASSERT NOT seriesKey IS NULL; 74 IF (seriesKey IS NULL OR NOT instanceKey IS NULL) THEN
65 ASSERT instanceKey IS NULL; 75 RAISE EXCEPTION 'Broken invariant';
76 END IF;
66 77
67 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey; 78 INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey;
68 isNewInstance := 1; 79 isNewInstance := 1;
69 80
70 -- Move the patient to the end of the recycling order 81 -- Move the patient to the end of the recycling order