Skip to content
Permalink
Browse files

Combining simple Postgres queries.

  • Loading branch information
MorriganR authored and nyalldawson committed Mar 30, 2021
1 parent f466f42 commit 19fa471d4fe78240a5c9dab35098a453d6fc1906
Showing with 10 additions and 8 deletions.
  1. +10 −8 src/providers/postgres/qgspostgresconn.cpp
@@ -1364,33 +1364,35 @@ int QgsPostgresConn::PQCancel()
bool QgsPostgresConn::openCursor( const QString &cursorName, const QString &sql )
{
QMutexLocker locker( &mLock ); // to protect access to mOpenCursors
QString preStr = "";

if ( mOpenCursors++ == 0 && !mTransaction )
{
QgsDebugMsgLevel( QStringLiteral( "Starting read-only transaction: %1" ).arg( mPostgresqlVersion ), 4 );
if ( mPostgresqlVersion >= 80000 )
PQexecNR( QStringLiteral( "BEGIN READ ONLY" ) );
preStr = QStringLiteral( "BEGIN READ ONLY;" );
else
PQexecNR( QStringLiteral( "BEGIN" ) );
preStr = QStringLiteral( "BEGIN;" );
}
QgsDebugMsgLevel( QStringLiteral( "Binary cursor %1 for %2" ).arg( cursorName, sql ), 3 );
return PQexecNR( QStringLiteral( "DECLARE %1 BINARY CURSOR%2 FOR %3" ).
arg( cursorName, !mTransaction ? QString() : QStringLiteral( " WITH HOLD" ), sql ) );
return PQexecNR( QStringLiteral( "%1DECLARE %2 BINARY CURSOR%3 FOR %4" ).
arg( preStr, cursorName, !mTransaction ? QString() : QStringLiteral( " WITH HOLD" ), sql ) );
}

bool QgsPostgresConn::closeCursor( const QString &cursorName )
{
QMutexLocker locker( &mLock ); // to protect access to mOpenCursors

if ( !PQexecNR( QStringLiteral( "CLOSE %1" ).arg( cursorName ) ) )
return false;
QString postStr = "";

if ( --mOpenCursors == 0 && !mTransaction )
{
QgsDebugMsgLevel( QStringLiteral( "Committing read-only transaction" ), 4 );
PQexecNR( QStringLiteral( "COMMIT" ) );
postStr = QStringLiteral( ";COMMIT" );
}

if ( !PQexecNR( QStringLiteral( "CLOSE %1%2" ).arg( cursorName, postStr ) ) )
return false;

return true;
}

0 comments on commit 19fa471

Please sign in to comment.