From d19f5de7bff8ba128fbc66d3a765889c195843f2 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sun, 11 Dec 2016 11:05:51 +0100 Subject: [PATCH] SqlDatabase: Skip copy when executing query on main thread --- src/lib/tools/sqldatabase.cpp | 8 +++++++- src/lib/tools/sqldatabase.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib/tools/sqldatabase.cpp b/src/lib/tools/sqldatabase.cpp index b37dd38c9..db6e1d7cd 100644 --- a/src/lib/tools/sqldatabase.cpp +++ b/src/lib/tools/sqldatabase.cpp @@ -19,6 +19,7 @@ #include #include +#include #include @@ -54,8 +55,13 @@ QSqlDatabase SqlDatabase::databaseForThread(QThread* thread) return m_databases[thread]; } -QSqlQuery SqlDatabase::exec(const QSqlQuery &query) +QSqlQuery SqlDatabase::exec(QSqlQuery &query) { + if (QThread::currentThread() == qApp->thread()) { + query.exec(); + return query; + } + QSqlQuery out(databaseForThread(QThread::currentThread())); out.prepare(query.lastQuery()); diff --git a/src/lib/tools/sqldatabase.h b/src/lib/tools/sqldatabase.h index c836e8965..98e7e5dd7 100644 --- a/src/lib/tools/sqldatabase.h +++ b/src/lib/tools/sqldatabase.h @@ -39,7 +39,7 @@ public: QSqlDatabase databaseForThread(QThread* thread); // Executes query using correct database for current thread - QSqlQuery exec(const QSqlQuery &query); + QSqlQuery exec(QSqlQuery &query); // Executes query asynchronously on one thread from QThreadPool QFuture execAsync(const QSqlQuery &query);