123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- /****************************************************************************
- **
- ** https://www.qxorm.com/
- ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
- **
- ** This file is part of the QxOrm library
- **
- ** This software is provided 'as-is', without any express or implied
- ** warranty. In no event will the authors be held liable for any
- ** damages arising from the use of this software
- **
- ** Commercial Usage
- ** Licensees holding valid commercial QxOrm licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and Lionel Marty
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3.0 as published by the Free Software
- ** Foundation and appearing in the file 'license.gpl3.txt' included in the
- ** packaging of this file. Please review the following information to
- ** ensure the GNU General Public License version 3.0 requirements will be
- ** met : http://www.gnu.org/copyleft/gpl.html
- **
- ** If you are unsure which license is appropriate for your use, or
- ** if you have questions regarding the use of this file, please contact :
- ** contact@qxorm.com
- **
- ****************************************************************************/
- #ifndef _QX_DAO_SESSION_H_
- #define _QX_DAO_SESSION_H_
- #ifdef _MSC_VER
- #pragma once
- #endif
- /*!
- * \file QxSession.h
- * \author Lionel Marty
- * \ingroup QxDao
- * \brief Define a session to manage automatically database transactions (using C++ RAII)
- */
- #include <QtSql/qsqldatabase.h>
- #include <QtSql/qsqlquery.h>
- #include <QtSql/qsqlerror.h>
- #include <QtSql/qsqldriver.h>
- #include <QtCore/qlist.h>
- #include <QxCommon/QxBool.h>
- #include <QxDao/QxDao.h>
- #include <QxDao/QxSqlQuery.h>
- #include <QxDao/QxSqlError.h>
- #include <QxRegister/QxClass.h>
- namespace qx {
- /*!
- * \ingroup QxDao
- * \brief qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
- *
- * A database <b>transaction</b> 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 <b>commits</b> the transaction.
- * If an error occurs during the transaction, or if the user specifies a <b>rollback</b> operation, the data manipulations within the transaction are not persisted to the database.
- *
- * The <i>qx::QxSession</i> class of QxOrm library is designed to manage automatically database transactions (using <i>C++ RAII</i>) :
- * \code
- { // 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)
- * \endcode
- *
- * <i>Note :</i> a session can throw a <i>qx::dao::sql_error</i> exception when a SQL error occured (by default, there is no exception).
- * You can setup this feature using :
- * - <i>qx::QxSession</i> constructor (for a specific session) ;
- * - <i>qx::QxSqlDatabase::getSingleton()->setSessionThrowable(bool b)</i> parameter (for all sessions).
- *
- * <i>Other note :</i> don't forget to pass the session database connexion to each <i>qx::dao::xxx</i> functions (using <i>session.database()</i> method).
- * Moreover, you can manage your own database connexion (from a connexion pool for example) using constructor of <i>qx::QxSession</i> class.
- *
- * <i>qx::QxSession</i> class provides also persistent methods (CRUD) to make easier to write C++ code.
- * Here is the same example using methods of <i>qx::QxSession</i> class instead of functions into namespace <i>qx::dao</i> :
- * \code
- { // 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)
- * \endcode
- */
- class QX_DLL_EXPORT QxSession
- {
- private:
- struct QxSessionImpl;
- std::shared_ptr<QxSessionImpl> m_pImpl; //!< Private implementation idiom (use std::shared_ptr instead of std::unique_ptr because of incomplete type)
- public:
- 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);
- static QxSession * getActiveSession(QSqlDatabase * db);
- 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())
- { return qx::dao::count<T>(query, this->database()); }
- template <class T>
- QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery())
- { return qx::dao::count<T>(lCount, query, this->database()); }
- template <class T>
- T * fetchById(const QVariant & id, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
- {
- IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
- IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
- if (! pDataMemberId) { qAssert(false); return NULL; }
- T * t = new T(); QSqlError err;
- pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
- if (relation.count() == 0) { err = qx::dao::fetch_by_id((* t), this->database(), columns); }
- else { err = qx::dao::fetch_by_id_with_relation(relation, (* t), this->database()); }
- if (err.isValid()) { delete t; t = NULL; (* this) += err; }
- return t;
- }
- template <class T>
- QSqlError fetchById(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::fetch_by_id(t, this->database(), columns); }
- else { err = qx::dao::fetch_by_id_with_relation(relation, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError fetchAll(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::fetch_all(t, this->database(), columns); }
- else { err = qx::dao::fetch_all_with_relation(relation, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError fetchByQuery(const qx::QxSqlQuery & query, T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, t, this->database(), columns); }
- else { err = qx::dao::fetch_by_query_with_relation(relation, query, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError insert(T & t, const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::insert(t, this->database()); }
- else { err = qx::dao::insert_with_relation(relation, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError update(T & t, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::update_by_query(query, t, this->database(), columns); }
- else { err = qx::dao::update_by_query_with_relation(relation, query, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError save(T & t, const QStringList & relation = QStringList())
- {
- QSqlError err;
- if (relation.count() == 0) { err = qx::dao::save(t, this->database()); }
- else { err = qx::dao::save_with_relation(relation, t, this->database()); }
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError deleteById(const QVariant & id)
- {
- IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
- IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
- if (! pDataMemberId) { qAssert(false); return QSqlError(); }
- std::shared_ptr<T> t = std::make_shared<T>();
- pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
- QSqlError err = qx::dao::delete_by_id((* t), this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError deleteById(T & t)
- {
- QSqlError err = qx::dao::delete_by_id(t, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError deleteAll()
- {
- QSqlError err = qx::dao::delete_all<T>(this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError deleteByQuery(const qx::QxSqlQuery & query)
- {
- QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError destroyById(const QVariant & id)
- {
- IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
- IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
- if (! pDataMemberId) { qAssert(false); return QSqlError(); }
- std::shared_ptr<T> t = std::make_shared<T>();
- pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
- QSqlError err = qx::dao::destroy_by_id((* t), this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError destroyById(T & t)
- {
- QSqlError err = qx::dao::destroy_by_id(t, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError destroyAll()
- {
- QSqlError err = qx::dao::destroy_all<T>(this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError destroyByQuery(const qx::QxSqlQuery & query)
- {
- QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- QSqlError executeQuery(qx::QxSqlQuery & query, T & t)
- {
- QSqlError err = qx::dao::execute_query<T>(query, t, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- QSqlError callQuery(qx::QxSqlQuery & query)
- {
- QSqlError err = qx::dao::call_query(query, this->database());
- if (err.isValid()) { (* this) += err; }
- return err;
- }
- template <class T>
- qx_bool exist(T & t)
- { return qx::dao::exist(t, this->database()); }
- private:
- QxSession(const QxSession & other) { Q_UNUSED(other); }
- QxSession & operator=(const QxSession & other) { Q_UNUSED(other); return (* this); }
- };
- } // namespace qx
- #endif // _QX_DAO_SESSION_H_
|