mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
unique sql database connection is created on different threads
Signed-off-by: Paulchen-Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
parent
90599e820a
commit
c676b640f1
@ -32,7 +32,7 @@ public:
|
|||||||
/// set root path
|
/// set root path
|
||||||
void setRootPath(const QString& rootPath);
|
void setRootPath(const QString& rootPath);
|
||||||
/// define the database to work with
|
/// define the database to work with
|
||||||
void setDB(const QString& dbn);
|
void setDatabaseName(const QString& dbn) { _dbn = dbn; };
|
||||||
/// set a table to work with
|
/// set a table to work with
|
||||||
void setTable(const QString& table);
|
void setTable(const QString& table);
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public:
|
|||||||
{
|
{
|
||||||
// Init Hyperion database usage
|
// Init Hyperion database usage
|
||||||
setRootPath(rootPath);
|
setRootPath(rootPath);
|
||||||
setDB("hyperion");
|
setDatabaseName("hyperion");
|
||||||
|
|
||||||
// Init instance table
|
// Init instance table
|
||||||
setTable("instances");
|
setTable("instances");
|
||||||
|
@ -4,10 +4,13 @@
|
|||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QSqlRecord>
|
#include <QSqlRecord>
|
||||||
|
#include <QThreadStorage>
|
||||||
|
#include <QUuid>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
|
||||||
// not in header because of linking
|
// not in header because of linking
|
||||||
static QString _rootPath;
|
static QString _rootPath;
|
||||||
|
static QThreadStorage<QSqlDatabase> _databasePool;
|
||||||
|
|
||||||
DBManager::DBManager(QObject* parent)
|
DBManager::DBManager(QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
@ -26,22 +29,6 @@ void DBManager::setRootPath(const QString& rootPath)
|
|||||||
QDir().mkpath(_rootPath+"/db");
|
QDir().mkpath(_rootPath+"/db");
|
||||||
}
|
}
|
||||||
|
|
||||||
void DBManager::setDB(const QString& dbn)
|
|
||||||
{
|
|
||||||
_dbn = dbn;
|
|
||||||
// new database connection if not found
|
|
||||||
if(!QSqlDatabase::contains(dbn))
|
|
||||||
{
|
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",dbn);
|
|
||||||
db.setDatabaseName(_rootPath+"/db/"+dbn+".db");
|
|
||||||
if(!db.open())
|
|
||||||
{
|
|
||||||
Error(_log, QSTRING_CSTR(db.lastError().text()));
|
|
||||||
throw std::runtime_error("Failed to open database connection!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DBManager::setTable(const QString& table)
|
void DBManager::setTable(const QString& table)
|
||||||
{
|
{
|
||||||
_table = table;
|
_table = table;
|
||||||
@ -49,17 +36,20 @@ void DBManager::setTable(const QString& table)
|
|||||||
|
|
||||||
QSqlDatabase DBManager::getDB() const
|
QSqlDatabase DBManager::getDB() const
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::database(_dbn);
|
if(_databasePool.hasLocalData())
|
||||||
|
return _databasePool.localData();
|
||||||
if (db.isOpen() && db.isValid())
|
|
||||||
return db;
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
db = QSqlDatabase::addDatabase("QSQLITE", _dbn);
|
auto db = QSqlDatabase::addDatabase("QSQLITE", QUuid::createUuid().toString());
|
||||||
|
_databasePool.setLocalData(db);
|
||||||
db.setDatabaseName(_rootPath+"/db/"+_dbn+".db");
|
db.setDatabaseName(_rootPath+"/db/"+_dbn+".db");
|
||||||
|
if(!db.open())
|
||||||
|
{
|
||||||
|
Error(_log, QSTRING_CSTR(db.lastError().text()));
|
||||||
|
throw std::runtime_error("Failed to open database connection!");
|
||||||
|
}
|
||||||
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
return db;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
bool DBManager::createRecord(const VectorPair& conditions, const QVariantMap& columns) const
|
||||||
|
Loading…
Reference in New Issue
Block a user