From ab814ef9b08e4b27204e7b64379cec78d46fc0ae Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Wed, 1 Feb 2017 11:04:35 +0200 Subject: [PATCH] [Qt] Guarantee QSqlDatabase connection name uniqueness Guarantee QSqlDatabase connection name uniqueness by using a static counter together with the current thread. --- platform/qt/src/sqlite3.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/platform/qt/src/sqlite3.cpp b/platform/qt/src/sqlite3.cpp index 85de73e6fc0..3470c369105 100644 --- a/platform/qt/src/sqlite3.cpp +++ b/platform/qt/src/sqlite3.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -52,10 +53,16 @@ void checkDatabaseError(const QSqlDatabase &db) { class DatabaseImpl { public: DatabaseImpl(const char* filename, int flags) { + static uint64_t count = 0; + const QString connectionName = QString::number(uint64_t(QThread::currentThread())) + QString::number(count++); + if (!QSqlDatabase::drivers().contains("QSQLITE")) { throw Exception { Exception::Code::CANTOPEN, "SQLite driver not found." }; } - db.reset(new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", QString::number(qrand())))); + + assert(!QSqlDatabase::contains(connectionName)); + db.reset(new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", connectionName))); + QString connectOptions = db->connectOptions(); if (flags & OpenFlag::ReadOnly) { if (!connectOptions.isEmpty()) connectOptions.append(';');