QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
105#ifndef SIP_RUN
106#include "qgsconfig.h"
107constexpr int sQueryLoggerFilePrefixLength = CMAKE_SOURCE_DIR[sizeof( CMAKE_SOURCE_DIR ) - 1] == '/' ? sizeof( CMAKE_SOURCE_DIR ) + 1 : sizeof( CMAKE_SOURCE_DIR );
108#define QgsSetQueryLogClass(entry, _class) entry.initiatorClass = _class; entry.origin = QString(QString( __FILE__ ).mid( sQueryLoggerFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + ")");
109#define QGS_QUERY_LOG_ORIGIN QString(QString( __FILE__ ).mid( sQueryLoggerFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + ")")
110#endif
111
131class CORE_EXPORT QgsDatabaseQueryLog: public QObject
132{
133 Q_OBJECT
134
135 public:
136
143 QgsDatabaseQueryLog( QObject *parent = nullptr );
144
154 static void setEnabled( bool enabled ) SIP_SKIP { sEnabled = enabled; }
155
161 static bool enabled() { return sEnabled; }
162
168 static void log( const QgsDatabaseQueryLogEntry &query );
169
175 static void finished( const QgsDatabaseQueryLogEntry &query );
176
177 public slots:
178
184 void queryStartedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
185
191 void queryFinishedPrivate( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
192
193 signals:
194
201
208
209 private:
210
211 static bool sEnabled;
212
213};
214
215#ifndef SIP_RUN
217
221class QgsDatabaseQueryLogWrapper
222{
223
224 public:
225
226 QgsDatabaseQueryLogWrapper( const QString &query, const QString &uri, const QString &provider, const QString &initiatorClass, const QString &origin )
227 : mEntry( query )
228 {
229 mEntry.uri = uri;
230 mEntry.origin = origin;
231 mEntry.initiatorClass = initiatorClass;
232 mEntry.provider = provider;
233 QgsDatabaseQueryLog::log( mEntry );
234 }
235
236 ~QgsDatabaseQueryLogWrapper( )
237 {
239 }
240
241 void setFetchedRows( long long fetchedRows )
242 {
243 mEntry.fetchedRows = fetchedRows;
244 }
245
246 void setQuery( const QString &query )
247 {
248 mEntry.query = query;
249 }
250
251 void setError( const QString &error )
252 {
253 mEntry.error = error;
254 }
255
256 void setCanceled( )
257 {
258 mEntry.canceled = true;
259 }
260
261 private:
262
264
265};
266
268#endif
269
270#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
constexpr int sQueryLoggerFilePrefixLength
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)