Akonadi

countquerybuilder.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_COUNTQUERYBUILDER_H
21 #define AKONADI_COUNTQUERYBUILDER_H
22 
23 #include "storage/querybuilder.h"
24 #include "akonadiserver_debug.h"
25 
26 #include <QSqlError>
27 
28 namespace Akonadi
29 {
30 namespace Server
31 {
32 
37 {
38 public:
39  enum CountMode {
40  All,
41  Distinct
42  };
43 
47  explicit inline CountQueryBuilder(const QString &table)
48  : QueryBuilder(table, Select)
49  {
50  addColumn(QStringLiteral("count(*)"));
51  }
52 
57  inline CountQueryBuilder(const QString &table, const QString &column, CountMode mode)
58  : QueryBuilder(table, Select)
59  {
60  Q_ASSERT(!table.isEmpty());
61  Q_ASSERT(!column.isEmpty());
62  QString s = QStringLiteral("count(");
63  if (mode == Distinct) {
64  s += QLatin1String("DISTINCT ");
65  }
66  s += column;
67  s += QLatin1Char(')');
68  addColumn(s);
69  }
70 
75  inline int result()
76  {
77  if (!query().next()) {
78  qCDebug(AKONADISERVER_LOG) << "Error during retrieving result of query:" << query().lastError().text();
79  return -1;
80  }
81  const auto result = query().value(0).toInt();
82  query().finish();
83  return result;
84  }
85 };
86 
87 } // namespace Server
88 } // namespace Akonadi
89 
90 #endif
QSqlQuery & query()
Returns the query, only valid after exec().
CountQueryBuilder(const QString &table)
Creates a new query builder that counts all entries in table.
void addColumn(const QString &col)
Adds the given column to a select query.
int toInt(bool *ok) const const
QVariant value(int index) const const
int result()
Returns the result of this query.
bool isEmpty() const const
CountQueryBuilder(const QString &table, const QString &column, CountMode mode)
Creates a new query builder that counts entries in column of table.
void finish()
Helper integration between Akonadi and Qt.
QSqlError lastError() const const
Helper class for creating queries to count elements in a database.
QString text() const const
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:46:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.