QgsTransaction class
This class allows including a set of layers in a database-side transaction, provided the layer data providers support transactions and are compatible with each other.
Contents
- Reference
Only layers which are not in edit mode can be included in a transaction, and all layers need to be in read-only mode for a transaction to be committed or rolled back.
Layers can only be included in one transaction at a time.
When editing layers which are part of a transaction group, all changes are sent directly to the data provider (bypassing the undo/redo stack), and the changes can either be committed or rolled back on the database side via the QgsTransaction::
As long as the transaction is active, the state of all layer features reflects the current state in the transaction.
Edits on features can get rejected if another conflicting transaction is active.
Public static functions
- static auto create(const QString& connString, const QString& providerKey) -> QgsTransaction*
- Create a transaction for the specified connection string connString and provider with providerKey.
- static auto create(const QSet<QgsVectorLayer*>& layers) -> QgsTransaction*
- Create a transaction which includes the layers.
- static auto supportsTransaction(const QgsVectorLayer* layer) -> bool
- Checks if the provider of a given layer supports transactions.
Public functions
- auto addLayer(QgsVectorLayer* layer) -> bool
- Add the layer to the transaction.
- auto begin(QString& errorMsg, int statementTimeout = 20) -> bool
- Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to block QGIS before it is aborted.
- auto commit(QString& errorMsg) -> bool
- Commit transaction.
- auto createSavepoint(QString& error) -> QString
- creates a save point returns empty string on error returns the last created savepoint if it's not dirty
- auto createSavepoint(const QString& savePointId, QString& error) -> QString
- creates a save point returns empty string on error
- void dirtyLastSavePoint()
- dirty save point such that next call to createSavepoint will create a new one
- auto executeSql(const QString& sql, QString& error, bool isDirty = false, const QString& name = QString()) -> bool pure virtual
- Execute the sql string.
- auto lastSavePointIsDirty() const -> bool
- returns the last created savepoint
- auto rollback(QString& errorMsg) -> bool
- Roll back transaction.
- auto rollbackToSavepoint(const QString& name, QString& error) -> bool
- rollback to save point, the save point is maintained and is "undertied"
- auto savePoints() const -> QList<QString>
- returns savepoints
Signals
- void afterRollback()
- Emitted after a rollback.
- void dirtied(const QString& sql, const QString& name)
- Emitted if a sql query is executed and the underlying data is modified.
Function documentation
static QgsTransaction* QgsTransaction:: create(const QSet<QgsVectorLayer*>& layers)
Create a transaction which includes the layers.
All layers are expected to have the same connection string and data provider.
bool QgsTransaction:: addLayer(QgsVectorLayer* layer)
Add the layer to the transaction.
The layer must not be in edit mode and the connection string must match.
bool QgsTransaction:: begin(QString& errorMsg,
int statementTimeout = 20)
Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to block QGIS before it is aborted.
Statements can block, if multiple transactions are active and a statement would produce a conflicting state. In these cases, the statements block until the conflicting transaction is committed or rolled back. Some providers might not honour the statement timeout.
QString QgsTransaction:: createSavepoint(QString& error)
creates a save point returns empty string on error returns the last created savepoint if it's not dirty
QString QgsTransaction:: createSavepoint(const QString& savePointId,
QString& error)
creates a save point returns empty string on error
void QgsTransaction:: dirtyLastSavePoint()
dirty save point such that next call to createSavepoint will create a new one
bool QgsTransaction:: executeSql(const QString& sql,
QString& error,
bool isDirty = false,
const QString& name = QString()) pure virtual
Execute the sql string.
| Parameters | |
|---|---|
| sql | The sql query to execute |
| error | The error message |
| isDirty | Flag to indicate if the underlying data will be modified |
| name | Name of the transaction ( only used if isDirty is true) |
| Returns | true if everything is OK, false otherwise |
bool QgsTransaction:: lastSavePointIsDirty() const
returns the last created savepoint
bool QgsTransaction:: rollbackToSavepoint(const QString& name,
QString& error)
rollback to save point, the save point is maintained and is "undertied"
QList<QString> QgsTransaction:: savePoints() const
returns savepoints