EasyQtSql
Easy SQL data access helper for QtSql
Transaction Class Reference

QSqlDatabase transaction wrapper. More...

#include <EasyQtSql_Transaction.h>

Inheritance diagram for Transaction:
Database

Public Member Functions

 Transaction (const QSqlDatabase &db=QSqlDatabase())
 
 Transaction (Transaction &&other)
 
Transactionoperator= (Transaction &&other)
 
 ~Transaction ()
 
bool commit ()
 Commits transaction. More...
 
bool rollback ()
 Rolls back transaction. More...
 
bool started () const
 Returns true if the transaction has been started successfully. Otherwise it returns false. More...
 
bool commited () const
 Returns true if the transaction has been commited successfully. Otherwise it returns false. More...
 
- Public Member Functions inherited from Database
 Database (const QSqlDatabase &db=QSqlDatabase())
 
 Database (Database &&other)
 
Databaseoperator= (Database &&other)
 
QSqlError lastError () const
 Returns information about the last error that occurred on the underlying database. More...
 
NonQueryResult execNonQuery (const QString &sql) const
 Executes non-query SQL statement (DELETE, INSERT, UPDATE, CREATE, ALTER, etc.) More...
 
QueryResult execQuery (const QString &sql) const
 Executes SELECT query. More...
 
InsertQuery insertInto (const QString &table) const
 Creates INSERT query wrapper. More...
 
DeleteQuery deleteFrom (const QString &table) const
 Creates DELETE query wrapper. More...
 
UpdateQuery update (const QString &table) const
 Creates UPDATE query wrapper. More...
 
PreparedQuery prepare (const QString &sql, bool forwardOnly=true) const
 Prepares SQL statement. More...
 
QSqlDatabase & qSqlDatabase ()
 Returns a reference to the wrapped QSqlDatabase object. More...
 
template<typename Func >
int each (const QString &query, Func &&f) const
 Executes query and applies function f to each result row. More...
 
template<typename Func >
int first (const QString &query, Func &&f) const
 Executes query and applies function f to the first result row. More...
 
template<typename Func >
int range (const QString &query, int start, int count, Func &&f) const
 Executes query and applies function f to count result rows starting from index start. More...
 
template<typename Func >
int top (const QString &query, int topCount, Func &&f) const
 Executes query and applies function f to topCount result rows. More...
 
template<typename T >
scalar (const QString &query) const
 Executes query and returns scalar value converted to T. More...
 
QVariant scalar (const QString &query) const
 Executes query and returns scalar value. More...
 

Additional Inherited Members

- Protected Attributes inherited from Database
QSqlDatabase m_db
 

Detailed Description

QSqlDatabase transaction wrapper.

Features:

void test()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();
try
{
Transaction t(db);
t.execNonQuery("CREATE TABLE table (a int, b int, c int, d text)");
t.insertInto("table (a, b, c, d)")
.values(1, 2, 3, "row1")
.values(4, 5, 6, "row2")
.values(7, 8, 9, "row3")
.exec();
PreparedQuery query = t.prepare("SELECT a, b, c, d FROM table");
QueryResult res = query.exec();
while (res.next())
{
QVariantMap map = res.toMap();
qDebug() << map;
}
t.update("table")
.set("a", 111)
.set("b", 222)
.where("c = ? OR c = ?", 3, 6);
res = query.exec();
while (res.next())
{
QVariantMap map = res.toMap();
qDebug() << map;
}
t.commit(); //the transaction will be rolled back on exit from the scope (when calling the destructor) if you do not explicitly commit
catch (const DBException &e)
{
//you can handle all the errors at one point
//the transaction will be automatically rolled back on exception
qDebug() << e.lastError << e.lastQuery;
}
}

Constructor & Destructor Documentation

◆ Transaction() [1/2]

Transaction::Transaction ( const QSqlDatabase &  db = QSqlDatabase())
inlineexplicit

◆ Transaction() [2/2]

Transaction::Transaction ( Transaction &&  other)
inline

◆ ~Transaction()

Transaction::~Transaction ( )
inline

Member Function Documentation

◆ commit()

bool Transaction::commit ( )
inline

Commits transaction.

The transaction will be rolled back on calling the destructor if not explicitly commited

Exceptions
DBException

◆ commited()

bool Transaction::commited ( ) const
inline

Returns true if the transaction has been commited successfully. Otherwise it returns false.

◆ operator=()

Transaction& Transaction::operator= ( Transaction &&  other)
inline

◆ rollback()

bool Transaction::rollback ( )
inline

Rolls back transaction.

◆ started()

bool Transaction::started ( ) const
inline

Returns true if the transaction has been started successfully. Otherwise it returns false.


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