1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#ifndef AKONADI_QUERY_H
21#define AKONADI_QUERY_H
22
23#include <QtCore/QString>
24#include <QtCore/QVariant>
25#include <QtCore/QVector>
26
27namespace Akonadi {
28namespace Server {
29
30class QueryBuilder;
31
32/**
33 Building blocks for SQL queries.
34 @see QueryBuilder
35*/
36namespace Query {
37
38/**
39 Compare operators to be used in query conditions.
40*/
41enum CompareOperator {
42 Equals,
43 NotEquals,
44 Is,
45 IsNot,
46 Less,
47 LessOrEqual,
48 Greater,
49 GreaterOrEqual,
50 In,
51 NotIn,
52 Like
53};
54
55/**
56 Logic operations used to combine multiple query conditions.
57*/
58enum LogicOperator {
59 And,
60 Or
61};
62
63/**
64 Sort orders.
65*/
66enum SortOrder {
67 Ascending,
68 Descending
69};
70
71/**
72 Represents a WHERE condition tree.
73*/
74class Condition
75{
76 friend class Akonadi::Server::QueryBuilder;
77public:
78 /** A list of conditions. */
79 typedef QVector<Condition> List;
80
81 /**
82 Create an empty condition.
83 @param op how to combine sub queries.
84 */
85 Condition(LogicOperator op = And);
86
87 /**
88 Add a WHERE condition which compares a column with a given value.
89 @param column The column that should be compared.
90 @param op The operator used for comparison
91 @param value The value @p column is compared to.
92 */
93 void addValueCondition(const QString &column, CompareOperator op, const QVariant &value);
94
95 /**
96 Add a WHERE condition which compares a column with another column.
97 @param column The column that should be compared.
98 @param op The operator used for comparison.
99 @param column2 The column @p column is compared to.
100 */
101 void addColumnCondition(const QString &column, CompareOperator op, const QString &column2);
102
103 /**
104 Add a WHERE condition. Use this method to build hierarchical conditions.
105 */
106 void addCondition(const Condition &condition);
107
108 /**
109 Set how sub-conditions should be combined, default is And.
110 */
111 void setSubQueryMode(LogicOperator op);
112
113 /**
114 Returns if there are sub conditions.
115 */
116 bool isEmpty() const;
117
118 /**
119 Returns the list of sub-conditions.
120 */
121 Condition::List subConditions() const;
122
123private:
124 Condition::List mSubConditions;
125 QString mColumn;
126 QString mComparedColumn;
127 QVariant mComparedValue;
128 CompareOperator mCompareOp;
129 LogicOperator mCombineOp;
130
131}; // class Condition
132
133} // namespace Query
134} // namespace Server
135} // namespace Akonadi
136
137Q_DECLARE_TYPEINFO(Akonadi::Server::Query::Condition, Q_MOVABLE_TYPE);
138
139#endif
140