Skip to content

Commit ca4d12a

Browse files
committed
Signal transaction rollback and some refactoring of transactions
1 parent d388a4f commit ca4d12a

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

src/core/CMakeLists.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -397,47 +397,47 @@ ADD_FLEX_FILES(QGIS_CORE_SRCS qgsexpressionlexer.ll)
397397
ADD_BISON_FILES(QGIS_CORE_SRCS qgsexpressionparser.yy)
398398

399399
SET(QGIS_CORE_MOC_HDRS
400-
401400
qgsapplication.h
402401
qgsbrowsermodel.h
403402
qgscontexthelp.h
404403
qgscoordinatetransform.h
404+
qgscredentials.h
405405
qgsdataitem.h
406406
qgsdataprovider.h
407407
qgseditformconfig.h
408+
qgsgeometryvalidator.h
408409
qgsgml.h
409410
qgsgmlschema.h
410411
qgsmaplayer.h
411412
qgsmaplayerlegend.h
412413
qgsmaplayerregistry.h
413414
qgsmaplayerstylemanager.h
414-
qgsmaprenderer.h
415415
qgsmaprenderercache.h
416416
qgsmaprenderercustompainterjob.h
417+
qgsmaprenderer.h
417418
qgsmaprendererjob.h
418419
qgsmaprendererparalleljob.h
419420
qgsmaprenderersequentialjob.h
420-
qgsmessageoutput.h
421421
qgsmessagelog.h
422-
qgsnetworkreplyparser.h
422+
qgsmessageoutput.h
423+
qgsnetworkaccessmanager.h
423424
qgsnetworkcontentfetcher.h
425+
qgsnetworkreplyparser.h
424426
qgsofflineediting.h
425-
qgscredentials.h
426427
qgspluginlayer.h
427428
qgspointlocator.h
428429
qgsproject.h
429-
qgsrunprocess.h
430430
qgsrelationmanager.h
431+
qgsrunprocess.h
431432
qgssnappingutils.h
432-
qgsvectorlayer.h
433-
qgsvectorlayereditpassthrough.h
434-
qgsvectorlayereditbuffer.h
435-
qgsnetworkaccessmanager.h
433+
qgstransaction.h
436434
qgsvectordataprovider.h
437435
qgsvectorlayercache.h
436+
qgsvectorlayereditbuffer.h
437+
qgsvectorlayereditpassthrough.h
438+
qgsvectorlayer.h
438439
qgsvectorlayerjoinbuffer.h
439440
qgsvisibilitypresetcollection.h
440-
qgsgeometryvalidator.h
441441
qgswebview.h
442442

443443
auth/qgsauthmanager.h
@@ -632,7 +632,7 @@ SET(QGIS_CORE_HDRS
632632
qgsstatisticalsummary.h
633633
qgsstringutils.h
634634
qgstolerance.h
635-
qgstransaction.h
635+
636636
qgsvectordataprovider.h
637637
qgsvectorlayercache.h
638638
qgsvectorfilewriter.h

src/core/qgstransaction.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ bool QgsTransaction::addLayer( const QString& layerId )
9999
}
100100

101101
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
102+
return addLayer( layer );
103+
}
104+
105+
bool QgsTransaction::addLayer( QgsVectorLayer* layer )
106+
{
107+
if ( mTransactionActive )
108+
{
109+
return false;
110+
}
111+
102112
if ( !layer )
103113
{
104114
return false;
@@ -124,11 +134,13 @@ bool QgsTransaction::addLayer( const QString& layerId )
124134
if ( QgsDataSourceURI( layer->source() ).connectionInfo() != mConnString )
125135
{
126136
QgsDebugMsg( QString( "Couldn't start transaction because connection string for layer %1 : '%2' does not match '%3'" ).arg(
127-
layerId, QgsDataSourceURI( layer->source() ).connectionInfo(), mConnString ) );
137+
layer->id(), QgsDataSourceURI( layer->source() ).connectionInfo(), mConnString ) );
128138
return false;
129139
}
130140

131-
mLayers.insert( layerId );
141+
connect( this, SIGNAL( afterRollback() ), layer->dataProvider(), SIGNAL( dataChanged() ) );
142+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( onLayersDeleted( QStringList ) ) );
143+
mLayers.insert( layer );
132144
return true;
133145
}
134146

@@ -157,9 +169,8 @@ bool QgsTransaction::commit( QString& errorMsg )
157169
return false;
158170
}
159171

