1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-14 19:12:11 +01:00

SqlDatabase: Correctly create new connections for threads

In Qt 5.11 QSqlDatabase can be accessed only from thread where
it was created, so it is no longer possible to clone it from other
threads.
This commit is contained in:
David Rosca 2018-01-28 12:49:35 +01:00
parent 2a22f61dec
commit fa8fe079cb
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
3 changed files with 21 additions and 3 deletions

View File

@ -21,6 +21,7 @@
#include "updater.h" #include "updater.h"
#include "qztools.h" #include "qztools.h"
#include "restoremanager.h" #include "restoremanager.h"
#include "sqldatabase.h"
#include <QDir> #include <QDir>
#include <QSqlError> #include <QSqlError>
@ -273,4 +274,6 @@ void ProfileManager::connectDatabase()
} }
} }
} }
SqlDatabase::instance()->setDatabase(db);
} }

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2014-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -44,7 +44,9 @@ QSqlDatabase SqlDatabase::database()
if (!s_databases.hasLocalData()) { if (!s_databases.hasLocalData()) {
const QString threadStr = QString::number((quintptr) QThread::currentThread()); const QString threadStr = QString::number((quintptr) QThread::currentThread());
QSqlDatabase db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(), QL1S("Falkon/") + threadStr); QSqlDatabase db = QSqlDatabase::addDatabase(QSL("QSQLITE"), QSL("Falkon/") + threadStr);
db.setDatabaseName(m_databaseName);
db.setConnectOptions(m_connectOptions);
db.open(); db.open();
s_databases.setLocalData(db); s_databases.setLocalData(db);
} }
@ -52,6 +54,12 @@ QSqlDatabase SqlDatabase::database()
return s_databases.localData(); return s_databases.localData();
} }
void SqlDatabase::setDatabase(const QSqlDatabase &database)
{
m_databaseName = database.databaseName();
m_connectOptions = database.connectOptions();
}
// instance // instance
SqlDatabase* SqlDatabase::instance() SqlDatabase* SqlDatabase::instance()
{ {

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2014-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -36,7 +36,14 @@ public:
// Returns database connection for current thread // Returns database connection for current thread
QSqlDatabase database(); QSqlDatabase database();
// Sets database to be created for other threads
void setDatabase(const QSqlDatabase &database);
static SqlDatabase* instance(); static SqlDatabase* instance();
private:
QString m_databaseName;
QString m_connectOptions;
}; };
#endif // SQLDATABASE_H #endif // SQLDATABASE_H