Command: Take SqliteDbService instead of connection string

This commit is contained in:
Albert S. 2019-04-16 08:54:49 +02:00
bovenliggende 97710e2180
commit 4078cecad8
4 gewijzigde bestanden met toevoegingen van 20 en 66 verwijderingen

Bestand weergeven

@ -4,49 +4,5 @@
#include "command.h"
#include "qssgeneralexception.h"
bool Command::fileExistsInDatabase(QSqlDatabase &db, QString path, qint64 mtime)
{
auto query = QSqlQuery("SELECT 1 FROM file WHERE path = ? and mtime = ?", db);
query.addBindValue(path);
query.addBindValue(mtime);
if(!query.exec())
{ throw QSSGeneralException("Error while trying to query for file existance");
}
if(!query.next())
{
return false;
}
return query.value(0).toBool();
}
bool Command::fileExistsInDatabase(QSqlDatabase &db, QString path)
{
auto query = QSqlQuery("SELECT 1 FROM file WHERE path = ?", db);
query.addBindValue(path);
if(!query.exec())
{ throw QSSGeneralException("Error while trying to query for file existance");
}
if(!query.next())
{
return false;
}
return query.value(0).toBool();
}
QSqlDatabase Command::dbConnection()
{
if(dbStore.hasLocalData())
{
return dbStore.localData();
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "QSS" + QString::number((quint64)QThread::currentThread(), 16));
db.setDatabaseName(this->dbConnectionString);
if(!db.open())
{
Utils::error() << "Failed to open the database: " << this->dbConnectionString;
}
dbStore.setLocalData(db);
return db;
}

Bestand weergeven

@ -6,18 +6,17 @@
#include <QThreadStorage>
#include <QVariant>
#include "utils.h"
#include "sqlitedbservice.h"
class Command
{
protected:
bool fileExistsInDatabase(QSqlDatabase &db, QString path);
bool fileExistsInDatabase(QSqlDatabase &db, QString path, qint64 mtime);
QByteArray readFile(QString path) const;
SqliteDbService *dbService;
QString dbConnectionString;
QThreadStorage<QSqlDatabase> dbStore;
public:
Command(QString dbConnectionString)
Command(SqliteDbService &dbService)
{
this->dbConnectionString = dbConnectionString;
this->dbService = &dbService;
}
QSqlDatabase dbConnection();

Bestand weergeven

@ -2,18 +2,11 @@
#define COMMANDADD_H
#include <QMutex>
#include "command.h"
enum AddFileResult
{
OK,
SKIPPED,
DBFAIL
};
#include "filesaver.h"
class CommandAdd : public Command
{
private:
AddFileResult addFile(QString path);
QMutex writeMutex;
SaveFileResult addFile(QString path);
public:
using Command::Command;

Bestand weergeven

@ -19,24 +19,27 @@
#include "command.h"
#include "commandadd.h"
#include "commanddelete.h"
#include "commandupdate.h"
#include "databasefactory.h"
#include "logger.h"
void printUsage(QString argv0)
{
qInfo() << "Usage: " << argv0 << "command";
}
Command *commandFromName(QString name, QString connectionstring)
Command *commandFromName(QString name, SqliteDbService &dbService)
{
if(name == "add")
{
return new CommandAdd(connectionstring);
return new CommandAdd(dbService);
}
if(name == "delete")
{
return new CommandDelete(connectionstring);
return new CommandDelete(dbService);
}
if(name == "update")
{
return new CommandUpdate(dbService);
}
if(name == "search")
{
@ -58,7 +61,10 @@ int main(int argc, char *argv[])
}
QString commandName = args.first();
Command *cmd = commandFromName(commandName, QProcessEnvironment::systemEnvironment().value("QSS_PATH"));
QString connectionString = QProcessEnvironment::systemEnvironment().value("QSS_PATH");
DatabaseFactory dbFactory(connectionString);
SqliteDbService dbService(dbFactory);
Command *cmd = commandFromName(commandName, dbService);
if(cmd != nullptr)
{
try
@ -67,12 +73,12 @@ int main(int argc, char *argv[])
}
catch(const QSSGeneralException &e)
{
Utils::error() << "Exception caught, message: " << e.message << endl;
Logger::error() << "Exception caught, message: " << e.message << endl;
}
}
else
{
Utils::error() << "Unknown command " << commandName << endl;
Logger::error() << "Unknown command " << commandName << endl;
}
return 1;
}