changeset 239:e9ba888f371b

fix IndexBackend::SetGlobalProperty(), lighten GlobalProperty enum
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Apr 2021 20:30:15 +0200
parents f033cc039264
children c82c2cf84ae8
files Framework/Plugins/GlobalProperties.h Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexUnitTests.h
diffstat 3 files changed, 90 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/GlobalProperties.h	Thu Apr 08 19:33:36 2021 +0200
+++ b/Framework/Plugins/GlobalProperties.h	Thu Apr 08 20:30:15 2021 +0200
@@ -27,20 +27,19 @@
 namespace Orthanc
 {
   /**
-   * The enum "GlobalProperty" below must use same values as in the
-   * source code of the Orthanc server:
+   * The enum "GlobalProperty" is a subset of the "GlobalProperty_XXX"
+   * values from the Orthanc server that have a special meaning to the
+   * database plugins:
    * https://hg.orthanc-server.com/orthanc/file/default/OrthancServer/Sources/ServerEnumerations.h
+   *
+   * WARNING: The values must be the same between the Orthanc core and
+   * this enum!
    **/
   
   enum GlobalProperty
   {
     GlobalProperty_DatabaseSchemaVersion = 1,   // Unused in the Orthanc core as of Orthanc 0.9.5
-    GlobalProperty_FlushSleep = 2,
-    GlobalProperty_AnonymizationSequence = 3,
-    GlobalProperty_JobsRegistry = 5,
     GlobalProperty_GetTotalSizeIsFast = 6,      // New in Orthanc 1.5.2
-    GlobalProperty_Modalities = 20,             // New in Orthanc 1.5.0
-    GlobalProperty_Peers = 21,                  // New in Orthanc 1.5.0
 
     // Reserved values for internal use by the database plugins
     GlobalProperty_DatabasePatchLevel = 4,
@@ -53,6 +52,6 @@
     GlobalProperty_DatabaseInternal6 = 16,
     GlobalProperty_DatabaseInternal7 = 17,
     GlobalProperty_DatabaseInternal8 = 18,
-    GlobalProperty_DatabaseInternal9 = 19
+    GlobalProperty_DatabaseInternal9 = 19   // Only used in unit tests
   };
 }
--- a/Framework/Plugins/IndexBackend.cpp	Thu Apr 08 19:33:36 2021 +0200
+++ b/Framework/Plugins/IndexBackend.cpp	Thu Apr 08 20:30:15 2021 +0200
@@ -1395,6 +1395,44 @@
     }
   }
 
