Skip to content
Permalink
Browse files
Add bulk addEntries method for efficiently adding many entries at onc…
…e to history
  • Loading branch information
nyalldawson committed Dec 21, 2021
1 parent 09389ca commit dd5cbb746ef7a6fc98a8608b7e7d95a2f38c33fc
@@ -137,6 +137,11 @@ Entry options are specified via the ``options`` argument.
bool addEntry( const QgsHistoryEntry &entry, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );
%Docstring
Adds an ``entry`` to the history logs.
%End

bool addEntries( const QList< QgsHistoryEntry > &entries, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );
%Docstring
Adds a list of ``entries`` to the history logs.
%End

QList< QgsHistoryEntry > queryEntries( const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime(),
@@ -131,6 +131,19 @@ bool QgsHistoryProviderRegistry::addEntry( const QgsHistoryEntry &entry, History
return true;
}

bool QgsHistoryProviderRegistry::addEntries( const QList<QgsHistoryEntry> &entries, HistoryEntryOptions options )
{
if ( options.storageBackends & Qgis::HistoryProviderBackend::LocalProfile )
{
runEmptyQuery( QStringLiteral( "BEGIN TRANSACTION;" ) );
for ( const QgsHistoryEntry &entry : entries )
addEntry( entry, options );
runEmptyQuery( QStringLiteral( "COMMIT TRANSACTION;" ) );
}

return true;
}

QList<QgsHistoryEntry> QgsHistoryProviderRegistry::queryEntries( const QDateTime &start, const QDateTime &end, const QString &providerId, Qgis::HistoryProviderBackends backends ) const
{
QList<QgsHistoryEntry> entries;
@@ -164,6 +164,11 @@ class GUI_EXPORT QgsHistoryProviderRegistry : public QObject
*/
bool addEntry( const QgsHistoryEntry &entry, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );

/**
* Adds a list of \a entries to the history logs.
*/
bool addEntries( const QList< QgsHistoryEntry > &entries, QgsHistoryProviderRegistry::HistoryEntryOptions options = QgsHistoryProviderRegistry::HistoryEntryOptions() );

/**
* Queries history entries which occurred between the specified \a start and \a end times.
*
@@ -148,6 +148,18 @@ def test_registry_entries(self):
self.assertTrue(reg.clearHistory(Qgis.HistoryProviderBackend.LocalProfile))
self.assertFalse(reg.queryEntries())

# bulk add entries
self.assertTrue(reg.addEntries([
QgsHistoryEntry('my provider 4', QDateTime(2021, 1, 2, 3, 4, 5), {'var': 7}),
QgsHistoryEntry('my provider 5', QDateTime(2021, 1, 2, 3, 4, 5), {'var': 8})
]))
self.assertEqual(len(reg.queryEntries()), 2)
self.assertEqual(reg.queryEntries()[0].providerId, 'my provider 4')
self.assertEqual(reg.queryEntries()[0].entry, {'var': 7})

self.assertEqual(reg.queryEntries()[1].providerId, 'my provider 5')
self.assertEqual(reg.queryEntries()[1].entry, {'var': 8})


if __name__ == '__main__':
unittest.main()

0 comments on commit dd5cbb7

Please sign in to comment.