Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[Qt] Guarantee QSqlDatabase connection name uniqueness
Browse files Browse the repository at this point in the history
Guarantee QSqlDatabase connection name uniqueness by using a static
counter together with the current thread.
  • Loading branch information
brunoabinader committed Feb 1, 2017
1 parent 47b67fe commit ab814ef
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion platform/qt/src/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QSqlError>
#include <QSqlQuery>
#include <QStringList>
#include <QThread>
#include <QVariant>

#include <cassert>
Expand Down Expand Up @@ -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(';');
Expand Down

0 comments on commit ab814ef

Please sign in to comment.