# HG changeset patch # User Sebastien Jodogne # Date 1547658868 -3600 # Node ID e61587582cefce3fa2d63dad4c076e22b8ecceec # Parent 3f917ed95dafc933b040d8fc462f4db276efd2c3 moved extension TagMostRecentPatient into stored procedure CreateInstance diff -r 3f917ed95daf -r e61587582cef PostgreSQL/Plugins/CreateInstance.sql --- a/PostgreSQL/Plugins/CreateInstance.sql Wed Jan 16 16:37:52 2019 +0100 +++ b/PostgreSQL/Plugins/CreateInstance.sql Wed Jan 16 18:14:28 2019 +0100 @@ -12,6 +12,10 @@ OUT seriesKey BIGINT, OUT instanceKey BIGINT) AS $body$ +DECLARE + patientSeq BIGINT; + countRecycling BIGINT; + BEGIN SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3; @@ -62,6 +66,19 @@ INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey; isNewInstance := 1; + + -- Move the patient to the end of the recycling order + SELECT seq FROM PatientRecyclingOrder WHERE patientId = patientKey INTO patientSeq; + + IF NOT (patientSeq IS NULL) THEN + -- The patient is not protected + SELECT COUNT(*) FROM (SELECT * FROM PatientRecyclingOrder WHERE seq >= patientSeq LIMIT 2) AS tmp INTO countRecycling; + IF countRecycling = 2 THEN + -- The patient was not at the end of the recycling order + DELETE FROM PatientRecyclingOrder WHERE seq = patientSeq; + INSERT INTO PatientRecyclingOrder VALUES(DEFAULT, patientKey); + END IF; + END IF; END IF; END; diff -r 3f917ed95daf -r e61587582cef PostgreSQL/Plugins/PostgreSQLIndex.cpp --- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp Wed Jan 16 16:37:52 2019 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp Wed Jan 16 18:14:28 2019 +0100 @@ -175,31 +175,32 @@ { PostgreSQLTransaction t(*db); - int hasCreateInstance = 0; - if (!LookupGlobalIntegerProperty(hasCreateInstance, *db, t, + int property = 0; + if (!LookupGlobalIntegerProperty(property, *db, t, Orthanc::GlobalProperty_HasCreateInstance) || - hasCreateInstance != 1) + property != 2) { LOG(INFO) << "Installing the CreateInstance extension"; + if (property == 1) + { + // Drop older, experimental versions of this extension + db->Execute("DROP FUNCTION CreateInstance(" + "IN patient TEXT, IN study TEXT, IN series TEXT, in instance TEXT)"); + } + std::string query; Orthanc::EmbeddedResources::GetFileResource (query, Orthanc::EmbeddedResources::POSTGRESQL_CREATE_INSTANCE); db->Execute(query); - SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasCreateInstance, 1); + SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasCreateInstance, 2); } - t.Commit(); - } - - { - PostgreSQLTransaction t(*db); - - int hasFastTotalSize = 0; - if (!LookupGlobalIntegerProperty(hasFastTotalSize, *db, t, + + if (!LookupGlobalIntegerProperty(property, *db, t, Orthanc::GlobalProperty_GetTotalSizeIsFast) || - hasFastTotalSize != 1) + property != 1) { LOG(INFO) << "Installing the FastTotalSize extension"; @@ -211,15 +212,10 @@ SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_GetTotalSizeIsFast, 1); } - t.Commit(); - } - - { - PostgreSQLTransaction t(*db); // Installing this extension requires the "GlobalIntegers" table // created by the "FastTotalSize" extension - int property = 0; + property = 0; if (!LookupGlobalIntegerProperty(property, *db, t, Orthanc::GlobalProperty_HasFastCountResources) || property != 1) @@ -234,15 +230,10 @@ SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasFastCountResources, 1); } - t.Commit(); - } - - { - PostgreSQLTransaction t(*db); // Installing this extension requires the "GlobalIntegers" table // created by the "GetLastChangeIndex" extension - int property = 0; + property = 0; if (!LookupGlobalIntegerProperty(property, *db, t, Orthanc::GlobalProperty_GetLastChangeIndex) || property != 1) @@ -381,9 +372,6 @@ result.patientId = ReadInteger64(statement, 4); result.studyId = ReadInteger64(statement, 5); result.seriesId = ReadInteger64(statement, 6); - - // TODO - Move this to the stored procedure - TagMostRecentPatient(result.patientId); } } #endif @@ -434,4 +422,12 @@ return ReadInteger64(statement, 0); } + + + void PostgreSQLIndex::TagMostRecentPatient(int64_t patient) + { + // This behavior is implemented in "CreateInstance()", and no + // backward compatibility is necessary + throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); + } } diff -r 3f917ed95daf -r e61587582cef PostgreSQL/Plugins/PostgreSQLIndex.h --- a/PostgreSQL/Plugins/PostgreSQLIndex.h Wed Jan 16 16:37:52 2019 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLIndex.h Wed Jan 16 18:14:28 2019 +0100 @@ -96,5 +96,7 @@ ORTHANC_OVERRIDE; virtual int64_t GetLastChangeIndex() ORTHANC_OVERRIDE; + + virtual void TagMostRecentPatient(int64_t patient) ORTHANC_OVERRIDE; }; }