From 0f47f581b388ef9463e6e9ff9de9e3260a40fa00 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 14 May 2023 14:21:22 +0200 Subject: [PATCH] shared: SqliteDbService: Add insertOutline(), Use DocumentProcessResult --- shared/sqlitedbservice.cpp | 35 ++++++++++++++++++++++++++++++++--- shared/sqlitedbservice.h | 5 +++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/shared/sqlitedbservice.cpp b/shared/sqlitedbservice.cpp index 930346c..940bc8c 100644 --- a/shared/sqlitedbservice.cpp +++ b/shared/sqlitedbservice.cpp @@ -253,6 +253,29 @@ bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid return true; } +bool SqliteDbService::insertOutline(QSqlDatabase &db, int fileid, const QVector &outlines) +{ + QSqlQuery outlineQuery(db); + outlineQuery.prepare("INSERT INTO outline(fileid, text, page) VALUES(?,?,?)"); + outlineQuery.addBindValue(fileid); + for(const DocumentOutlineEntry &outline : outlines) + { + outlineQuery.bindValue(1, outline.text.toLower()); + outlineQuery.bindValue(2, outline.destinationPage); + if(!outlineQuery.exec()) + { + Logger::error() << "Failed outline insertion " << outlineQuery.lastError() << Qt::endl; + return false; + } + if(!insertOutline(db, fileid, outline.children)) + { + Logger::error() << "Failed outline insertion (children)) " << outlineQuery.lastError() << Qt::endl; + return false; + } + } + return true; +} + QSqlQuery SqliteDbService::exec(QString querystr, std::initializer_list args) { auto query = QSqlQuery(dbFactory->forCurrentThread()); @@ -278,7 +301,7 @@ bool SqliteDbService::execBool(QString querystr, std::initializer_list return query.value(0).toBool(); } -SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector &pageData, bool pathsOnly) +SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, DocumentProcessResult &processResult, bool pathsOnly) { QString absPath = fileInfo.absoluteFilePath(); auto mtime = fileInfo.lastModified().toSecsSinceEpoch(); @@ -323,18 +346,24 @@ SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector & if(!pathsOnly) { int lastid = inserterQuery.lastInsertId().toInt(); - if(!insertToFTS(false, db, lastid, pageData)) + if(!insertToFTS(false, db, lastid, processResult.pages)) { db.rollback(); Logger::error() << "Failed to insert data to FTS index " << Qt::endl; return DBFAIL; } - if(!insertToFTS(true, db, lastid, pageData)) + if(!insertToFTS(true, db, lastid, processResult.pages)) { db.rollback(); Logger::error() << "Failed to insert data to FTS index " << Qt::endl; return DBFAIL; } + if(!insertOutline(db, lastid, processResult.outlines)) + { + db.rollback(); + Logger::error() << "Failed to insert outline data " << Qt::endl; + return DBFAIL; + } } if(!db.commit()) diff --git a/shared/sqlitedbservice.h b/shared/sqlitedbservice.h index 758818c..8f4dbed 100644 --- a/shared/sqlitedbservice.h +++ b/shared/sqlitedbservice.h @@ -5,7 +5,7 @@ #include "databasefactory.h" #include "utils.h" -#include "pagedata.h" +#include "documentprocessresult.h" #include "filedata.h" #include "../shared/sqlitesearch.h" #include "../shared/token.h" @@ -22,7 +22,7 @@ class SqliteDbService public: SqliteDbService(DatabaseFactory &dbFactory); - SaveFileResult saveFile(QFileInfo fileInfo, QVector &pageData, bool pathsOnly); + SaveFileResult saveFile(QFileInfo fileInfo, DocumentProcessResult &pageData, bool pathsOnly); bool deleteFile(QString path); bool fileExistsInDatabase(QString path); @@ -42,6 +42,7 @@ class SqliteDbService QVector search(const LooqsQuery &query); std::optional queryFileType(QString absPath); + bool insertOutline(QSqlDatabase &db, int fileid, const QVector &outlines); }; #endif // SQLITEDBSERVICE_H