File indexing completed on 2025-01-05 04:47:01

0001 /*
0002     SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "tagqueryhelper.h"
0009 
0010 #include "commandcontext.h"
0011 #include "handler.h"
0012 #include "storage/querybuilder.h"
0013 #include "storage/queryhelper.h"
0014 
0015 #include "private/imapset_p.h"
0016 #include "private/scope_p.h"
0017 
0018 using namespace Akonadi;
0019 using namespace Akonadi::Server;
0020 
0021 void TagQueryHelper::remoteIdToQuery(const QStringList &rids, const CommandContext &context, QueryBuilder &qb)
0022 {
0023     qb.addJoin(QueryBuilder::InnerJoin, TagRemoteIdResourceRelation::tableName(), Tag::idFullColumnName(), TagRemoteIdResourceRelation::tagIdFullColumnName());
0024     qb.addValueCondition(TagRemoteIdResourceRelation::resourceIdFullColumnName(), Query::Equals, context.resource().id());
0025 
0026     if (rids.size() == 1) {
0027         qb.addValueCondition(TagRemoteIdResourceRelation::remoteIdFullColumnName(), Query::Equals, rids.first());
0028     } else {
0029         qb.addValueCondition(TagRemoteIdResourceRelation::remoteIdFullColumnName(), Query::In, rids);
0030     }
0031 }
0032 
0033 void TagQueryHelper::gidToQuery(const QStringList &gids, const CommandContext &context, QueryBuilder &qb)
0034 {
0035     if (context.resource().isValid()) {
0036         qb.addJoin(QueryBuilder::InnerJoin,
0037                    TagRemoteIdResourceRelation::tableName(),
0038                    Tag::idFullColumnName(),
0039                    TagRemoteIdResourceRelation::tagIdFullColumnName());
0040         qb.addValueCondition(TagRemoteIdResourceRelation::resourceIdFullColumnName(), Query::Equals, context.resource().id());
0041     }
0042 
0043     if (gids.size() == 1) {
0044         qb.addValueCondition(Tag::gidFullColumnName(), Query::Equals, gids.first());
0045     } else {
0046         qb.addValueCondition(Tag::gidFullColumnName(), Query::In, gids);
0047     }
0048 }
0049 
0050 void TagQueryHelper::scopeToQuery(const Scope &scope, const CommandContext &context, QueryBuilder &qb)
0051 {
0052     if (scope.scope() == Scope::Uid) {
0053         QueryHelper::setToQuery(scope.uidSet(), Tag::idFullColumnName(), qb);
0054         return;
0055     }
0056 
0057     if (scope.scope() == Scope::Gid) {
0058         TagQueryHelper::gidToQuery(scope.gidSet(), context, qb);
0059         return;
0060     }
0061 
0062     if (scope.scope() == Scope::Rid) {
0063         if (!context.resource().isValid()) {
0064             throw HandlerException("Operations based on remote identifiers require a resource or collection context");
0065         }
0066 
0067         if (scope.scope() == Scope::Rid) {
0068             TagQueryHelper::remoteIdToQuery(scope.ridSet(), context, qb);
0069         }
0070     }
0071 
0072     throw HandlerException("HRID tag operations are not permitted");
0073 }