Kgapi

searchquery.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
3 * SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "searchquery.h"
9
10#include <QDateTime>
11#include <QString>
12
13using namespace KGAPI2;
14using namespace KGAPI2::Drive;
15
16class Q_DECL_HIDDEN SearchQuery::Private : public QSharedData
17{
18public:
19 Private() = default;
20 Private(const Private &other) = default;
21 ~Private() = default;
22
23 static QString compareOperatorToString(CompareOperator op);
24 static QString logicOperatorToString(LogicOperator op);
25
26 QList<SearchQuery> subqueries;
27 QString field;
28 QString value;
29 CompareOperator compareOp;
30 LogicOperator logicOp;
31};
32
33QString SearchQuery::Private::compareOperatorToString(CompareOperator op)
34{
35 switch (op) {
36 case Contains:
37 return QStringLiteral(" contains ");
38 case Equals:
39 return QStringLiteral(" = ");
40 case NotEquals:
41 return QStringLiteral(" != ");
42 case Less:
43 return QStringLiteral(" < ");
44 case LessOrEqual:
45 return QStringLiteral(" <= ");
46 case Greater:
47 return QStringLiteral(" > ");
48 case GreaterOrEqual:
49 return QStringLiteral(" >= ");
50 case In:
51 return QStringLiteral(" in ");
52 case Has:
53 return QStringLiteral(" has ");
54 }
55
56 Q_ASSERT(false);
57 return QString();
58}
59
60QString SearchQuery::Private::logicOperatorToString(SearchQuery::LogicOperator op)
61{
62 switch (op) {
63 case And:
64 return QStringLiteral(" and ");
65 case Or:
66 return QStringLiteral(" or ");
67 }
68
69 Q_ASSERT(false);
70 return QString();
71}
72
73SearchQuery::SearchQuery(SearchQuery::LogicOperator op)
74 : d(new Private)
75{
76 d->logicOp = op;
77}
78
79SearchQuery::SearchQuery(const SearchQuery &other) = default;
80
81SearchQuery::~SearchQuery() = default;
82
83SearchQuery &SearchQuery::operator=(const SearchQuery &other) = default;
84
85void SearchQuery::addQuery(const QString &field, SearchQuery::CompareOperator op, const QString &value)
86{
88 query.d->field = field;
89 query.d->compareOp = op;
90 query.d->value = value;
91 d->subqueries.append(query);
92}
93
94void SearchQuery::addQuery(const SearchQuery &query)
95{
96 d->subqueries.append(query);
97}
98
99bool SearchQuery::isEmpty() const
100{
101 return d->value.isNull() && d->subqueries.isEmpty();
102}
103
104QString SearchQuery::serialize() const
105{
106 if (isEmpty()) {
107 return QString();
108 }
109
110 QString r;
111 r = QLatin1Char('(');
112 if (d->subqueries.isEmpty()) {
113 if (d->compareOp == In) {
114 r += QStringLiteral("%1 in %2").arg(d->value, d->field);
115 } else {
116 r += d->field % Private::compareOperatorToString(d->compareOp) % d->value;
117 }
118 } else {
121 for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) {
122 if (iter != d->subqueries.constBegin()) {
123 r += Private::logicOperatorToString(d->logicOp);
124 }
125 r += (*iter).serialize();
126 }
127 }
128 r += QLatin1Char(')');
129
130 return r;
131}
SearchQuery class should be used as a base class for building file/team search queries.
Definition searchquery.h:24
std::optional< QSqlQuery > query(const QString &queryStatement)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
const QList< QKeySequence > & end()
QString arg(Args &&... args) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.