Skip to content

Commit afb1bce

Browse files
authoredJan 15, 2018
Merge pull request #5663 from pblottiere/bugfix-transaction-name
[FEATURE] Add a name for transactions created from executeSql
2 parents b8518ae + 5505202 commit afb1bce

13 files changed

+57
-18
lines changed
 

‎python/core/qgstransaction.sip

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ Commit transaction.
8484
Roll back transaction.
8585
%End
8686

87-
virtual bool executeSql( const QString &sql, QString &error /Out/, bool isDirty = false ) = 0;
87+
virtual bool executeSql( const QString &sql, QString &error /Out/, bool isDirty = false, const QString &name = QString() ) = 0;
8888
%Docstring
8989
Execute the ``sql`` string.
9090

9191
:param sql: The sql query to execute
9292
:param error: The error message
9393
:param isDirty: Flag to indicate if the underlying data will be modified
94+
:param name: Name of the transaction ( only used if `isDirty` is true)
9495

9596
:return: true if everything is OK, false otherwise
9697
%End
@@ -152,7 +153,7 @@ returns the last created savepoint
152153
Emitted after a rollback
153154
%End
154155

155-
void dirtied( const QString &sql );
156+
void dirtied( const QString &sql, const QString &name );
156157
%Docstring
157158
Emitted if a sql query is executed and the underlying data is modified
158159
%End

‎python/core/qgsvectorlayereditpassthrough.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ class QgsVectorLayerEditPassthrough : QgsVectorLayerEditBuffer
4141
virtual void rollBack();
4242

4343

44-
bool update( QgsTransaction *transaction, const QString &sql );
44+
bool update( QgsTransaction *transaction, const QString &sql, const QString &name );
4545
%Docstring
4646
Update underlying data with a SQL query embedded in a transaction.
4747

4848
:param transaction: Transaction in which the sql query has been run
4949
:param sql: The SQL query updating data
50+
:param name: The name of the undo/redo command
5051

5152
:return: true if the undo/redo command is well added to the stack, false otherwise
5253

‎python/core/qgsvectorlayerundopassthroughcommand.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,14 @@ class QgsVectorLayerUndoPassthroughCommandUpdate : QgsVectorLayerUndoPassthrough
303303
%End
304304
public:
305305

306-
QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer /Transfer/, QgsTransaction *transaction, const QString &sql );
306+
QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer /Transfer/, QgsTransaction *transaction, const QString &sql, const QString &name );
307307
%Docstring
308308
Constructor for :py:class:`QgsVectorLayerUndoCommandUpdate`
309309

310310
:param buffer: associated edit buffer
311311
:param transaction: transaction running the sql query
312312
:param sql: the query
313+
:param name: The name of the command
313314
%End
314315

315316
virtual void undo();

‎src/core/qgstransaction.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
108108
* \param sql The sql query to execute
109109
* \param error The error message
110110
* \param isDirty Flag to indicate if the underlying data will be modified
111+
* \param name Name of the transaction ( only used if `isDirty` is true)
111112
*
112113
* \returns true if everything is OK, false otherwise
113114
*/
114-
virtual bool executeSql( const QString &sql, QString &error SIP_OUT, bool isDirty = false ) = 0;
115+
virtual bool executeSql( const QString &sql, QString &error SIP_OUT, bool isDirty = false, const QString &name = QString() ) = 0;
115116

116117
/**
117118
* Checks if the provider of a given \a layer supports transactions.
@@ -167,7 +168,7 @@ class CORE_EXPORT QgsTransaction : public QObject SIP_ABSTRACT
167168
/**
168169
* Emitted if a sql query is executed and the underlying data is modified
169170
*/
170-
void dirtied( const QString &sql );
171+
void dirtied( const QString &sql, const QString &name );
171172

172173
protected:
173174
QgsTransaction( const QString &connString ) SIP_SKIP;

‎src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4591,11 +4591,11 @@ bool QgsVectorLayer::readExtentFromXml() const
45914591
return mReadExtentFromXml;
45924592
}
45934593

4594-
void QgsVectorLayer::onDirtyTransaction( const QString &sql )
4594+
void QgsVectorLayer::onDirtyTransaction( const QString &sql, const QString &name )
45954595
{
45964596
QgsTransaction *tr = dataProvider()->transaction();
45974597
if ( tr && mEditBuffer )
45984598
{
4599-
qobject_cast<QgsVectorLayerEditPassthrough *>( mEditBuffer )->update( tr, sql );
4599+
qobject_cast<QgsVectorLayerEditPassthrough *>( mEditBuffer )->update( tr, sql, name );
46004600
}
46014601
}

‎src/core/qgsvectorlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
22362236
void onFeatureDeleted( QgsFeatureId fid );
22372237
void onRelationsLoaded();
22382238
void onSymbolsCounted();
2239-
void onDirtyTransaction( const QString &sql );
2239+
void onDirtyTransaction( const QString &sql, const QString &name );
22402240

22412241
protected:
22422242
//! Set the extent

‎src/core/qgsvectorlayereditpassthrough.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void QgsVectorLayerEditPassthrough::rollBack()
112112
mModified = false;
113113
}
114114