160-
Q_FOREACH ( const QString& layerid, mLayers )
172+
Q_FOREACH ( QgsVectorLayer* l, mLayers )
161173
{
162-
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer( layerid );
163174
if ( !l || l->isEditable() )
164175
{
165176
return false;
@@ -183,10 +194,9 @@ bool QgsTransaction::rollback( QString& errorMsg )
183194
return false;
184195
}
185196

186-
Q_FOREACH ( const QString& layerid, mLayers )
197+
Q_FOREACH ( QgsVectorLayer* l, mLayers )
187198
{
188-
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer( layerid );
189-
if ( !l || l->isEditable() )
199+
if ( l->isEditable() )
190200
{
191201
return false;
192202
}
@@ -199,15 +209,25 @@ bool QgsTransaction::rollback( QString& errorMsg )
199209

200210
setLayerTransactionIds( 0 );
201211
mTransactionActive = false;
212+
213+
emit afterRollback();
214+
202215
return true;
203216
}
204217

218+
void QgsTransaction::onLayersDeleted( const QStringList& layerids )
219+
{
220+
Q_FOREACH ( QString layerid, layerids )
221+
Q_FOREACH ( QgsVectorLayer* l, mLayers )
222+
if ( l->id() == layerid )
223+
mLayers.remove( l );
224+
}
225+
205226
void QgsTransaction::setLayerTransactionIds( QgsTransaction* transaction )
206227
{
207-
Q_FOREACH ( const QString& layerid, mLayers )
228+
Q_FOREACH ( QgsVectorLayer* vl, mLayers )
208229
{
209-
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerid ) );
210-
if ( vl && vl->dataProvider() )
230+
if ( vl->dataProvider() )
211231
{
212232
vl->dataProvider()->setTransaction( transaction );
213233
}

src/core/qgstransaction.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QString>
2323

2424
class QgsVectorDataProvider;
25+
class QgsVectorLayer;
2526

2627
/**
2728
* This class allows to include a set of layers in a database-side transaction,
@@ -44,8 +45,10 @@ class QgsVectorDataProvider;
4445
*
4546
* Edits on features can get rejected if another conflicting transaction is active.
4647
*/
47-
class CORE_EXPORT QgsTransaction
48+
class CORE_EXPORT QgsTransaction : public QObject
4849
{
50+
Q_OBJECT
51+
4952
public:
5053
/** Creates a transaction for the specified connection string and provider */
5154
static QgsTransaction* create( const QString& connString, const QString& providerKey );
@@ -59,6 +62,9 @@ class CORE_EXPORT QgsTransaction
5962
/** Add layer to the transaction. The layer must not be in edit mode. The transaction must not be active. */
6063
bool addLayer( const QString& layerId );
6164

65+
/** Add layer to the transaction. The layer must not be in edit mode. The transaction must not be active. */
66+
bool addLayer( QgsVectorLayer* layer );
67+
6268
/** Begin transaction
6369
* The statement timeout, in seconds, specifies how long an sql statement
6470
* is allowed to block QGIS before it is aborted. Statements can block,
@@ -78,6 +84,12 @@ class CORE_EXPORT QgsTransaction
7884
/** Executes sql */
7985
virtual bool executeSql( const QString& sql, QString& error ) = 0;
8086

87+
signals:
88+
void afterRollback();
89+
90+
private slots:
91+
void onLayersDeleted( const QStringList& layerids );
92+
8193
protected:
8294
QgsTransaction( const QString& connString );
8395

@@ -88,7 +100,7 @@ class CORE_EXPORT QgsTransaction
88100
const QgsTransaction& operator=( const QgsTransaction& other );
89101

90102
bool mTransactionActive;
91-
QSet<QString> mLayers;
103+
QSet<QgsVectorLayer*> mLayers;
92104

93105
void setLayerTransactionIds( QgsTransaction *transaction );
94106

src/providers/postgres/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ SET(PG_SRCS
1515
qgscolumntypethread.cpp
1616
qgspostgresexpressioncompiler.cpp
1717
)
18+
1819
SET(PG_MOC_HDRS
19-
qgspostgresprovider.h
20+
qgscolumntypethread.h
21+
qgspgnewconnection.h
22+
qgspgsourceselect.h
23+
qgspgtablemodel.h
2024
qgspostgresconn.h
2125
qgspostgresconnpool.h
2226
qgspostgresdataitems.h
23-
qgspgsourceselect.h
24-
qgspgnewconnection.h
25-
qgspgtablemodel.h
26-
qgscolumntypethread.h
27+
qgspostgresprovider.h
28+
qgspostgrestransaction.h
29+
2730
)
2831

2932
SET(PG_HDRS
30-
qgspostgrestransaction.h
3133
qgspostgresexpressioncompiler.h
3234
)
3335

src/providers/postgres/qgspostgrestransaction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class QgsPostgresConn;
2424

2525
class QgsPostgresTransaction : public QgsTransaction
2626
{
27+
Q_OBJECT
28+
2729
public:
2830
explicit QgsPostgresTransaction( const QString& connString );
2931
bool executeSql( const QString& sql, QString& error ) override;

0 commit comments

Comments
 (0)