looqs/shared/databasefactory.cpp

55 行
1.7 KiB
C++

#include <QThread>
2024-05-23 18:08:09 +02:00
#include <QSqlQuery>
#include "databasefactory.h"
#include "logger.h"
2022-02-27 23:13:02 +01:00
#include "looqsgeneralexception.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", "LOOQS" + QString::number(counter++));
db.setDatabaseName(this->connectionString);
if(!db.open())
{
Logger::error() << "Failed to open the database: " << this->connectionString << Qt::endl;
2021-06-12 14:59:58 +02:00
throw LooqsGeneralException("Failed to create open new connection");
}
return db;
}
QSqlDatabase DatabaseFactory::forCurrentThread()
{
if(dbStore.hasLocalData())
{
return dbStore.localData();
}
QSqlDatabase db =
QSqlDatabase::addDatabase("QSQLITE", "LOOQS" + QString::number((quint64)QThread::currentThread(), 16));
db.setDatabaseName(this->connectionString);
2024-05-23 18:08:09 +02:00
db.setConnectOptions("QSQLITE_BUSY_TIMEOUT=30000");
if(!db.open())
{
Logger::error() << "Failed to open the database: " << this->connectionString << Qt::endl;
2021-06-12 14:59:58 +02:00
throw LooqsGeneralException("Failed to create open new connection");
}
2024-05-23 18:08:09 +02:00
QSqlQuery q(db);
if(!q.exec("PRAGMA journal_mode=WAL;"))
{
Logger::error() << "Failed to set WAL mode: " << this->connectionString << Qt::endl;
throw LooqsGeneralException("Failed to set WAL mode on sqlite database");
}
if(!q.exec("PRAGMA wal_autocheckpoint=250;"))
{
Logger::error() << "Failed to set WAL autocheckpoint: " << this->connectionString << Qt::endl;
throw LooqsGeneralException("Failed to set WAL autocheckpoint on sqlite database");
}
dbStore.setLocalData(db);
return db;
}