diff --git a/shared/sqlitedbservice.cpp b/shared/sqlitedbservice.cpp index f4075bb..ede4e26 100644 --- a/shared/sqlitedbservice.cpp +++ b/shared/sqlitedbservice.cpp @@ -5,22 +5,6 @@ #include "sqlitedbservice.h" #include "filedata.h" #include "logger.h" -bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime) -{ - auto query = QSqlQuery(dbFactory->forCurrentThread()); - query.prepare("SELECT 1 FROM file WHERE path = ? and mtime = ?"); - query.addBindValue(path); - query.addBindValue(mtime); - if(!query.exec()) - { - throw LooqsGeneralException("Error while trying to query for file existance: " + query.lastError().text()); - } - if(!query.next()) - { - return false; - } - return query.value(0).toBool(); -} QVector SqliteDbService::search(const LooqsQuery &query) { @@ -31,13 +15,7 @@ QVector SqliteDbService::search(const LooqsQuery &query) std::optional SqliteDbService::queryFileType(QString absPath) { - auto query = QSqlQuery(dbFactory->forCurrentThread()); - query.prepare("SELECT filetype FROM file WHERE path = ?"); - query.addBindValue(absPath); - if(!query.exec()) - { - throw LooqsGeneralException("Error while trying to query for file type: " + query.lastError().text()); - } + auto query = exec("SELECT filetype FROM file WHERE path = ?", {absPath}); if(!query.next()) { return {}; @@ -47,18 +25,17 @@ std::optional SqliteDbService::queryFileType(QString absPath) bool SqliteDbService::fileExistsInDatabase(QString path) { - auto query = QSqlQuery(dbFactory->forCurrentThread()); - query.prepare("SELECT 1 FROM file WHERE path = ?"); - query.addBindValue(path); - if(!query.exec()) - { - throw LooqsGeneralException("Error while trying to query for file existance: " + query.lastError().text()); - } - if(!query.next()) - { - return false; - } - return query.value(0).toBool(); + return execBool("SELECT 1 FROM file WHERE path = ?", {path}); +} + +bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime) +{ + return execBool("SELECT 1 FROM file WHERE path = ? AND mtime = ?", {path, mtime}); +} + +bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime, QChar fileType) +{ + return execBool("SELECT 1 FROM file WHERE path = ? AND mtime = ? AND filetype = ?", {path, mtime, fileType}); } SqliteDbService::SqliteDbService(DatabaseFactory &dbFactory) @@ -164,6 +141,31 @@ bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid return true; } +QSqlQuery SqliteDbService::exec(QString querystr, std::initializer_list args) +{ + auto query = QSqlQuery(dbFactory->forCurrentThread()); + query.prepare(querystr); + for(const QVariant &v : args) + { + query.addBindValue(v); + } + if(!query.exec()) + { + throw LooqsGeneralException("Error while exec(): " + query.lastError().text() + " for query: " + querystr); + } + return query; +} + +bool SqliteDbService::execBool(QString querystr, std::initializer_list args) +{ + auto query = exec(querystr, args); + if(!query.next()) + { + return false; + } + return query.value(0).toBool(); +} + SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector &pageData, bool pathsOnly) { QString absPath = fileInfo.absoluteFilePath(); diff --git a/shared/sqlitedbservice.h b/shared/sqlitedbservice.h index 8682166..09509f6 100644 --- a/shared/sqlitedbservice.h +++ b/shared/sqlitedbservice.h @@ -17,6 +17,9 @@ class SqliteDbService DatabaseFactory *dbFactory = nullptr; bool insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector &pageData); + QSqlQuery exec(QString query, std::initializer_list args); + bool execBool(QString querystr, std::initializer_list args); + public: SqliteDbService(DatabaseFactory &dbFactory); SaveFileResult saveFile(QFileInfo fileInfo, QVector &pageData, bool pathsOnly); @@ -24,6 +27,7 @@ class SqliteDbService bool deleteFile(QString path); bool fileExistsInDatabase(QString path); bool fileExistsInDatabase(QString path, qint64 mtime); + bool fileExistsInDatabase(QString path, qint64 mtime, QChar filetype); QVector search(const LooqsQuery &query); std::optional queryFileType(QString absPath);