Skip to content

Commit

Permalink
Signal transaction rollback and some refactoring of transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 1, 2015
1 parent d388a4f commit ca4d12a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
24 changes: 12 additions & 12 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,47 +397,47 @@ ADD_FLEX_FILES(QGIS_CORE_SRCS qgsexpressionlexer.ll)
ADD_BISON_FILES(QGIS_CORE_SRCS qgsexpressionparser.yy)

SET(QGIS_CORE_MOC_HDRS

qgsapplication.h
qgsbrowsermodel.h
qgscontexthelp.h
qgscoordinatetransform.h
qgscredentials.h
qgsdataitem.h
qgsdataprovider.h
qgseditformconfig.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
qgsmaplayer.h
qgsmaplayerlegend.h
qgsmaplayerregistry.h
qgsmaplayerstylemanager.h
qgsmaprenderer.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h
qgsmaprenderer.h
qgsmaprendererjob.h
qgsmaprendererparalleljob.h
qgsmaprenderersequentialjob.h
qgsmessageoutput.h
qgsmessagelog.h
qgsnetworkreplyparser.h
qgsmessageoutput.h
qgsnetworkaccessmanager.h
qgsnetworkcontentfetcher.h
qgsnetworkreplyparser.h
qgsofflineediting.h
qgscredentials.h
qgspluginlayer.h
qgspointlocator.h
qgsproject.h
qgsrunprocess.h
qgsrelationmanager.h
qgsrunprocess.h
qgssnappingutils.h
qgsvectorlayer.h
qgsvectorlayereditpassthrough.h
qgsvectorlayereditbuffer.h
qgsnetworkaccessmanager.h
qgstransaction.h
qgsvectordataprovider.h
qgsvectorlayercache.h
qgsvectorlayereditbuffer.h
qgsvectorlayereditpassthrough.h
qgsvectorlayer.h
qgsvectorlayerjoinbuffer.h
qgsvisibilitypresetcollection.h
qgsgeometryvalidator.h
qgswebview.h

auth/qgsauthmanager.h
Expand Down Expand Up @@ -632,7 +632,7 @@ SET(QGIS_CORE_HDRS
qgsstatisticalsummary.h
qgsstringutils.h
qgstolerance.h
qgstransaction.h

qgsvectordataprovider.h
qgsvectorlayercache.h
qgsvectorfilewriter.h
Expand Down
40 changes: 30 additions & 10 deletions src/core/qgstransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ bool QgsTransaction::addLayer( const QString& layerId )
}

QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
return addLayer( layer );
}

bool QgsTransaction::addLayer( QgsVectorLayer* layer )
{
if ( mTransactionActive )
{
return false;
}

if ( !layer )
{
return false;
Expand All @@ -124,11 +134,13 @@ bool QgsTransaction::addLayer( const QString& layerId )
if ( QgsDataSourceURI( layer->source() ).connectionInfo() != mConnString )
{
QgsDebugMsg( QString( "Couldn't start transaction because connection string for layer %1 : '%2' does not match '%3'" ).arg(
layerId, QgsDataSourceURI( layer->source() ).connectionInfo(), mConnString ) );
layer->id(), QgsDataSourceURI( layer->source() ).connectionInfo(), mConnString ) );
return false;
}

mLayers.insert( layerId );
connect( this, SIGNAL( afterRollback() ), layer->dataProvider(), SIGNAL( dataChanged() ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( onLayersDeleted( QStringList ) ) );
mLayers.insert( layer );
return true;
}

Expand Down Expand Up @@ -157,9 +169,8 @@ bool QgsTransaction::commit( QString& errorMsg )
return false;
}

