QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches

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 ()
 
QxSessionoperator+= (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 QxSessiongetActiveSession (QSqlDatabase *db)
 

Private Member Functions

 QxSession (const QxSession &other)
 
QxSessionoperator= (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)
 

Detailed Description

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)
qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
Definition QxSession.h:119
const QSqlDatabase * database() const
bool isValid() const
QSqlError firstError() const
QSqlError fetch_by_id(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
Definition QxDao.h:636
QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered i...
Definition QxDao.h:176
QSqlError insert(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert an element or a list of elements into database.
Definition QxDao.h:142
QSqlError update(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList(), bool bUseExecBatch=false)
Update an element or a list of elements into database.
Definition QxDao.h:683

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)
QSqlError deleteById(const QVariant &id)
Definition QxSession.h:241
T * fetchById(const QVariant &id, const QStringList &columns=QStringList(), const QStringList &relation=QStringList())
Definition QxSession.h:167
QSqlError update(T &t, const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), bool bUseExecBatch=false)
Definition QxSession.h:221
QSqlError insert(T &t, const QStringList &relation=QStringList(), bool bUseExecBatch=false)
Definition QxSession.h:211

Definition at line 118 of file QxSession.h.

Constructor & Destructor Documentation

◆ QxSession() [1/5]

qx::QxSession::QxSession ( )

◆ QxSession() [2/5]

qx::QxSession::QxSession ( const QSqlDatabase & database)

◆ QxSession() [3/5]

qx::QxSession::QxSession ( const QSqlDatabase & database,
bool bOpenTransaction )

◆ QxSession() [4/5]

qx::QxSession::QxSession ( const QSqlDatabase & database,
bool bOpenTransaction,
bool bThrowable,
bool bAutoRollbackWhenDestroyed = false )

◆ ~QxSession()

virtual qx::QxSession::~QxSession ( )
virtual

◆ QxSession() [5/5]

qx::QxSession::QxSession ( const QxSession & other)
inlineprivate

Definition at line 335 of file QxSession.h.

Member Function Documentation

◆ allErrors()

QList< QSqlError > qx::QxSession::allErrors ( ) const

◆ callQuery()

QSqlError qx::QxSession::callQuery ( qx::QxSqlQuery & query)
inline

Definition at line 322 of file QxSession.h.

◆ checkIgnoreSoftDelete()

bool qx::QxSession::checkIgnoreSoftDelete ( const QString & classKey) const

◆ close()

bool qx::QxSession::close ( )

◆ commit()

bool qx::QxSession::commit ( )

◆ count() [1/2]

template<class T >
long qx::QxSession::count ( const qx::QxSqlQuery & query = qx::QxSqlQuery())
inline

Definition at line 159 of file QxSession.h.

◆ count() [2/2]

template<class T >
QSqlError qx::QxSession::count ( long & lCount,
const qx::QxSqlQuery & query = qx::QxSqlQuery() )
inline

Definition at line 163 of file QxSession.h.

◆ database() [1/2]

QSqlDatabase * qx::QxSession::database ( )

◆ database() [2/2]

const QSqlDatabase * qx::QxSession::database ( ) const

◆ deleteAll()

template<class T >
QSqlError qx::QxSession::deleteAll ( )
inline

Definition at line 262 of file QxSession.h.

◆ deleteById() [1/2]

template<class T >
QSqlError qx::QxSession::deleteById ( const QVariant & id)
inline

Definition at line 241 of file QxSession.h.

◆ deleteById() [2/2]

template<class T >
QSqlError qx::QxSession::deleteById ( T & t,
bool bUseExecBatch = false )
inline

Definition at line 254 of file QxSession.h.

◆ deleteByQuery()

template<class T >
QSqlError qx::QxSession::deleteByQuery ( const qx::QxSqlQuery & query)
inline

Definition at line 270 of file QxSession.h.

◆ destroyAll()

template<class T >
QSqlError qx::QxSession::destroyAll ( )
inline

Definition at line 299 of file QxSession.h.

◆ destroyById() [1/2]

template<class T >
QSqlError qx::QxSession::destroyById ( const QVariant & id)
inline

Definition at line 278 of file QxSession.h.

◆ destroyById() [2/2]

template<class T >
QSqlError qx::QxSession::destroyById ( T & t,
bool bUseExecBatch = false )
inline

Definition at line 291 of file QxSession.h.

◆ destroyByQuery()

template<class T >
QSqlError qx::QxSession::destroyByQuery ( const qx::QxSqlQuery & query)
inline

Definition at line 307 of file QxSession.h.

◆ executeQuery()

template<class T >
QSqlError qx::QxSession::executeQuery ( qx::QxSqlQuery & query,
T & t )
inline

Definition at line 315 of file QxSession.h.

◆ exist()

template<class T >
qx_bool qx::QxSession::exist ( T & t)
inline

Definition at line 330 of file QxSession.h.

◆ fetchAll()

template<class T >
QSqlError qx::QxSession::fetchAll ( T & t,
const QStringList & columns = QStringList(),
const QStringList & relation = QStringList() )
inline

Definition at line 191 of file QxSession.h.

◆ fetchById() [1/2]

template<class T >
T * qx::QxSession::fetchById ( const QVariant & id,
const QStringList & columns = QStringList(),
const QStringList & relation = QStringList() )
inline

Definition at line 167 of file QxSession.h.

◆ fetchById() [2/2]

template<class T >
QSqlError qx::QxSession::fetchById ( T & t,
const QStringList & columns = QStringList(),
const QStringList & relation = QStringList() )
inline

Definition at line 181 of file QxSession.h.

◆ fetchByQuery()

template<class T >
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.

◆ firstError()

QSqlError qx::QxSession::firstError ( ) const

◆ getActiveSession()

static QxSession * qx::QxSession::getActiveSession ( QSqlDatabase * db)
static

◆ getIgnoreSoftDeleteHash()

QString qx::QxSession::getIgnoreSoftDeleteHash ( ) const

◆ ignoreSoftDelete()

void qx::QxSession::ignoreSoftDelete ( bool bIgnoreSoftDelete = true,
const QStringList & classesToIgnore = QStringList() )

◆ insert()

template<class T >
QSqlError qx::QxSession::insert ( T & t,
const QStringList & relation = QStringList(),
bool bUseExecBatch = false )
inline

Definition at line 211 of file QxSession.h.

◆ isAutoRollbackWhenDestroyed()

bool qx::QxSession::isAutoRollbackWhenDestroyed ( ) const

◆ isOpened()

bool qx::QxSession::isOpened ( ) const

◆ isThrowable()

bool qx::QxSession::isThrowable ( ) const

◆ isValid()

bool qx::QxSession::isValid ( ) const

◆ lastError()

QSqlError qx::QxSession::lastError ( ) const

◆ open()

bool qx::QxSession::open ( )

◆ operator+=()

QxSession & qx::QxSession::operator+= ( const QSqlError & err)

◆ operator=()

QxSession & qx::QxSession::operator= ( const QxSession & other)
inlineprivate

Definition at line 336 of file QxSession.h.

◆ rollback()

bool qx::QxSession::rollback ( )

◆ save()

template<class T >
QSqlError qx::QxSession::save ( T & t,
const QStringList & relation = QStringList() )
inline

Definition at line 231 of file QxSession.h.

◆ setAutoRollbackWhenDestroyed()

void qx::QxSession::setAutoRollbackWhenDestroyed ( bool b)

◆ update()

template<class T >
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.

Member Data Documentation

◆ m_pImpl

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 124 of file QxSession.h.


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