Akonadi

countquerybuilder.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 "akonadiserver_debug.h"
10#include "storage/datastore.h"
11#include "storage/querybuilder.h"
12
13#include <QSqlError>
14
15namespace Akonadi
16{
17namespace Server
18{
19/**
20 Helper class for creating queries to count elements in a database.
21*/
23{
24public:
25 enum CountMode {
26 All,
27 Distinct,
28 };
29
30 /**
31 Creates a new query builder that counts all entries in @p table.
32 */
33 explicit inline CountQueryBuilder(const QString &table)
34 : CountQueryBuilder(DataStore::self(), table)
35 {
36 }
37
38 inline CountQueryBuilder(DataStore *store, const QString &table)
39 : QueryBuilder(store, table, Select)
40 {
41 addColumn(QStringLiteral("count(*)"));
42 }
43
44 /**
45 * Creates a new query builder that counts entries in @p column of @p table.
46 * If @p mode is set to @c Distinct, duplicate entries in that column are ignored.
47 */
48 inline CountQueryBuilder(const QString &table, const QString &column, CountMode mode)
49 : CountQueryBuilder(DataStore::self(), table, column, mode)
50 {
51 }
52
53 inline CountQueryBuilder(DataStore *store, const QString &table, const QString &column, CountMode mode)
54 : QueryBuilder(store, table, Select)
55 {
56 Q_ASSERT(!table.isEmpty());
57 Q_ASSERT(!column.isEmpty());
58 QString s = QStringLiteral("count(");
59 if (mode == Distinct) {
60 s += QLatin1StringView("DISTINCT ");
61 }
62 s += column;
63 s += QLatin1Char(')');
64 addColumn(s);
65 }
66
67 /**
68 Returns the result of this query.
69 @returns -1 on error.
70 */
71 inline int result()
72 {
73 if (!query().next()) {
74 qCDebug(AKONADISERVER_LOG) << "Error during retrieving result of query:" << query().lastError().text();
75 return -1;
76 }
77 const auto result = query().value(0).toInt();
78 query().finish();
79 return result;
80 }
81};
82
83} // namespace Server
84} // namespace Akonadi
Helper class for creating queries to count elements in a database.
CountQueryBuilder(const QString &table)
Creates a new query builder that counts all entries in table.
int result()
Returns the result of this query.
CountQueryBuilder(const QString &table, const QString &column, CountMode mode)
Creates a new query builder that counts entries in column of table.
This class handles all the database access.
Definition datastore.h:95
Helper class to construct arbitrary SQL queries.
QSqlQuery & query()
Returns the query, only valid after exec().
void addColumn(const QString &col)
Adds the given column to a select query.
Helper integration between Akonadi and Qt.
QString text() const const
void finish()
QSqlError lastError() const const
QVariant value(const QString &name) const const
bool isEmpty() const const
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:52:53 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.