From 17de42a947872aebd6cbaa6aa472aa04f32d0e24 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Sun, 12 Jul 2020 12:21:41 +0200 Subject: [PATCH] Implement locking in SQLite --- src/engine/server/databases/sqlite.cpp | 10 ++++++++++ src/engine/server/databases/sqlite.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/engine/server/databases/sqlite.cpp b/src/engine/server/databases/sqlite.cpp index f3cd29d4d..c15227980 100644 --- a/src/engine/server/databases/sqlite.cpp +++ b/src/engine/server/databases/sqlite.cpp @@ -11,6 +11,7 @@ CSqliteConnection::CSqliteConnection(const char *pFilename, bool Setup) : m_pDb(nullptr), m_pStmt(nullptr), m_Done(true), + m_Locked(false), m_InUse(false) { str_copy(m_aFilename, pFilename, sizeof(m_aFilename)); @@ -67,6 +68,7 @@ IDbConnection::Status CSqliteConnection::Connect() return Status::ERROR; m_Setup = false; } + m_Locked = false; return Status::SUCCESS; } @@ -80,10 +82,18 @@ void CSqliteConnection::Disconnect() void CSqliteConnection::Lock(const char *pTable) { + // locks the whole database read/write + Execute("BEGIN EXCLUSIVE TRANSACTION;"); + m_Locked = true; } void CSqliteConnection::Unlock() { + if(m_Locked) + { + Execute("COMMIT TRANSACTION;"); + m_Locked = false; + } } void CSqliteConnection::PrepareStatement(const char *pStmt) diff --git a/src/engine/server/databases/sqlite.h b/src/engine/server/databases/sqlite.h index 8e45c0ded..f68c05185 100644 --- a/src/engine/server/databases/sqlite.h +++ b/src/engine/server/databases/sqlite.h @@ -45,6 +45,7 @@ private: sqlite3 *m_pDb; sqlite3_stmt *m_pStmt; bool m_Done; // no more rows available for Step + bool m_Locked; // returns true, if the query succeded bool Execute(const char *pQuery);