QxOrm  1.4.4
C++ Object Relational Mapping library
IxPersistable.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 _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 
00049 #include <QxRegister/QxClass.h>
00050 
00051 #include <QxCollection/QxCollection.h>
00052 
00053 #include <QxTraits/get_primary_key.h>
00054 
00055 #include <QxValidator/QxInvalidValueX.h>
00056 #include <QxValidator/QxValidatorFct.h>
00057 
00058 namespace qx {
00059 
00078 class QX_DLL_EXPORT IxPersistable
00079 {
00080 
00081 public:
00082 
00083    IxPersistable();
00084    virtual ~IxPersistable();
00085 
00095    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00096 
00107    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00108 
00121    virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00122 
00136    virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00137 
00152    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00153 
00164    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00165 
00178    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00179 
00192    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00193 
00207    virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
00208 
00221    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
00222 
00236    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00237 
00248    virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
00249 
00259    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
00260 
00271    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00272 
00279    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00280 
00288    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL) = 0;
00289 
00300    virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
00301 
00309    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
00310 
00315    virtual qx::IxCollection_ptr qxNewPersistableCollection() const = 0;
00316 
00321    virtual qx::IxClass * qxClass() const = 0;
00322 
00323 public:
00324 
00325    static qx::IxCollection_ptr qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00326    static qx::IxCollection_ptr qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
00327    static qx::IxCollection_ptr qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
00328 
00329 };
00330 
00331 typedef std::shared_ptr<qx::IxPersistable> IxPersistable_ptr;
00332 
00337 template <typename T>
00338 class IxPersistableCollection
00339 {
00340 
00341 private:
00342 
00343    typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key;
00344    typedef std::shared_ptr<T> qx_type_ptr;
00345 
00346 public:
00347 
00348    typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type;
00349 
00350 };
00351 
00352 } // namespace qx
00353 
00354 namespace qx {
00355 namespace trait {
00356 
00361 template <typename T>
00362 struct is_ix_persistable
00363 { enum { value = std::is_base_of<qx::IxPersistable, T>::value }; };
00364 
00365 } // namespace trait
00366 } // namespace qx
00367 
00368 #define QX_PERSISTABLE_HPP(className) \
00369 public: \
00370 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
00371 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
00372 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00373 virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00374 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00375 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00376 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00377 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
00378 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
00379 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \
00380 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00381 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
00382 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \
00383 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00384 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
00385 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL); \
00386 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
00387 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \
00388 virtual qx::IxCollection_ptr qxNewPersistableCollection() const; \
00389 virtual qx::IxClass * qxClass() const;
00390 
00391 #ifdef _QX_NO_RTTI
00392 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
00393 qx::IxPersistableCollection< className >::type * list_typed = static_cast< qx::IxPersistableCollection< className >::type * >(& list);
00394 #else // _QX_NO_RTTI
00395 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
00396 qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list);
00397 #endif // _QX_NO_RTTI
00398 
00399 #define QX_PERSISTABLE_CPP(className) \
00400 \
00401 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00402 { \
00403    return qx::dao::count< className >(query, pDatabase); \
00404 } \
00405 \
00406 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00407 { \
00408    return qx::dao::count< className >(lCount, query, pDatabase); \
00409 } \
00410 \
00411 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00412 { \
00413    if (id.isValid()) \
00414    { \
00415       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00416       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00417       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00418       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00419       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00420    } \
00421    QSqlError err; \
00422    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
00423    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
00424    return err; \
00425 } \
00426 \
00427 QSqlError className::qxFetchAll(qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00428 { \
00429    QX_PERSISTABLE_CAST_COLLECTION(className) \
00430    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); } \
00431    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); } \
00432    QSqlError err; \
00433    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
00434    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
00435    return err; \
00436 } \
00437 \
00438 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00439 { \
00440    QX_PERSISTABLE_CAST_COLLECTION(className) \
00441    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); } \
00442    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); } \
00443    QSqlError err; \
00444    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
00445    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
00446    return err; \
00447 } \
00448 \
00449 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \
00450 { \
00451    QSqlError err; \
00452    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \
00453    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
00454    return err; \
00455 } \
00456 \
00457 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
00458 { \
00459    QSqlError err; \
00460    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \
00461    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
00462    return err; \
00463 } \
00464 \
00465 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \
00466 { \
00467    QSqlError err; \
00468    if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
00469    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
00470    return err; \
00471 } \
00472 \
00473 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \
00474 { \
00475    if (id.isValid()) \
00476    { \
00477       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00478       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00479       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00480       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00481       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00482    } \
00483    return qx::dao::delete_by_id((* this), pDatabase); \
00484 } \
00485 \
00486 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
00487 { \
00488    return qx::dao::delete_all< className >(pDatabase); \
00489 } \
00490 \
00491 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00492 { \
00493    return qx::dao::delete_by_query< className >(query, pDatabase); \
00494 } \
00495 \
00496 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \
00497 { \
00498    if (id.isValid()) \
00499    { \
00500       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00501       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00502       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
00503       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
00504       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00505    } \
00506    return qx::dao::destroy_by_id((* this), pDatabase); \
00507 } \
00508 \
00509 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
00510 { \
00511    return qx::dao::destroy_all< className >(pDatabase); \
00512 } \
00513 \
00514 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00515 { \
00516    return qx::dao::destroy_by_query< className >(query, pDatabase); \
00517 } \
00518 \
00519 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
00520 { \
00521    return qx::dao::execute_query(query, (* this), pDatabase); \
00522 } \
00523 \
00524 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase) \
00525 { \
00526    QX_PERSISTABLE_CAST_COLLECTION(className) \
00527    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); } \
00528    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); } \
00529    return qx::dao::execute_query(query, (* list_typed), pDatabase); \
00530 } \
00531 \
00532 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
00533 { \
00534    if (id.isValid()) \
00535    { \
00536       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
00537       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
00538       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
00539       if (! pDataMemberId) { return qx_bool(false); } \
00540       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
00541    } \
00542    return qx::dao::exist((* this), pDatabase); \
00543 } \
00544 \
00545 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
00546 { \
00547    return qx::validate((* this), groups); \
00548 } \
00549 \
00550 qx::IxCollection_ptr className::qxNewPersistableCollection() const \
00551 { \
00552    qx::IxCollection_ptr coll; \
00553    coll.reset(new qx::IxPersistableCollection< className >::type()); \
00554    return coll; \
00555 } \
00556 \
00557 qx::IxClass * className::qxClass() const \
00558 { \
00559    return qx::QxClass< className >::getSingleton(); \
00560 } \
00561 
00562 #endif // _IX_PERSISTABLE_H_