+
+  static void RunSetGlobalPropertyStatement(DatabaseManager::CachedStatement& statement,
+                                            bool hasServer,
+                                            bool hasValue,
+                                            const char* serverIdentifier,
+                                            int32_t property,
+                                            const char* utf8)
+  {
+    Dictionary args;
+
+    statement.SetParameterType("property", ValueType_Integer64);
+    args.SetIntegerValue("property", static_cast<int>(property));
+
+    if (hasValue)
+    {
+      assert(utf8 != NULL);
+      statement.SetParameterType("value", ValueType_Utf8String);
+      args.SetUtf8Value("value", utf8);
+    }
+    else
+    {
+      assert(utf8 == NULL);
+    }        
+
+    if (hasServer)
+    {
+      assert(serverIdentifier != NULL && strlen(serverIdentifier) > 0);
+      statement.SetParameterType("server", ValueType_Utf8String);
+      args.SetUtf8Value("server", serverIdentifier);
+    }
+    else
+    {
+      assert(serverIdentifier == NULL);
+    }
+      
+    statement.Execute(args);
+  }
+
     
   void IndexBackend::SetGlobalProperty(DatabaseManager& manager,
                                        const char* serverIdentifier,
@@ -1409,98 +1447,62 @@
     {
       bool hasServer = (strlen(serverIdentifier) != 0);
 
-      std::unique_ptr<DatabaseManager::CachedStatement> statement;
-
       if (hasServer)
       {
-        statement.reset(new DatabaseManager::CachedStatement(
-                          STATEMENT_FROM_HERE, manager,
-                          "INSERT OR REPLACE INTO ServerProperties VALUES (${server}, ${property}, ${value})"));
+        DatabaseManager::CachedStatement statement(
+          STATEMENT_FROM_HERE, manager,
+          "INSERT OR REPLACE INTO ServerProperties VALUES (${server}, ${property}, ${value})");
+
+        RunSetGlobalPropertyStatement(statement, true, true, serverIdentifier, property, utf8);
       }
       else
       {
-        statement.reset(new DatabaseManager::CachedStatement(
-                          STATEMENT_FROM_HERE, manager,
-                          "INSERT OR REPLACE INTO GlobalProperties VALUES (${property}, ${value})"));
+        DatabaseManager::CachedStatement statement(
+          STATEMENT_FROM_HERE, manager,
+          "INSERT OR REPLACE INTO GlobalProperties VALUES (${property}, ${value})");
+
+        RunSetGlobalPropertyStatement(statement, false, true, NULL, property, utf8);
       }
-        
-      statement->SetParameterType("property", ValueType_Integer64);
-      statement->SetParameterType("value", ValueType_Utf8String);
-        
-      Dictionary args;
-      args.SetIntegerValue("property", static_cast<int>(property));
-      args.SetUtf8Value("value", utf8);
-
-      if (hasServer)
-      {
-        statement->SetParameterType("server", ValueType_Utf8String);
-        args.SetUtf8Value("server", serverIdentifier);
-      }
-      
-      statement->Execute(args);
     }
     else
     {
       bool hasServer = (strlen(serverIdentifier) != 0);
 
-      std::unique_ptr<DatabaseManager::CachedStatement> statement;
-
+      if (hasServer)
       {
-        if (hasServer)
-        {
-          statement.reset(new DatabaseManager::CachedStatement(
-                            STATEMENT_FROM_HERE, manager,
-                            "DELETE FROM ServerProperties WHERE server=${server} AND property=${property}"));
-        }
-        else
-        {
-          statement.reset(new DatabaseManager::CachedStatement(
-                            STATEMENT_FROM_HERE, manager,
-                            "DELETE FROM GlobalProperties WHERE property=${property}"));
-        }
-        
-        statement->SetParameterType("property", ValueType_Integer64);
-        
-        Dictionary args;
-        args.SetIntegerValue("property", property);
-
-        if (hasServer)
         {
-          statement->SetParameterType("server", ValueType_Utf8String);
-          args.SetUtf8Value("server", serverIdentifier);
-        }
-        
-        statement->Execute(args);
-      }
+          DatabaseManager::CachedStatement statement(
+            STATEMENT_FROM_HERE, manager,
+            "DELETE FROM ServerProperties WHERE server=${server} AND property=${property}");
 
-      {
-        if (hasServer)
-        {
-          statement.reset(new DatabaseManager::CachedStatement(
-                            STATEMENT_FROM_HERE, manager,
-                            "INSERT INTO ServerProperties VALUES (${server}, ${property}, ${value})"));
-        }
-        else
-        {
-          statement.reset(new DatabaseManager::CachedStatement(
-                            STATEMENT_FROM_HERE, manager,
-                            "INSERT INTO GlobalProperties VALUES (${property}, ${value})"));
-        }
-        
-        statement->SetParameterType("property", ValueType_Integer64);
-        statement->SetParameterType("value", ValueType_Utf8String);
-        
-        Dictionary args;
-        args.SetIntegerValue("property", static_cast<int>(property));
-        args.SetUtf8Value("value", utf8);
-        
-        if (hasServer)
-        {
-          statement->SetParameterType("server", ValueType_Utf8String);
-          args.SetUtf8Value("server", serverIdentifier);
+          RunSetGlobalPropertyStatement(statement, true, false, serverIdentifier, property, NULL);
         }
 
-        statement->Execute(args);
+        {
+          DatabaseManager::CachedStatement statement(
+            STATEMENT_FROM_HERE, manager,
+            "INSERT INTO ServerProperties VALUES (${server}, ${property}, ${value})");
+
+          RunSetGlobalPropertyStatement(statement, true, true, serverIdentifier, property, utf8);
+        }
+      }
+      else
+      {
+        {
+          DatabaseManager::CachedStatement statement(
+            STATEMENT_FROM_HERE, manager,
+            "DELETE FROM GlobalProperties WHERE property=${property}");
+      
+          RunSetGlobalPropertyStatement(statement, false, false, NULL, property, NULL);
+        }
+
+        {
+          DatabaseManager::CachedStatement statement(
+            STATEMENT_FROM_HERE, manager,
+            "INSERT INTO GlobalProperties VALUES (${property}, ${value})");
+      
+          RunSetGlobalPropertyStatement(statement, false, true, NULL, property, utf8);
+        }
       }
     }
   }
--- a/Framework/Plugins/IndexUnitTests.h	Thu Apr 08 19:33:36 2021 +0200
+++ b/Framework/Plugins/IndexUnitTests.h	Thu Apr 08 20:30:15 2021 +0200
@@ -190,12 +190,12 @@
   ASSERT_TRUE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion));
   ASSERT_EQ("6", s);
 
-  ASSERT_FALSE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_AnonymizationSequence));
-  db.SetGlobalProperty(*manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_AnonymizationSequence, "Hello");
-  ASSERT_TRUE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_AnonymizationSequence));
+  ASSERT_FALSE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseInternal9));
+  db.SetGlobalProperty(*manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseInternal9, "Hello");
+  ASSERT_TRUE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseInternal9));
   ASSERT_EQ("Hello", s);
-  db.SetGlobalProperty(*manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_AnonymizationSequence, "HelloWorld");
-  ASSERT_TRUE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_AnonymizationSequence));
+  db.SetGlobalProperty(*manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseInternal9, "HelloWorld");
+  ASSERT_TRUE(db.LookupGlobalProperty(s, *manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseInternal9));
   ASSERT_EQ("HelloWorld", s);
 
   int64_t a = db.CreateResource(*manager, "study", OrthancPluginResourceType_Study);