Skip to content

Commit 53b6366

Browse files
committed
postgres provider: fix handling of broken connections
1 parent 4bb662e commit 53b6366

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/providers/postgres/qgspostgresconn.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ QgsPostgresResult &QgsPostgresResult::operator=( const QgsPostgresResult & src )
5757

5858
ExecStatusType QgsPostgresResult::PQresultStatus()
5959
{
60-
Q_ASSERT( mRes );
61-
return ::PQresultStatus( mRes );
60+
return mRes ? ::PQresultStatus( mRes ) : PGRES_FATAL_ERROR;
6261
}
6362

6463
QString QgsPostgresResult::PQresultErrorMessage()
6564
{
66-
Q_ASSERT( mRes );
67-
return QString::fromUtf8( ::PQresultErrorMessage( mRes ) );
65+
return mRes ? QString::fromUtf8( ::PQresultErrorMessage( mRes ) ) : QObject::tr( "no result buffer" );
6866
}
6967

7068
int QgsPostgresResult::PQntuples()

src/providers/postgres/qgspostgresprovider.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
19331933

19341934
QgsDebugMsg( QString( "prepare addfeatures: %1" ).arg( insert ) );
19351935
QgsPostgresResult stmt = mConnectionRW->PQprepare( "addfeatures", insert, fieldId.size() + offset - 1, NULL );
1936-
if ( stmt.PQresultStatus() == PGRES_FATAL_ERROR )
1936+
if ( stmt.PQresultStatus() != PGRES_COMMAND_OK )
19371937
throw PGException( stmt );
19381938

19391939
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
@@ -2047,7 +2047,7 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds & id )
20472047

20482048
//send DELETE statement and do error handling
20492049
QgsPostgresResult result = mConnectionRW->PQexec( sql );
2050-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2050+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
20512051
throw PGException( result );
20522052

20532053
QVariant v = mFidToKey[ *it ];
@@ -2105,7 +2105,7 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes )
21052105

21062106
//send sql statement and do error handling
21072107
QgsPostgresResult result = mConnectionRW->PQexec( sql );
2108-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2108+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
21092109
throw PGException( result );
21102110

21112111
if ( !iter->comment().isEmpty() )
@@ -2115,7 +2115,7 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes )
21152115
.arg( quotedIdentifier( iter->name() ) )
21162116
.arg( quotedValue( iter->comment() ) );
21172117
result = mConnectionRW->PQexec( sql );
2118-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2118+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
21192119
throw PGException( result );
21202120
}
21212121
}
@@ -2160,7 +2160,7 @@ bool QgsPostgresProvider::deleteAttributes( const QgsAttributeIds& ids )
21602160

21612161
//send sql statement and do error handling
21622162
QgsPostgresResult result = mConnectionRW->PQexec( sql );
2163-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2163+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
21642164
throw PGException( result );
21652165

21662166
//delete the attribute from mAttributeFields
@@ -2246,7 +2246,7 @@ bool QgsPostgresProvider::changeAttributeValues( const QgsChangedAttributesMap &
22462246
sql += QString( " WHERE %1" ).arg( whereClause( fid ) );
22472247

22482248
QgsPostgresResult result = mConnectionRW->PQexec( sql );
2249-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2249+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
22502250
throw PGException( result );
22512251

22522252
// update feature id map if key was changed
@@ -2328,7 +2328,7 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
23282328
QgsDebugMsg( "updating: " + update );
23292329

23302330
QgsPostgresResult result = mConnectionRW->PQprepare( "updatefeatures", update, 2, NULL );
2331-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2331+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
23322332
throw PGException( result );
23332333

23342334
for ( QgsGeometryMap::iterator iter = geometry_map.begin();
@@ -2350,7 +2350,7 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
23502350
appendPkParams( iter.key(), params );
23512351

23522352
result = mConnectionRW->PQexecPrepared( "updatefeatures", params );
2353-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2353+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
23542354
throw PGException( result );
23552355
} // for each feature
23562356

@@ -2398,7 +2398,7 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL, bool updateFeatureCou
23982398
sql += " LIMIT 0";
23992399

24002400
QgsPostgresResult res = mConnectionRO->PQexec( sql );
2401-
if ( res.PQresultStatus() != PGRES_COMMAND_OK && res.PQresultStatus() != PGRES_TUPLES_OK )
2401+
if ( res.PQresultStatus() != PGRES_TUPLES_OK )
24022402
{
24032403
pushError( res.PQresultErrorMessage() );
24042404
mSqlWhereClause = prevWhere;
@@ -2936,7 +2936,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29362936
.arg( quotedValue( schemaName ) );
29372937

29382938
QgsPostgresResult result = conn->PQexec( sql );
2939-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2939+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
29402940
throw PGException( result );
29412941

29422942
bool exists = result.PQntuples() > 0;
@@ -2952,7 +2952,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29522952
.arg( quotedValue( tableName ) );
29532953

29542954
result = conn->PQexec( sql );
2955-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2955+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
29562956
throw PGException( result );
29572957
}
29582958

@@ -2962,7 +2962,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29622962
.arg( primaryKeyType );
29632963

29642964
result = conn->PQexec( sql );
2965-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2965+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
29662966
throw PGException( result );
29672967

29682968
// get geometry type, dim and srid
@@ -2983,7 +2983,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29832983
.arg( dim );
29842984

29852985
result = conn->PQexec( sql );
2986-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
2986+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
29872987
throw PGException( result );
29882988
}
29892989
else

0 commit comments

Comments
 (0)