![]() |
QxOrm
1.4.7
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) |
template<class T > | |
long | count (const qx::QxSqlQuery &query=qx::QxSqlQuery()) |
template<class T > | |
QSqlError | count (long &lCount, const qx::QxSqlQuery &query=qx::QxSqlQuery()) |
template<class T > | |
T * | fetchById (const QVariant &id, const QStringList &columns=QStringList(), const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | fetchById (T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | fetchAll (T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | fetchByQuery (const qx::QxSqlQuery &query, T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | insert (T &t, const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | update (T &t, const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | save (T &t, const QStringList &relation=QStringList()) |
template<class T > | |
QSqlError | deleteById (const QVariant &id) |
template<class T > | |
QSqlError | deleteById (T &t) |
template<class T > | |
QSqlError | deleteAll () |
template<class T > | |
QSqlError | deleteByQuery (const qx::QxSqlQuery &query) |
template<class T > | |
QSqlError | destroyById (const QVariant &id) |
template<class T > | |
QSqlError | destroyById (T &t) |
template<class T > | |
QSqlError | destroyAll () |
template<class T > | |
QSqlError | destroyByQuery (const qx::QxSqlQuery &query) |
template<class T > | |
QSqlError | executeQuery (qx::QxSqlQuery &query, T &t) |
QSqlError | callQuery (qx::QxSqlQuery &query) |
template<class T > | |
qx_bool | exist (T &t) |
Private Member Functions | |
void | appendSqlError (const QSqlError &err) |
void | clear () |
QxSession (const QxSession &other) | |
QxSession & | operator= (const QxSession &other) |
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)
A database transaction is a sequence of operations performed as a single logical unit of work. If no errors occurred during the execution of the transaction then the system commits the transaction. If an error occurs during the transaction, or if the user specifies a rollback operation, the data manipulations within the transaction are not persisted to the database.
The qx::QxSession class of QxOrm library is designed 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.
qx::QxSession class provides also persistent methods (CRUD) to make easier to write C++ code. Here is the same example using methods of qx::QxSession class instead of functions into namespace qx::dao :
{ // 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 session.insert(my_object); session.update(my_object); session.fetchById(my_object); session.deleteById(my_object); // 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)
Definition at line 118 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 136 of file QxSession.h.
qx::QxSession::QxSession | ( | const QxSession & | other | ) | [inline, private] |
Definition at line 338 of file QxSession.h.
QList<QSqlError> qx::QxSession::allErrors | ( | ) | const [inline] |
Definition at line 143 of file QxSession.h.
void qx::QxSession::appendSqlError | ( | const QSqlError & | err | ) | [private] |
QSqlError qx::QxSession::callQuery | ( | qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 325 of file QxSession.h.
void qx::QxSession::clear | ( | ) | [private] |
bool qx::QxSession::close | ( | ) |
bool qx::QxSession::commit | ( | ) |
long qx::QxSession::count | ( | const qx::QxSqlQuery & | query = qx::QxSqlQuery() | ) | [inline] |
Definition at line 162 of file QxSession.h.
QSqlError qx::QxSession::count | ( | long & | lCount, |
const qx::QxSqlQuery & | query = qx::QxSqlQuery() |
||
) | [inline] |
Definition at line 166 of file QxSession.h.
const QSqlDatabase* qx::QxSession::database | ( | ) | const [inline] |
Definition at line 144 of file QxSession.h.
QSqlDatabase* qx::QxSession::database | ( | ) | [inline] |
Definition at line 145 of file QxSession.h.
QSqlError qx::QxSession::deleteAll | ( | ) | [inline] |
Definition at line 265 of file QxSession.h.
QSqlError qx::QxSession::deleteById | ( | const QVariant & | id | ) | [inline] |
Definition at line 244 of file QxSession.h.
QSqlError qx::QxSession::deleteById | ( | T & | t | ) | [inline] |
Definition at line 257 of file QxSession.h.
QSqlError qx::QxSession::deleteByQuery | ( | const qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 273 of file QxSession.h.
QSqlError qx::QxSession::destroyAll | ( | ) | [inline] |
Definition at line 302 of file QxSession.h.
QSqlError qx::QxSession::destroyById | ( | const QVariant & | id | ) | [inline] |
Definition at line 281 of file QxSession.h.
QSqlError qx::QxSession::destroyById | ( | T & | t | ) | [inline] |
Definition at line 294 of file QxSession.h.
QSqlError qx::QxSession::destroyByQuery | ( | const qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 310 of file QxSession.h.
QSqlError qx::QxSession::executeQuery | ( | qx::QxSqlQuery & | query, |
T & | t | ||
) | [inline] |
Definition at line 318 of file QxSession.h.
qx_bool qx::QxSession::exist | ( | T & | t | ) | [inline] |
Definition at line 333 of file QxSession.h.
QSqlError qx::QxSession::fetchAll | ( | T & | t, |
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 194 of file QxSession.h.
T* qx::QxSession::fetchById | ( | const QVariant & | id, |
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 170 of file QxSession.h.
QSqlError qx::QxSession::fetchById | ( | T & | t, |
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 184 of file QxSession.h.
QSqlError qx::QxSession::fetchByQuery | ( | const qx::QxSqlQuery & | query, |
T & | t, | ||
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 204 of file QxSession.h.
QSqlError qx::QxSession::firstError | ( | ) | const [inline] |
Definition at line 141 of file QxSession.h.
QSqlError qx::QxSession::insert | ( | T & | t, |
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 214 of file QxSession.h.
bool qx::QxSession::isOpened | ( | ) | const [inline] |
Definition at line 139 of file QxSession.h.
bool qx::QxSession::isThrowable | ( | ) | const [inline] |
Definition at line 138 of file QxSession.h.
bool qx::QxSession::isValid | ( | ) | const [inline] |
Definition at line 140 of file QxSession.h.
QSqlError qx::QxSession::lastError | ( | ) | const [inline] |
Definition at line 142 of file QxSession.h.
bool qx::QxSession::open | ( | ) |
QxSession& qx::QxSession::operator+= | ( | const QSqlError & | err | ) |
Definition at line 339 of file QxSession.h.
bool qx::QxSession::rollback | ( | ) |
QSqlError qx::QxSession::save | ( | T & | t, |
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 234 of file QxSession.h.
QSqlError qx::QxSession::update | ( | T & | t, |
const qx::QxSqlQuery & | query = qx::QxSqlQuery() , |
||
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 224 of file QxSession.h.
bool qx::QxSession::m_bAutoOpenClose [private] |
Open and close automatically connection to database.
Definition at line 128 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 126 of file QxSession.h.
bool qx::QxSession::m_bThrowInEvent [private] |
An exception of type qx::dao::sql_error is throwing.
Definition at line 127 of file QxSession.h.
bool qx::QxSession::m_bTransaction [private] |
Flag to know if a transaction is opened or not.
Definition at line 125 of file QxSession.h.
QSqlDatabase qx::QxSession::m_database [private] |
Database connexion of current session.
Definition at line 123 of file QxSession.h.
QList<QSqlError> qx::QxSession::m_lstSqlError [private] |
List of SQL errors.
Definition at line 124 of file QxSession.h.