QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsdbquerylog.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsdbquerylog.h
3 ------------
4 Date : October 2021
5 Copyright : (C) 2021 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSDBQUERYLOG_H
17#define QGSDBQUERYLOG_H
18
19#include "qgis_core.h"
20#include "qgis.h"
21#include <QString>
22#include <QDateTime>
23
31class CORE_EXPORT QgsDatabaseQueryLogEntry
32{
33 public:
34
38 QgsDatabaseQueryLogEntry( const QString &query = QString() );
39
45 int queryId = 0;
46
48 QString uri;
49
51 QString provider;
52
54 QString query;
55
61 quint64 startedTime = 0;
62
66 quint64 finishedTime = 0;
67
74
80 QString origin;
81
86 long long fetchedRows = -1;
87
91 QString error;
92
96 bool canceled = false;
97
98 private:
99
100 static QAtomicInt sQueryId;
101};
102
104
124class CORE_EXPORT QgsDatabaseQueryLog: public QObject
125{
126 Q_OBJECT
127
128 public:
129
136 QgsDatabaseQueryLog( QObject *parent = nullptr );
137
147 static void setEnabled( bool enabled ) SIP_SKIP { sEnabled = enabled; }
148
154 static bool enabled() { return sEnabled; }
155
161 static void log( const QgsDatabaseQueryLogEntry &query );
162
168 static void finished( const QgsDatabaseQueryLogEntry &query );
169
170 public slots:
171
177 void queryStartedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
178
184 void queryFinishedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
185
186 signals:
187
194
201
202 private:
203
204 static bool sEnabled;
205
206};
207
208#ifndef SIP_RUN
210
214class QgsDatabaseQueryLogWrapper
215{
216
217 public:
218
219 QgsDatabaseQueryLogWrapper( const QString &query, const QString &uri, const QString &provider, const QString &initiatorClass, const QString &origin )
220 : mEntry( query )
221 {
222 mEntry.uri = uri;
223 mEntry.origin = origin;
224 mEntry.initiatorClass = initiatorClass;
225 mEntry.provider = provider;
226 QgsDatabaseQueryLog::log( mEntry );
227 }
228
229 ~QgsDatabaseQueryLogWrapper( )
230 {
232 }
233
234 void setFetchedRows( long long fetchedRows )
235 {
236 mEntry.fetchedRows = fetchedRows;
237 }
238
239 void setQuery( const QString &query )
240 {
241 mEntry.query = query;
242 }
243
244 void setError( const QString &error )
245 {
246 mEntry.error = error;
247 }
248
249 void setCanceled( )
250 {
251 mEntry.canceled = true;
252 }
253
254 private:
255
257
258};
259
261#endif
262
263#endif // QGSDBQUERYLOG_H
Encapsulates a logged database query.
Definition: qgsdbquerylog.h:32
QString query
The logged database query (e.g. the SQL query)
Definition: qgsdbquerylog.h:54
QString uri
Database URI.
Definition: qgsdbquerylog.h:48
QString provider
Provider key.
Definition: qgsdbquerylog.h:51
QString initiatorClass
The QGIS class which initiated the query.
Definition: qgsdbquerylog.h:73
QString origin
Code file location for the query origin.
Definition: qgsdbquerylog.h:80
QString error
Error reported by the provider, normally blank.
Definition: qgsdbquerylog.h:91
Handles logging of database queries.
static void log(const QgsDatabaseQueryLogEntry &query)
Logs a database query as starting.
static bool enabled()
Returns true if logging is enabled.
static void setEnabled(bool enabled)
Enables query logging.
void queryFinished(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query has finished executing.
static void finished(const QgsDatabaseQueryLogEntry &query)
Records that the database query has finished.
void queryStarted(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query is started.
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)