Skip to content

Commit

Permalink
Merge pull request #51313 from elpaso/ogrprovider-sql-logging
Browse files Browse the repository at this point in the history
Ogrprovider sql logging
  • Loading branch information
elpaso committed Dec 29, 2022
2 parents 2712aed + 98366e1 commit 8bb9a01
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/core/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -22,14 +22,12 @@
#include "qgsogrutils.h"
#include "qgsapplication.h"
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgssettings.h"
#include "qgsexception.h"
#include "qgswkbtypes.h"
#include "qgsogrtransaction.h"
#include "qgssymbol.h"
#include "qgsgeometryengine.h"
#include "qgsdbquerylog.h"

#include <QTextCodec>
#include <QFile>
Expand Down Expand Up @@ -279,6 +277,34 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool
std::sort( mRequestAttributes.begin(), mRequestAttributes.end() );
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,7,0)
// Install query logger
// Note: this logger won't track insert/update/delete operations,
// in order to do that the callback would need to be installed
// in the provider's dataset, but because the provider life-cycle
// is significantly longer than this iterator it wouldn't be possible
// to install the callback at a later time if the provider was created
// when the logger was disabled.
// There is currently no API to connect the change of state of the
// logger to the data provider.
if ( QgsApplication::databaseQueryLog()->enabled() )
{
GDALDatasetSetQueryLoggerFunc( mConn->ds, [ ]( const char *pszSQL, const char *pszError, int64_t lNumRecords, int64_t lExecutionTimeMilliseconds, void *pQueryLoggerArg )
{
QgsDatabaseQueryLogEntry entry;
entry.initiatorClass = QStringLiteral( "QgsOgrFeatureIterator" );
entry.origin = QGS_QUERY_LOG_ORIGIN;
entry.provider = QStringLiteral( "ogr" );
entry.uri = *reinterpret_cast<QString *>( pQueryLoggerArg );
entry.query = QString( pszSQL );
entry.error = QString( pszError );
entry.startedTime = QDateTime::currentMSecsSinceEpoch() - lExecutionTimeMilliseconds;
entry.fetchedRows = lNumRecords;
QgsApplication::databaseQueryLog()->log( entry );
QgsApplication::databaseQueryLog()->finished( entry );
}, reinterpret_cast<void *>( &mConn->path ) );
}
#endif
//start with first feature
rewind();

Expand Down
1 change: 1 addition & 0 deletions src/core/providers/ogr/qgsogrfeatureiterator.h
Expand Up @@ -126,6 +126,7 @@ class QgsOgrFeatureIterator final: public QgsAbstractFeatureIteratorFromSource<Q
bool fetchFeatureWithId( QgsFeatureId id, QgsFeature &feature ) const;

void resetReading();

};

///@endcond
Expand Down

0 comments on commit 8bb9a01

Please sign in to comment.