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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines