QxOrm  1.5.0
C++ Object Relational Mapping library
IxPersistable.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 _IX_PERSISTABLE_H_
00033 #define _IX_PERSISTABLE_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDao/QxSqlQuery.h>
00047 #include <QxDao/QxDao.h>
00048 #include <QxDao/QxSqlSaveMode.h>
00049 
00050 #include <QxRegister/QxClass.h>
00051 
00052 #include <QxCollection/QxCollection.h>
00053 
00054 #include <QxValidator/QxInvalidValueX.h>
00055 #include <QxValidator/QxValidatorFct.h>
00056 
00057 #include <QxTraits/get_class_name.h>
00058 
00059 namespace qx {
00060 
00061 class IxPersistableCollection;
00062 
00081 class QX_DLL_EXPORT IxPersistable
00082 {
00083 
00084 public:
00085 
00086    IxPersistable();
00087    virtual ~IxPersistable();
00088 
00099    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) = 0;
00100 
00112    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) = 0;
00113 
00126    virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00127 
00141    virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00142 
00157    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00158 
00170    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) = 0;
00171 
00185    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) = 0;
00186 
00200    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none) = 0;
00201 
00216    virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) = 0;
00217 
00230    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
00231 
00245    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00246 
00258    virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) = 0;
00259 
00269    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
00270 
00281    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00282 
00289    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00290 
00298    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL) = 0;
00299 
00310    virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
00311 
00319    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
00320 
00326    virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const = 0;
00327 
00332    virtual qx::IxClass * qxClass() const = 0;
00333 
00334 #ifndef _QX_NO_JSON
00335    virtual QString toJson(const QString & format = QString()) const = 0;
00336    virtual QJsonValue toJson_(const QString & format = QString()) const = 0;
00337    virtual qx_bool fromJson(const QString & json, const QString & format = QString()) = 0;
00338    virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString()) = 0;
00339 #endif // _QX_NO_JSON
00340 
00341 public:
00342 
00343    static std::shared_ptr<qx::IxPersistableCollection> qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bAsList = false);
00344    static std::shared_ptr<qx::IxPersistableCollection> qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bAsList = false);
00345    static std::shared_ptr<qx::IxPersistableCollection> qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL, bool bAsList = false);
00346 
00347 };
00348 
00349 typedef std::shared_ptr<qx::IxPersistable> IxPersistable_ptr;
00350 
00351 } // namespace qx
00352 
00353 namespace qx {
00354 namespace trait {
00355 
00360 template <typename T>
00361 struct is_ix_persistable
00362 { enum { value = std::is_base_of<qx::IxPersistable, T>::value }; };
00363 
00364 } // namespace trait
00365 } // namespace qx
00366 
00367 #ifndef _QX_NO_JSON
00368 #define QX_PERSISTABLE_JSON_HPP(className) \
00369 virtual QString toJson(const QString & format = QString()) const; \
00370 virtual QJsonValue toJson_(const QString & format = QString()) const; \
00371 virtual qx_bool fromJson(const QString & json, const QString & format = QString()); \
00372 virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString());
00373 #else // _QX_NO_JSON
00374 #define QX_PERSISTABLE_JSON_HPP(className) /* Nothing */
00375 #endif // _QX_NO_JSON
00376 
00377 #define QX_PERSISTABLE_HPP(className) \
00378 public: \
00379 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()); \
00380 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()); \
00381 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00382 virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00383 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00384 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false); \
00385 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false); \
00386 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none); \
00387 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false); \
00388 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \
00389 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00390 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false); \
00391 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \
00392 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00393 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00394 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL); \
00395 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
00396 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \
00397 virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const; \
00398 virtual qx::IxClass * qxClass() const; \
00399 QX_PERSISTABLE_JSON_HPP(className)
00400 
00401 #ifdef _QX_NO_RTTI
00402 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
00403 if (! list) { return QSqlError(); } \
00404 qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = static_cast< qx::QxPersistableCollectionHelper< className >::type * >(list);
00405 #else // _QX_NO_RTTI
00406 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
00407 if (! list) { return QSqlError(); } \
00408 qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = dynamic_cast< qx::QxPersistableCollectionHelper< className >::type * >(list);
00409 #endif // _QX_NO_RTTI
00410 
00411 #ifndef _QX_NO_JSON
00412 #define QX_PERSISTABLE_JSON_CPP(className) \
00413 QString className::toJson(const QString & format) const { return qx::serialization::json::to_string((* this), 1, format); } \
00414 QJsonValue className::toJson_(const QString & format) const { return qx::cvt::to_json((* this), format); } \
00415 qx_bool className::fromJson(const QString & json, const QString & format) { return qx::serialization::json::from_string((* this), json, 1, format); } \
00416 qx_bool className::fromJson_(const QJsonValue & json, const QString & format) { return qx::cvt::from_json(json, (* this), format); }
00417 #else // _QX_NO_JSON
00418 #define QX_PERSISTABLE_JSON_CPP(className) /* Nothing */
00419 #endif // _QX_NO_JSON
00420 
00421 #define QX_PERSISTABLE_CPP(className) \
00422 \
00423 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase, const QStringList & relation) \
00424 { \
00425    if (relation.count() == 0) { return qx::dao::count< className >(query, pDatabase); } \
00426    else { long lCount(0); qx::dao::count_with_relation< className >(lCount, relation, query, pDatabase); return lCount; } \
00427 } \
00428 \
00429 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase, const QStringList & relation) \
00430 { \
00431    if (relation.count() == 0) { return qx::dao::count< className >(lCount, query, pDatabase); } \
00432    else { return qx::dao::count_with_relation< className >(lCount, relation, query, pDatabase); } \
00433 } \
00434 \
00435 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00436 { \
00437    if (id.isValid()) \
00438    { \
00439       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00440       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00441       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00442       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00443       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00444    } \
00445    QSqlError err; \
00446    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
00447    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
00448    return err; \
00449 } \
00450 \
00451 QSqlError className::qxFetchAll(qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00452 { \
00453    QX_PERSISTABLE_CAST_COLLECTION(className) \
00454    if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchAll()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >"); qAssert(false); } \
00455    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchAll()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >'", "", QSqlError::UnknownError); } \
00456    QSqlError err; \
00457    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
00458    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
00459    return err; \
00460 } \
00461 \
00462 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00463 { \
00464    QX_PERSISTABLE_CAST_COLLECTION(className) \
00465    if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchByQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >"); qAssert(false); } \
00466    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchByQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >'", "", QSqlError::UnknownError); } \
00467    QSqlError err; \
00468    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
00469    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
00470    return err; \
00471 } \
00472 \
00473 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase, bool bUseExecBatch) \
00474 { \
00475    QSqlError err; \
00476    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase, bUseExecBatch); } \
00477    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
00478    return err; \
00479 } \
00480 \
00481 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase, bool bUseExecBatch) \
00482 { \
00483    QSqlError err; \
00484    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns, bUseExecBatch); } \
00485    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
00486    return err; \
00487 } \
00488 \
00489 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase, qx::dao::save_mode::e_save_mode eSaveRecursiveMode) \
00490 { \
00491    QSqlError err; \
00492    if (eSaveRecursiveMode != qx::dao::save_mode::e_none) { err = qx::dao::save_with_relation_recursive((* this), eSaveRecursiveMode, pDatabase); } \
00493    else if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
00494    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
00495    return err; \
00496 } \
00497 \
00498 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase, bool bUseExecBatch) \
00499 { \
00500    if (id.isValid()) \
00501    { \
00502       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00503       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00504       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00505       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00506       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00507    } \
00508    return qx::dao::delete_by_id((* this), pDatabase, bUseExecBatch); \
00509 } \
00510 \
00511 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
00512 { \
00513    return qx::dao::delete_all< className >(pDatabase); \
00514 } \
00515 \
00516 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00517 { \
00518    return qx::dao::delete_by_query< className >(query, pDatabase); \
00519 } \
00520 \
00521 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase, bool bUseExecBatch) \
00522 { \
00523    if (id.isValid()) \
00524    { \
00525       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00526       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00527       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00528       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00529       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00530    } \
00531    return qx::dao::destroy_by_id((* this), pDatabase, bUseExecBatch); \
00532 } \
00533 \
00534 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
00535 { \
00536    return qx::dao::destroy_all< className >(pDatabase); \
00537 } \
00538 \
00539 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00540 { \
00541    return qx::dao::destroy_by_query< className >(query, pDatabase); \
00542 } \
00543 \
00544 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00545 { \
00546    return qx::dao::execute_query(query, (* this), pDatabase); \
00547 } \
00548 \
00549 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list, QSqlDatabase * pDatabase) \
00550 { \
00551    QX_PERSISTABLE_CAST_COLLECTION(className) \
00552    if (! list_typed) { qDebug("[QxOrm] problem with 'qxExecuteQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >"); qAssert(false); } \
00553    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxExecuteQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr<type> >'", "", QSqlError::UnknownError); } \
00554    return qx::dao::execute_query(query, (* list_typed), pDatabase); \
00555 } \
00556 \
00557 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
00558 { \
00559    if (id.isValid()) \
00560    { \
00561       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00562       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00563       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
00564       if (! pDataMemberId) { return qx_bool(false); } \
00565       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00566    } \
00567    return qx::dao::exist((* this), pDatabase); \
00568 } \
00569 \
00570 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
00571 { \
00572    return qx::validate((* this), groups); \
00573 } \
00574 \
00575 std::shared_ptr<qx::IxPersistableCollection> className::qxNewPersistableCollection(bool bAsList) const \
00576 { \
00577    if (bAsList) { std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableList< className > >(); return coll; } \
00578    else { std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableCollectionHelper< className >::type>(); return coll; } \
00579 } \
00580 \
00581 qx::IxClass * className::qxClass() const \
00582 { \
00583    return qx::QxClass< className >::getSingleton(); \
00584 } \
00585 \
00586 QX_PERSISTABLE_JSON_CPP(className)
00587 
00588 QX_REGISTER_CLASS_NAME(qx::IxPersistable)
00589 
00590 #include <QxDao/IxPersistableCollection.h>
00591 
00592 #endif // _IX_PERSISTABLE_H_