QxOrm  1.2.4
C++ Object Relational Mapping library
QxDaoAsync.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_ASYNC_H_
00027 #define _QX_DAO_ASYNC_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <QtCore/qqueue.h>
00041 
00042 #include <QtSql/qsqlerror.h>
00043 
00044 #include <QxDao/IxPersistable.h>
00045 #include <QxDao/QxSqlQuery.h>
00046 
00047 namespace qx {
00048 namespace dao {
00049 namespace detail {
00050 
00055 struct QxDaoAsyncParams
00056 {
00057 
00058    enum dao_action { dao_none, dao_count, dao_fetch_by_id, dao_fetch_all, dao_fetch_by_query, dao_insert, 
00059                      dao_update, dao_save, dao_delete_by_id, dao_delete_all, dao_delete_by_query, 
00060                      dao_destroy_by_id, dao_destroy_all, dao_destroy_by_query, dao_execute_query, dao_call_query };
00061 
00062    dao_action           daoAction;           
00063    QString              className;           
00064    qx::QxSqlQuery       query;               
00065    QSqlDatabase *       pDatabase;           
00066    IxPersistable_ptr    pInstance;           
00067    IxCollection_ptr     pListOfInstances;    
00068    QStringList          listColumns;         
00069    QStringList          listRelations;       
00070    QVariant             id;                  
00071    long                 daoCount;            
00072 
00073    QxDaoAsyncParams() : daoAction(dao_none), pDatabase(NULL), daoCount(0) { ; }
00074    ~QxDaoAsyncParams() { ; }
00075 
00076 };
00077 
00078 typedef boost::shared_ptr<QxDaoAsyncParams> QxDaoAsyncParams_ptr;
00079 
00084 class QX_DLL_EXPORT QxDaoAsyncRunner : public QObject
00085 { Q_OBJECT
00086 
00087 public:
00088 
00089    QxDaoAsyncRunner();
00090    virtual ~QxDaoAsyncRunner();
00091 
00092 protected:
00093 
00094    QSqlError runQuery(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00095 
00096 Q_SIGNALS:
00097 
00098    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00099 
00100 public Q_SLOTS:
00101 
00102    void onQueryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00103 
00104 };
00105 
00106 } // namespace detail
00107 } // namespace dao
00108 
00152 class QX_DLL_EXPORT QxDaoAsync : public QThread
00153 { Q_OBJECT
00154 
00155 protected:
00156 
00157    QMutex m_mutex;                                       
00158    qx::dao::detail::QxDaoAsyncParams_ptr m_pDaoParams;   
00159 
00160 public:
00161 
00162    QxDaoAsync();
00163    virtual ~QxDaoAsync();
00164 
00165    bool asyncCount(const QString & className, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);
00166    bool asyncFetchById(IxPersistable_ptr pToFetch, const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00167    bool asyncFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00168    bool asyncFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00169    bool asyncInsert(IxPersistable_ptr pToInsert, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00170    bool asyncUpdate(IxPersistable_ptr pToUpdate, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00171    bool asyncSave(IxPersistable_ptr pToSave, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00172    bool asyncDeleteById(IxPersistable_ptr pToDelete, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00173    bool asyncDeleteAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00174    bool asyncDeleteByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00175    bool asyncDestroyById(IxPersistable_ptr pToDestroy, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00176    bool asyncDestroyAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00177    bool asyncDestroyByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00178    bool asyncExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00179    bool asyncCallQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00180 
00181    bool isQueryRunning() const { return (m_pDaoParams.get() != NULL); }
00182 
00183 protected:
00184 
00185    virtual void run();
00186 
00187    void startQuery();
00188 
00189 Q_SIGNALS:
00190 
00191    void queryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00192    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00193 
00194 private Q_SLOTS:
00195 
00196    void onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00197 
00198 };
00199 
00200 typedef boost::shared_ptr<QxDaoAsync> QxDaoAsync_ptr;
00201 
00202 } // namespace qx
00203 
00204 #endif // _QX_DAO_ASYNC_H_