aboutsummaryrefslogtreecommitdiff
path: root/src/sql/Sql.cpp
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-05-15 00:09:17 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-05-15 00:09:20 +0200
commit4a0d1ff39ec150d8e8c04ca846b3116884e1774e (patch)
tree9c8d64cfdbcd395ab0e1f94751791ed3020c4b84 /src/sql/Sql.cpp
parent2260790118358449a18527fcd9e6ff46d2caccf7 (diff)
Implement exception safe sqlite transaction class (rollback)
Diffstat (limited to 'src/sql/Sql.cpp')
-rw-r--r--src/sql/Sql.cpp20
1 files changed, 20 insertions, 0 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);
+ }
}