29#include "moc_qgstransaction.cpp"
38 if ( layers.isEmpty() )
50 if ( !transaction->addLayer( layer,
false ) )
57 return transaction.release();
68 setLayerTransactionIds(
nullptr );
78QString QgsTransaction::cleanupConnectionString(
const QString &str )
82 static const QStringList toRemove
84 { QStringLiteral(
"|layername=" )},
85 { QStringLiteral(
"|layerid=" )},
86 { QStringLiteral(
"|subset=" )},
89 for (
const auto &strToRm : std::as_const( toRemove ) )
91 const int pos = res.indexOf( strToRm );
94 const int end = res.indexOf(
'|', pos + 1 );
97 res = res.mid( 0, pos ) + res.mid( end );
101 res = res.mid( 0, pos );
111 QString connString = QgsDataSourceUri( layerUri ).connectionInfo(
false );
115 if ( connString.isEmpty() )
117 connString = cleanupConnectionString( layerUri );
128 if ( ! addLayersInEditMode
143 QgsDebugError( 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 );
164 if ( !beginTransaction( errorMsg, statementTimeout ) )
167 setLayerTransactionIds(
this );
178 if ( !commitTransaction( errorMsg ) )
181 setLayerTransactionIds(
nullptr );
192 if ( !rollbackTransaction( errorMsg ) )
195 setLayerTransactionIds(
nullptr );
213void QgsTransaction::onLayerDeleted()
218void QgsTransaction::setLayerTransactionIds(
QgsTransaction *transaction )
220 const auto constMLayers = mLayers;
221 for ( QgsVectorLayer *vl : constMLayers )
223 if ( vl->dataProvider() )
225 vl->dataProvider()->setTransaction( transaction );
240 const QString name( QStringLiteral(
"qgis" ) + ( QUuid::createUuid().toString().mid( 1, 24 ).replace(
'-', QString() ) ) );
@ TransactionSupport
Supports transactions.
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
QString source() const
Returns the source for the layer.
QString providerType() const
Returns the provider type (provider key) for this layer.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
QgsTransaction * createTransaction(const QString &providerKey, const QString &connString)
Returns new instance of transaction.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Allows creation of a multi-layer database-side transaction.
QStack< QString > mSavepoints
void afterRollbackToSavepoint(const QString &savepointName)
Emitted after a rollback to savepoint.
bool mLastSavePointIsDirty
void afterRollback()
Emitted after a rollback.
bool rollback(QString &errorMsg)
Roll back transaction.
bool addLayer(QgsVectorLayer *layer, bool addLayersInEditMode=false)
Add the layer to the transaction.
bool begin(QString &errorMsg, int statementTimeout=20)
Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to...
static bool supportsTransaction(const QgsVectorLayer *layer)
Checks if the provider of a given layer supports transactions.
virtual bool rollbackToSavepoint(const QString &name, QString &error)
rollback to save point, the save point is maintained and is "undertied"
virtual bool executeSql(const QString &sql, QString &error, bool isDirty=false, const QString &name=QString())=0
Execute the sql string.
QgsTransaction(const QString &connString)
QString connectionString() const
Returns the connection string of the transaction.
bool commit(QString &errorMsg)
Commit transaction.
~QgsTransaction() override
static QgsTransaction * create(const QString &connString, const QString &providerKey)
Create a transaction for the specified connection string connString and provider with providerKey.
QString createSavepoint(QString &error)
creates a save point returns empty string on error returns the last created savepoint if it's not dir...
void dirtyLastSavePoint()
dirty save point such that next call to createSavepoint will create a new one
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
Represents a vector layer which manages a vector based dataset.
bool isEditable() const final
Returns true if the provider is in editing mode.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
#define QgsDebugError(str)