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