shared: sqlitedbservice: Add setTags(),getTagsForPath(),getTags()
This commit is contained in:
parent
f324da0369
commit
cd400b2d71
@ -104,6 +104,91 @@ unsigned int SqliteDbService::getFiles(QVector<FileData> &results, QString wildC
|
||||
return processedRows;
|
||||
}
|
||||
|
||||
QVector<QString> SqliteDbService::getTags()
|
||||
{
|
||||
QVector<QString> result;
|
||||
auto query = QSqlQuery(dbFactory->forCurrentThread());
|
||||
query.prepare("SELECT name FROM tag ORDER by name ASC");
|
||||
query.setForwardOnly(true);
|
||||
if(!query.exec())
|
||||
{
|
||||
throw LooqsGeneralException("Error while trying to retrieve tags from database: " + query.lastError().text());
|
||||
}
|
||||
while(query.next())
|
||||
{
|
||||
QString tagname = query.value(0).toString();
|
||||
result.append(tagname);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QVector<QString> SqliteDbService::getTagsForPath(QString path)
|
||||
{
|
||||
QVector<QString> result;
|
||||
auto query = QSqlQuery(dbFactory->forCurrentThread());
|
||||
query.prepare("SELECT name FROM tag INNER JOIN filetag ON tag.id = filetag.tagid INNER JOIN file ON filetag.fileid "
|
||||
"= file.id WHERE file.path = ? ORDER BY name ASC");
|
||||
query.addBindValue(path);
|
||||
query.setForwardOnly(true);
|
||||
if(!query.exec())
|
||||
{
|
||||
throw LooqsGeneralException("Error while trying to retrieve tags from database: " + query.lastError().text());
|
||||
}
|
||||
while(query.next())
|
||||
{
|
||||
QString tagname = query.value(0).toString();
|
||||
result.append(tagname);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SqliteDbService::setTags(QString path, const QSet<QString> &tags)
|
||||
{
|
||||
QSqlDatabase db = dbFactory->forCurrentThread();
|
||||
if(!db.transaction())
|
||||
{
|
||||
Logger::error() << "Failed to open transaction for " << path << " : " << db.lastError() << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery deletionQuery = QSqlQuery(db);
|
||||
deletionQuery.prepare("DELETE FROM filetag WHERE fileid = (SELECT id FROM file WHERE path = ?)");
|
||||
deletionQuery.addBindValue(path);
|
||||
if(!deletionQuery.exec())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed to delete existing tags " << deletionQuery.lastError() << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for(const QString &tag : tags)
|
||||
{
|
||||
QSqlQuery tagQuery = QSqlQuery(db);
|
||||
tagQuery.prepare("INSERT OR IGNORE INTO tag (name) VALUES(?)");
|
||||
tagQuery.addBindValue(tag.toLower());
|
||||
if(!tagQuery.exec())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed to insert tag " << tagQuery.lastError() << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
QSqlQuery fileTagQuery(db);
|
||||
fileTagQuery.prepare(
|
||||
"INSERT INTO filetag(fileid, tagid) VALUES((SELECT id FROM file WHERE path = ?), (SELECT id "
|
||||
"FROM tag WHERE name = ?))");
|
||||
fileTagQuery.bindValue(0, path);
|
||||
fileTagQuery.bindValue(1, tag);
|
||||
if(!fileTagQuery.exec())
|
||||
{
|
||||
db.rollback();
|
||||
Logger::error() << "Failed to assign tag to file" << Qt::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
db.commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData)
|
||||
{
|
||||
QString ftsInsertStatement;
|
||||
@ -248,6 +333,8 @@ bool SqliteDbService::addTag(QString tag, const QVector<QString> &paths)
|
||||
QSqlQuery tagQuery(db);
|
||||
QSqlQuery fileTagQuery(db);
|
||||
|
||||
tag = tag.toLower();
|
||||
|
||||
tagQuery.prepare("INSERT OR IGNORE INTO tag (name) VALUES(?)");
|
||||
tagQuery.addBindValue(tag);
|
||||
|
||||
|
@ -23,13 +23,19 @@ class SqliteDbService
|
||||
public:
|
||||
SqliteDbService(DatabaseFactory &dbFactory);
|
||||
SaveFileResult saveFile(QFileInfo fileInfo, QVector<PageData> &pageData, bool pathsOnly);
|
||||
unsigned int getFiles(QVector<FileData> &results, QString wildCardPattern, int offset, int limit);
|
||||
|
||||
bool deleteFile(QString path);
|
||||
bool fileExistsInDatabase(QString path);
|
||||
bool fileExistsInDatabase(QString path, qint64 mtime);
|
||||
bool fileExistsInDatabase(QString path, qint64 mtime, QChar filetype);
|
||||
unsigned int getFiles(QVector<FileData> &results, QString wildCardPattern, int offset, int limit);
|
||||
|
||||
bool addTag(QString tag, QString path);
|
||||
bool addTag(QString tag, const QVector<QString> &paths);
|
||||
QVector<QString> getTags();
|
||||
QVector<QString> getTagsForPath(QString path);
|
||||
bool setTags(QString path, const QSet<QString> &tags);
|
||||
|
||||
QVector<SearchResult> search(const LooqsQuery &query);
|
||||
|
||||
std::optional<QChar> queryFileType(QString absPath);
|
||||
|
Loading…
Reference in New Issue
Block a user