summaryrefslogtreecommitdiff
path: root/platform/default/sqlite3.cpp
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-04-11 19:46:45 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-04-11 19:46:45 -0700
commitc3bed1dab8ec429d100d383ad93410d1cfaa16fb (patch)
treed54859fa4910a9b443892dc60e9495a6143c2cba /platform/default/sqlite3.cpp
parentbe47e35a975ff7c43755e8c3898512147bb37904 (diff)
downloadqtlocation-mapboxgl-c3bed1dab8ec429d100d383ad93410d1cfaa16fb.tar.gz
[core] Fix race condition that could lead to a UNIQUE constraint failure (#4677)
Diffstat (limited to 'platform/default/sqlite3.cpp')
-rw-r--r--platform/default/sqlite3.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/platform/default/sqlite3.cpp b/platform/default/sqlite3.cpp
index bf0bbfc683..a5d619406f 100644
--- a/platform/default/sqlite3.cpp
+++ b/platform/default/sqlite3.cpp
@@ -338,5 +338,40 @@ void Statement::clearBindings() {
sqlite3_clear_bindings(stmt);
}
+Transaction::Transaction(Database& db_, Mode mode)
+ : db(db_) {
+ switch (mode) {
+ case Deferred:
+ db.exec("BEGIN DEFERRED TRANSACTION");
+ break;
+ case Immediate:
+ db.exec("BEGIN IMMEDIATE TRANSACTION");
+ break;
+ case Exclusive:
+ db.exec("BEGIN EXCLUSIVE TRANSACTION");
+ break;
+ }
+}
+
+Transaction::~Transaction() {
+ if (needRollback) {
+ try {
+ rollback();
+ } catch (...) {
+ // Ignore failed rollbacks in destructor.
+ }
+ }
+}
+
+void Transaction::commit() {
+ needRollback = false;
+ db.exec("COMMIT TRANSACTION");
+}
+
+void Transaction::rollback() {
+ needRollback = false;
+ db.exec("ROLLBACK TRANSACTION");
+}
+
} // namespace sqlite
} // namespace mapbox