QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsdbquerylog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdbquerylog.cpp
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 #include "qgsdbquerylog.h"
17 #include "qgsapplication.h"
18 #include <QDateTime>
19 
20 //
21 // QgsDatabaseQueryLogEntry
22 //
23 
24 QAtomicInt QgsDatabaseQueryLogEntry::sQueryId = 0;
25 
27  : queryId( ++sQueryId )
28  , query( query )
29  , startedTime( QDateTime::currentMSecsSinceEpoch() )
30 {}
31 
32 
33 //
34 // QgsDatabaseQueryLog
35 //
36 
37 bool QgsDatabaseQueryLog::sEnabled = false;
38 
40  : QObject( parent )
41 {
42 
43 }
44 
46 {
47  if ( !sEnabled )
48  return;
49 
50  QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryStartedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, query ) );
51 }
52 
54 {
55  if ( !sEnabled )
56  return;
57 
58  // record time of completion
59  QgsDatabaseQueryLogEntry finishedQuery = query;
60  finishedQuery.finishedTime = QDateTime::currentMSecsSinceEpoch();
61 
62  QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryFinishedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, finishedQuery ) );
63 }
64 
66 {
67  QgsDebugMsg( query.query );
68  emit queryStarted( query );
69 }
70 
72 {
73  QgsDebugMsg( query.query );
74  emit queryFinished( query );
75 }
76 
QgsDatabaseQueryLogEntry
Encapsulates a logged database query.
Definition: qgsdbquerylog.h:31
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsDatabaseQueryLogEntry::finishedTime
quint64 finishedTime
Time when the query finished (in milliseconds since epoch), if available.
Definition: qgsdbquerylog.h:66
QgsDatabaseQueryLog::queryStarted
void queryStarted(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query is started.
qgsapplication.h
QgsDatabaseQueryLogEntry::query
QString query
The logged database query (e.g. the SQL query)
Definition: qgsdbquerylog.h:54
QgsApplication::databaseQueryLog
static QgsDatabaseQueryLog * databaseQueryLog()
Returns the database query log.
Definition: qgsapplication.cpp:2440
QgsDatabaseQueryLogEntry::QgsDatabaseQueryLogEntry
QgsDatabaseQueryLogEntry(const QString &query=QString())
Constructor for QgsDatabaseQueryLogEntry.
Definition: qgsdbquerylog.cpp:26
qgsdbquerylog.h
QgsDatabaseQueryLog::queryFinished
void queryFinished(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query has finished executing.
QgsDatabaseQueryLog::finished
static void finished(const QgsDatabaseQueryLogEntry &query)
Records that the database query has finished.
Definition: qgsdbquerylog.cpp:53
QgsDatabaseQueryLog::queryStartedPrivate
void queryStartedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as start.
Definition: qgsdbquerylog.cpp:65
QgsDatabaseQueryLog::queryFinishedPrivate
void queryFinishedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as finished.
Definition: qgsdbquerylog.cpp:71
QgsDatabaseQueryLog::log
static void log(const QgsDatabaseQueryLogEntry &query)
Logs a database query as starting.
Definition: qgsdbquerylog.cpp:45
QgsDatabaseQueryLog::QgsDatabaseQueryLog
QgsDatabaseQueryLog(QObject *parent=nullptr)
Creates a new query log.
Definition: qgsdbquerylog.cpp:39