115-
bool QgsVectorLayerEditPassthrough::update( QgsTransaction *tr, const QString &sql )
115+
bool QgsVectorLayerEditPassthrough::update( QgsTransaction *tr, const QString &sql, const QString &name )
116116
{
117-
return modify( new QgsVectorLayerUndoPassthroughCommandUpdate( this, tr, sql ) );
117+
return modify( new QgsVectorLayerUndoPassthroughCommandUpdate( this, tr, sql, name ) );
118118
}

‎src/core/qgsvectorlayereditpassthrough.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
4949
*
5050
* \param transaction Transaction in which the sql query has been run
5151
* \param sql The SQL query updating data
52+
* \param name The name of the undo/redo command
5253
*
5354
* \returns true if the undo/redo command is well added to the stack, false otherwise
5455
*
5556
* \since QGIS 3.0
5657
*/
57-
bool update( QgsTransaction *transaction, const QString &sql );
58+
bool update( QgsTransaction *transaction, const QString &sql, const QString &name );
5859

5960
private:
6061
bool mModified;

‎src/core/qgsvectorlayerundopassthroughcommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ void QgsVectorLayerUndoPassthroughCommandRenameAttribute::redo()
352352
}
353353
}
354354

355-
QgsVectorLayerUndoPassthroughCommandUpdate::QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer, QgsTransaction *transaction, const QString &sql )
356-
: QgsVectorLayerUndoPassthroughCommand( buffer, QObject::tr( "custom transaction" ), false )
355+
QgsVectorLayerUndoPassthroughCommandUpdate::QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer, QgsTransaction *transaction, const QString &sql, const QString &name )
356+
: QgsVectorLayerUndoPassthroughCommand( buffer, name.isEmpty() ? QObject::tr( "custom transaction" ) : name, false )
357357
, mTransaction( transaction )
358358
, mSql( sql )
359359
{

‎src/core/qgsvectorlayerundopassthroughcommand.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ class CORE_EXPORT QgsVectorLayerUndoPassthroughCommandUpdate : public QgsVectorL
297297
* \param buffer associated edit buffer
298298
* \param transaction transaction running the sql query
299299
* \param sql the query
300+
* \param name The name of the command
300301
*/
301-
QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer SIP_TRANSFER, QgsTransaction *transaction, const QString &sql );
302+
QgsVectorLayerUndoPassthroughCommandUpdate( QgsVectorLayerEditBuffer *buffer SIP_TRANSFER, QgsTransaction *transaction, const QString &sql, const QString &name );
302303

303304
void undo() override;
304305
void redo() override;

‎src/providers/postgres/qgspostgrestransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool QgsPostgresTransaction::rollbackTransaction( QString &error )
5757
return false;
5858
}
5959

60-
bool QgsPostgresTransaction::executeSql( const QString &sql, QString &errorMsg, bool isDirty )
60+
bool QgsPostgresTransaction::executeSql( const QString &sql, QString &errorMsg, bool isDirty, const QString &name )
6161
{
6262
if ( !mConn )
6363
{
@@ -91,7 +91,7 @@ bool QgsPostgresTransaction::executeSql( const QString &sql, QString &errorMsg,
9191
if ( isDirty )
9292
{
9393
dirtyLastSavePoint();
94-
emit dirtied( sql );
94+
emit dirtied( sql, name );
9595
}
9696

9797
QgsDebugMsg( QString( "Status %1 (OK)" ).arg( r.PQresultStatus() ) );

‎src/providers/postgres/qgspostgrestransaction.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ class QgsPostgresTransaction : public QgsTransaction
2929

3030
public:
3131
explicit QgsPostgresTransaction( const QString &connString );
32-
bool executeSql( const QString &sql, QString &error, bool isDirty = false ) override;
32+
33+
/**
34+
* Executes the SQL query in database.
35+
*
36+
* \param sql The SQL query to execute
37+
* \param error The error or an empty string if none
38+
* \param isDirty True to add an undo/redo command in the edition buffer, false otherwise
39+
* \param name Name of the operation ( only used if `isDirty` is true)
40+
*/
41+
bool executeSql( const QString &sql, QString &error, bool isDirty = false, const QString &name = QString() ) override;
42+
3343
QgsPostgresConn *connection() const { return mConn; }
3444

3545

‎tests/src/python/test_provider_postgres.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,29 @@ def testTimeout(self):
355355
for i in range(100):
356356
iterators.append(self.vl.getFeatures(request))
357357

358+
def testTransactionDirtyName(self):
359+
# create a vector ayer based on postgres
360+
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
361+
self.assertTrue(vl.isValid())
362+
363+
# prepare a project with transactions enabled
364+
p = QgsProject()
365+
p.setAutoTransaction(True)
366+
p.addMapLayers([vl])
367+
vl.startEditing()
368+
369+
# update the data within the transaction
370+
tr = vl.dataProvider().transaction()
371+
sql = "update qgis_test.some_poly_data set pk=1 where pk=1"
372+
name = "My Awesome Transaction!"
373+
self.assertTrue(tr.executeSql(sql, True, name)[0])
374+
375+
# test name
376+
self.assertEqual(vl.undoStack().command(0).text(), name)
377+
378+
# rollback
379+
vl.rollBack()
380+
358381
def testTransactionDirty(self):
359382
# create a vector layer based on postgres
360383
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')

0 commit comments

Comments
 (0)
Please sign in to comment.