# HG changeset patch # User Sebastien Jodogne # Date 1617906615 -7200 # Node ID e9ba888f371bc0d46dd2a02b44e39be6d3c0ea77 # Parent f033cc03926491e4a3bef6d31049c82e730b3637 fix IndexBackend::SetGlobalProperty(), lighten GlobalProperty enum diff -r f033cc039264 -r e9ba888f371b Framework/Plugins/GlobalProperties.h --- 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 }; } diff -r f033cc039264 -r e9ba888f371b Framework/Plugins/IndexBackend.cpp --- 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(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 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(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 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(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); + } } } } diff -r f033cc039264 -r e9ba888f371b Framework/Plugins/IndexUnitTests.h --- 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);