From 4078cecad80aa2fbcaeaafdd7df8309198827bd4 Mon Sep 17 00:00:00 2001 From: Albert S Date: Tue, 16 Apr 2019 08:54:49 +0200 Subject: [PATCH] Command: Take SqliteDbService instead of connection string --- cli/command.cpp | 44 -------------------------------------------- cli/command.h | 11 +++++------ cli/commandadd.h | 11 ++--------- cli/main.cpp | 20 +++++++++++++------- 4 files changed, 20 insertions(+), 66 deletions(-) diff --git a/cli/command.cpp b/cli/command.cpp index 434f555..8d29f8e 100644 --- a/cli/command.cpp +++ b/cli/command.cpp @@ -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; -} - diff --git a/cli/command.h b/cli/command.h index 67a9c4a..cbcca31 100644 --- a/cli/command.h +++ b/cli/command.h @@ -6,18 +6,17 @@ #include #include #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 dbStore; public: - Command(QString dbConnectionString) + Command(SqliteDbService &dbService) { - this->dbConnectionString = dbConnectionString; + this->dbService = &dbService; } QSqlDatabase dbConnection(); diff --git a/cli/commandadd.h b/cli/commandadd.h index bdaa689..970fe1f 100644 --- a/cli/commandadd.h +++ b/cli/commandadd.h @@ -2,18 +2,11 @@ #define COMMANDADD_H #include #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; diff --git a/cli/main.cpp b/cli/main.cpp index d9b5619..08186a3 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -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; }