QxOrm  1.2.8
C++ Object Relational Mapping library
QxDaoAsync.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://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 #include <QtCore/qqueue.h>
00047 
00048 #include <QtSql/qsqlerror.h>
00049 
00050 #ifndef Q_MOC_RUN
00051 #include <QxDao/IxPersistable.h>
00052 #include <QxDao/QxSqlQuery.h>
00053 #endif // Q_MOC_RUN
00054 
00055 namespace qx {
00056 namespace dao {
00057 namespace detail {
00058 
00063 struct QxDaoAsyncParams
00064 {
00065 
00066    enum dao_action { dao_none, dao_count, dao_fetch_by_id, dao_fetch_all, dao_fetch_by_query, dao_insert, 
00067                      dao_update, dao_save, dao_delete_by_id, dao_delete_all, dao_delete_by_query, 
00068                      dao_destroy_by_id, dao_destroy_all, dao_destroy_by_query, dao_execute_query, dao_call_query };
00069 
00070    dao_action           daoAction;           
00071    QString              className;           
00072    qx::QxSqlQuery       query;               
00073    QSqlDatabase *       pDatabase;           
00074    IxPersistable_ptr    pInstance;           
00075    IxCollection_ptr     pListOfInstances;    
00076    QStringList          listColumns;         
00077    QStringList          listRelations;       
00078    QVariant             id;                  
00079    long                 daoCount;            
00080 
00081    QxDaoAsyncParams() : daoAction(dao_none), pDatabase(NULL), daoCount(0) { ; }
00082    ~QxDaoAsyncParams() { ; }
00083 
00084 };
00085 
00086 typedef boost::shared_ptr<QxDaoAsyncParams> QxDaoAsyncParams_ptr;
00087 
00092 class QX_DLL_EXPORT QxDaoAsyncRunner : public QObject
00093 { Q_OBJECT
00094 
00095 public:
00096 
00097    QxDaoAsyncRunner();
00098    virtual ~QxDaoAsyncRunner();
00099 
00100 protected:
00101 
00102    QSqlError runQuery(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00103 
00104 Q_SIGNALS:
00105 
00106    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00107 
00108 public Q_SLOTS:
00109 
00110    void onQueryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00111 
00112 };
00113 
00114 } // namespace detail
00115 } // namespace dao
00116 
00160 class QX_DLL_EXPORT QxDaoAsync : public QThread
00161 { Q_OBJECT
00162 
00163 protected:
00164 
00165    QMutex m_mutex;                                       
00166    qx::dao::detail::QxDaoAsyncParams_ptr m_pDaoParams;   
00167 
00168 public:
00169 
00170    QxDaoAsync();
00171    virtual ~QxDaoAsync();
00172 
00173    bool asyncCount(const QString & className, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);
00174    bool asyncFetchById(IxPersistable_ptr pToFetch, const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00175    bool asyncFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00176    bool asyncFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00177    bool asyncInsert(IxPersistable_ptr pToInsert, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00178    bool asyncUpdate(IxPersistable_ptr pToUpdate, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00179    bool asyncSave(IxPersistable_ptr pToSave, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00180    bool asyncDeleteById(IxPersistable_ptr pToDelete, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00181    bool asyncDeleteAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00182    bool asyncDeleteByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00183    bool asyncDestroyById(IxPersistable_ptr pToDestroy, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00184    bool asyncDestroyAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00185    bool asyncDestroyByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00186    bool asyncExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00187    bool asyncCallQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00188 
00189    bool isQueryRunning() const { return (m_pDaoParams.get() != NULL); }
00190 
00191 protected:
00192 
00193    virtual void run();
00194 
00195    void startQuery();
00196 
00197 Q_SIGNALS:
00198 
00199    void queryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00200    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00201 
00202 private Q_SLOTS:
00203 
00204    void onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00205 
00206 };
00207 
00208 typedef boost::shared_ptr<QxDaoAsync> QxDaoAsync_ptr;
00209 
00210 } // namespace qx
00211 
00212 #endif // _QX_DAO_ASYNC_H_