FOSSredder 0.5.0
Loading...
Searching...
No Matches
SqliteTransaction.cpp
Go to the documentation of this file.
1
5
7
8#include <stdexcept>
9#include <string>
10
11static void exec(sqlite3* db, const char* sql) {
12 char* err = nullptr;
13 if (sqlite3_exec(db, sql, nullptr, nullptr, &err) != SQLITE_OK) {
14 std::string e = err ? err : "unknown";
15 if (err) {
16 sqlite3_free(err);
17 }
18 throw std::runtime_error(e);
19 }
20}
21
23 if (!db_) {
24 throw std::runtime_error("db is null");
25 }
26
27 exec(db_, "BEGIN IMMEDIATE;");
28}
29
31 if (!db_) {
32 return;
33 }
34
35 try {
36 if (!committed_) {
37 exec(db_, "ROLLBACK;");
38 }
39 } catch (...) {}
40}
41
43 if (committed_) {
44 return;
45 }
46
47 exec(db_, "COMMIT;");
48 committed_ = true;
49}
static void exec(sqlite3 *db, const char *sql)
static void exec(sqlite3 *db, const char *sql)
Declares a lightweight SQLite transaction guard.
void commit()
Commit the transaction.
SqliteTransaction(sqlite3 *db)
Start a SQLite transaction.
~SqliteTransaction()
Roll back the transaction unless it was committed.