shared: sqlitedbservice: Begin addTag()
This commit is contained in:
szülő
a0b95479e2
commit
4fe745e858
@ -2,6 +2,7 @@
|
||||
#include <QFileInfo>
|
||||
#include <QDateTime>
|
||||
#include <QSqlError>
|
||||
#include "looqsgeneralexception.h"
|
||||
#include "sqlitedbservice.h"
|
||||
#include "filedata.h"
|
||||
#include "logger.h"
|
||||
@ -233,3 +234,56 @@ SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool SqliteDbService::addTag(QString tag, QString path)
|
||||
{
|
||||
QVector<QString> paths;
|
||||
paths.append(path);
|
||||
return addTag(tag, paths);
|
||||
}
|
||||
|
||||
bool SqliteDbService::addTag(QString tag, const QVector<QString> &paths)
|
||||
{
|
||||
QSqlDatabase db = dbFactory->forCurrentThread();
|
||||
QSqlQuery tagQuery(db);
|
||||
QSqlQuery fileTagQuery(db);
|
||||
|
||||
tagQuery.prepare("INSERT OR IGNORE INTO tag (name) VALUES(?)");
|
||||
tagQuery.addBindValue(tag);
|
||||
|
||||
fileTagQuery.prepare("INSERT INTO filetag(fileid, tagid) VALUES((SELECT id FROM file WHERE path = ?), (SELECT id "
|
||||
"FROM tag WHERE name = ?))");
|
||||
fileTagQuery.bindValue(1, tag);
|
||||
if(!db.transaction())
|
||||
{
|
||||
Logger::error() << "Failed to open transaction to add paths for tag " << tag << " : " << db.lastError()
|
||||
<< Qt::endl;
|
||||
return false;
|
||||
}
|
||||
if(!tagQuery.exec())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed INSERT query" << tagQuery.lastError() << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for(const QString &path : paths)
|
||||
{
|
||||
fileTagQuery.bindValue(0, path);
|
||||
if(!fileTagQuery.exec())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed to add paths to tag" << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!db.commit())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed to commit tag insertion transaction" << db.lastError() << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ class SqliteDbService
|
||||
bool fileExistsInDatabase(QString path);
|
||||
bool fileExistsInDatabase(QString path, qint64 mtime);
|
||||
bool fileExistsInDatabase(QString path, qint64 mtime, QChar filetype);
|
||||
bool addTag(QString tag, QString path);
|
||||
bool addTag(QString tag, const QVector<QString> &paths);
|
||||
QVector<SearchResult> search(const LooqsQuery &query);
|
||||
|
||||
std::optional<QChar> queryFileType(QString absPath);
|
||||
|
Betöltés…
Reference in New Issue
Block a user