Akonadi

querybuilder.h
1 /*
2  Copyright (c) 2007 Volker Krause <[email protected]>
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_QUERYBUILDER_H
21 #define AKONADI_QUERYBUILDER_H
22 
23 #include "query.h"
24 #include "dbtype.h"
25 
26 #include <QPair>
27 #include <QString>
28 #include <QStringList>
29 #include <QVariant>
30 #include <QVector>
31 #include <QSqlQuery>
32 
33 #ifdef QUERYBUILDER_UNITTEST
34 class QueryBuilderTest;
35 #endif
36 
37 namespace Akonadi
38 {
39 namespace Server
40 {
41 
46 {
47 public:
48  enum QueryType {
49  Select,
50  Insert,
51  Update,
52  Delete
53  };
54 
60  enum JoinType {
65  };
66 
76 
77  NUM_CONDITIONS
78  };
79 
85  explicit QueryBuilder(const QString &table, QueryType type = Select);
86 
91  void setDatabaseType(DbType::Type type);
92 
101  void addJoin(JoinType joinType, const QString &table, const Query::Condition &condition);
102 
113  void addJoin(JoinType joinType, const QString &table, const QString &col1, const QString &col2);
114 
119  void addColumns(const QStringList &cols);
120 
125  void addColumn(const QString &col);
126 
131  void addColumn(const Query::Case &caseStmt);
132 
138  void addAggregation(const QString &col, const QString &aggregate);
139 
145  void addAggregation(const Query::Case &caseStmt, const QString &aggregate);
146 
155  void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type = WhereCondition);
156 
165  void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, ConditionType type = WhereCondition);
166 
173  void addCondition(const Query::Condition &condition, ConditionType type = WhereCondition);
174 
183 
189  void addSortColumn(const QString &column, Query::SortOrder order = Query::Ascending);
190 
196  void addGroupColumn(const QString &column);
197 
203  void addGroupColumns(const QStringList &columns);
204 
210  void setColumnValue(const QString &column, const QVariant &value);
211 
216  void setDistinct(bool distinct);
217 
223  void setLimit(int limit);
224 
233  void setIdentificationColumn(const QString &column);
234 
238  QSqlQuery &query();
239 
243  bool exec();
244 
251  qint64 insertId();
252 
259  void setForUpdate(bool forUpdate = true);
260 
261 private:
262  void buildQuery(QString *query);
263  void bindValue(QString *query, const QVariant &value);
264  void buildWhereCondition(QString *query, const Query::Condition &cond);
265  void buildCaseStatement(QString *query, const Query::Case &caseStmt);
266 
271  void sqliteAdaptUpdateJoin(Query::Condition &cond);
272 
273 private:
274  QString mTable;
275  DbType::Type mDatabaseType;
276  Query::Condition mRootCondition[NUM_CONDITIONS];
277  QSqlQuery mQuery;
278  QueryType mType;
279  QStringList mColumns;
280  QVector<QVariant> mBindValues;
282  QStringList mGroupColumns;
283  QVector<QPair<QString, QVariant> > mColumnValues;
284  QString mIdentificationColumn;
285 
286  // we must make sure that the tables are joined in the correct order
287  // QMap sorts by key which might invalidate the queries
288  QStringList mJoinedTables;
290  int mLimit;
291  bool mDistinct;
292  bool mForUpdate = false;
293 #ifdef QUERYBUILDER_UNITTEST
294  QString mStatement;
295  friend class ::QueryBuilderTest;
296 #endif
297 };
298 
299 } // namespace Server
300 } // namespace Akonadi
301 
302 #endif
void setDistinct(bool distinct)
Specify whether duplicates should be included in the result.
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.
QSqlQuery & query()
Returns the query, only valid after exec().
void setForUpdate(bool forUpdate=true)
Indicate to the database to acquire an exclusive lock on the rows already during SELECT statement...
void setColumnValue(const QString &column, const QVariant &value)
Sets a column to the given value (only valid for INSERT and UPDATE queries).
void setDatabaseType(DbType::Type type)
Sets the database which should execute the query.
Type
Supported database types.
Definition: dbtype.h:35
qint64 insertId()
Returns the ID of the newly created record (only valid for INSERT queries)
void addAggregation(const QString &col, const QString &aggregate)
Adds an aggregation statement.
JoinType
When the same table gets joined as both, Inner- and LeftJoin, it will be merged into a single InnerJo...
Definition: querybuilder.h:60
void setSubQueryMode(Query::LogicOperator op, ConditionType type=WhereCondition)
Define how WHERE or HAVING conditions are combined.
NOTE: only supported for UPDATE and SELECT queries.
Definition: querybuilder.h:62
void addJoin(JoinType joinType, const QString &table, const Query::Condition &condition)
Join a table to the query.
void addColumn(const QString &col)
Adds the given column to a select query.
void setIdentificationColumn(const QString &column)
Sets the column used for identification in an INSERT statement.
add condition to WHERE part of the query
Definition: querybuilder.h:72
SortOrder
Sort orders.
Definition: query.h:69
CompareOperator
Compare operators to be used in query conditions.
Definition: query.h:44
void addGroupColumn(const QString &column)
Add a GROUP BY column.
QueryBuilder(const QString &table, QueryType type=Select)
Creates a new query builder.
add condition to HAVING part of the query NOTE: only supported for SELECT queries ...
Definition: querybuilder.h:75
void addSortColumn(const QString &column, Query::SortOrder order=Query::Ascending)
Add sort column.
ConditionType
Defines the place at which a condition should be evaluated.
Definition: querybuilder.h:70
NOTE: only supported for SELECT queries.
Definition: querybuilder.h:64
void addGroupColumns(const QStringList &columns)
Add list of columns to GROUP BY.
Helper integration between Akonadi and Qt.
LogicOperator
Logic operations used to combine multiple query conditions.
Definition: query.h:61
void setLimit(int limit)
Limits the amount of retrieved rows.
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.
Represents a WHERE condition tree.
Definition: query.h:77
void addColumns(const QStringList &cols)
Adds the given columns to a select query.
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:45
void addCondition(const Query::Condition &condition, ConditionType type=WhereCondition)
Add a WHERE condition.
bool exec()
Executes the query, returns true on success.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 1 2020 23:07:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.