Akonadi

searchhelper.cpp
1 /***************************************************************************
2  * Copyright (C) 2006 by Tobias Koenig <[email protected]> *
3  * Copyright (C) 2014 by Daniel Vrátil <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Library General Public License as *
7  * published by the Free Software Foundation; either version 2 of the *
8  * License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Library General Public *
16  * License along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "searchhelper.h"
22 
23 #include "entities.h"
24 #include "storage/countquerybuilder.h"
25 
26 #include <private/protocol_p.h>
27 
28 using namespace Akonadi::Server;
29 
30 static qint64 parentCollectionId(qint64 collectionId)
31 {
32  QueryBuilder qb(Collection::tableName(), QueryBuilder::Select);
33  qb.addColumn(Collection::parentIdColumn());
34  qb.addValueCondition(Collection::idColumn(), Query::Equals, collectionId);
35  if (!qb.exec()) {
36  return -1;
37  }
38  if (!qb.query().next()) {
39  return -1;
40  }
41  const auto parentId = qb.query().value(0).toLongLong();
42  qb.query().finish();
43  return parentId;
44 }
45 
46 QVector<qint64> SearchHelper::matchSubcollectionsByMimeType(const QVector<qint64> &ancestors, const QStringList &mimeTypes)
47 {
48  // Get all collections with given mime types
49  QueryBuilder qb(Collection::tableName(), QueryBuilder::Select);
50  qb.setDistinct(true);
51  qb.addColumn(Collection::idFullColumnName());
52  qb.addColumn(Collection::parentIdFullColumnName());
53  qb.addJoin(QueryBuilder::LeftJoin, CollectionMimeTypeRelation::tableName(),
54  CollectionMimeTypeRelation::leftFullColumnName(), Collection::idFullColumnName());
56  CollectionMimeTypeRelation::rightFullColumnName(), MimeType::idFullColumnName());
57  Query::Condition cond(Query::Or);
58  for (const QString &mt : mimeTypes) {
59  cond.addValueCondition(MimeType::nameFullColumnName(), Query::Equals, mt);
60  }
61  if (!cond.isEmpty()) {
62  qb.addCondition(cond);
63  }
64 
65  if (!qb.exec()) {
66  qCWarning(AKONADISERVER_LOG) << "Failed to query search collections";
67  return QVector<qint64>();
68  }
69 
70  QMap<qint64 /* parentId */, QVector<qint64> /* collectionIds */> candidateCollections;
71  while (qb.query().next()) {
72  candidateCollections[qb.query().value(1).toLongLong()].append(qb.query().value(0).toLongLong());
73  }
74  qb.query().finish();
75 
76  // If the ancestors list contains root, then return what we got, since everything
77  // is sub collection of root
78  QVector<qint64> results;
79  if (ancestors.contains(0)) {
80  for (const QVector<qint64> &res : qAsConst(candidateCollections)) {
81  results += res;
82  }
83  return results;
84  }
85 
86  // Try to resolve direct descendants
87  Q_FOREACH (qint64 ancestor, ancestors) {
88  const QVector<qint64> cols = candidateCollections.take(ancestor);
89  if (!cols.isEmpty()) {
90  results += cols;
91  }
92  }
93 
94  for (auto iter = candidateCollections.begin(), iterEnd = candidateCollections.end() ; iter != iterEnd ; ++iter) {
95  // Traverse the collection chain up to root
96  qint64 parentId = iter.key();
97  while (!ancestors.contains(parentId) && parentId > 0) {
98  parentId = parentCollectionId(parentId);
99  }
100  // Ok, we found a requested ancestor in the parent chain
101  if (parentId > 0) {
102  results += iter.value();
103  }
104  }
105 
106  return results;
107 }
static QString tableName()
Returns the name of the SQL table.
Definition: entities.cpp:3325
void append(const T &value)
QVector::iterator begin()
T value(int i) const const
bool contains(const T &value) const const
NOTE: only supported for SELECT queries.
Definition: querybuilder.h:64
static QString tableName()
Returns the name of the SQL table.
Definition: entities.cpp:1640
bool isEmpty() const const
Represents a WHERE condition tree.
Definition: query.h:77
QVector::iterator end()
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.