| QxOrm 1.3.1 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

QxSession.h
Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** http://www.qxorm.com/
    00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
    00005 **
    00006 ** This file is part of the QxOrm library
    00007 **
    00008 ** This software is provided 'as-is', without any express or implied
    00009 ** warranty. In no event will the authors be held liable for any
    00010 ** damages arising from the use of this software
    00011 **
    00012 ** Commercial Usage
    00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
    00014 ** accordance with the commercial license agreement provided with the
    00015 ** Software or, alternatively, in accordance with the terms contained in
    00016 ** a written agreement between you and Lionel Marty
    00017 **
    00018 ** GNU General Public License Usage
    00019 ** Alternatively, this file may be used under the terms of the GNU
    00020 ** General Public License version 3.0 as published by the Free Software
    00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
    00022 ** packaging of this file. Please review the following information to
    00023 ** ensure the GNU General Public License version 3.0 requirements will be
    00024 ** met : http://www.gnu.org/copyleft/gpl.html
    00025 **
    00026 ** If you are unsure which license is appropriate for your use, or
    00027 ** if you have questions regarding the use of this file, please contact :
    00028 ** contact@qxorm.com
    00029 **
    00030 ****************************************************************************/
    00031
    00032 #ifndef _QX_DAO_SESSION_H_
    00033 #define _QX_DAO_SESSION_H_
    00034
    00035 #ifdef _MSC_VER
    00036 #pragma once
    00037 #endif
    00038
    00046 #include <boost/noncopyable.hpp>
    00047 #include <boost/shared_ptr.hpp>
    00048
    00049 #include <QtSql/qsqldatabase.h>
    00050 #include <QtSql/qsqlquery.h>
    00051 #include <QtSql/qsqlerror.h>
    00052 #include <QtSql/qsqldriver.h>
    00053
    00054 #include <QtCore/qlist.h>
    00055
    00056 #include <QxCommon/QxBool.h>
    00057
    00058 #include <QxDao/QxDao.h>
    00059 #include <QxDao/QxSqlQuery.h>
    00060 #include <QxDao/QxSqlError.h>
    00061
    00062 #include <QxRegister/QxClass.h>
    00063
    00064 namespace qx {
    00065
    00121 class QX_DLL_EXPORT QxSession : private boost::noncopyable
    00122 {
    00123
    00124 private:
    00125
    00126    QSqlDatabase m_database;
    00127    QList<QSqlError> m_lstSqlError;
    00128    bool m_bTransaction;
    00129    bool m_bThrowable;
    00130    bool m_bThrowInEvent;
    00131    bool m_bAutoOpenClose;
    00132
    00133 public:
    00134
    00135    QxSession();
    00136    QxSession(const QSqlDatabase & database);
    00137    QxSession(const QSqlDatabase & database, bool bOpenTransaction);
    00138    QxSession(const QSqlDatabase & database, bool bOpenTransaction, bool bThrowable);
    00139    virtual ~QxSession() { close(); }
    00140
    00141    inline bool isThrowable() const              { return m_bThrowable; }
    00142    inline bool isOpened() const                 { return m_bTransaction; }
    00143    inline bool isValid() const                  { return (m_lstSqlError.count() <= 0); }
    00144    inline QSqlError firstError() const          { return ((m_lstSqlError.count() > 0) ? m_lstSqlError.first() : QSqlError()); }
    00145    inline QSqlError lastError() const           { return ((m_lstSqlError.count() > 0) ? m_lstSqlError.last() : QSqlError()); }
    00146    inline QList<QSqlError> allErrors() const    { return m_lstSqlError; }
    00147    inline const QSqlDatabase * database() const { return (& m_database); }
    00148    inline QSqlDatabase * database()             { return (& m_database); }
    00149
    00150    bool open();
    00151    bool close();
    00152    bool commit();
    00153    bool rollback();
    00154
    00155    QxSession & operator+= (const QSqlError & err);
    00156
    00157 private:
    00158
    00159    void appendSqlError(const QSqlError & err);
    00160    void clear();
    00161
    00162 public:
    00163
    00164    template <class T>
    00165    long count(const qx::QxSqlQuery & query = qx::QxSqlQuery())
    00166    { return qx::dao::count<T>(query, this->database()); }
    00167
    00168    template <class T>
    00169    QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery())
    00170    { return qx::dao::count<T>(lCount, query, this->database()); }
    00171
    00172    template <class T>
    00173    T * fetchById(const QVariant & id, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00174    {
    00175       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00176       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00177       if (! pDataMemberId) { qAssert(false); return NULL; }
    00178       T * t = new T(); QSqlError err;
    00179       pDataMemberId->fromVariant(t, id);
    00180       if (relation.count() == 0) { err = qx::dao::fetch_by_id((* t), this->database(), columns); }
    00181       else { err = qx::dao::fetch_by_id_with_relation(relation, (* t), this->database()); }
    00182       if (err.isValid()) { delete t; t = NULL; (* this) += err; }
    00183       return t;
    00184    }
    00185
    00186    template <class T>
    00187    QSqlError fetchById(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00188    {
    00189       QSqlError err;
    00190       if (relation.count() == 0) { err = qx::dao::fetch_by_id(t, this->database(), columns); }
    00191       else { err = qx::dao::fetch_by_id_with_relation(relation, t, this->database()); }
    00192       if (err.isValid()) { (* this) += err; }
    00193       return err;
    00194    }
    00195
    00196    template <class T>
    00197    QSqlError fetchAll(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00198    {
    00199       QSqlError err;
    00200       if (relation.count() == 0) { err = qx::dao::fetch_all(t, this->database(), columns); }
    00201       else { err = qx::dao::fetch_all_with_relation(relation, t, this->database()); }
    00202       if (err.isValid()) { (* this) += err; }
    00203       return err;
    00204    }
    00205
    00206    template <class T>
    00207    QSqlError fetchByQuery(const qx::QxSqlQuery & query, T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00208    {
    00209       QSqlError err;
    00210       if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, t, this->database(), columns); }
    00211       else { err = qx::dao::fetch_by_query_with_relation(relation, query, t, this->database()); }
    00212       if (err.isValid()) { (* this) += err; }
    00213       return err;
    00214    }
    00215
    00216    template <class T>
    00217    QSqlError insert(T & t, const QStringList & relation = QStringList())
    00218    {
    00219       QSqlError err;
    00220       if (relation.count() == 0) { err = qx::dao::insert(t, this->database()); }
    00221       else { err = qx::dao::insert_with_relation(relation, t, this->database()); }
    00222       if (err.isValid()) { (* this) += err; }
    00223       return err;
    00224    }
    00225
    00226    template <class T>
    00227    QSqlError update(T & t, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00228    {
    00229       QSqlError err;
    00230       if (relation.count() == 0) { err = qx::dao::update_by_query(query, t, this->database(), columns); }
    00231       else { err = qx::dao::update_by_query_with_relation(relation, query, t, this->database()); }
    00232       if (err.isValid()) { (* this) += err; }
    00233       return err;
    00234    }
    00235
    00236    template <class T>
    00237    QSqlError save(T & t, const QStringList & relation = QStringList())
    00238    {
    00239       QSqlError err;
    00240       if (relation.count() == 0) { err = qx::dao::save(t, this->database()); }
    00241       else { err = qx::dao::save_with_relation(relation, t, this->database()); }
    00242       if (err.isValid()) { (* this) += err; }
    00243       return err;
    00244    }
    00245
    00246    template <class T>
    00247    QSqlError deleteById(const QVariant & id)
    00248    {
    00249       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00250       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00251       if (! pDataMemberId) { qAssert(false); return QSqlError(); }
    00252       boost::shared_ptr<T> t; t.reset(new T());
    00253       pDataMemberId->fromVariant(t.get(), id);
    00254       QSqlError err = qx::dao::delete_by_id((* t), this->database());
    00255       if (err.isValid()) { (* this) += err; }
    00256       return err;
    00257    }
    00258
    00259    template <class T>
    00260    QSqlError deleteById(T & t)
    00261    {
    00262       QSqlError err = qx::dao::delete_by_id(t, this->database());
    00263       if (err.isValid()) { (* this) += err; }
    00264       return err;
    00265    }
    00266
    00267    template <class T>
    00268    QSqlError deleteAll()
    00269    {
    00270       QSqlError err = qx::dao::delete_all<T>(this->database());
    00271       if (err.isValid()) { (* this) += err; }
    00272       return err;
    00273    }
    00274
    00275    template <class T>
    00276    QSqlError deleteByQuery(const qx::QxSqlQuery & query)
    00277    {
    00278       QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
    00279       if (err.isValid()) { (* this) += err; }
    00280       return err;
    00281    }
    00282
    00283    template <class T>
    00284    QSqlError destroyById(const QVariant & id)
    00285    {
    00286       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00287       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00288       if (! pDataMemberId) { qAssert(false); return QSqlError(); }
    00289       boost::shared_ptr<T> t; t.reset(new T());
    00290       pDataMemberId->fromVariant(t.get(), id);
    00291       QSqlError err = qx::dao::destroy_by_id((* t), this->database());
    00292       if (err.isValid()) { (* this) += err; }
    00293       return err;
    00294    }
    00295
    00296    template <class T>
    00297    QSqlError destroyById(T & t)
    00298    {
    00299       QSqlError err = qx::dao::destroy_by_id(t, this->database());
    00300       if (err.isValid()) { (* this) += err; }
    00301       return err;
    00302    }
    00303
    00304    template <class T>
    00305    QSqlError destroyAll()
    00306    {
    00307       QSqlError err = qx::dao::destroy_all<T>(this->database());
    00308       if (err.isValid()) { (* this) += err; }
    00309       return err;
    00310    }
    00311
    00312    template <class T>
    00313    QSqlError destroyByQuery(const qx::QxSqlQuery & query)
    00314    {
    00315       QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
    00316       if (err.isValid()) { (* this) += err; }
    00317       return err;
    00318    }
    00319
    00320    template <class T>
    00321    QSqlError executeQuery(qx::QxSqlQuery & query, T & t)
    00322    {
    00323       QSqlError err = qx::dao::execute_query<T>(query, t, this->database());
    00324       if (err.isValid()) { (* this) += err; }
    00325       return err;
    00326    }
    00327
    00328    QSqlError callQuery(qx::QxSqlQuery & query)
    00329    {
    00330       QSqlError err = qx::dao::call_query(query, this->database());
    00331       if (err.isValid()) { (* this) += err; }
    00332       return err;
    00333    }
    00334
    00335    template <class T>
    00336    qx_bool exist(T & t)
    00337    { return qx::dao::exist(t, this->database()); }
    00338
    00339 };
    00340
    00341 } // namespace qx
    00342
    00343 #endif // _QX_DAO_SESSION_H_