From 54f53857279644914e0c2a2aed24060f31ed2575 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 15 May 2018 00:09:17 +0200 Subject: Implement exception safe sqlite transaction class (rollback) --- src/sql/Sql.cpp | 20 ++++++++++++++++++++ src/sql/SqlExec.cpp | 4 ---- src/sql/SqlQuery.cpp | 5 ----- 3 files changed, 20 insertions(+), 9 deletions(-) (limited to 'src/sql') diff --git a/src/sql/Sql.cpp b/src/sql/Sql.cpp index 754a30d..4e65ddb 100644 --- a/src/sql/Sql.cpp +++ b/src/sql/Sql.cpp @@ -1,5 +1,7 @@ #include "../../include/odhtdb/sql/Sql.hpp" #include +#include +#include namespace odhtdb { @@ -18,4 +20,22 @@ namespace odhtdb } return SQLITE_OK; } + + SqlTransaction::SqlTransaction(sqlite3 *_db) : + db(_db) + { + assert(db); + sqlite3_exec(db, "BEGIN", 0, 0, 0); + } + + SqlTransaction::~SqlTransaction() + { + if(std::uncaught_exception()) + sqlite3_exec(db, "ROLLBACK", 0, 0, 0); + } + + void SqlTransaction::commit() + { + sqlite3_exec(db, "COMMIT", 0, 0, 0); + } } diff --git a/src/sql/SqlExec.cpp b/src/sql/SqlExec.cpp index 732b2f1..bdb0fbd 100644 --- a/src/sql/SqlExec.cpp +++ b/src/sql/SqlExec.cpp @@ -12,7 +12,6 @@ namespace odhtdb { std::string errMsg = "Failed to prepare sqlite statement, error: "; errMsg += sqlite3_errmsg(db); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlExecException(errMsg); } } @@ -37,7 +36,6 @@ namespace odhtdb errMsg += " parameters, got "; errMsg += std::to_string(args.size()); errMsg += " arguments"; - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlExecException(errMsg); } @@ -49,7 +47,6 @@ namespace odhtdb { std::string errMsg = "Failed to bind param, error code: "; errMsg += std::to_string(rc); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlExecException(errMsg); } ++paramIndex; @@ -60,7 +57,6 @@ namespace odhtdb { std::string errMsg = "Failed to perform sql exec, error: "; errMsg += sqlite3_errmsg(db); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlExecException(errMsg); } } diff --git a/src/sql/SqlQuery.cpp b/src/sql/SqlQuery.cpp index b99f92d..6201332 100644 --- a/src/sql/SqlQuery.cpp +++ b/src/sql/SqlQuery.cpp @@ -13,7 +13,6 @@ namespace odhtdb { std::string errMsg = "Failed to prepare sqlite statement, error: "; errMsg += sqlite3_errmsg(db); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlQueryException(errMsg); } @@ -27,7 +26,6 @@ namespace odhtdb errMsg += " arguments"; sqlite3_finalize(stmt); stmt = nullptr; - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlQueryException(errMsg); } @@ -41,7 +39,6 @@ namespace odhtdb errMsg += std::to_string(rc); sqlite3_finalize(stmt); stmt = nullptr; - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlQueryException(errMsg); } ++paramIndex; @@ -62,7 +59,6 @@ namespace odhtdb { std::string errMsg = "Failed to perform sql select, error: "; errMsg += sqlite3_errmsg(db); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlQueryException(errMsg); } @@ -88,7 +84,6 @@ namespace odhtdb errMsg += " has to be between 0 and "; errMsg += std::to_string(numColumns - 1); } - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); throw SqlQueryException(errMsg); } } -- cgit v1.2.3