Command: Take SqliteDbService instead of connection string

Esse commit está contido em:
Albert S. 2019-04-16 08:54:49 +02:00
commit f24f6bd966
4 arquivos alterados com 19 adições e 69 exclusões

Ver arquivo

@ -3,51 +3,3 @@
#include <QDebug>
#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;
}

Ver arquivo

@ -6,19 +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();

Ver arquivo

@ -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;

Ver arquivo

@ -19,23 +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")
{
@ -56,7 +60,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
@ -65,12 +72,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;
}