Skip to content

Commit

Permalink
Merge pull request #5628 from pblottiere/bugfix-transaction-tuple
Browse files Browse the repository at this point in the history
executeSql in transaction does not return an error on tuple
  • Loading branch information
pblottiere committed Nov 16, 2017
2 parents 15c32c2 + c6b054e commit 898eb4a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 1 addition & 2 deletions python/core/qgstransaction.sip
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgstransaction.h
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/providers/postgres/qgspostgrestransaction.cpp
Expand Up @@ -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 );
Expand Down
23 changes: 20 additions & 3 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -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())

Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit 898eb4a

Please sign in to comment.