diff Framework/Plugins/IndexBackend.cpp @ 88:eb08ec14fb04 db-changes

new extension implemented: TagMostRecentPatient
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 15 Jan 2019 21:10:50 +0100
parents 1012fe77241c
children ca0ecd412988
line wrap: on
line diff
--- a/Framework/Plugins/IndexBackend.cpp	Thu Jan 10 20:39:36 2019 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Tue Jan 15 21:10:50 2019 +0100
@@ -1882,4 +1882,73 @@
 
     ReadListOfStrings(target, statement, args);
   }
+
+
+  // New primitive since Orthanc 1.5.2
+  void IndexBackend::TagMostRecentPatient(int64_t patient)
+  {
+    int64_t seq;
+    
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "SELECT * FROM PatientRecyclingOrder WHERE seq >= "
+        "(SELECT seq FROM PatientRecyclingOrder WHERE patientid=${id}) ORDER BY seq LIMIT 2");
+
+      statement.SetReadOnly(true);
+      statement.SetParameterType("id", ValueType_Integer64);
+
+      Dictionary args;
+      args.SetIntegerValue("id", patient);
+
+      statement.Execute(args);
+      
+      if (statement.IsDone())
+      {
+        // The patient is protected, don't add it to the recycling order
+        return;
+      }
+
+      seq = ReadInteger64(statement, 0);
+
+      statement.Next();
+
+      if (statement.IsDone())
+      {
+        // The patient is already at the end of the recycling order
+        // (because of the "LIMIT 2" above), no need to modify the table
+        return;
+      }
+    }
+
+    // Delete the old position of the patient in the recycling order
+
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "DELETE FROM PatientRecyclingOrder WHERE seq=${seq}");
+        
+      statement.SetParameterType("seq", ValueType_Integer64);
+        
+      Dictionary args;
+      args.SetIntegerValue("seq", seq);
+        
+      statement.Execute(args);
+    }
+
+    // Add the patient to the end of the recycling order
+
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "INSERT INTO PatientRecyclingOrder VALUES(${}, ${id})");
+        
+      statement.SetParameterType("id", ValueType_Integer64);
+        
+      Dictionary args;
+      args.SetIntegerValue("id", patient);
+        
+      statement.Execute(args);
+    }
+  }
 }