changeset 69:19764fc60ade db-changes

compatibility with Orthanc SDDK 0.9.5
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 03 Jan 2019 10:07:27 +0100
parents babc1e0eb7f2
children e6c13ddd26d9
files Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h Framework/Plugins/OrthancCppDatabasePlugin.h MySQL/CMakeLists.txt PostgreSQL/CMakeLists.txt Resources/CMake/DatabasesPluginConfiguration.cmake SQLite/CMakeLists.txt
diffstat 7 files changed, 164 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/IndexBackend.cpp	Mon Dec 24 13:36:44 2018 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Thu Jan 03 10:07:27 2019 +0100
@@ -29,6 +29,7 @@
 #include <Core/Logging.h>
 #include <Core/OrthancException.h>
 #include <OrthancServer/ServerEnumerations.h>
+#include <OrthancServer/Search/ISqlLookupFormatter.h>
 
 
 namespace OrthancDatabases
@@ -1579,4 +1580,55 @@
 
     ReadListOfStrings(childrenPublicIds, statement, args);
   }
+
+
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+  class IndexBackend::LookupFormatter : public Orthanc::ISqlLookupFormatter
+  {
+  private:
+    Dialect  dialect_;
+
+  public:
+    LookupFormatter(Dialect  dialect) :
+      dialect_(dialect)
+    {
+    }
+
+    virtual std::string GenerateParameter(const std::string& value)
+    {
+      switch (dialect_)
+      {
+        case Dialect_MySQL:
+          break;
+        
+        case Dialect_PostgreSQL:
+          break;
+
+        case Dialect_SQLite:
+          break;
+
+        default:
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      }
+    }
+  };
+#endif
+
+  
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+  // New primitive since Orthanc 1.5.2
+  void IndexBackend::LookupResources(const std::vector<Orthanc::DatabaseConstraint>& lookup,
+                                     OrthancPluginResourceType queryLevel,
+                                     uint32_t limit,
+                                     bool requestSomeInstance)
+  {
+    std::string sql;
+
+    {
+      LookupFormatter formatter(manager_.GetDialect());
+      Orthanc::ISqlLookupFormatter::Apply(sql, formatter, lookup,
+                                          Orthanc::Plugins::Convert(queryLevel), limit);
+    }
+  }
+#endif
 }
--- a/Framework/Plugins/IndexBackend.h	Mon Dec 24 13:36:44 2018 +0100
+++ b/Framework/Plugins/IndexBackend.h	Thu Jan 03 10:07:27 2019 +0100
@@ -30,6 +30,8 @@
   class IndexBackend : public OrthancPlugins::IDatabaseBackend
   {
   private:
+    class LookupFormatter;
+    
     DatabaseManager   manager_;
 
   protected:
@@ -242,7 +244,6 @@
     
     virtual void ClearMainDicomTags(int64_t internalId);
 
-
     // For unit testing only!
     virtual uint64_t GetResourcesCount();
 
@@ -256,5 +257,13 @@
     // For unit tests only!
     virtual void GetChildren(std::list<std::string>& childrenPublicIds,
                              int64_t id);
+
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+    // New primitive since Orthanc 1.5.2
+    virtual void LookupResources(const std::vector<Orthanc::DatabaseConstraint>& lookup,
+                                 OrthancPluginResourceType queryLevel,
+                                 uint32_t limit,
+                                 bool requestSomeInstance);
+#endif
   };
 }
--- a/Framework/Plugins/OrthancCppDatabasePlugin.h	Mon Dec 24 13:36:44 2018 +0100
+++ b/Framework/Plugins/OrthancCppDatabasePlugin.h	Thu Jan 03 10:07:27 2019 +0100
@@ -32,9 +32,14 @@
 #  error HAS_ORTHANC_EXCEPTION must be set to 1
 #endif
 
+#if ORTHANC_ENABLE_PLUGINS != 1
+#  error ORTHANC_ENABLE_PLUGINS must be set to 1
+#endif
 
-#include <orthanc/OrthancCDatabasePlugin.h>
+
 #include <Core/OrthancException.h>
+#include <OrthancServer/Search/DatabaseConstraint.h>
+
 
 
 #define ORTHANC_PLUGINS_DATABASE_CATCH                            \
@@ -75,7 +80,8 @@
       AllowedAnswers_Attachment,
       AllowedAnswers_Change,
       AllowedAnswers_DicomTag,
