shared: sqlitedbservice: Insert to trigram index too

This commit is contained in:
Albert S. 2022-10-18 16:05:00 +02:00
parent 45659cdc59
commit 583d5babf3
2 changed files with 49 additions and 16 deletions

View File

@ -110,6 +110,44 @@ unsigned int SqliteDbService::getFiles(QVector<FileData> &results, QString wildC
return processedRows; return processedRows;
} }
bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData)
{
QString ftsInsertStatement;
QString contentInsertStatement;
if(useTrigrams)
{
ftsInsertStatement = "INSERT INTO fts_trigram(content) VALUES(?)";
contentInsertStatement = "INSERT INTO content(fileid, page, fts_trigramid) VALUES(?, ?, last_insert_rowid())";
}
else
{
ftsInsertStatement = "INSERT INTO fts(content) VALUES(?)";
contentInsertStatement = "INSERT INTO content(fileid, page, ftsid) VALUES(?, ?, last_insert_rowid())";
}
for(const PageData &data : pageData)
{
QSqlQuery ftsQuery(db);
ftsQuery.prepare(ftsInsertStatement);
ftsQuery.addBindValue(data.content);
if(!ftsQuery.exec())
{
Logger::error() << "Failed fts insertion " << ftsQuery.lastError() << Qt::endl;
return false;
}
QSqlQuery contentQuery(db);
contentQuery.prepare(contentInsertStatement);
contentQuery.addBindValue(fileid);
contentQuery.addBindValue(data.pagenumber);
if(!contentQuery.exec())
{
Logger::error() << "Failed content insertion " << contentQuery.lastError() << Qt::endl;
return false;
}
}
return true;
}
SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData) SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData)
{ {
QString absPath = fileInfo.absoluteFilePath(); QString absPath = fileInfo.absoluteFilePath();
@ -149,24 +187,18 @@ SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &
} }
int lastid = inserterQuery.lastInsertId().toInt(); int lastid = inserterQuery.lastInsertId().toInt();
for(const PageData &data : pageData) if(!insertToFTS(false, db, lastid, pageData))
{ {
QSqlQuery ftsQuery(db); db.rollback();
ftsQuery.prepare("INSERT INTO fts(content) VALUES(?)"); Logger::error() << "Failed to insert data to FTS index " << Qt::endl;
ftsQuery.addBindValue(data.content); return DBFAIL;
ftsQuery.exec(); }
QSqlQuery contentQuery(db); if(!insertToFTS(true, db, lastid, pageData))
contentQuery.prepare("INSERT INTO content(fileid, page, ftsid) VALUES(?, ?, last_insert_rowid())"); {
contentQuery.addBindValue(lastid); db.rollback();
contentQuery.addBindValue(data.pagenumber); Logger::error() << "Failed to insert data to FTS index " << Qt::endl;
if(!contentQuery.exec()) return DBFAIL;
{
db.rollback();
Logger::error() << "Failed content insertion " << contentQuery.lastError() << Qt::endl;
return DBFAIL;
}
} }
if(!db.commit()) if(!db.commit())
{ {
db.rollback(); db.rollback();

View File

@ -13,6 +13,7 @@ class SqliteDbService
{ {
private: private:
DatabaseFactory *dbFactory = nullptr; DatabaseFactory *dbFactory = nullptr;
bool insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData);
public: public:
SqliteDbService(DatabaseFactory &dbFactory); SqliteDbService(DatabaseFactory &dbFactory);