Akonadi

querybuilder.h
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef AKONADI_QUERYBUILDER_H
8 #define AKONADI_QUERYBUILDER_H
9 
10 #include "query.h"
11 #include "dbtype.h"
12 
13 #include <QPair>
14 #include <QString>
15 #include <QStringList>
16 #include <QVariant>
17 #include <QVector>
18 #include <QSqlQuery>
19 
20 #ifdef QUERYBUILDER_UNITTEST
21 class QueryBuilderTest;
22 #endif
23 
24 namespace Akonadi
25 {
26 namespace Server
27 {
28 
33 {
34 public:
35  enum QueryType {
36  Select,
37  Insert,
38  Update,
39  Delete
40  };
41 
47  enum JoinType {
52  };
53 
63 
64  NUM_CONDITIONS
65  };
66 
72  explicit QueryBuilder(const QString &table, QueryType type = Select);
73 
78  void setDatabaseType(DbType::Type type);
79 
88  void addJoin(JoinType joinType, const QString &table, const Query::Condition &condition);
89 
100  void addJoin(JoinType joinType, const QString &table, const QString &col1, const QString &col2);
101 
106  void addColumns(const QStringList &cols);
107 
112  void addColumn(const QString &col);
113 
118  void addColumn(const Query::Case &caseStmt);
119 
125  void addAggregation(const QString &col, const QString &aggregate);
126 
132  void addAggregation(const Query::Case &caseStmt, const QString &aggregate);
133 
142  void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type = WhereCondition);
143 
152  void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, ConditionType type = WhereCondition);
153 
160  void addCondition(const Query::Condition &condition, ConditionType type = WhereCondition);
161 
170 
176  void addSortColumn(const QString &column, Query::SortOrder order = Query::Ascending);
177 
183  void addGroupColumn(const QString &column);
184 
190  void addGroupColumns(const QStringList &columns);
191 
197  void setColumnValue(const QString &column, const QVariant &value);
198 
203  void setDistinct(bool distinct);
204 
210  void setLimit(int limit);
211 
220  void setIdentificationColumn(const QString &column);
221 
225  QSqlQuery &query();
226 
230  bool exec();
231 
238  qint64 insertId();
239 
246  void setForUpdate(bool forUpdate = true);
247 
248 private:
249  void buildQuery(QString *query);
250  void bindValue(QString *query, const QVariant &value);
251  void buildWhereCondition(QString *query, const Query::Condition &cond);
252  void buildCaseStatement(QString *query, const Query::Case &caseStmt);
253 
258  void sqliteAdaptUpdateJoin(Query::Condition &cond);
259 
260 private:
261  QString mTable;
262  DbType::Type mDatabaseType;
263  Query::Condition mRootCondition[NUM_CONDITIONS];
264  QSqlQuery mQuery;
265  QueryType mType;
266  QStringList mColumns;
267  QVector<QVariant> mBindValues;
269  QStringList mGroupColumns;
270  QVector<QPair<QString, QVariant> > mColumnValues;
271  QString mIdentificationColumn;
272 
273  // we must make sure that the tables are joined in the correct order
274  // QMap sorts by key which might invalidate the queries
275  QStringList mJoinedTables;
277  int mLimit;
278  bool mDistinct;
279  bool mForUpdate = false;
280 #ifdef QUERYBUILDER_UNITTEST
281  QString mStatement;
282  friend class ::QueryBuilderTest;
283 #endif
284 };
285 
286 } // namespace Server
287 } // namespace Akonadi
288 
289 #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:22
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:47
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:49
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:59
SortOrder
Sort orders.
Definition: query.h:56
CompareOperator
Compare operators to be used in query conditions.
Definition: query.h:31
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:62
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:57
NOTE: only supported for SELECT queries.
Definition: querybuilder.h:51
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:48
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:64
void addColumns(const QStringList &cols)
Adds the given columns to a select query.
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:32
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-2021 The KDE developers.
Generated on Sat Jan 23 2021 07:17:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.