Command: Take SqliteDbService instead of connection string

This commit is contained in:
Albert S. 2019-04-16 08:54:49 +02:00
parent 97710e2180
commit 4078cecad8
4 changed files with 20 additions and 66 deletions

View File

@ -4,49 +4,5 @@
#include "command.h" #include "command.h"
#include "qssgeneralexception.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;
}

View File

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

View File

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

View File

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