From ade8cc43b138fb776c16fe2b4de18c92e148c9c7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 16 Feb 2023 22:35:14 -0500 Subject: Added query timeout --- hkutil/database.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hkutil/database.h b/hkutil/database.h index f4e7a9a..7425756 100644 --- a/hkutil/database.h +++ b/hkutil/database.h @@ -194,6 +194,11 @@ namespace hatkirby { std::string queryString, std::list bindings = {}) { + if (timeout_ > 0) + { + deadline_ = static_cast(std::time(nullptr)) + timeout_; + } + sqlite3_stmt* tempStmt; int ret = sqlite3_prepare_v2( @@ -310,6 +315,22 @@ namespace hatkirby { return result; } + void setTimeout(int seconds) + { + timeout_ = seconds; + if (timeout_ > 0) + { + sqlite3_progress_handler(ppdb_.get(), 1000, [](void* db){ + if (static_cast(std::time(nullptr)) > static_cast(db)->deadline_) { + return 1; + } + return 0; + }, this); + } else { + sqlite3_progress_handler(ppdb_.get(), 0, nullptr, nullptr); + } + } + private: class sqlite3_deleter { @@ -392,6 +413,8 @@ namespace hatkirby { } ppdb_type ppdb_; + long long timeout_ = -1; + long long deadline_ = 0; }; -- cgit 1.4.1