From cefa3cb85a266b96a1b8bc78c15ac673effdb920 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 13 Feb 2018 09:24:22 +0100 Subject: [PATCH] feat(MainDb): better code, L_SAFE_TRANSACTION supports query without returned value --- src/db/main-db.cpp | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index f06ce52fb..f992d72a1 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -80,12 +80,18 @@ struct SafeTransactionInfo { template class SafeTransaction { + using InternalReturnType = typename remove_reference()())>::type; + public: - using ReturnType = typename remove_reference()())>::type; + using ReturnType = typename std::conditional< + std::is_same::value, + bool, + InternalReturnType + >::type; SafeTransaction (SafeTransactionInfo &info, Function function) : mFunction(move(function)) { try { - mResult = mFunction(); + mResult= exec(); } catch (const soci::soci_error &e) { lWarning() << "Catched exception in MainDb::" << info.name << "(" << e.what() << ")."; soci::soci_error::error_category category = e.get_error_category(); @@ -94,9 +100,9 @@ public: info.mainDb->forceReconnect() ) { try { - mResult = mFunction(); + mResult = exec(); } catch (const exception &e) { - lWarning() << "Unable to execute query after reconnect in MainDb::" << info.name << "(" << e.what() << ")."; + lError() << "Unable to execute query after reconnect in MainDb::" << info.name << "(" << e.what() << ")."; } return; } @@ -114,6 +120,17 @@ public: } private: + template + typename std::enable_if::value, bool>::type exec () const { + mFunction(); + return true; + } + + template + typename std::enable_if::value, T>::type exec () const { + return mFunction(); + } + static const char *getErrorCategoryAsString (soci::soci_error::error_category category) { switch (category) { case soci::soci_error::connection_error: @@ -2104,8 +2121,6 @@ void MainDb::markChatMessagesAsRead (const ChatRoomId &chatRoomId) const { *session << query, soci::use(dbChatRoomId); tr.commit(); } - - return true; }; } @@ -2337,8 +2352,6 @@ void MainDb::cleanHistory (const ChatRoomId &chatRoomId, FilterMask mask) { *session << "DELETE FROM event WHERE id IN (" + query + ")", soci::use(dbChatRoomId); tr.commit(); - - return true; }; } @@ -2492,8 +2505,6 @@ void MainDb::insertChatRoom (const shared_ptr &chatRoom) { d->insertChatRoom(chatRoom); tr.commit(); - - return true; }; } @@ -2518,8 +2529,6 @@ void MainDb::deleteChatRoom (const ChatRoomId &chatRoomId) { *session << "DELETE FROM chat_room WHERE id = :chatRoomId", soci::use(dbChatRoomId); tr.commit(); - - return true; }; } @@ -2585,8 +2594,6 @@ void MainDb::migrateBasicToClientGroupChatRoom ( } tr.commit(); - - return true; }; } @@ -2681,8 +2688,6 @@ void MainDb::insertOneToOneConferenceChatRoom (const shared_ptr soci::use(stateInt), soci::use(participantId), soci::use(participantSipDeviceAddressId); tr.commit(); - - return true; }; } @@ -2751,12 +2752,10 @@ bool MainDb::import (Backend, const string ¶meters) { // TODO: Remove condition after cpp migration in friends/friends list. if (false) d->importLegacyFriends(inDbSession); - return true; }; L_SAFE_TRANSACTION { d->importLegacyHistory(inDbSession); - return true; }; return true;