diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-02-01 11:04:35 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-02-01 11:15:34 +0200 |
commit | 9810dbd7a2959ac114ede3b3c0d452b862f69544 (patch) | |
tree | 36ddce775112541d8f149ca37af08994faf8c379 | |
parent | 691398e9bde410efa095bb648366779995b98e84 (diff) | |
download | qtlocation-mapboxgl-9810dbd7a2959ac114ede3b3c0d452b862f69544.tar.gz |
[Qt] Guarantee QSqlDatabase connection name uniqueness
Guarantee QSqlDatabase connection name uniqueness by using a static
counter together with the current thread.
-rw-r--r-- | platform/qt/src/sqlite3.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/platform/qt/src/sqlite3.cpp b/platform/qt/src/sqlite3.cpp index 367b51811d..0880d34d1f 100644 --- a/platform/qt/src/sqlite3.cpp +++ b/platform/qt/src/sqlite3.cpp @@ -4,6 +4,7 @@ #include <QSqlError> #include <QSqlQuery> #include <QStringList> +#include <QThread> #include <QVariant> #include <cassert> @@ -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(';'); |