![]() |
QxOrm
1.2.4
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 () |
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 115 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 133 of file QxSession.h.
| QList<QSqlError> qx::QxSession::allErrors | ( | ) | const [inline] |
Definition at line 140 of file QxSession.h.
| void qx::QxSession::appendSqlError | ( | const QSqlError & | err | ) | [private] |
| QSqlError qx::QxSession::callQuery | ( | qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 322 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 159 of file QxSession.h.
| QSqlError qx::QxSession::count | ( | long & | lCount, |
| const qx::QxSqlQuery & | query = qx::QxSqlQuery() |
||
| ) | [inline] |
Definition at line 163 of file QxSession.h.
| const QSqlDatabase* qx::QxSession::database | ( | ) | const [inline] |
Definition at line 141 of file QxSession.h.
| QSqlDatabase* qx::QxSession::database | ( | ) | [inline] |
Definition at line 142 of file QxSession.h.
| QSqlError qx::QxSession::deleteAll | ( | ) | [inline] |
Definition at line 262 of file QxSession.h.
| QSqlError qx::QxSession::deleteById | ( | const QVariant & | id | ) | [inline] |
Definition at line 241 of file QxSession.h.
| QSqlError qx::QxSession::deleteById | ( | T & | t | ) | [inline] |
Definition at line 254 of file QxSession.h.
| QSqlError qx::QxSession::deleteByQuery | ( | const qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 270 of file QxSession.h.
| QSqlError qx::QxSession::destroyAll | ( | ) | [inline] |
Definition at line 299 of file QxSession.h.
| QSqlError qx::QxSession::destroyById | ( | const QVariant & | id | ) | [inline] |
Definition at line 278 of file QxSession.h.
| QSqlError qx::QxSession::destroyById | ( | T & | t | ) | [inline] |
Definition at line 291 of file QxSession.h.
| QSqlError qx::QxSession::destroyByQuery | ( | const qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 307 of file QxSession.h.
| QSqlError qx::QxSession::executeQuery | ( | qx::QxSqlQuery & | query, |
| T & | t | ||
| ) | [inline] |
Definition at line 315 of file QxSession.h.
| qx_bool qx::QxSession::exist | ( | T & | t | ) | [inline] |
Definition at line 330 of file QxSession.h.
| QSqlError qx::QxSession::fetchAll | ( | T & | t, |
| const QStringList & | columns = QStringList(), |
||
| const QStringList & | relation = QStringList() |
||
| ) | [inline] |
Definition at line 191 of file QxSession.h.
| T* qx::QxSession::fetchById | ( | const QVariant & | id, |
| const QStringList & | columns = QStringList(), |
||
| const QStringList & | relation = QStringList() |
||
| ) | [inline] |
Definition at line 167 of file QxSession.h.
| QSqlError qx::QxSession::fetchById | ( | T & | t, |
| const QStringList & | columns = QStringList(), |
||
| const QStringList & | relation = QStringList() |
||
| ) | [inline] |
Definition at line 181 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 201 of file QxSession.h.
| QSqlError qx::QxSession::firstError | ( | ) | const [inline] |
Definition at line 138 of file QxSession.h.
| QSqlError qx::QxSession::insert | ( | T & | t, |
| const QStringList & | relation = QStringList() |
||
| ) | [inline] |
Definition at line 211 of file QxSession.h.
| bool qx::QxSession::isOpened | ( | ) | const [inline] |
Definition at line 136 of file QxSession.h.
| bool qx::QxSession::isThrowable | ( | ) | const [inline] |
Definition at line 135 of file QxSession.h.
| bool qx::QxSession::isValid | ( | ) | const [inline] |
Definition at line 137 of file QxSession.h.
| QSqlError qx::QxSession::lastError | ( | ) | const [inline] |
Definition at line 139 of file QxSession.h.
| bool qx::QxSession::open | ( | ) |
| QxSession& qx::QxSession::operator+= | ( | const QSqlError & | err | ) |
| bool qx::QxSession::rollback | ( | ) |
| QSqlError qx::QxSession::save | ( | T & | t, |
| const QStringList & | relation = QStringList() |
||
| ) | [inline] |
Definition at line 231 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 221 of file QxSession.h.
bool qx::QxSession::m_bAutoOpenClose [private] |
Open and close automatically connection to database.
Definition at line 125 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 123 of file QxSession.h.
bool qx::QxSession::m_bThrowInEvent [private] |
An exception of type qx::dao::sql_error is throwing.
Definition at line 124 of file QxSession.h.
bool qx::QxSession::m_bTransaction [private] |
Flag to know if a transaction is opened or not.
Definition at line 122 of file QxSession.h.
QSqlDatabase qx::QxSession::m_database [private] |
Database connexion of current session.
Definition at line 120 of file QxSession.h.
QList<QSqlError> qx::QxSession::m_lstSqlError [private] |
List of SQL errors.
Definition at line 121 of file QxSession.h.