QxOrm  1.4.5
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 
00087    QxDaoAsyncParams() : daoAction(dao_none), pDatabase(NULL), daoCount(0) { ; }
00088    ~QxDaoAsyncParams() { ; }
00089 
00090 };
00091 
00092 typedef std::shared_ptr<QxDaoAsyncParams> QxDaoAsyncParams_ptr;
00093 
00098 class QX_DLL_EXPORT QxDaoAsyncRunner : public QObject
00099 {
00100 
00101    Q_OBJECT
00102 
00103 public:
00104 
00105    QxDaoAsyncRunner();
00106    virtual ~QxDaoAsyncRunner();
00107 
00108 protected:
00109 
00110    QSqlError runQuery(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00111 
00112 Q_SIGNALS:
00113 
00114    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00115 
00116 public Q_SLOTS:
00117 
00118    void onQueryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00119 
00120 };
00121 
00122 } // namespace detail
00123 } // namespace dao
00124 
00169 class QX_DLL_EXPORT QxDaoAsync : public QThread
00170 {
00171 
00172    Q_OBJECT
00173 
00174 protected:
00175 
00176    QMutex m_mutex;                                       
00177    qx::dao::detail::QxDaoAsyncParams_ptr m_pDaoParams;   
00178 
00179 public:
00180 
00181    QxDaoAsync();
00182    virtual ~QxDaoAsync();
00183 
00184    bool asyncCount(const QString & className, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);
00185    bool asyncFetchById(IxPersistable_ptr pToFetch, const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00186    bool asyncFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00187    bool asyncFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00188    bool asyncInsert(IxPersistable_ptr pToInsert, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00189    bool asyncUpdate(IxPersistable_ptr pToUpdate, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00190    bool asyncSave(IxPersistable_ptr pToSave, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00191    bool asyncDeleteById(IxPersistable_ptr pToDelete, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00192    bool asyncDeleteAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00193    bool asyncDeleteByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00194    bool asyncDestroyById(IxPersistable_ptr pToDestroy, const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL);
00195    bool asyncDestroyAll(const QString & className, QSqlDatabase * pDatabase = NULL);
00196    bool asyncDestroyByQuery(const QString & className, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00197    bool asyncExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00198    bool asyncCallQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00199 
00200    bool isQueryRunning() const { return (m_pDaoParams.get() != NULL); }
00201 
00202 protected:
00203 
00204    virtual void run();
00205 
00206    void startQuery();
00207 
00208 Q_SIGNALS:
00209 
00210    void queryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00211    void queryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00212 
00213 private Q_SLOTS:
00214 
00215    void onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
00216 
00217 };
00218 
00219 typedef std::shared_ptr<QxDaoAsync> QxDaoAsync_ptr;
00220 
00221 } // namespace qx
00222 
00223 #endif // _QX_DAO_ASYNC_H_