# HG changeset patch # User Sebastien Jodogne # Date 1680521406 -7200 # Node ID a9d00b17a48ed962b46f3428f57cb9bc24118767 # Parent 055428d927721057e4701bd0994c5d973f9fc239 implemented OrthancPluginDatabaseV4::Upgrade() diff -r 055428d92772 -r a9d00b17a48e OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp --- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Mon Apr 03 11:18:55 2023 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Mon Apr 03 13:30:06 2023 +0200 @@ -30,6 +30,7 @@ #include "../../../OrthancFramework/Sources/Logging.h" #include "../../../OrthancFramework/Sources/OrthancException.h" #include "../../Sources/Database/ResourcesContent.h" +#include "../../Sources/Database/VoidDatabaseListener.h" #include "PluginsEnumerations.h" #include "OrthancDatabasePlugin.pb.h" // Auto-generated file @@ -225,21 +226,54 @@ public: Transaction(OrthancPluginDatabaseV4& database, IDatabaseListener& listener, - void* transaction) : + TransactionType type) : database_(database), listener_(listener), - transaction_(transaction) + transaction_(NULL) { + DatabasePluginMessages::DatabaseRequest request; + + switch (type) + { + case TransactionType_ReadOnly: + request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_ONLY); + break; + + case TransactionType_ReadWrite: + request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_WRITE); + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + DatabasePluginMessages::DatabaseResponse response; + ExecuteDatabase(response, database, DatabasePluginMessages::OPERATION_START_TRANSACTION, request); + + transaction_ = reinterpret_cast(response.start_transaction().transaction()); + + if (transaction_ == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } } virtual ~Transaction() { - DatabasePluginMessages::DatabaseRequest request; - request.mutable_finalize_transaction()->set_transaction(reinterpret_cast(transaction_)); + try + { + DatabasePluginMessages::DatabaseRequest request; + request.mutable_finalize_transaction()->set_transaction(reinterpret_cast(transaction_)); - DatabasePluginMessages::DatabaseResponse response; - ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request); + DatabasePluginMessages::DatabaseResponse response; + ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request); + } + catch (OrthancException& e) + { + // Destructors must not throw exceptions + LOG(ERROR) << "Cannot finalize the database engine: " << e.What(); + } } @@ -1204,27 +1238,10 @@ { throw OrthancException(ErrorCode_BadSequenceOfCalls); } - - DatabasePluginMessages::DatabaseRequest request; - - switch (type) + else { - case TransactionType_ReadOnly: - request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_ONLY); - break; - - case TransactionType_ReadWrite: - request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_WRITE); - break; - - default: - throw OrthancException(ErrorCode_InternalError); + return new Transaction(*this, listener, type); } - - DatabasePluginMessages::DatabaseResponse response; - ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_START_TRANSACTION, request); - - return new Transaction(*this, listener, reinterpret_cast(response.start_transaction().transaction())); } @@ -1250,8 +1267,25 @@ } else { - // TODO - throw OrthancException(ErrorCode_NotImplemented); + VoidDatabaseListener listener; + Transaction transaction(*this, listener, TransactionType_ReadWrite); + + try + { + DatabasePluginMessages::DatabaseRequest request; + request.mutable_upgrade()->set_target_version(targetVersion); + request.mutable_upgrade()->set_storage_area(reinterpret_cast(&storageArea)); + + DatabasePluginMessages::DatabaseResponse response; + + ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_UPGRADE, request); + transaction.Commit(0); + } + catch (OrthancException& e) + { + transaction.Rollback(); + throw; + } } }