aboutsummaryrefslogtreecommitdiff
path: root/src/sql
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-15 00:09:17 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commit54f53857279644914e0c2a2aed24060f31ed2575 (patch)
tree1244fa141230afe174af56710ebf47219051aaeb /src/sql
parentfa8d14018891d90e23fce0e510a01728970932f2 (diff)
Implement exception safe sqlite transaction class (rollback)
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/Sql.cpp20
-rw-r--r--src/sql/SqlExec.cpp4
-rw-r--r--src/sql/SqlQuery.cpp5
3 files changed, 20 insertions, 9 deletions
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 <sqlite3.h>
+#include <exception>
+#include <cassert>
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);
}
}