Skip to content

Commit

Permalink
Update sqlite fetch functions to standard thread safe layer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 13, 2022
1 parent c075f3e commit 7dfdfb2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -2150,10 +2150,11 @@ static QVariant fcnSqliteFetchAndIncrement( const QVariantList &values, const Qg
static QMap<QString, qlonglong> counterCache;
QVariant functionResult;

std::function<void()> fetchAndIncrementFunc = [ =, &functionResult ]()
auto fetchAndIncrementFunc = [ values, parent, &functionResult ]( QgsMapLayer * mapLayer, const QString & databaseArgument )
{
QString database;
const QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), context, parent );

const QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( mapLayer );

if ( layer )
{
Expand All @@ -2166,7 +2167,7 @@ static QVariant fcnSqliteFetchAndIncrement( const QVariantList &values, const Qg
}
else
{
database = values.at( 0 ).toString();
database = databaseArgument;
}

const QString table = values.at( 1 ).toString();
Expand Down Expand Up @@ -2301,7 +2302,19 @@ static QVariant fcnSqliteFetchAndIncrement( const QVariantList &values, const Qg
functionResult = QVariant();
};

QgsThreadingUtils::runOnMainThread( fetchAndIncrementFunc );
bool foundLayer = false;
QgsExpressionUtils::executeLambdaForMapLayer( values.at( 0 ), context, parent, [&fetchAndIncrementFunc]( QgsMapLayer * layer )
{
fetchAndIncrementFunc( layer, QString() );
}, foundLayer );
if ( !foundLayer )
{
const QString databasePath = values.at( 0 ).toString();
QgsThreadingUtils::runOnMainThread( [&fetchAndIncrementFunc, databasePath]
{
fetchAndIncrementFunc( nullptr, databasePath );
} );
}

return functionResult;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsthreadingutils.h
Expand Up @@ -77,7 +77,7 @@ class CORE_EXPORT QgsThreadingUtils
QSemaphore semaphoreWorkerThreadReady( 1 );

// Acquire both semaphores. We want the main thread and the current thread to be blocked
// until it's save to continue.
// until it's safe to continue.
semaphoreMainThreadReady.acquire();
semaphoreWorkerThreadReady.acquire();

Expand Down

0 comments on commit 7dfdfb2

Please sign in to comment.