![]() |
QxOrm 1.1.8
C++ Object Relational Mapping library
|
qx::QxSession : define a session to manage automatically database transactions (using C++ RAII) More...
#include <QxSession.h>
Public Member Functions | |
QxSession () | |
QxSession (const QSqlDatabase &database) | |
QxSession (const QSqlDatabase &database, bool bOpenTransaction) | |
QxSession (const QSqlDatabase &database, bool bOpenTransaction, bool bThrowable) | |
virtual | ~QxSession () |
bool | isThrowable () const |
bool | isOpened () const |
bool | isValid () const |
QSqlError | firstError () const |
QSqlError | lastError () const |
QList< QSqlError > | allErrors () const |
const QSqlDatabase * | database () const |
QSqlDatabase * | database () |
bool | open () |
bool | close () |
bool | commit () |
bool | rollback () |
QxSession & | operator+= (const QSqlError &err) |
Private Member Functions | |
void | appendSqlError (const QSqlError &err) |
void | clear () |
Private Attributes | |
QSqlDatabase | m_database |
Database connexion of current session. | |
QList< QSqlError > | m_lstSqlError |
List of SQL errors. | |
bool | m_bTransaction |
Flag to know if a transaction is opened or not. | |
bool | m_bThrowable |
When a SQL error is appended, an exception of type qx::dao::sql_error is thrown. | |
bool | m_bThrowInEvent |
An exception of type qx::dao::sql_error is throwing. | |
bool | m_bAutoOpenClose |
Open and close automatically connection to database. |
qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
{ // Start a scope where a new session is instantiated // Create a session : a valid database connexion by thread is automatically assigned to the session and a transaction is opened qx::QxSession session; // Execute some operations with database (using += operator of qx::QxSession class and session database connexion) session += qx::dao::insert(my_object, session.database()); session += qx::dao::update(my_object, session.database()); session += qx::dao::fetch_by_id(my_object, session.database()); session += qx::dao::delete_by_id(my_object, session.database()); // If the session is not valid (so an error occured) => display first error if (! session.isValid()) { qDebug("[QxOrm] session error : '%s'", qPrintable(session.firstError().text())); } } // End of scope : session is destroyed (transaction => automatically commit or rollback if there is an error)
Note : a session can throw a qx::dao::sql_error exception when a SQL error occured (by default, there is no exception). You can setup this feature using :
Other note : don't forget to pass the session database connexion to each qx::dao::xxx functions (using session.database() method). Moreover, you can manage your own database connexion (from a connexion pool for example) using constructor of qx::QxSession class.
Definition at line 83 of file QxSession.h.
qx::QxSession::QxSession | ( | ) |
qx::QxSession::QxSession | ( | const QSqlDatabase & | database | ) |
qx::QxSession::QxSession | ( | const QSqlDatabase & | database, |
bool | bOpenTransaction | ||
) |
qx::QxSession::QxSession | ( | const QSqlDatabase & | database, |
bool | bOpenTransaction, | ||
bool | bThrowable | ||
) |
virtual qx::QxSession::~QxSession | ( | ) | [inline, virtual] |
Definition at line 101 of file QxSession.h.
QList<QSqlError> qx::QxSession::allErrors | ( | ) | const [inline] |
Definition at line 108 of file QxSession.h.
void qx::QxSession::appendSqlError | ( | const QSqlError & | err | ) | [private] |
void qx::QxSession::clear | ( | ) | [private] |
bool qx::QxSession::close | ( | ) |
bool qx::QxSession::commit | ( | ) |
QSqlDatabase* qx::QxSession::database | ( | ) | [inline] |
Definition at line 110 of file QxSession.h.
const QSqlDatabase* qx::QxSession::database | ( | ) | const [inline] |
Definition at line 109 of file QxSession.h.
QSqlError qx::QxSession::firstError | ( | ) | const [inline] |
Definition at line 106 of file QxSession.h.
bool qx::QxSession::isOpened | ( | ) | const [inline] |
Definition at line 104 of file QxSession.h.
bool qx::QxSession::isThrowable | ( | ) | const [inline] |
Definition at line 103 of file QxSession.h.
bool qx::QxSession::isValid | ( | ) | const [inline] |
Definition at line 105 of file QxSession.h.
QSqlError qx::QxSession::lastError | ( | ) | const [inline] |
Definition at line 107 of file QxSession.h.
bool qx::QxSession::open | ( | ) |
QxSession& qx::QxSession::operator+= | ( | const QSqlError & | err | ) |
bool qx::QxSession::rollback | ( | ) |
bool qx::QxSession::m_bAutoOpenClose [private] |
Open and close automatically connection to database.
Definition at line 93 of file QxSession.h.
bool qx::QxSession::m_bThrowable [private] |
When a SQL error is appended, an exception of type qx::dao::sql_error is thrown.
Definition at line 91 of file QxSession.h.
bool qx::QxSession::m_bThrowInEvent [private] |
An exception of type qx::dao::sql_error is throwing.
Definition at line 92 of file QxSession.h.
bool qx::QxSession::m_bTransaction [private] |
Flag to know if a transaction is opened or not.
Definition at line 90 of file QxSession.h.
QSqlDatabase qx::QxSession::m_database [private] |
Database connexion of current session.
Definition at line 88 of file QxSession.h.
QList<QSqlError> qx::QxSession::m_lstSqlError [private] |
List of SQL errors.
Definition at line 89 of file QxSession.h.