Commit c0e48a83 authored by Ronan's avatar Ronan

feat(MainDb): add a SmartTransaction component to log start/end

parent 53296fb8
......@@ -293,7 +293,8 @@ if (SOCI_FOUND)
add_definitions(-DSOCI_ENABLED)
list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
db/internal/safe-transaction.h
db/internal/db-exception-handler.h
db/internal/smart-transaction.h
db/internal/statements.h
)
......
/*
* safe-transaction.h
* db-exception-handler.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* This program is free software; you can redistribute it and/or
......@@ -17,8 +17,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_SAFE_TRANSACTION_H_
#define _L_SAFE_TRANSACTION_H_
#ifndef _L_DB_EXCEPTION_HANDLER_H_
#define _L_DB_EXCEPTION_HANDLER_H_
#include <soci/soci.h>
......@@ -27,15 +27,15 @@
// =============================================================================
#define L_SAFE_TRANSACTION_C(CONTEXT) \
LinphonePrivate::SafeTransactionInfo().set(__func__, CONTEXT) * [&]()
#define L_DB_EXCEPTION_HANDLER_C(CONTEXT) \
LinphonePrivate::DbExceptionHandlerInfo().set(__func__, CONTEXT) * [&]()
#define L_SAFE_TRANSACTION L_SAFE_TRANSACTION_C(this)
#define L_DB_EXCEPTION_HANDLER L_DB_EXCEPTION_HANDLER_C(this)
LINPHONE_BEGIN_NAMESPACE
struct SafeTransactionInfo {
SafeTransactionInfo &set (const char *_name, const MainDb *_mainDb) {
struct DbExceptionHandlerInfo {
DbExceptionHandlerInfo &set (const char *_name, const MainDb *_mainDb) {
name = _name;
mainDb = const_cast<MainDb *>(_mainDb);
return *this;
......@@ -46,7 +46,7 @@ struct SafeTransactionInfo {
};
template<typename Function>
class SafeTransaction {
class DbExceptionHandler {
using InternalReturnType = typename std::remove_reference<decltype(std::declval<Function>()())>::type;
public:
......@@ -56,7 +56,7 @@ public:
InternalReturnType
>::type;
SafeTransaction (SafeTransactionInfo &info, Function function) : mFunction(std::move(function)) {
DbExceptionHandler (DbExceptionHandlerInfo &info, Function function) : mFunction(std::move(function)) {
try {
mResult= exec<InternalReturnType>();
} catch (const soci::soci_error &e) {
......@@ -80,7 +80,7 @@ public:
}
}
SafeTransaction (SafeTransaction &&safeTransaction) : mFunction(std::move(safeTransaction.mFunction)) {}
DbExceptionHandler (DbExceptionHandler &&dbExceptionHandler) : mFunction(std::move(dbExceptionHandler.mFunction)) {}
operator ReturnType () const {
return mResult;
......@@ -128,14 +128,14 @@ private:
Function mFunction;
ReturnType mResult{};
L_DISABLE_COPY(SafeTransaction);
L_DISABLE_COPY(DbExceptionHandler);
};
template<typename Function>
typename SafeTransaction<Function>::ReturnType operator* (SafeTransactionInfo &info, Function &&function) {
return SafeTransaction<Function>(info, std::forward<Function>(function));
typename DbExceptionHandler<Function>::ReturnType operator* (DbExceptionHandlerInfo &info, Function &&function) {
return DbExceptionHandler<Function>(info, std::forward<Function>(function));
}
LINPHONE_END_NAMESPACE
#endif // ifndef _L_SAFE_TRANSACTION_H_
#endif // ifndef _L_DB_EXCEPTION_HANDLER_H_
/*
* smart-transaction.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_SMART_TRANSACTION_H_
#define _L_SMART_TRANSACTION_H_
#include <soci/soci.h>
#include "logger/logger.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class SmartTransaction {
public:
SmartTransaction (soci::session *session, const char *name) : mTransaction(*session), mName(name) {
lInfo() << "Start transaction " << this << " in `" << mName << "`.";
}
~SmartTransaction () {
lInfo() << "Rollback transaction " << this << " in `" << mName << "`.";
}
void commit () {
lInfo() << "Commit transaction " << this << " in `" << mName << "`.";
mTransaction.commit();
}
private:
soci::transaction mTransaction;
const char *mName;
L_DISABLE_COPY(SmartTransaction);
};
LINPHONE_END_NAMESPACE
#endif // ifndef _L_SMART_TRANSACTION_H_
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment