Skip to content
Permalink
Browse files

Merge pull request #5628 from pblottiere/bugfix-transaction-tuple

executeSql in transaction does not return an error on tuple
  • Loading branch information
pblottiere committed Nov 16, 2017
2 parents 15c32c2 + c6b054e commit 898eb4abf4443ecc38b21e64f353deee395f5015
@@ -92,8 +92,7 @@ class QgsTransaction : QObject /Abstract/

virtual bool executeSql( const QString &sql, QString &error /Out/, bool isDirty = false ) = 0;
%Docstring
Execute the ``sql`` string. The result must not be a tuple, so running a
``SELECT`` query will return an error.
Execute the ``sql`` string.

\param sql The sql query to execute
\param error The error message
@@ -103,8 +103,7 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
bool rollback( QString &errorMsg SIP_OUT );

/**
* Execute the \a sql string. The result must not be a tuple, so running a
* ``SELECT`` query will return an error.
* Execute the \a sql string.
*
* \param sql The sql query to execute
* \param error The error message
@@ -74,7 +74,8 @@ bool QgsPostgresTransaction::executeSql( const QString &sql, QString &errorMsg,
mConn->lock();
QgsPostgresResult r( mConn->PQexec( sql, true ) );
mConn->unlock();
if ( r.PQresultStatus() != PGRES_COMMAND_OK )
if ( r.PQresultStatus() == PGRES_BAD_RESPONSE ||
r.PQresultStatus() == PGRES_FATAL_ERROR )
{
errorMsg = QStringLiteral( "Status %1 (%2)" ).arg( r.PQresultStatus() ).arg( r.PQresultErrorMessage() );
QgsDebugMsg( errorMsg );
@@ -351,8 +351,8 @@ def testTimeout(self):
for i in range(100):
iterators.append(self.vl.getFeatures(request))

def testTransactionNotDirty(self):
# create a vector ayer based on postgres
def testTransactionDirty(self):
# create a vector layer based on postgres
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
self.assertTrue(vl.isValid())

@@ -397,7 +397,24 @@ def testTransactionNotDirty(self):
ft1 = vl.getFeatures('pk=1')
self.assertFalse(ft1.nextFeature(f))

p.setAutoTransaction(False)
def testTransactionTuple(self):
# create a vector layer based on postgres
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
self.assertTrue(vl.isValid())

# prepare a project with transactions enabled
p = QgsProject()
p.setAutoTransaction(True)
p.addMapLayers([vl])
vl.startEditing()

# execute a query which returns a tuple
tr = vl.dataProvider().transaction()
sql = "select * from qgis_test.some_poly_data"
self.assertTrue(tr.executeSql(sql, False)[0])

# underlying data has not been modified
self.assertFalse(vl.isModified())

def testDomainTypes(self):
"""Test that domain types are correctly mapped"""

0 comments on commit 898eb4a

Please sign in to comment.
You can’t perform that action at this time.