diff OrthancServer/Sources/Search/ISqlLookupFormatter.cpp @ 5826:d73dfb4548c6 find-refactoring

tools/find: ParentPatient and co ..
author Alain Mazy <am@orthanc.team>
date Mon, 07 Oct 2024 10:51:27 +0200
parents 881cd0965146
children 976872a99d39
line wrap: on
line diff
--- a/OrthancServer/Sources/Search/ISqlLookupFormatter.cpp	Fri Oct 04 19:03:14 2024 +0200
+++ b/OrthancServer/Sources/Search/ISqlLookupFormatter.cpp	Mon Oct 07 10:51:27 2024 +0200
@@ -57,7 +57,28 @@
         throw OrthancException(ErrorCode_InternalError);
     }
   }      
-  
+
+  static std::string FormatLevel(const char* prefix, ResourceType level)
+  {
+    switch (level)
+    {
+      case ResourceType_Patient:
+        return std::string(prefix) + "patients";
+        
+      case ResourceType_Study:
+        return std::string(prefix) + "studies";
+        
+      case ResourceType_Series:
+        return std::string(prefix) + "series";
+        
+      case ResourceType_Instance:
+        return std::string(prefix) + "instances";
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+  }      
+
 
   static bool FormatComparison(std::string& target,
                                ISqlLookupFormatter& formatter,
@@ -745,28 +766,37 @@
 
     std::string joins, comparisons;
 
-    if (request.GetOrthancIdentifiers().IsDefined() && request.GetOrthancIdentifiers().DetectLevel() <= queryLevel)
     {
-      // single child resource matching, there should not be other constraints (at least for now)
-      assert(request.GetDicomTagConstraints().GetSize() == 0);
-      assert(request.GetLabels().size() == 0);
-      assert(request.HasLimits() == false);
+      // handle parent constraints
+      if (request.GetOrthancIdentifiers().IsDefined() && request.GetOrthancIdentifiers().DetectLevel() <= queryLevel)
+      {
+        ResourceType topParentLevel = request.GetOrthancIdentifiers().DetectLevel();
 
-      ResourceType topParentLevel = request.GetOrthancIdentifiers().DetectLevel();
-      const std::string& strTopParentLevel = FormatLevel(topParentLevel);
+        if (topParentLevel == queryLevel)
+        {
+          comparisons += " AND " + FormatLevel(topParentLevel) + ".publicId = " + formatter.GenerateParameter(request.GetOrthancIdentifiers().GetLevel(topParentLevel));
+        }
+        else
+        {
+          comparisons += " AND " + FormatLevel("parent", topParentLevel) + ".publicId = " + formatter.GenerateParameter(request.GetOrthancIdentifiers().GetLevel(topParentLevel));
 
-      comparisons = " AND " + strTopParentLevel + ".publicId = " + formatter.GenerateParameter(request.GetOrthancIdentifiers().GetLevel(topParentLevel));
+          for (int level = queryLevel; level > topParentLevel; level--)
+          {
+            joins += " INNER JOIN Resources " +
+                    FormatLevel("parent", static_cast<ResourceType>(level - 1)) + " ON " +
+                    FormatLevel("parent", static_cast<ResourceType>(level - 1)) + ".internalId = ";
+            if (level == queryLevel)
+            {
+              joins += FormatLevel(static_cast<ResourceType>(level)) + ".parentId";
+            }
+            else
+            {
+              joins += FormatLevel("parent", static_cast<ResourceType>(level)) + ".parentId";
+            }
+          }
+        }
+      }
 
-      for (int level = queryLevel; level > topParentLevel; level--)
-      {
-        sql += (" INNER JOIN Resources " +
-                FormatLevel(static_cast<ResourceType>(level - 1)) + " ON " +
-                FormatLevel(static_cast<ResourceType>(level - 1)) + ".internalId=" +
-                FormatLevel(static_cast<ResourceType>(level)) + ".parentId");
-      }
-    }
-    else
-    {
       size_t count = 0;
       
       const DatabaseConstraints& dicomTagsConstraints = request.GetDicomTagConstraints();