QxOrm
1.5.0
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, bool bAutoRollbackWhenDestroyed=false) | |
virtual | ~QxSession () |
bool | isThrowable () const |
bool | isOpened () const |
bool | isValid () const |
bool | isAutoRollbackWhenDestroyed () const |
void | setAutoRollbackWhenDestroyed (bool b) |
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) |
void | ignoreSoftDelete (bool bIgnoreSoftDelete=true, const QStringList &classesToIgnore=QStringList()) |
bool | checkIgnoreSoftDelete (const QString &classKey) const |
QString | getIgnoreSoftDeleteHash () const |
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(), bool bUseExecBatch=false) |
template<class T > | |
QSqlError | update (T &t, const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), bool bUseExecBatch=false) |
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, bool bUseExecBatch=false) |
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, bool bUseExecBatch=false) |
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) |
Static Public Member Functions | |
static QxSession * | getActiveSession (QSqlDatabase *db) |
Private Member Functions | |
QxSession (const QxSession &other) | |
QxSession & | operator= (const QxSession &other) |
Private Attributes | |
std::shared_ptr< QxSessionImpl > | m_pImpl |
Private implementation idiom (use std::shared_ptr instead of std::unique_ptr because of incomplete type) |
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, | ||
bool | bAutoRollbackWhenDestroyed = false |
||
) |
virtual qx::QxSession::~QxSession | ( | ) | [virtual] |
qx::QxSession::QxSession | ( | const QxSession & | other | ) | [inline, private] |
Definition at line 335 of file QxSession.h.
QList<QSqlError> qx::QxSession::allErrors | ( | ) | const |
QSqlError qx::QxSession::callQuery | ( | qx::QxSqlQuery & | query | ) | [inline] |
Definition at line 322 of file QxSession.h.
bool qx::QxSession::checkIgnoreSoftDelete | ( | const QString & | classKey | ) | const |
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 |
QSqlDatabase* qx::QxSession::database | ( | ) |
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, |
bool | bUseExecBatch = false |
||
) | [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, |
bool | bUseExecBatch = false |
||
) | [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 |
static QxSession* qx::QxSession::getActiveSession | ( | QSqlDatabase * | db | ) | [static] |
QString qx::QxSession::getIgnoreSoftDeleteHash | ( | ) | const |
void qx::QxSession::ignoreSoftDelete | ( | bool | bIgnoreSoftDelete = true , |
const QStringList & | classesToIgnore = QStringList() |
||
) |
QSqlError qx::QxSession::insert | ( | T & | t, |
const QStringList & | relation = QStringList() , |
||
bool | bUseExecBatch = false |
||
) | [inline] |
Definition at line 211 of file QxSession.h.
bool qx::QxSession::isAutoRollbackWhenDestroyed | ( | ) | const |
bool qx::QxSession::isOpened | ( | ) | const |
bool qx::QxSession::isThrowable | ( | ) | const |
bool qx::QxSession::isValid | ( | ) | const |
QSqlError qx::QxSession::lastError | ( | ) | const |
bool qx::QxSession::open | ( | ) |
QxSession& qx::QxSession::operator+= | ( | const QSqlError & | err | ) |
Definition at line 336 of file QxSession.h.
bool qx::QxSession::rollback | ( | ) |
QSqlError qx::QxSession::save | ( | T & | t, |
const QStringList & | relation = QStringList() |
||
) | [inline] |
Definition at line 231 of file QxSession.h.
void qx::QxSession::setAutoRollbackWhenDestroyed | ( | bool | b | ) |
QSqlError qx::QxSession::update | ( | T & | t, |
const qx::QxSqlQuery & | query = qx::QxSqlQuery() , |
||
const QStringList & | columns = QStringList() , |
||
const QStringList & | relation = QStringList() , |
||
bool | bUseExecBatch = false |
||
) | [inline] |
Definition at line 221 of file QxSession.h.
std::shared_ptr<QxSessionImpl> qx::QxSession::m_pImpl [private] |
Private implementation idiom (use std::shared_ptr instead of std::unique_ptr because of incomplete type)
Definition at line 123 of file QxSession.h.