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