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 )
220 const auto constMLayers = mLayers;
223 if ( vl->dataProvider() )
225 vl->dataProvider()->setTransaction( transaction );
232 if ( !mTransactionActive )
235 if ( !mLastSavePointIsDirty && !mSavepoints.isEmpty() )
236 return mSavepoints.top();
238 const QString name( QUuid::createUuid().toString() );
246 mSavepoints.push( name );
247 mLastSavePointIsDirty =
false;
253 if ( !mTransactionActive )
262 mSavepoints.push( savePointId );
263 mLastSavePointIsDirty =
false;
269 if ( !mTransactionActive )
272 const int idx = mSavepoints.indexOf( name );
277 mSavepoints.resize( idx );
278 mLastSavePointIsDirty =
false;
284 mLastSavePointIsDirty =
true;
static QgsTransaction * create(const QString &connString, const QString &providerKey)
Create a transaction for the specified connection string connString and provider with providerKey...
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QgsTransaction * createTransaction_t(const QString &connString)
bool rollbackToSavepoint(const QString &name, QString &error)
rollback to save point, the save point is maintained and is "undertied"
QString connectionInfo(bool expandAuthConfig=true) const
Returns the connection part of the URI.
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()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
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.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
~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).
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
static bool supportsTransaction(const QgsVectorLayer *layer)
Checks if the provider of a given layer supports transactions.
void afterRollback()
Emitted after a rollback.
QgsTransaction(const QString &connString)
QString source() const
Returns the source for the layer.
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, it may be nullptr.
Class for storing the component parts of a RDBMS data source URI (e.g.
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...