39 if ( !createTransaction )
49 if ( layers.isEmpty() )
54 QString connStr = connectionString( firstLayer->
source() );
61 if ( !transaction->addLayer( layer ) )
68 return transaction.release();
74 , mTransactionActive( false )
75 , mLastSavePointIsDirty( true )
81 setLayerTransactionIds(
nullptr );
86 QString QgsTransaction::removeLayerIdOrName(
const QString &str )
90 for (
int i = 0; i < 2; i++ )
92 int pos = res.indexOf( i == 0 ? QLatin1String(
"|layername=" ) : QLatin1String(
"|layerid=" ) );
95 int end = res.indexOf(
'|', pos + 1 );
98 res = res.mid( 0, pos ) + res.mid( end );
102 res = res.mid( 0, pos );
110 QString QgsTransaction::connectionString(
const QString &layerName )
116 if ( connString.isEmpty() )
118 connString = removeLayerIdOrName( layerName );
143 QgsDebugMsg( QStringLiteral(
"Couldn't start transaction because connection string for layer %1 : '%2' does not match '%3'" ).arg(
149 connect( layer, &QgsVectorLayer::destroyed,
this, &QgsTransaction::onLayerDeleted );
150 mLayers.insert( layer );
152 if ( mTransactionActive )
160 if ( mTransactionActive )
164 if ( !beginTransaction( errorMsg, statementTimeout ) )
167 setLayerTransactionIds(
this );
168 mTransactionActive =
true;
175 if ( !mTransactionActive )
178 if ( !commitTransaction( errorMsg ) )
181 setLayerTransactionIds(
nullptr );
182 mTransactionActive =
false;
189 if ( !mTransactionActive )
192 if ( !rollbackTransaction( errorMsg ) )
195 setLayerTransactionIds(
nullptr );
196 mTransactionActive =
false;
213 void QgsTransaction::onLayerDeleted()
215 mLayers.remove( static_cast<QgsVectorLayer *>( sender() ) );
218 void QgsTransaction::setLayerTransactionIds(
QgsTransaction *transaction )
231 if ( !mTransactionActive )
234 if ( !mLastSavePointIsDirty && !mSavepoints.isEmpty() )
235 return mSavepoints.top();
237 const QString name( QUuid::createUuid().toString() );
245 mSavepoints.push( name );
246 mLastSavePointIsDirty =
false;
252 if ( !mTransactionActive )
261 mSavepoints.push( savePointId );
262 mLastSavePointIsDirty =
false;
268 if ( !mTransactionActive )
271 const int idx = mSavepoints.indexOf( name );
276 mSavepoints.resize( idx );
277 mLastSavePointIsDirty =
false;
283 mLastSavePointIsDirty =
true;
static QgsTransaction * create(const QString &connString, const QString &providerKey)
Create a transaction for the specified connection string connString and provider with providerKey...
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QgsTransaction * createTransaction_t(const QString &connString)
QString source() const
Returns the source for the layer.
bool rollbackToSavepoint(const QString &name, QString &error)
rollback to save point, the save point is maintained and is "undertied"
bool commit(QString &errorMsg)
Commit transaction.
virtual bool executeSql(const QString &sql, QString &error, bool isDirty=false, const QString &name=QString())=0
Execute the sql string.
virtual QString name() const =0
Returns a provider name.
void dataChanged()
This is emitted whenever an asynchronous operation has finished and the data should be redrawn...
QString connectionInfo(bool expandAuthConfig=true) const
Returns connection part of URI.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
~QgsTransaction() override
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
static bool supportsTransaction(const QgsVectorLayer *layer)
Checks if the provider of a given layer supports transactions.
void afterRollback()
Emitted after a rollback.
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
QgsTransaction(const QString &connString)
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
bool addLayer(QgsVectorLayer *layer)
Add the layer to the transaction.
bool rollback(QString &errorMsg)
Roll back transaction.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider.
Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
QString createSavepoint(QString &error)
creates a save point returns empty string on error returns the last created savepoint if it's not dir...
Represents a vector layer which manages a vector based data sets.
void dirtyLastSavePoint()
dirty save point such that next call to createSavepoint will create a new one
bool begin(QString &errorMsg, int statementTimeout=20)
Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to...