QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
QgsTransaction Class Referenceabstract

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. More...

#include <qgstransaction.h>

Inheritance diagram for QgsTransaction:
Inheritance graph
[legend]

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.
 

Public Member Functions

 ~QgsTransaction () override
 
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 block QGIS before it is aborted.
 
bool commit (QString &errorMsg)
 Commit transaction.
 
QString connectionString () const
 Returns the connection string of the transaction.
 
virtual QString createSavepoint (const QString &savePointId, QString &error)
 creates a save point returns empty string on error
 
QString createSavepoint (QString &error)
 creates a save point returns empty string on error returns the last created savepoint if it's not dirty
 
void dirtyLastSavePoint ()
 dirty save point such that next call to createSavepoint will create a new one
 
virtual bool executeSql (const QString &sql, QString &error, bool isDirty=false, const QString &name=QString())=0
 Execute the sql string.
 
bool lastSavePointIsDirty () const
 returns the last created savepoint
 
bool rollback (QString &errorMsg)
 Roll back transaction.
 
virtual bool rollbackToSavepoint (const QString &name, QString &error)
 rollback to save point, the save point is maintained and is "undertied"
 
QList< QString > savePoints () const
 returns savepoints
 

Static Public Member Functions

static QgsTransactioncreate (const QSet< QgsVectorLayer * > &layers)
 Create a transaction which includes the layers.
 
static QgsTransactioncreate (const QString &connString, const QString &providerKey)
 Create a transaction for the specified connection string connString and provider with providerKey.
 
static bool supportsTransaction (const QgsVectorLayer *layer)
 Checks if the provider of a given layer supports transactions.
 

Protected Member Functions

 QgsTransaction (const QString &connString)
 

Protected Attributes

QString mConnString
 
bool mLastSavePointIsDirty
 
QStack< QString > mSavepoints
 
bool mTransactionActive
 

Detailed Description

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.

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::commit and QgsTransaction::rollback methods.

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.

Definition at line 56 of file qgstransaction.h.

Constructor & Destructor Documentation

◆ ~QgsTransaction()

QgsTransaction::~QgsTransaction ( )
override

Definition at line 65 of file qgstransaction.cpp.

◆ QgsTransaction()

QgsTransaction::QgsTransaction ( const QString &  connString)
protected

Definition at line 58 of file qgstransaction.cpp.

Member Function Documentation

◆ addLayer()

bool QgsTransaction::addLayer ( QgsVectorLayer layer,
bool  addLayersInEditMode = false 
)

Add the layer to the transaction.

The connection string must match.

Parameters
layerthat will be added to the transaction
addLayersInEditModeif set layers that are already in edit mode can be added to the transaction
Since
QGIS 3.26

Definition at line 122 of file qgstransaction.cpp.

◆ afterRollback

void QgsTransaction::afterRollback ( )
signal

Emitted after a rollback.

◆ begin()

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.

Definition at line 157 of file qgstransaction.cpp.

◆ commit()

bool QgsTransaction::commit ( QString &  errorMsg)

Commit transaction.

Definition at line 172 of file qgstransaction.cpp.

◆ connectionString()

QString QgsTransaction::connectionString ( ) const

Returns the connection string of the transaction.

Since
QGIS 3.26

Definition at line 70 of file qgstransaction.cpp.

◆ create() [1/2]

QgsTransaction * QgsTransaction::create ( const QSet< QgsVectorLayer * > &  layers)
static

Create a transaction which includes the layers.

All layers are expected to have the same connection string and data provider.

Definition at line 33 of file qgstransaction.cpp.

◆ create() [2/2]

QgsTransaction * QgsTransaction::create ( const QString &  connString,
const QString &  providerKey 
)
static

Create a transaction for the specified connection string connString and provider with providerKey.

Definition at line 28 of file qgstransaction.cpp.

◆ createSavepoint() [1/2]

QString QgsTransaction::createSavepoint ( const QString &  savePointId,
QString &  error 
)
virtual

creates a save point returns empty string on error

Definition at line 243 of file qgstransaction.cpp.

◆ createSavepoint() [2/2]

QString QgsTransaction::createSavepoint ( QString &  error)

creates a save point returns empty string on error returns the last created savepoint if it's not dirty

Definition at line 229 of file qgstransaction.cpp.

◆ dirtied

void QgsTransaction::dirtied ( const QString &  sql,
const QString &  name 
)
signal

Emitted if a sql query is executed and the underlying data is modified.

◆ dirtyLastSavePoint()

void QgsTransaction::dirtyLastSavePoint ( )

dirty save point such that next call to createSavepoint will create a new one

Definition at line 277 of file qgstransaction.cpp.

◆ executeSql()

virtual bool QgsTransaction::executeSql ( const QString &  sql,
QString &  error,
bool  isDirty = false,
const QString &  name = QString() 
)
pure virtual

Execute the sql string.

Parameters
sqlThe sql query to execute
errorThe error message
isDirtyFlag to indicate if the underlying data will be modified
nameName of the transaction ( only used if isDirty is true)
Returns
true if everything is OK, false otherwise

◆ lastSavePointIsDirty()

bool QgsTransaction::lastSavePointIsDirty ( ) const
inline

returns the last created savepoint

Definition at line 162 of file qgstransaction.h.

◆ rollback()

bool QgsTransaction::rollback ( QString &  errorMsg)

Roll back transaction.

Definition at line 186 of file qgstransaction.cpp.

◆ rollbackToSavepoint()

bool QgsTransaction::rollbackToSavepoint ( const QString &  name,
QString &  error 
)
virtual

rollback to save point, the save point is maintained and is "undertied"

Definition at line 259 of file qgstransaction.cpp.

◆ savePoints()

QList< QString > QgsTransaction::savePoints ( ) const
inline

returns savepoints

Definition at line 157 of file qgstransaction.h.

◆ supportsTransaction()

bool QgsTransaction::supportsTransaction ( const QgsVectorLayer layer)
static

Checks if the provider of a given layer supports transactions.

Definition at line 203 of file qgstransaction.cpp.

Member Data Documentation

◆ mConnString

QString QgsTransaction::mConnString
protected

Definition at line 184 of file qgstransaction.h.

◆ mLastSavePointIsDirty

bool QgsTransaction::mLastSavePointIsDirty
protected

Definition at line 187 of file qgstransaction.h.

◆ mSavepoints

QStack< QString > QgsTransaction::mSavepoints
protected

Definition at line 186 of file qgstransaction.h.

◆ mTransactionActive

bool QgsTransaction::mTransactionActive
protected

Definition at line 185 of file qgstransaction.h.


The documentation for this class was generated from the following files: