QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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 "qgslogger.h"
19
20#include <QDateTime>
21
22//
23// QgsDatabaseQueryLogEntry
24//
25
26QAtomicInt QgsDatabaseQueryLogEntry::sQueryId = 0;
27
29 : queryId( ++sQueryId )
30 , query( query )
31 , startedTime( QDateTime::currentMSecsSinceEpoch() )
32{}
33
34
35//
36// QgsDatabaseQueryLog
37//
38
39bool QgsDatabaseQueryLog::sEnabled = false;
40
42 : QObject( parent )
43{
44
45}
46
48{
49 if ( !sEnabled )
50 return;
51
52 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryStartedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, query ) );
53}
54
56{
57 if ( !sEnabled )
58 return;
59
60 // record time of completion
61 QgsDatabaseQueryLogEntry finishedQuery = query;
62 finishedQuery.finishedTime = QDateTime::currentMSecsSinceEpoch();
63
64 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryFinishedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, finishedQuery ) );
65}
66
68{
69 QgsDebugMsgLevel( query.query, 2 );
70 emit queryStarted( query );
71}
72
74{
75 QgsDebugMsgLevel( query.query, 2 );
76 emit queryFinished( query );
77}
78
static QgsDatabaseQueryLog * databaseQueryLog()
Returns the database query log.
Encapsulates a logged database query.
quint64 finishedTime
Time when the query finished (in milliseconds since epoch), if available.
QString query
The logged database query (e.g. the SQL query)
QgsDatabaseQueryLogEntry(const QString &query=QString())
Constructor for QgsDatabaseQueryLogEntry.
static void log(const QgsDatabaseQueryLogEntry &query)
Logs a database query as starting.
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.
void queryFinishedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as finished.
void queryStartedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as start.
QgsDatabaseQueryLog(QObject *parent=nullptr)
Creates a new query log.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39