From 296f37262daa6a3ebb5bf9b32701d26ec5315132 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Sat, 25 Jan 2025 15:18:34 -0600 Subject: [PATCH] Also use exponential backoff for a locked db in the initial database schema, and support passing a QString to exec() --- cpp/db_common.cpp | 14 ++++++++------ cpp/db_common.h | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cpp/db_common.cpp b/cpp/db_common.cpp index 3222afc..f6d42ba 100644 --- a/cpp/db_common.cpp +++ b/cpp/db_common.cpp @@ -56,11 +56,13 @@ QSqlDatabase get_thread_connection() { return thread_db; } -bool ci_query_exec(QSqlQuery* query) { +bool ci_query_exec(QSqlQuery* query, const QString query_string) { bool passed = false; int attempt = 0; while (!passed) { - passed = query->exec(); + if (query_string.isEmpty()) passed = query->exec(); + else passed = query->exec(query_string); + if (passed) return true; attempt++; @@ -79,9 +81,9 @@ bool init_database(const QString& database_path) { // Apply PRAGMAs { QSqlQuery pragma_query(get_thread_connection()); - pragma_query.exec("PRAGMA journal_mode = WAL;"); - pragma_query.exec("PRAGMA synchronous = NORMAL;"); - pragma_query.exec("PRAGMA foreign_keys = ON;"); + ci_query_exec(&pragma_query, "PRAGMA journal_mode = WAL;"); + ci_query_exec(&pragma_query, "PRAGMA synchronous = NORMAL;"); + ci_query_exec(&pragma_query, "PRAGMA foreign_keys = ON;"); } // Run the schema creation (or migration) statements @@ -200,7 +202,7 @@ bool init_database(const QString& database_path) { for (const QString &statement : sql_statements) { QSqlQuery query(get_thread_connection()); QString trimmed = statement.trimmed(); - if (!trimmed.isEmpty() && !query.exec(trimmed)) { + if (!trimmed.isEmpty() && !ci_query_exec(&query, trimmed)) { qDebug() << "Failed to execute SQL: " << trimmed << "\nError: " << query.lastError().text(); return false; diff --git a/cpp/db_common.h b/cpp/db_common.h index 01d43aa..60a3d9b 100644 --- a/cpp/db_common.h +++ b/cpp/db_common.h @@ -18,9 +18,10 @@ #include #include +#include QSqlDatabase get_thread_connection(); -bool ci_query_exec(QSqlQuery* query); +bool ci_query_exec(QSqlQuery* query, const QString query_string = ""); bool init_database(const QString& database_path); #endif // DB_COMMON_H