@@ -211,6 +211,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
211211 , mNextCursorId( 0 )
212212 , mShared( shared )
213213 , mTransaction( transaction )
214+ , mLock( QMutex::Recursive )
214215{
215216 QgsDebugMsg ( QStringLiteral ( " New PostgreSQL connection for " ) + conninfo );
216217
@@ -1065,33 +1066,37 @@ PGresult *QgsPostgresConn::PQexec( const QString &query, bool logError ) const
10651066 }
10661067
10671068 QgsDebugMsgLevel ( QStringLiteral ( " Executing SQL: %1" ).arg ( query ), 3 );
1068- PGresult *res = ::PQexec ( mConn , query.toUtf8 () );
1069-
1070- if ( res )
1069+ PGresult *res = nullptr ;
10711070 {
1072- int errorStatus = PQresultStatus ( res );
1073- if ( errorStatus != PGRES_COMMAND_OK && errorStatus != PGRES_TUPLES_OK )
1071+ QMutexLocker locker ( &mLock );
1072+ res = ::PQexec ( mConn , query.toUtf8 () );
1073+
1074+ if ( res )
10741075 {
1075- if ( logError )
1076- {
1077- QgsMessageLog::logMessage ( tr ( " Erroneous query: %1 returned %2 [%3]" )
1078- .arg ( query ).arg ( errorStatus ).arg ( PQresultErrorMessage ( res ) ),
1079- tr ( " PostGIS" ) );
1080- }
1081- else
1076+ int errorStatus = PQresultStatus ( res );
1077+ if ( errorStatus != PGRES_COMMAND_OK && errorStatus != PGRES_TUPLES_OK )
10821078 {
1083- QgsDebugMsg ( QStringLiteral ( " Not logged erroneous query: %1 returned %2 [%3]" )
1084- .arg ( query ).arg ( errorStatus ).arg ( PQresultErrorMessage ( res ) ) );
1079+ if ( logError )
1080+ {
1081+ QgsMessageLog::logMessage ( tr ( " Erroneous query: %1 returned %2 [%3]" )
1082+ .arg ( query ).arg ( errorStatus ).arg ( PQresultErrorMessage ( res ) ),
1083+ tr ( " PostGIS" ) );
1084+ }
1085+ else
1086+ {
1087+ QgsDebugMsg ( QStringLiteral ( " Not logged erroneous query: %1 returned %2 [%3]" )
1088+ .arg ( query ).arg ( errorStatus ).arg ( PQresultErrorMessage ( res ) ) );
1089+ }
10851090 }
10861091 }
1087- }
1088- else if ( logError )
1089- {
1090- QgsMessageLog::logMessage ( tr ( " Query failed: %1 \n Error: no result buffer " ). arg ( query ), tr ( " PostGIS " ) );
1091- }
1092- else
1093- {
1094- QgsDebugMsg ( QStringLiteral ( " Not logged query failed: %1 \n Error: no result buffer " ). arg ( query ) );
1092+ else if ( logError )
1093+ {
1094+ QgsMessageLog::logMessage ( tr ( " Query failed: %1 \n Error: no result buffer " ). arg ( query ), tr ( " PostGIS " ) );
1095+ }
1096+ else
1097+ {
1098+ QgsDebugMsg ( QStringLiteral ( " Not logged query failed: %1 \n Error: no result buffer " ). arg ( query ) );
1099+ }
10951100 }
10961101
10971102 return res;
@@ -1194,11 +1199,15 @@ PGresult *QgsPostgresConn::PQgetResult()
11941199
11951200PGresult *QgsPostgresConn::PQprepare ( const QString &stmtName, const QString &query, int nParams, const Oid *paramTypes )
11961201{
1202+ QMutexLocker locker ( &mLock );
1203+
11971204 return ::PQprepare ( mConn , stmtName.toUtf8 (), query.toUtf8 (), nParams, paramTypes );
11981205}
11991206
12001207PGresult *QgsPostgresConn::PQexecPrepared ( const QString &stmtName, const QStringList ¶ms )
12011208{
1209+ QMutexLocker locker ( &mLock );
1210+
12021211 const char **param = new const char *[ params.size ()];
12031212 QList<QByteArray> qparam;
12041213
@@ -1222,19 +1231,25 @@ PGresult *QgsPostgresConn::PQexecPrepared( const QString &stmtName, const QStrin
12221231
12231232void QgsPostgresConn::PQfinish ()
12241233{
1234+ QMutexLocker locker ( &mLock );
1235+
12251236 Q_ASSERT ( mConn );
12261237 ::PQfinish ( mConn );
12271238 mConn = nullptr ;
12281239}
12291240
12301241int QgsPostgresConn::PQstatus () const
12311242{
1243+ QMutexLocker locker ( &mLock );
1244+
12321245 Q_ASSERT ( mConn );
12331246 return ::PQstatus ( mConn );
12341247}
12351248
12361249QString QgsPostgresConn::PQerrorMessage () const
12371250{
1251+ QMutexLocker locker ( &mLock );
1252+
12381253 Q_ASSERT ( mConn );
12391254 return QString::fromUtf8 ( ::PQerrorMessage ( mConn ) );
12401255}
0 commit comments