| QxOrm 1.4.7 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 ** https://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 <QtSql/qsqldatabase.h>
    00047 #include <QtSql/qsqlquery.h>
    00048 #include <QtSql/qsqlerror.h>
    00049 #include <QtSql/qsqldriver.h>
    00050
    00051 #include <QtCore/qlist.h>
    00052
    00053 #include <QxCommon/QxBool.h>
    00054
    00055 #include <QxDao/QxDao.h>
    00056 #include <QxDao/QxSqlQuery.h>
    00057 #include <QxDao/QxSqlError.h>
    00058
    00059 #include <QxRegister/QxClass.h>
    00060
    00061 namespace qx {
    00062
    00118 class QX_DLL_EXPORT QxSession
    00119 {
    00120
    00121 private:
    00122
    00123    QSqlDatabase m_database;
    00124    QList<QSqlError> m_lstSqlError;
    00125    bool m_bTransaction;
    00126    bool m_bThrowable;
    00127    bool m_bThrowInEvent;
    00128    bool m_bAutoOpenClose;
    00129
    00130 public:
    00131
    00132    QxSession();
    00133    QxSession(const QSqlDatabase & database);
    00134    QxSession(const QSqlDatabase & database, bool bOpenTransaction);
    00135    QxSession(const QSqlDatabase & database, bool bOpenTransaction, bool bThrowable);
    00136    virtual ~QxSession() { close(); }
    00137
    00138    inline bool isThrowable() const              { return m_bThrowable; }
    00139    inline bool isOpened() const                 { return m_bTransaction; }
    00140    inline bool isValid() const                  { return (m_lstSqlError.count() <= 0); }
    00141    inline QSqlError firstError() const          { return ((m_lstSqlError.count() > 0) ? m_lstSqlError.first() : QSqlError()); }
    00142    inline QSqlError lastError() const           { return ((m_lstSqlError.count() > 0) ? m_lstSqlError.last() : QSqlError()); }
    00143    inline QList<QSqlError> allErrors() const    { return m_lstSqlError; }
    00144    inline const QSqlDatabase * database() const { return (& m_database); }
    00145    inline QSqlDatabase * database()             { return (& m_database); }
    00146
    00147    bool open();
    00148    bool close();
    00149    bool commit();
    00150    bool rollback();
    00151
    00152    QxSession & operator+= (const QSqlError & err);
    00153
    00154 private:
    00155
    00156    void appendSqlError(const QSqlError & err);
    00157    void clear();
    00158
    00159 public:
    00160
    00161    template <class T>
    00162    long count(const qx::QxSqlQuery & query = qx::QxSqlQuery())
    00163    { return qx::dao::count<T>(query, this->database()); }
    00164
    00165    template <class T>
    00166    QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery())
    00167    { return qx::dao::count<T>(lCount, query, this->database()); }
    00168
    00169    template <class T>
    00170    T * fetchById(const QVariant & id, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00171    {
    00172       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00173       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00174       if (! pDataMemberId) { qAssert(false); return NULL; }
    00175       T * t = new T(); QSqlError err;
    00176       pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
    00177       if (relation.count() == 0) { err = qx::dao::fetch_by_id((* t), this->database(), columns); }
    00178       else { err = qx::dao::fetch_by_id_with_relation(relation, (* t), this->database()); }
    00179       if (err.isValid()) { delete t; t = NULL; (* this) += err; }
    00180       return t;
    00181    }
    00182
    00183    template <class T>
    00184    QSqlError fetchById(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00185    {
    00186       QSqlError err;
    00187       if (relation.count() == 0) { err = qx::dao::fetch_by_id(t, this->database(), columns); }
    00188       else { err = qx::dao::fetch_by_id_with_relation(relation, t, this->database()); }
    00189       if (err.isValid()) { (* this) += err; }
    00190       return err;
    00191    }
    00192
    00193    template <class T>
    00194    QSqlError fetchAll(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00195    {
    00196       QSqlError err;
    00197       if (relation.count() == 0) { err = qx::dao::fetch_all(t, this->database(), columns); }
    00198       else { err = qx::dao::fetch_all_with_relation(relation, t, this->database()); }
    00199       if (err.isValid()) { (* this) += err; }
    00200       return err;
    00201    }
    00202
    00203    template <class T>
    00204    QSqlError fetchByQuery(const qx::QxSqlQuery & query, T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00205    {
    00206       QSqlError err;
    00207       if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, t, this->database(), columns); }
    00208       else { err = qx::dao::fetch_by_query_with_relation(relation, query, t, this->database()); }
    00209       if (err.isValid()) { (* this) += err; }
    00210       return err;
    00211    }
    00212
    00213    template <class T>
    00214    QSqlError insert(T & t, const QStringList & relation = QStringList())
    00215    {
    00216       QSqlError err;
    00217       if (relation.count() == 0) { err = qx::dao::insert(t, this->database()); }
    00218       else { err = qx::dao::insert_with_relation(relation, t, this->database()); }
    00219       if (err.isValid()) { (* this) += err; }
    00220       return err;
    00221    }
    00222
    00223    template <class T>
    00224    QSqlError update(T & t, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
    00225    {
    00226       QSqlError err;
    00227       if (relation.count() == 0) { err = qx::dao::update_by_query(query, t, this->database(), columns); }
    00228       else { err = qx::dao::update_by_query_with_relation(relation, query, t, this->database()); }
    00229       if (err.isValid()) { (* this) += err; }
    00230       return err;
    00231    }
    00232
    00233    template <class T>
    00234    QSqlError save(T & t, const QStringList & relation = QStringList())
    00235    {
    00236       QSqlError err;
    00237       if (relation.count() == 0) { err = qx::dao::save(t, this->database()); }
    00238       else { err = qx::dao::save_with_relation(relation, t, this->database()); }
    00239       if (err.isValid()) { (* this) += err; }
    00240       return err;
    00241    }
    00242
    00243    template <class T>
    00244    QSqlError deleteById(const QVariant & id)
    00245    {
    00246       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00247       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00248       if (! pDataMemberId) { qAssert(false); return QSqlError(); }
    00249       std::shared_ptr<T> t = std::make_shared<T>();
    00250       pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
    00251       QSqlError err = qx::dao::delete_by_id((* t), this->database());
    00252       if (err.isValid()) { (* this) += err; }
    00253       return err;
    00254    }
    00255
    00256    template <class T>
    00257    QSqlError deleteById(T & t)
    00258    {
    00259       QSqlError err = qx::dao::delete_by_id(t, this->database());
    00260       if (err.isValid()) { (* this) += err; }
    00261       return err;
    00262    }
    00263
    00264    template <class T>
    00265    QSqlError deleteAll()
    00266    {
    00267       QSqlError err = qx::dao::delete_all<T>(this->database());
    00268       if (err.isValid()) { (* this) += err; }
    00269       return err;
    00270    }
    00271
    00272    template <class T>
    00273    QSqlError deleteByQuery(const qx::QxSqlQuery & query)
    00274    {
    00275       QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
    00276       if (err.isValid()) { (* this) += err; }
    00277       return err;
    00278    }
    00279
    00280    template <class T>
    00281    QSqlError destroyById(const QVariant & id)
    00282    {
    00283       IxDataMemberX * pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
    00284       IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
    00285       if (! pDataMemberId) { qAssert(false); return QSqlError(); }
    00286       std::shared_ptr<T> t = std::make_shared<T>();
    00287       pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
    00288       QSqlError err = qx::dao::destroy_by_id((* t), this->database());
    00289       if (err.isValid()) { (* this) += err; }
    00290       return err;
    00291    }
    00292
    00293    template <class T>
    00294    QSqlError destroyById(T & t)
    00295    {
    00296       QSqlError err = qx::dao::destroy_by_id(t, this->database());
    00297       if (err.isValid()) { (* this) += err; }
    00298       return err;
    00299    }
    00300
    00301    template <class T>
    00302    QSqlError destroyAll()
    00303    {
    00304       QSqlError err = qx::dao::destroy_all<T>(this->database());
    00305       if (err.isValid()) { (* this) += err; }
    00306       return err;
    00307    }
    00308
    00309    template <class T>
    00310    QSqlError destroyByQuery(const qx::QxSqlQuery & query)
    00311    {
    00312       QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
    00313       if (err.isValid()) { (* this) += err; }
    00314       return err;
    00315    }
    00316
    00317    template <class T>
    00318    QSqlError executeQuery(qx::QxSqlQuery & query, T & t)
    00319    {
    00320       QSqlError err = qx::dao::execute_query<T>(query, t, this->database());
    00321       if (err.isValid()) { (* this) += err; }
    00322       return err;
    00323    }
    00324
    00325    QSqlError callQuery(qx::QxSqlQuery & query)
    00326    {
    00327       QSqlError err = qx::dao::call_query(query, this->database());
    00328       if (err.isValid()) { (* this) += err; }
    00329       return err;
    00330    }
    00331
    00332    template <class T>
    00333    qx_bool exist(T & t)
    00334    { return qx::dao::exist(t, this->database()); }
    00335
    00336 private:
    00337
    00338    QxSession(const QxSession & other) { Q_UNUSED(other); }
    00339    QxSession & operator=(const QxSession & other) { Q_UNUSED(other); return (* this); }
    00340
    00341 };
    00342
    00343 } // namespace qx
    00344
    00345 #endif // _QX_DAO_SESSION_H_