QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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.h"
20#include "qgis_core.h"
21
22#include <QDateTime>
23#include <QString>
24
32class CORE_EXPORT QgsDatabaseQueryLogEntry
33{
34 public:
35
39 QgsDatabaseQueryLogEntry( const QString &query = QString() );
40
46 int queryId = 0;
47
49 QString uri;
50
52 QString provider;
53
55 QString query;
56
62 quint64 startedTime = 0;
63
67 quint64 finishedTime = 0;
68
75
81 QString origin;
82
87 long long fetchedRows = -1;
88
92 QString error;
93
97 bool canceled = false;
98
99 private:
100
101 static QAtomicInt sQueryId;
102};
103
105
125class CORE_EXPORT QgsDatabaseQueryLog: public QObject
126{
127 Q_OBJECT
128
129 public:
130
137 QgsDatabaseQueryLog( QObject *parent = nullptr );
138
148 static void setEnabled( bool enabled ) SIP_SKIP { sEnabled = enabled; }
149
155 static bool enabled() { return sEnabled; }
156
162 static void log( const QgsDatabaseQueryLogEntry &query );
163
169 static void finished( const QgsDatabaseQueryLogEntry &query );
170
171 public slots:
172
178 void queryStartedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
179
185 void queryFinishedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
186
187 signals:
188
195
202
203 private:
204
205 static bool sEnabled;
206
207};
208
209#ifndef SIP_RUN
211
215class QgsDatabaseQueryLogWrapper
216{
217
218 public:
219
220 QgsDatabaseQueryLogWrapper( const QString &query, const QString &uri, const QString &provider, const QString &initiatorClass, const QString &origin )
221 : mEntry( query )
222 {
223 mEntry.uri = uri;
224 mEntry.origin = origin;
225 mEntry.initiatorClass = initiatorClass;
226 mEntry.provider = provider;
227 QgsDatabaseQueryLog::log( mEntry );
228 }
229
230 ~QgsDatabaseQueryLogWrapper( )
231 {
233 }
234
235 void setFetchedRows( long long fetchedRows )
236 {
237 mEntry.fetchedRows = fetchedRows;
238 }
239
240 void setQuery( const QString &query )
241 {
242 mEntry.query = query;
243 }
244
245 void setError( const QString &error )
246 {
247 mEntry.error = error;
248 }
249
250 void setCanceled( )
251 {
252 mEntry.canceled = true;
253 }
254
255 private:
256
257 QgsDatabaseQueryLogEntry mEntry;
258
259};
260
262#endif
263
264#endif // QGSDBQUERYLOG_H
Encapsulates a logged database query.
quint64 finishedTime
Time when the query finished (in milliseconds since epoch), if available.
long long fetchedRows
Number of fetched/affected rows.
QString query
The logged database query (e.g. the SQL query).
QString uri
Database URI.
QString provider
Provider key.
QString initiatorClass
The QGIS class which initiated the query.
QString origin
Code file location for the query origin.
quint64 startedTime
Time when the query started (in milliseconds since epoch).
bool canceled
Canceled flag for user canceled queries.
int queryId
Unique query ID.
QgsDatabaseQueryLogEntry(const QString &query=QString())
Constructor for QgsDatabaseQueryLogEntry.
QString error
Error reported by the provider, normally blank.
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.
QgsDatabaseQueryLog(QObject *parent=nullptr)
Creates a new query log.
#define SIP_SKIP
Definition qgis_sip.h:134
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)