![]() |
QxOrm
1.2.4
C++ Object Relational Mapping library
|
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 _IX_PERSISTABLE_H_ 00027 #define _IX_PERSISTABLE_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <boost/type_traits/is_base_of.hpp> 00041 00042 #include <QxDao/QxSqlQuery.h> 00043 #include <QxDao/QxDao.h> 00044 00045 #include <QxRegister/QxClass.h> 00046 00047 #include <QxCollection/QxCollection.h> 00048 00049 #include <QxTraits/get_primary_key.h> 00050 00051 #include <QxValidator/QxInvalidValueX.h> 00052 #include <QxValidator/QxValidatorFct.h> 00053 00054 namespace qx { 00055 00074 class QX_DLL_EXPORT IxPersistable 00075 { 00076 00077 public: 00078 00079 IxPersistable(); 00080 virtual ~IxPersistable(); 00081 00091 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0; 00092 00103 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0; 00104 00117 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00118 00132 virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00133 00148 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00149 00160 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00161 00174 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00175 00188 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00189 00203 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; 00204 00217 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0; 00218 00232 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00233 00244 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; 00245 00255 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0; 00256 00267 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00268 00275 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00276 00284 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL) = 0; 00285 00296 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; 00297 00305 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0; 00306 00311 virtual qx::IxCollection_ptr qxNewPersistableCollection() const = 0; 00312 00317 virtual qx::IxClass * qxClass() const = 0; 00318 00319 public: 00320 00321 static qx::IxCollection_ptr qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); 00322 static qx::IxCollection_ptr qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); 00323 static qx::IxCollection_ptr qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); 00324 00325 }; 00326 00327 typedef boost::shared_ptr<qx::IxPersistable> IxPersistable_ptr; 00328 00333 template <typename T> 00334 class IxPersistableCollection 00335 { 00336 00337 private: 00338 00339 typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key; 00340 typedef boost::shared_ptr<T> qx_type_ptr; 00341 00342 public: 00343 00344 typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type; 00345 00346 }; 00347 00348 } // namespace qx 00349 00350 namespace qx { 00351 namespace trait { 00352 00357 template <typename T> 00358 struct is_ix_persistable 00359 { enum { value = boost::is_base_of<qx::IxPersistable, T>::value }; }; 00360 00361 } // namespace trait 00362 } // namespace qx 00363 00364 #define QX_PERSISTABLE_HPP(className) \ 00365 public: \ 00366 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \ 00367 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \ 00368 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00369 virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00370 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00371 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00372 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00373 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ 00374 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ 00375 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \ 00376 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ 00377 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ 00378 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \ 00379 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ 00380 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ 00381 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL); \ 00382 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ 00383 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \ 00384 virtual qx::IxCollection_ptr qxNewPersistableCollection() const; \ 00385 virtual qx::IxClass * qxClass() const; 00386 00387 #define QX_PERSISTABLE_CPP(className) \ 00388 \ 00389 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ 00390 { \ 00391 return qx::dao::count< className >(query, pDatabase); \ 00392 } \ 00393 \ 00394 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ 00395 { \ 00396 return qx::dao::count< className >(lCount, query, pDatabase); \ 00397 } \ 00398 \ 00399 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ 00400 { \ 00401 if (id.isValid()) \ 00402 { \ 00403 qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ 00404 qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ 00405 if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \ 00406 if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ 00407 pDataMemberId->fromVariant(this, id); \ 00408 } \ 00409 QSqlError err; \ 00410 if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \ 00411 else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \ 00412 return err; \ 00413 } \ 00414 \ 00415 QSqlError className::qxFetchAll(qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ 00416 { \ 00417 qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \ 00418 if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchAll()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >"); qAssert(false); } \ 00419 if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchAll()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >'", "", QSqlError::UnknownError); } \ 00420 QSqlError err; \ 00421 if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \ 00422 else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \ 00423 return err; \ 00424 } \ 00425 \ 00426 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ 00427 { \ 00428 qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \ 00429 if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchByQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >"); qAssert(false); } \ 00430 if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchByQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >'", "", QSqlError::UnknownError); } \ 00431 QSqlError err; \ 00432 if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \ 00433 else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \ 00434 return err; \ 00435 } \ 00436 \ 00437 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \ 00438 { \ 00439 QSqlError err; \ 00440 if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \ 00441 else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \ 00442 return err; \ 00443 } \ 00444 \ 00445 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ 00446 { \ 00447 QSqlError err; \ 00448 if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \ 00449 else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \ 00450 return err; \ 00451 } \ 00452 \ 00453 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \ 00454 { \ 00455 QSqlError err; \ 00456 if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \ 00457 else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \ 00458 return err; \ 00459 } \ 00460 \ 00461 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \ 00462 { \ 00463 if (id.isValid()) \ 00464 { \ 00465 qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ 00466 qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ 00467 if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \ 00468 if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ 00469 pDataMemberId->fromVariant(this, id); \ 00470 } \ 00471 return qx::dao::delete_by_id((* this), pDatabase); \ 00472 } \ 00473 \ 00474 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \ 00475 { \ 00476 return qx::dao::delete_all< className >(pDatabase); \ 00477 } \ 00478 \ 00479 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ 00480 { \ 00481 return qx::dao::delete_by_query< className >(query, pDatabase); \ 00482 } \ 00483 \ 00484 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \ 00485 { \ 00486 if (id.isValid()) \ 00487 { \ 00488 qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ 00489 qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ 00490 if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \ 00491 if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ 00492 pDataMemberId->fromVariant(this, id); \ 00493 } \ 00494 return qx::dao::destroy_by_id((* this), pDatabase); \ 00495 } \ 00496 \ 00497 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \ 00498 { \ 00499 return qx::dao::destroy_all< className >(pDatabase); \ 00500 } \ 00501 \ 00502 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ 00503 { \ 00504 return qx::dao::destroy_by_query< className >(query, pDatabase); \ 00505 } \ 00506 \ 00507 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ 00508 { \ 00509 return qx::dao::execute_query(query, (* this), pDatabase); \ 00510 } \ 00511 \ 00512 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase) \ 00513 { \ 00514 qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \ 00515 if (! list_typed) { qDebug("[QxOrm] problem with 'qxExecuteQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >"); qAssert(false); } \ 00516 if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxExecuteQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, boost::shared_ptr<type> >'", "", QSqlError::UnknownError); } \ 00517 return qx::dao::execute_query(query, (* list_typed), pDatabase); \ 00518 } \ 00519 \ 00520 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \ 00521 { \ 00522 if (id.isValid()) \ 00523 { \ 00524 qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ 00525 qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ 00526 if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \ 00527 if (! pDataMemberId) { return qx_bool(false); } \ 00528 pDataMemberId->fromVariant(this, id); \ 00529 } \ 00530 return qx::dao::exist((* this), pDatabase); \ 00531 } \ 00532 \ 00533 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \ 00534 { \ 00535 return qx::validate((* this), groups); \ 00536 } \ 00537 \ 00538 qx::IxCollection_ptr className::qxNewPersistableCollection() const \ 00539 { \ 00540 qx::IxCollection_ptr coll; \ 00541 coll.reset(new qx::IxPersistableCollection< className >::type()); \ 00542 return coll; \ 00543 } \ 00544 \ 00545 qx::IxClass * className::qxClass() const \ 00546 { \ 00547 return qx::QxClass< className >::getSingleton(); \ 00548 } \ 00549 00550 #endif // _IX_PERSISTABLE_H_