-      AllowedAnswers_ExportedResource
+      AllowedAnswers_ExportedResource,
+      AllowedAnswers_MatchingResource
     };
 
     OrthancPluginContext*         context_;
@@ -243,6 +249,43 @@
 
       OrthancPluginDatabaseAnswerExportedResource(context_, database_, &exported);
     }
+
+
+#if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE)         // Macro introduced in Orthanc 1.3.1
+#  if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 2)
+    void AnswerMatchingResource(const std::string& resourceId)
+    {
+      if (allowedAnswers_ != AllowedAnswers_All &&
+          allowedAnswers_ != AllowedAnswers_MatchingResource)
+      {
+        throw std::runtime_error("Cannot answer with an exported resource in the current state");
+      }
+
+      OrthancPluginMatchingResource match;
+      match.resourceId = resourceId.c_str();
+      match.someInstanceId = NULL;
+
+      OrthancPluginDatabaseAnswerMatchingResource(context_, database_, &match);
+    }
+
+
+    void AnswerMatchingResource(const std::string& resourceId,
+                                const std::string& someInstanceId)
+    {
+      if (allowedAnswers_ != AllowedAnswers_All &&
+          allowedAnswers_ != AllowedAnswers_MatchingResource)
+      {
+        throw std::runtime_error("Cannot answer with an exported resource in the current state");
+      }
+
+      OrthancPluginMatchingResource match;
+      match.resourceId = resourceId.c_str();
+      match.someInstanceId = someInstanceId.c_str();
+
+      OrthancPluginDatabaseAnswerMatchingResource(context_, database_, &match);
+    }
+#  endif
+#endif
   };
 
 
@@ -447,6 +490,13 @@
                                  OrthancPluginStorageArea* storageArea) = 0;
 
     virtual void ClearMainDicomTags(int64_t internalId) = 0;
+
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+    virtual void LookupResources(const std::vector<Orthanc::DatabaseConstraint>& lookup,
+                                 OrthancPluginResourceType queryLevel,
+                                 uint32_t limit,
+                                 bool requestSomeInstance) = 0;
+#endif
   };
 
 
@@ -1421,6 +1471,38 @@
       ORTHANC_PLUGINS_DATABASE_CATCH
     }
 
