Akonadi

query.h
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QString>
10 #include <QVariant>
11 #include <QVector>
12 
13 namespace Akonadi
14 {
15 namespace Server
16 {
17 class QueryBuilder;
18 
19 /**
20  Building blocks for SQL queries.
21  @see QueryBuilder
22 */
23 namespace 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 */
53 enum SortOrder {
54  Ascending,
55  Descending,
56 };
57 
58 /**
59  Represents a WHERE condition tree.
60 */
61 class Condition
62 {
63  friend class Akonadi::Server::QueryBuilder;
64 
65 public:
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  /**
84  Add a WHERE condition which compares a column with another column.
85  @param column The column that should be compared.
86  @param op The operator used for comparison.
87  @param column2 The column @p column is compared to.
88  */
89  void addColumnCondition(const QString &column, CompareOperator op, const QString &column2);
90 
91  /**
92  Add a WHERE condition. Use this method to build hierarchical conditions.
93  */
94  void addCondition(const Condition &condition);
95 
96  /**
97  Set how sub-conditions should be combined, default is And.
98  */
100 
101  /**
102  Returns if there are sub conditions.
103  */
104  bool isEmpty() const;
105 
106  /**
107  Returns the list of sub-conditions.
108  */
110 
111 private:
112  Condition::List mSubConditions;
113  QString mColumn;
114  QString mComparedColumn;
115  QVariant mComparedValue;
116  CompareOperator mCompareOp;
117  LogicOperator mCombineOp;
118 
119 }; // class Condition
120 
121 class Case
122 {
123  friend class Akonadi::Server::QueryBuilder;
124 
125 public:
126  Case(const Condition &when, const QString &then, const QString &elseBranch = QString());
127  Case(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &when, const QString &elseBranch = QString());
128 
129  void addCondition(const Condition &when, const QString &then);
130  void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &then);
131  void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, const QString &then);
132 
133  void setElse(const QString &elseBranch);
134 
135 private:
137  QString mElse;
138 };
139 
140 } // namespace Query
141 } // namespace Server
142 } // namespace Akonadi
143 
144 Q_DECLARE_TYPEINFO(Akonadi::Server::Query::Condition, Q_MOVABLE_TYPE);
145 
void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, ConditionType type=WhereCondition)
Add a WHERE or HAVING condition which compares a column with another column.
bool isEmpty() const
Returns if there are sub conditions.
Definition: query.cpp:39
LogicOperator
Logic operations used to combine multiple query conditions.
Definition: query.h:45
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.
void addCondition(const Condition &condition)
Add a WHERE condition.
Definition: query.cpp:54
Condition(LogicOperator op=And)
Create an empty condition.
Definition: query.cpp:33
SortOrder
Sort orders.
Definition: query.h:53
Represents a WHERE condition tree.
Definition: query.h:61
void addCondition(const Query::Condition &condition, ConditionType type=WhereCondition)
Add a WHERE condition.
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
CompareOperator
Compare operators to be used in query conditions.
Definition: query.h:28
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:31
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.