QxOrm  1.4.9
C++ Object Relational Mapping library
QxDaoAsync.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_ASYNC_H_
00033 #define _QX_DAO_ASYNC_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #ifdef _QX_NO_PRECOMPILED_HEADER
00047 #ifndef Q_MOC_RUN
00048 #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
00049 #endif // Q_MOC_RUN
00050 #endif // _QX_NO_PRECOMPILED_HEADER
00051 
00052 #include <QtCore/qqueue.h>
00053 
00054 #include <QtSql/qsqlerror.h>
00055 
00056 #ifndef Q_MOC_RUN
00057 #include <QxDao/IxPersistable.h>
00058 #include <QxDao/QxSqlQuery.h>
00059 #endif // Q_MOC_RUN
00060 
00061 namespace qx {
00062 namespace dao {
00063 namespace detail {
00064 
00069 struct QxDaoAsyncParams
00070 {
00071 
00072    enum dao_action { dao_none, dao_count, dao_fetch_by_id, dao_fetch_all, dao_fetch_by_query, dao_insert, 
00073                      dao_update, dao_save, dao_delete_by_id, dao_delete_all, dao_delete_by_query, 
00074                      dao_destroy_by_id, dao_destroy_all, dao_destroy_by_query, dao_execute_query, dao_call_query };
00075 
00076    dao_action                    daoAction;           
00077    QString                       className;           
00078    qx::QxSqlQuery                query;               
00079    QSqlDatabase *                pDatabase;           
00080    IxPersistable_ptr             pInstance;           
00081    IxPersistableCollection_ptr   pListOfInstances;    
00082    QStringList                   listColumns;         
00083    QStringList                   listRelations;       
00084    QVariant                      id;                  
00085    long                          daoCount;            
00086    bool                          useExecBatch;        
00087 
00088    QxDaoAsyncParams() : daoAction(dao_none), pDatabase(NULL), daoCount(0), useExecBatch(false) { ; }
00089    ~QxDaoAsyncParams() { ; }
00090 
00091 };
00092 
00093 typedef std::shared_ptr<QxDaoAsyncParams> QxDaoAsyncParams_ptr;
00094 
00099 class QX_DLL_EXPORT QxDaoAsyncRunner : public QObject
00100 {
00101 
00102    Q_OBJECT
00103 
00104 public:
00105 
00106    QxDaoAsyncRunner();
00107    virtual ~QxDaoAsyncRunner();
00108 
00109 protected:
00110 
00111    QSqlError runQuery(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00112 
00113 Q_SIGNALS:
00114 
00115    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00116 
00117 public Q_SLOTS:
00118 
00119    void onQueryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00120 
00121 };
00122 
00123 } // namespace detail
00124 } // namespace dao
00125 
00170 class QX_DLL_EXPORT QxDaoAsync : public QThread
00171 {
00172 
00173    Q_OBJECT
00174 
00175 protected:
00176 
00177    QMutex m_mutex;                                       
00178    qx::dao::detail::QxDaoAsyncParams_ptr m_pDaoParams;   
00179 
00180 public:
00181 
00182    QxDaoAsync();
00183    virtual ~QxDaoAsync();
00184 
00185    bool asyncCount(const QString & className, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);
00186    bool asyncFetchById(IxPersistable_ptr pToFetch, const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00187    bool asyncFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00188    bool asyncFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00189    bool asyncInsert(IxPersistable_ptr pToInsert, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00190    bool asyncUpdate(IxPersistable_ptr pToUpdate, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00191    bool asyncSave(IxPersistable_ptr pToSave, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00192    bool asyncDeleteById(IxPersistable_ptr pToDelete, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00193    bool asyncDeleteAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00194    bool asyncDeleteByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00195    bool asyncDestroyById(IxPersistable_ptr pToDestroy, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00196    bool asyncDestroyAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00197    bool asyncDestroyByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00198    bool asyncExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00199    bool asyncCallQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00200 
00201    bool isQueryRunning() const { return (m_pDaoParams.get() != NULL); }
00202 
00203 protected:
00204 
00205    virtual void run();
00206 
00207    void startQuery();
00208 
00209 Q_SIGNALS:
00210 
00211    void queryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00212    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00213 
00214 private Q_SLOTS:
00215 
00216    void onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00217 
00218 };
00219 
00220 typedef std::shared_ptr<QxDaoAsync> QxDaoAsync_ptr;
00221 
00222 } // namespace qx
00223 
00224 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::dao::detail::QxDaoAsyncParams & t) QX_USED;
00225 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::dao::detail::QxDaoAsyncParams & t) QX_USED;
00226 
00227 #endif // _QX_DAO_ASYNC_H_