+
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+    /* Use GetOutput().AnswerResource() */
+    static OrthancPluginErrorCode LookupResources(
+      OrthancPluginDatabaseContext* context,
+      void* payload,
+      uint32_t constraintsCount,
+      const OrthancPluginDatabaseConstraint* constraints,
+      OrthancPluginResourceType queryLevel,
+      uint32_t limit,
+      uint8_t requestSomeInstance)
+    {
+      IDatabaseBackend* backend = reinterpret_cast<IDatabaseBackend*>(payload);
+      backend->GetOutput().SetAllowedAnswers(DatabaseBackendOutput::AllowedAnswers_MatchingResource);
+
+      try
+      {
+        std::vector<Orthanc::DatabaseConstraint> lookup;
+        lookup.reserve(constraintsCount);
+
+        for (uint32_t i = 0; i < constraintsCount; i++)
+        {
+          lookup.push_back(Orthanc::DatabaseConstraint(constraints[i]));
+        }
+        
+        backend->LookupResources(lookup, queryLevel, limit, (requestSomeInstance != 0));
+        return OrthancPluginErrorCode_Success;
+      }
+      ORTHANC_PLUGINS_DATABASE_CATCH
+    }
+#endif
+
     
   public:
     /**
@@ -1468,7 +1550,7 @@
       params.logExportedResource = LogExportedResource;
       params.lookupAttachment = LookupAttachment;
       params.lookupGlobalProperty = LookupGlobalProperty;
-      params.lookupIdentifier = NULL;   // Unused starting with Orthanc 0.9.5 (db v6)
+      params.lookupIdentifier = NULL;    // Unused starting with Orthanc 0.9.5 (db v6)
       params.lookupIdentifier2 = NULL;   // Unused starting with Orthanc 0.9.5 (db v6)
       params.lookupMetadata = LookupMetadata;
       params.lookupParent = LookupParent;
@@ -1490,25 +1572,29 @@
       extensions.getDatabaseVersion = GetDatabaseVersion;
       extensions.upgradeDatabase = UpgradeDatabase;
       extensions.clearMainDicomTags = ClearMainDicomTags;
-      extensions.getAllInternalIds = GetAllInternalIds;    // New in Orthanc 0.9.5 (db v6)
-      extensions.lookupIdentifier3 = LookupIdentifier3;    // New in Orthanc 0.9.5 (db v6)
+      extensions.getAllInternalIds = GetAllInternalIds;     // New in Orthanc 0.9.5 (db v6)
+      extensions.lookupIdentifier3 = LookupIdentifier3;     // New in Orthanc 0.9.5 (db v6)
 
       bool performanceWarning = true;
 
 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE)         // Macro introduced in Orthanc 1.3.1
 #  if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 4, 0)
       extensions.lookupIdentifierRange = LookupIdentifierRange;    // New in Orthanc 1.4.0
-      performanceWarning = false;
 #  endif
 #endif
 
+#if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
+      extensions.lookupResources = LookupResources;   // New in Orthanc 1.5.2 (fast lookup)
+      performanceWarning = false;
+#endif      
+
       if (performanceWarning)
       {
         char info[1024];
         sprintf(info, 
                 "Performance warning: The database index plugin was compiled "
                 "against an old version of the Orthanc SDK (%d.%d.%d): "
-                "Consider upgrading to version 1.4.0 of the Orthanc SDK",
+                "Consider upgrading to version 1.5.2 of the Orthanc SDK",
                 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
                 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
                 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
--- a/MySQL/CMakeLists.txt	Mon Dec 24 13:36:44 2018 +0100
+++ b/MySQL/CMakeLists.txt	Thu Jan 03 10:07:27 2019 +0100
@@ -79,7 +79,6 @@
 
 add_definitions(
   -DORTHANC_PLUGIN_VERSION="${ORTHANC_PLUGIN_VERSION}"
-  -DHAS_ORTHANC_EXCEPTION=1
   )
 
 set_target_properties(OrthancMySQLStorage PROPERTIES 
--- a/PostgreSQL/CMakeLists.txt	Mon Dec 24 13:36:44 2018 +0100
+++ b/PostgreSQL/CMakeLists.txt	Thu Jan 03 10:07:27 2019 +0100
@@ -79,7 +79,6 @@
 
 add_definitions(
   -DORTHANC_PLUGIN_VERSION="${ORTHANC_PLUGIN_VERSION}"
-  -DHAS_ORTHANC_EXCEPTION=1
   )
 
 set_target_properties(OrthancPostgreSQLStorage PROPERTIES 
--- a/Resources/CMake/DatabasesPluginConfiguration.cmake	Mon Dec 24 13:36:44 2018 +0100
+++ b/Resources/CMake/DatabasesPluginConfiguration.cmake	Thu Jan 03 10:07:27 2019 +0100
@@ -40,6 +40,12 @@
 endif()
 
 
+add_definitions(
+  -DHAS_ORTHANC_EXCEPTION=1
+  -DORTHANC_ENABLE_PLUGINS=1
+  )
+
+
 list(APPEND DATABASES_SOURCES
   ${ORTHANC_CORE_SOURCES}
   ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/GlobalProperties.cpp
@@ -47,8 +53,7 @@
   ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/StorageBackend.cpp
   ${ORTHANC_ROOT}/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp
 
-  # New for "db-changes"
-  #${ORTHANC_ROOT}/OrthancServer/Search/ISqlLookupFormatter.cpp
-  #${ORTHANC_ROOT}/OrthancServer/Search/DatabaseConstraint.cpp
-  #${ORTHANC_ROOT}/OrthancServer/Search/DicomTagConstraint.cpp
+  # New from "db-changes"
+  ${ORTHANC_ROOT}/OrthancServer/Search/DatabaseConstraint.cpp
+  ${ORTHANC_ROOT}/OrthancServer/Search/ISqlLookupFormatter.cpp
   )
--- a/SQLite/CMakeLists.txt	Mon Dec 24 13:36:44 2018 +0100
+++ b/SQLite/CMakeLists.txt	Thu Jan 03 10:07:27 2019 +0100
@@ -35,7 +35,6 @@
 
 add_definitions(
   -DORTHANC_PLUGIN_VERSION="${ORTHANC_PLUGIN_VERSION}"
-  -DHAS_ORTHANC_EXCEPTION=1
   )
 
 #set_target_properties(OrthancSQLiteStorage PROPERTIES