DatabaseFactory: Move to /shared

This commit is contained in:
2022-02-27 23:13:02 +01:00
parent 7066cc1a45
commit 294455b861
3 changed files with 1 additions and 5 deletions

View File

@ -32,9 +32,7 @@ SOURCES += \
commanddelete.cpp \
commandupdate.cpp \
filesaver.cpp \
databasefactory.cpp \
sqlitedbservice.cpp \
logger.cpp \
commandsearch.cpp \
commandlist.cpp
@ -55,9 +53,7 @@ HEADERS += \
commanddelete.h \
commandupdate.h \
filesaver.h \
databasefactory.h \
sqlitedbservice.h \
logger.h \
commandsearch.h \
commandlist.h
INCLUDEPATH += /usr/include/poppler/qt5/ /usr/include/quazip5

View File

@ -1,40 +0,0 @@
#include <QThread>
#include "databasefactory.h"
#include "logger.h"
DatabaseFactory::DatabaseFactory(QString connectionString)
{
this->connectionString = connectionString;
}
static QThreadStorage<QSqlDatabase> dbStore;
// TODO: not threadsafe
QSqlDatabase DatabaseFactory::createNew()
{
static int counter = 0;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "QSS" + QString::number(counter++));
db.setDatabaseName(this->connectionString);
if(!db.open())
{
Logger::error() << "Failed to open the database: " << this->connectionString << Qt::endl;
throw LooqsGeneralException("Failed to create open new connection");
}
return db;
}
QSqlDatabase DatabaseFactory::forCurrentThread()
{
if(dbStore.hasLocalData())
{
return dbStore.localData();
}
QSqlDatabase db =
QSqlDatabase::addDatabase("QSQLITE", "QSS" + QString::number((quint64)QThread::currentThread(), 16));
db.setDatabaseName(this->connectionString);
if(!db.open())
{
Logger::error() << "Failed to open the database: " << this->connectionString << Qt::endl;
throw LooqsGeneralException("Failed to create open new connection");
}
dbStore.setLocalData(db);
return db;
}

View File

@ -1,17 +0,0 @@
#ifndef DATABASEFACTORY_H
#define DATABASEFACTORY_H
#include <QSqlDatabase>
#include <QThreadStorage>
#include "utils.h"
class DatabaseFactory
{
private:
QString connectionString;
public:
DatabaseFactory(QString connectionString);
QSqlDatabase createNew();
QSqlDatabase forCurrentThread();
};
#endif // DATABASEFACTORY_H