34 if ( layers.isEmpty() )
39 QString connStr = connectionString( firstLayer->
source() );
46 if ( !transaction->addLayer( layer ) )
53 return transaction.release();
59 , mTransactionActive( false )
60 , mLastSavePointIsDirty( true )
66 setLayerTransactionIds(
nullptr );
71 QString QgsTransaction::removeLayerIdOrName(
const QString &str )
75 for (
int i = 0; i < 2; i++ )
77 int pos = res.indexOf( i == 0 ? QLatin1String(
"|layername=" ) : QLatin1String(
"|layerid=" ) );
80 int end = res.indexOf(
'|', pos + 1 );
83 res = res.mid( 0, pos ) + res.mid( end );
87 res = res.mid( 0, pos );
95 QString QgsTransaction::connectionString(
const QString &layerName )
101 if ( connString.isEmpty() )
103 connString = removeLayerIdOrName( layerName );
128 QgsDebugMsg( QStringLiteral(
"Couldn't start transaction because connection string for layer %1 : '%2' does not match '%3'" ).arg(
134 connect( layer, &QgsVectorLayer::destroyed,
this, &QgsTransaction::onLayerDeleted );
135 mLayers.insert( layer );
137 if ( mTransactionActive )
145 if ( mTransactionActive )
149 if ( !beginTransaction( errorMsg, statementTimeout ) )
152 setLayerTransactionIds(
this );
153 mTransactionActive =
true;
160 if ( !mTransactionActive )
163 if ( !commitTransaction( errorMsg ) )
166 setLayerTransactionIds(
nullptr );
167 mTransactionActive =
false;
174 if ( !mTransactionActive )
177 if ( !rollbackTransaction( errorMsg ) )
180 setLayerTransactionIds(
nullptr );
181 mTransactionActive =
false;
198 void QgsTransaction::onLayerDeleted()
200 mLayers.remove( static_cast<QgsVectorLayer *>( sender() ) );
203 void QgsTransaction::setLayerTransactionIds(
QgsTransaction *transaction )
205 const auto constMLayers = mLayers;
208 if ( vl->dataProvider() )
210 vl->dataProvider()->setTransaction( transaction );
217 if ( !mTransactionActive )
220 if ( !mLastSavePointIsDirty && !mSavepoints.isEmpty() )
221 return mSavepoints.top();
223 const QString name( QStringLiteral(
"qgis" ) + ( QUuid::createUuid().toString().mid( 1, 24 ).replace(
'-', QString() ) ) );
231 mSavepoints.push( name );
232 mLastSavePointIsDirty =
false;
238 if ( !mTransactionActive )
247 mSavepoints.push( savePointId );
248 mLastSavePointIsDirty =
false;
254 if ( !mTransactionActive )
257 const int idx = mSavepoints.indexOf( name );
262 mSavepoints.resize( idx );
263 mLastSavePointIsDirty =
false;
269 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)
QString providerType() const
Returns the provider type (provider key) for this layer.
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.
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 * createTransaction(const QString &providerKey, const QString &connString)
Returns new instance of transaction.
~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...