Akonadi

relationfetchhandler.cpp
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]> *
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 
12 #include <private/imapset_p.h>
13 
14 using namespace Akonadi;
15 using namespace Akonadi::Server;
16 
17 RelationFetchHandler::RelationFetchHandler(AkonadiServer &akonadi)
18  : Handler(akonadi)
19 {
20 }
21 
22 bool RelationFetchHandler::parseStream()
23 {
24  const auto &cmd = Protocol::cmdCast<Protocol::FetchRelationsCommand>(m_command);
25 
26  SelectQueryBuilder<Relation> relationQuery;
27  if (cmd.side() > 0) {
29  c.setSubQueryMode(Query::Or);
30  c.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.side());
31  c.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.side());
32  relationQuery.addCondition(c);
33  } else {
34  if (cmd.left() > 0) {
35  relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
36  }
37  if (cmd.right() > 0) {
38  relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
39  }
40  }
41 
42  const auto cmdTypes = cmd.types();
43  if (!cmdTypes.isEmpty()) {
44  relationQuery.addJoin(QueryBuilder::InnerJoin, RelationType::tableName(), Relation::typeIdFullColumnName(), RelationType::idFullColumnName());
45  QStringList types;
46  types.reserve(cmdTypes.size());
47  for (const QByteArray &type : cmdTypes) {
48  types << QString::fromUtf8(type);
49  }
50  relationQuery.addValueCondition(RelationType::nameFullColumnName(), Query::In, types);
51  }
52  if (!cmd.resource().isEmpty()) {
53  Resource res = Resource::retrieveByName(cmd.resource());
54  if (!res.isValid()) {
55  return failureResponse("Invalid resource");
56  }
57  Query::Condition condition;
58  condition.setSubQueryMode(Query::Or);
59  condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::leftIdFullColumnName());
60  condition.addColumnCondition(PimItem::idFullColumnName(), Query::Equals, Relation::rightIdFullColumnName());
61  relationQuery.addJoin(QueryBuilder::InnerJoin, PimItem::tableName(), condition);
62 
63  relationQuery.addJoin(QueryBuilder::InnerJoin, Collection::tableName(), PimItem::collectionIdFullColumnName(), Collection::idFullColumnName());
64 
65  relationQuery.addValueCondition(Collection::resourceIdFullColumnName(), Query::Equals, res.id());
66  relationQuery.addGroupColumns(QStringList() << Relation::leftIdFullColumnName() << Relation::rightIdFullColumnName()
67  << Relation::typeIdFullColumnName());
68  }
69 
70  if (!relationQuery.exec()) {
71  return failureResponse("Failed to query for existing relation");
72  }
73  const Relation::List existingRelations = relationQuery.result();
74  for (const Relation &relation : existingRelations) {
75  sendResponse(HandlerHelper::fetchRelationsResponse(relation));
76  }
77 
78  return successResponse<Protocol::FetchRelationsResponse>();
79 }
QString fromUtf8(const char *str, int size)
An Akonadi Relation.
Definition: relation.h:39
void addJoin(JoinType joinType, const QString &table, const Query::Condition &condition)
Join a table to the query.
void reserve(int alloc)
QVector< T > result()
Returns the result of this SELECT query.
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
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 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.
bool exec()
Executes the query, returns true on success.
Helper class for creating and executing database SELECT queries.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
void addGroupColumns(const QStringList &columns)
Add list of columns to GROUP BY.
Represents a WHERE condition tree.
Definition: query.h:61
void addCondition(const Query::Condition &condition, ConditionType type=WhereCondition)
Add a WHERE condition.
void setSubQueryMode(LogicOperator op)
Set how sub-conditions should be combined, default is And.
Definition: query.cpp:49
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.