QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
31 class 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 
73  QString initiatorClass;
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"
107 constexpr 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 
131 class 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 
200  void queryStarted( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
201 
207  void queryFinished( const QgsDatabaseQueryLogEntry &query ) SIP_SKIP;
208 
209  private:
210 
211  static bool sEnabled;
212 
213 };
214 
215 #ifndef SIP_RUN
216 
221 class 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
QgsDatabaseQueryLogEntry::initiatorClass
QString initiatorClass
The QGIS class which initiated the query.
Definition: qgsdbquerylog.h:73
QgsDatabaseQueryLogEntry
Encapsulates a logged database query.
Definition: qgsdbquerylog.h:31
QgsDatabaseQueryLogEntry::uri
QString uri
Database URI.
Definition: qgsdbquerylog.h:48
QgsDatabaseQueryLogEntry::error
QString error
Error reported by the provider, normally blank.
Definition: qgsdbquerylog.h:91
qgis.h
QgsDatabaseQueryLog
Handles logging of database queries.
Definition: qgsdbquerylog.h:131
QgsDatabaseQueryLogEntry::query
QString query
The logged database query (e.g. the SQL query)
Definition: qgsdbquerylog.h:54
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsDatabaseQueryLog::enabled
static bool enabled()
Returns true if logging is enabled.
Definition: qgsdbquerylog.h:161
QgsDatabaseQueryLog::finished
static void finished(const QgsDatabaseQueryLogEntry &query)
Records that the database query has finished.
Definition: qgsdbquerylog.cpp:53
QgsDatabaseQueryLogEntry::provider
QString provider
Provider key.
Definition: qgsdbquerylog.h:51
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QgsDatabaseQueryLogEntry::origin
QString origin
Code file location for the query origin.
Definition: qgsdbquerylog.h:80
QgsDatabaseQueryLog::log
static void log(const QgsDatabaseQueryLogEntry &query)
Logs a database query as starting.
Definition: qgsdbquerylog.cpp:45
QgsDatabaseQueryLog::setEnabled
static void setEnabled(bool enabled)
Enables query logging.
Definition: qgsdbquerylog.h:154
sQueryLoggerFilePrefixLength
constexpr int sQueryLoggerFilePrefixLength
Definition: qgsdbquerylog.h:107