Implement transaction RAII.
[toast/confclerk.git] / src / sql / sqlengine.h
index 5cfc6bf..68d0679 100644 (file)
@@ -82,5 +82,24 @@ class SqlEngine : public QObject {
         void dbError(const QString& message);
 };
 
+
+class TransactionRaii {
+    SqlEngine& sqlEngine;
+    bool committed = false;
+public:
+    TransactionRaii(SqlEngine& sqlEngine): sqlEngine(sqlEngine) {
+        sqlEngine.beginTransaction();
+    }
+
+    void commit() {
+        sqlEngine.commitTransaction();
+        committed = true;
+    }
+
+    ~TransactionRaii() {
+        if (!committed) sqlEngine.rollbackTransaction();
+    }
+};
+
 #endif /* SQLENGINE_H */