Akonadi

query.h
1/*
2 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QList>
10#include <QString>
11#include <QVariant>
12
13namespace Akonadi
14{
15namespace Server
16{
17class QueryBuilder;
18
19/**
20 Building blocks for SQL queries.
21 @see QueryBuilder
22*/
23namespace Query
24{
25/**
26 Compare operators to be used in query conditions.
27*/
29 Equals,
30 NotEquals,
31 Is,
32 IsNot,
33 Less,
34 LessOrEqual,
35 Greater,
36 GreaterOrEqual,
37 In,
38 NotIn,
39 Like,
40};
41
42/**
43 Logic operations used to combine multiple query conditions.
44*/
46 And,
47 Or,
48};
49
50/**
51 Sort orders.
52*/
54 Ascending,
55 Descending,
56};
57
58/**
59 Represents a WHERE condition tree.
60*/
62{
64
65public:
66 /** A list of conditions. */
68
69 /**
70 Create an empty condition.
71 @param op how to combine sub queries.
72 */
73 explicit Condition(LogicOperator op = And);
74
75 /**
76 Add a WHERE condition which compares a column with a given value.
77 @param column The column that should be compared.
78 @param op The operator used for comparison
79 @param value The value @p column is compared to.
80 */
81 void addValueCondition(const QString &column, CompareOperator op, const QVariant &value);
82 /**
83 * Add a WHERE condition which compares a column with a given value.
84 *
85 * This is an overload specially for passing a list of IDs, which is a fairly common case in Akonadi.
86 *
87 * @param column The column that should be compared.
88 * @param op The operator used for comparison.
89 * @param value The value @p column is compared to.
90 */
91 void addValueCondition(const QString &column, CompareOperator op, const QList<qint64> &value)
92 {
93 addValueCondition(column, op, QVariant::fromValue(value));
94 }
95
96 /**
97 * Add a WHERE condition which compares a column with a given value.
98 *
99 * This is an overload specially for passing a list of IDs, which is a fairly common case in Akonadi.
100 *
101 * @param column The column that should be compared.
102 * @param op The operator used for comparison.
103 * @param value The value @p column is compared to.
104 */
105 void addValueCondition(const QString &column, CompareOperator op, const QSet<qint64> &value)
106 {
107 addValueCondition(column, op, QVariant::fromValue(value));
108 }
109
110 /**
111 Add a WHERE condition which compares a column with another column.
112 @param column The column that should be compared.
113 @param op The operator used for comparison.
114 @param column2 The column @p column is compared to.
115 */
116 void addColumnCondition(const QString &column, CompareOperator op, const QString &column2);
117
118 /**
119 Add a WHERE condition. Use this method to build hierarchical conditions.
120 */
121 void addCondition(const Condition &condition);
122
123 /**
124 Set how sub-conditions should be combined, default is And.
125 */
127
128 /**
129 Returns if there are sub conditions.
130 */
131 bool isEmpty() const;
132
133 /**
134 Returns the list of sub-conditions.
135 */
137
138private:
139 Condition::List mSubConditions;
140 QString mColumn;
141 QString mComparedColumn;
142 QVariant mComparedValue;
143 CompareOperator mCompareOp;
144 LogicOperator mCombineOp;
145
146}; // class Condition
147
148class Case
149{
151
152public:
153 Case(const Condition &when, const QString &then, const QString &elseBranch = QString());
154 Case(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &when, const QString &elseBranch = QString());
155
156 void addCondition(const Condition &when, const QString &then);
157 void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &then);
158 void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, const QString &then);
159
160 void setElse(const QString &elseBranch);
161
162private:
164 QString mElse;
165};
166
167} // namespace Query
168} // namespace Server
169} // namespace Akonadi
170
171Q_DECLARE_TYPEINFO(Akonadi::Server::Query::Condition, Q_RELOCATABLE_TYPE);
Helper class to construct arbitrary SQL queries.
Represents a WHERE condition tree.
Definition query.h:62
void addValueCondition(const QString &column, CompareOperator op, const QList< qint64 > &value)
Add a WHERE condition which compares a column with a given value.
Definition query.h:91
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
Condition::List subConditions() const
Returns the list of sub-conditions.
Definition query.cpp:44
void setSubQueryMode(LogicOperator op)
Set how sub-conditions should be combined, default is And.
Definition query.cpp:49
Condition(LogicOperator op=And)
Create an empty condition.
Definition query.cpp:33
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 isEmpty() const
Returns if there are sub conditions.
Definition query.cpp:39
void addValueCondition(const QString &column, CompareOperator op, const QSet< qint64 > &value)
Add a WHERE condition which compares a column with a given value.
Definition query.h:105
void addCondition(const Condition &condition)
Add a WHERE condition.
Definition query.cpp:54
CompareOperator
Compare operators to be used in query conditions.
Definition query.h:28
SortOrder
Sort orders.
Definition query.h:53
LogicOperator
Logic operations used to combine multiple query conditions.
Definition query.h:45
Helper integration between Akonadi and Qt.
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:01:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.