Q_FOREACH ( const QString& layerid, mLayers )
Q_FOREACH ( QgsVectorLayer* l, mLayers )
{
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer( layerid );
if ( !l || l->isEditable() )
{
return false;
Expand All @@ -183,10 +194,9 @@ bool QgsTransaction::rollback( QString& errorMsg )
return false;
}

Q_FOREACH ( const QString& layerid, mLayers )
Q_FOREACH ( QgsVectorLayer* l, mLayers )
{
QgsMapLayer* l = QgsMapLayerRegistry::instance()->mapLayer( layerid );
if ( !l || l->isEditable() )
if ( l->isEditable() )
{
return false;
}
Expand All @@ -199,15 +209,25 @@ bool QgsTransaction::rollback( QString& errorMsg )

setLayerTransactionIds( 0 );
mTransactionActive = false;

emit afterRollback();

return true;
}

void QgsTransaction::onLayersDeleted( const QStringList& layerids )
{
Q_FOREACH ( QString layerid, layerids )
Q_FOREACH ( QgsVectorLayer* l, mLayers )
if ( l->id() == layerid )
mLayers.remove( l );
}

void QgsTransaction::setLayerTransactionIds( QgsTransaction* transaction )
{
Q_FOREACH ( const QString& layerid, mLayers )
Q_FOREACH ( QgsVectorLayer* vl, mLayers )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerid ) );
if ( vl && vl->dataProvider() )
if ( vl->dataProvider() )
{
vl->dataProvider()->setTransaction( transaction );
}
Expand Down
16 changes: 14 additions & 2 deletions src/core/qgstransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QString>

class QgsVectorDataProvider;
class QgsVectorLayer;

/**
* This class allows to include a set of layers in a database-side transaction,
Expand All @@ -44,8 +45,10 @@ class QgsVectorDataProvider;
*
* Edits on features can get rejected if another conflicting transaction is active.
*/
class CORE_EXPORT QgsTransaction
class CORE_EXPORT QgsTransaction : public QObject
{
Q_OBJECT

public:
/** Creates a transaction for the specified connection string and provider */
static QgsTransaction* create( const QString& connString, const QString& providerKey );
Expand All @@ -59,6 +62,9 @@ class CORE_EXPORT QgsTransaction
/** Add layer to the transaction. The layer must not be in edit mode. The transaction must not be active. */
bool addLayer( const QString& layerId );

/** Add layer to the transaction. The layer must not be in edit mode. The transaction must not be active. */
bool addLayer( QgsVectorLayer* layer );

/** Begin transaction
* The statement timeout, in seconds, specifies how long an sql statement
* is allowed to block QGIS before it is aborted. Statements can block,
Expand All @@ -78,6 +84,12 @@ class CORE_EXPORT QgsTransaction
/** Executes sql */
virtual bool executeSql( const QString& sql, QString& error ) = 0;

signals:
void afterRollback();

private slots:
void onLayersDeleted( const QStringList& layerids );

protected:
QgsTransaction( const QString& connString );

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

bool mTransactionActive;
QSet<QString> mLayers;
QSet<QgsVectorLayer*> mLayers;

void setLayerTransactionIds( QgsTransaction *transaction );

Expand Down
14 changes: 8 additions & 6 deletions src/providers/postgres/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ SET(PG_SRCS
qgscolumntypethread.cpp
qgspostgresexpressioncompiler.cpp
)

SET(PG_MOC_HDRS
qgspostgresprovider.h
qgscolumntypethread.h
qgspgnewconnection.h
qgspgsourceselect.h
qgspgtablemodel.h
qgspostgresconn.h
qgspostgresconnpool.h
qgspostgresdataitems.h
qgspgsourceselect.h
qgspgnewconnection.h
qgspgtablemodel.h
qgscolumntypethread.h
qgspostgresprovider.h
qgspostgrestransaction.h

)

SET(PG_HDRS
qgspostgrestransaction.h
qgspostgresexpressioncompiler.h
)

Expand Down
2 changes: 2 additions & 0 deletions src/providers/postgres/qgspostgrestransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class QgsPostgresConn;

class QgsPostgresTransaction : public QgsTransaction
{
Q_OBJECT

public:
explicit QgsPostgresTransaction( const QString& connString );
bool executeSql( const QString& sql, QString& error ) override;
Expand Down

0 comments on commit ca4d12a

Please sign in to comment.