Skip to content
Permalink
Browse files
Fix PG error when failing to update geom & constrained field
The problem was that throwing an exception the prepared statement
was not deallocated.

Fix #45100
  • Loading branch information
elpaso authored and nyalldawson committed Nov 1, 2021
1 parent bc00191 commit adda8067548c282b65d63ecf79dceb1eb65b75b5
Showing with 15 additions and 0 deletions.
  1. +3 −0 src/providers/postgres/qgspostgresprovider.cpp
  2. +12 −0 tests/src/python/test_provider_postgres.py
@@ -3462,7 +3462,9 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma

result = conn->PQexecPrepared( QStringLiteral( "updatefeature" ), params );
if ( result.PQresultStatus() != PGRES_COMMAND_OK && result.PQresultStatus() != PGRES_TUPLES_OK )
{
throw PGException( result );
}

conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
}
@@ -3494,6 +3496,7 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma
{
pushError( tr( "PostGIS error while changing attributes: %1" ).arg( e.errorMessage() ) );
conn->rollback();
conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
returnvalue = false;
}

@@ -3199,6 +3199,18 @@ def testPkeyIntArray(self):
self.assertTrue(feat.isValid())
self.assertEqual(feat["name"], "test")

def testPreparedFailure(self):
"""Test error from issue GH #45100"""

layer = self.getEditableLayerWithCheckConstraint()
self.assertTrue(layer.startEditing())
old_value = layer.getFeature(1).attribute('i_will_fail_on_no_name')
layer.changeAttributeValue(1, 1, 'no name')
layer.changeGeometry(1, QgsGeometry.fromWkt('point(7 45)'))
self.assertFalse(layer.commitChanges())
layer.changeAttributeValue(1, 1, old_value)
self.assertTrue(layer.commitChanges())


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

0 comments on commit adda806

Please sign in to comment.