Akonadi

relationfetchhandler.cpp
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6
7#include "relationfetchhandler.h"
8#include "connection.h"
9#include "handlerhelper.h"
10#include "storage/selectquerybuilder.h"
11
12using namespace Akonadi;
13using namespace Akonadi::Server;
14
15RelationFetchHandler::RelationFetchHandler(AkonadiServer &akonadi)
16 : Handler(akonadi)
17{
18}
19
21{
22 const auto &cmd = Protocol::cmdCast<Protocol::FetchRelationsCommand>(m_command);
23
24 SelectQueryBuilder<Relation> relationQuery;
25 if (cmd.side() > 0) {
27 c.setSubQueryMode(Query::Or);
28 c.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.side());
29 c.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.side());
30 relationQuery.addCondition(c);
31 } else {
32 if (cmd.left() > 0) {
33 relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
34 }
35 if (cmd.right() > 0) {
36 relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
37 }
38 }
39
40 const auto cmdTypes = cmd.types();
41 if (!cmdTypes.isEmpty()) {
42 relationQuery.addJoin(QueryBuilder::InnerJoin, RelationType::tableName(), Relation::typeIdFullColumnName(), RelationType::idFullColumnName());
43 QStringList types;
44 types.reserve(cmdTypes.size());
45 for (const QByteArray &type : cmdTypes) {
46 types << QString::fromUtf8(type);
47 }
48 relationQuery.addValueCondition(RelationType::nameFullColumnName(), Query::In, types);
49 }
50 if (!cmd.resource().isEmpty()) {
51 Resource res = Resource::retrieveByName(cmd.resource());
52 if (!res.isValid()) {
53 return failureResponse("Invalid resource");
54 }
55 Query::Condition condition;
56 condition.setSubQueryMode(Query::Or);
57 condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::leftIdFullColumnName());
58 condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::rightIdFullColumnName());
59 relationQuery.addJoin(QueryBuilder::InnerJoin, PimItem::tableName(), condition);
60
61 relationQuery.addJoin(QueryBuilder::InnerJoin, Collection::tableName(), PimItem::collectionIdFullColumnName(), Collection::idFullColumnName());
62
63 relationQuery.addValueCondition(Collection::resourceIdFullColumnName(), Query::Equals, res.id());
64 relationQuery.addGroupColumns(QStringList() << Relation::leftIdFullColumnName() << Relation::rightIdFullColumnName()
65 << Relation::typeIdFullColumnName());
66 }
67
68 if (!relationQuery.exec()) {
69 return failureResponse("Failed to query for existing relation");
70 }
71 const Relation::List existingRelations = relationQuery.result();
72 for (const Relation &relation : existingRelations) {
73 sendResponse(HandlerHelper::fetchRelationsResponse(relation));
74 }
75
76 return successResponse<Protocol::FetchRelationsResponse>();
77}
An Akonadi Relation.
Definition relation.h:41
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition handler.h:32
void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type=WhereCondition)
Add a WHERE or HAVING condition which compares a column with a given value.
void addJoin(JoinType joinType, const QString &table, const Query::Condition &condition)
Join a table to the query.
bool exec()
Executes the query, returns true on success.
void addCondition(const Query::Condition &condition, ConditionType type=WhereCondition)
Add a WHERE condition.
void addGroupColumns(const QStringList &columns)
Add list of columns to GROUP BY.
@ InnerJoin
NOTE: only supported for UPDATE and SELECT queries.
Represents a WHERE condition tree.
Definition query.h:62
void addColumnCondition(const QString &column, CompareOperator op, const QString &column2)
Add a WHERE condition which compares a column with another column.
Definition query.cpp:22
void setSubQueryMode(LogicOperator op)
Set how sub-conditions should be combined, default is And.
Definition query.cpp:49
void addValueCondition(const QString &column, CompareOperator op, const QVariant &value)
Add a WHERE condition which compares a column with a given value.
Definition query.cpp:12
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
Helper class for creating and executing database SELECT queries.
QList< T > result()
Returns the result of this SELECT query.
Helper integration between Akonadi and Qt.
void reserve(qsizetype size)
T qobject_cast(QObject *object)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:56:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.