Kgapi

searchquery.cpp
1 /*
2  * Copyright (C) 2014 Daniel Vrátil <[email protected]>
3  * Copyright (C) 2019 David Barchiesi <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3, or any
9  * later version accepted by the membership of KDE e.V. (or its
10  * successor approved by the membership of KDE e.V.), which shall
11  * act as a proxy defined in Section 6 of version 3 of the license.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "searchquery.h"
23 
24 #include <QString>
25 #include <QDateTime>
26 
27 using namespace KGAPI2;
28 using namespace KGAPI2::Drive;
29 
30 class Q_DECL_HIDDEN SearchQuery::Private : public QSharedData
31 {
32 public:
33  Private() = default;
34  Private(const Private &other) = default;
35  ~Private() = default;
36 
37  static QString compareOperatorToString(CompareOperator op);
38  static QString logicOperatorToString(LogicOperator op);
39 
40  QList<SearchQuery> subqueries;
41  QString field;
42  QString value;
43  CompareOperator compareOp;
44  LogicOperator logicOp;
45 };
46 
47 QString SearchQuery::Private::compareOperatorToString(CompareOperator op)
48 {
49  switch (op) {
50  case Contains:
51  return QStringLiteral(" contains ");
52  case Equals:
53  return QStringLiteral(" = ");
54  case NotEquals:
55  return QStringLiteral(" != ");
56  case Less:
57  return QStringLiteral(" < ");
58  case LessOrEqual:
59  return QStringLiteral(" <= ");
60  case Greater:
61  return QStringLiteral(" > ");
62  case GreaterOrEqual:
63  return QStringLiteral(" >= ");
64  case In:
65  return QStringLiteral(" in ");
66  case Has:
67  return QStringLiteral(" has ");
68  }
69 
70  Q_ASSERT(false);
71  return QString();
72 }
73 
74 QString SearchQuery::Private::logicOperatorToString(SearchQuery::LogicOperator op)
75 {
76  switch (op) {
77  case And:
78  return QStringLiteral(" and ");
79  case Or:
80  return QStringLiteral(" or ");
81  }
82 
83  Q_ASSERT(false);
84  return QString();
85 }
86 
87 SearchQuery::SearchQuery(SearchQuery::LogicOperator op)
88  : d(new Private)
89 {
90  d->logicOp = op;
91 }
92 
93 SearchQuery::SearchQuery(const SearchQuery &other) = default;
94 
95 SearchQuery::~SearchQuery() = default;
96 
97 SearchQuery &SearchQuery::operator=(const SearchQuery &other) = default;
98 
99 void SearchQuery::addQuery(const QString &field, SearchQuery::CompareOperator op, const QString &value)
100 {
102  query.d->field = field;
103  query.d->compareOp = op;
104  query.d->value = value;
105  d->subqueries.append(query);
106 }
107 
108 void SearchQuery::addQuery(const SearchQuery &query)
109 {
110  d->subqueries.append(query);
111 }
112 
113 bool SearchQuery::isEmpty() const
114 {
115  return d->value.isNull() && d->subqueries.isEmpty();
116 }
117 
118 QString SearchQuery::serialize() const
119 {
120  if (isEmpty()) {
121  return QString();
122  }
123 
124  QString r;
125  r = QLatin1Char('(');
126  if (d->subqueries.isEmpty()) {
127  if (d->compareOp == In) {
128  r += QStringLiteral("%1 in %2").arg(d->value, d->field);
129  } else {
130  r += d->field % Private::compareOperatorToString(d->compareOp) % d->value;
131  }
132  } else {
134  for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) {
135  if (iter != d->subqueries.constBegin()) {
136  r += Private::logicOperatorToString(d->logicOp);
137  }
138  r += (*iter).serialize();
139  }
140  }
141  r += QLatin1Char(')');
142 
143  return r;
144 }
145 
146 
std::optional< QSqlQuery > query(const QString &queryStatement)
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:30
const QList< QKeySequence > & end()
SearchQuery class should be used as a base class for building file/team search queries.
Definition: searchquery.h:38
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:36:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.