QGIS API Documentation 4.1.0-Master (60fea48833c)
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:
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 static QAtomicInt sQueryId;
100};
101
103
123class CORE_EXPORT QgsDatabaseQueryLog : public QObject
124{
125 Q_OBJECT
126
127 public:
134 QgsDatabaseQueryLog( QObject *parent = nullptr );
135
145 static void setEnabled( bool enabled ) SIP_SKIP { sEnabled = enabled; }
146
152 static bool enabled() { return sEnabled; }
153
159 static void log( const QgsDatabaseQueryLogEntry &query );
160
166 static void finished( const QgsDatabaseQueryLogEntry &query );
167
168 public slots:
169
175 void queryStartedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
176
182 void queryFinishedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
183
184 signals:
185
192
199
200 private:
201 static bool sEnabled;
202};
203
204#ifndef SIP_RUN
206
210class QgsDatabaseQueryLogWrapper
211{
212 public:
213 QgsDatabaseQueryLogWrapper( const QString &query, const QString &uri, const QString &provider, const QString &initiatorClass, const QString &origin )
214 : mEntry( query )
215 {
216 mEntry.uri = uri;
217 mEntry.origin = origin;
218 mEntry.initiatorClass = initiatorClass;
219 mEntry.provider = provider;
220 QgsDatabaseQueryLog::log( mEntry );
221 }
222
223 ~QgsDatabaseQueryLogWrapper() { QgsDatabaseQueryLog::finished( mEntry ); }
224
225 void setFetchedRows( long long fetchedRows ) { mEntry.fetchedRows = fetchedRows; }
226
227 void setQuery( const QString &query ) { mEntry.query = query; }
228
229 void setError( const QString &error ) { mEntry.error = error; }
230
231 void setCanceled() { mEntry.canceled = true; }
232
233 private:
234 QgsDatabaseQueryLogEntry mEntry;
235};
236
238#endif
239
240#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:133
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)