| QxOrm 1.4.2 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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 <boost/type_traits/is_base_of.hpp>
    00047
    00048 #include <QxDao/QxSqlQuery.h>
    00049 #include <QxDao/QxDao.h>
    00050
    00051 #include <QxRegister/QxClass.h>
    00052
    00053 #include <QxCollection/QxCollection.h>
    00054
    00055 #include <QxTraits/get_primary_key.h>
    00056
    00057 #include <QxValidator/QxInvalidValueX.h>
    00058 #include <QxValidator/QxValidatorFct.h>
    00059
    00060 namespace qx {
    00061
    00080 class QX_DLL_EXPORT IxPersistable
    00081 {
    00082
    00083 public:
    00084
    00085    IxPersistable();
    00086    virtual ~IxPersistable();
    00087
    00097    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
    00098
    00109    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
    00110
    00123    virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00124
    00138    virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00139
    00154    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00155
    00166    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00167
    00180    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00181
    00194    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00195
    00209    virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00210
    00223    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
    00224
    00238    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
    00239
    00250    virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00251
    00261    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
    00262
    00273    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
    00274
    00281    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
    00282
    00290    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL) = 0;
    00291
    00302    virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00303
    00311    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
    00312
    00317    virtual qx::IxCollection_ptr qxNewPersistableCollection() const = 0;
    00318
    00323    virtual qx::IxClass * qxClass() const = 0;
    00324
    00325 public:
    00326
    00327    static qx::IxCollection_ptr qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
    00328    static qx::IxCollection_ptr qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
    00329    static qx::IxCollection_ptr qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
    00330
    00331 };
    00332
    00333 typedef qx_shared_ptr<qx::IxPersistable> IxPersistable_ptr;
    00334
    00339 template <typename T>
    00340 class IxPersistableCollection
    00341 {
    00342
    00343 private:
    00344
    00345    typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key;
    00346    typedef qx_shared_ptr<T> qx_type_ptr;
    00347
    00348 public:
    00349
    00350    typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type;
    00351
    00352 };
    00353
    00354 } // namespace qx
    00355
    00356 namespace qx {
    00357 namespace trait {
    00358
    00363 template <typename T>
    00364 struct is_ix_persistable
    00365 { enum { value = boost::is_base_of<qx::IxPersistable, T>::value }; };
    00366
    00367 } // namespace trait
    00368 } // namespace qx
    00369
    00370 #define QX_PERSISTABLE_HPP(className) \
    00371 public: \
    00372 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
    00373 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
    00374 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00375 virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00376 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00377 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00378 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00379 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00380 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00381 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \
    00382 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00383 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00384 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \
    00385 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00386 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00387 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase = NULL); \
    00388 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00389 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \
    00390 virtual qx::IxCollection_ptr qxNewPersistableCollection() const; \
    00391 virtual qx::IxClass * qxClass() const;
    00392
    00393 #ifdef _QX_NO_RTTI
    00394 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
    00395 qx::IxPersistableCollection< className >::type * list_typed = static_cast< qx::IxPersistableCollection< className >::type * >(& list);
    00396 #else // _QX_NO_RTTI
    00397 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
    00398 qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list);
    00399 #endif // _QX_NO_RTTI
    00400
    00401 #define QX_PERSISTABLE_CPP(className) \
    00402 \
    00403 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00404 { \
    00405    return qx::dao::count< className >(query, pDatabase); \
    00406 } \
    00407 \
    00408 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00409 { \
    00410    return qx::dao::count< className >(lCount, query, pDatabase); \
    00411 } \
    00412 \
    00413 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00414 { \
    00415    if (id.isValid()) \
    00416    { \
    00417       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00418       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00419       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00420       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00421       pDataMemberId->fromVariant(this, id); \
    00422    } \
    00423    QSqlError err; \
    00424    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
    00425    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
    00426    return err; \
    00427 } \
    00428 \
    00429 QSqlError className::qxFetchAll(qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00430 { \
    00431    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00432    if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchAll()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >"); qAssert(false); } \
    00433    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchAll()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >'", "", QSqlError::UnknownError); } \
    00434    QSqlError err; \
    00435    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
    00436    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
    00437    return err; \
    00438 } \
    00439 \
    00440 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00441 { \
    00442    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00443    if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchByQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >"); qAssert(false); } \
    00444    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchByQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >'", "", QSqlError::UnknownError); } \
    00445    QSqlError err; \
    00446    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
    00447    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
    00448    return err; \
    00449 } \
    00450 \
    00451 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \
    00452 { \
    00453    QSqlError err; \
    00454    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \
    00455    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
    00456    return err; \
    00457 } \
    00458 \
    00459 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00460 { \
    00461    QSqlError err; \
    00462    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \
    00463    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
    00464    return err; \
    00465 } \
    00466 \
    00467 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \
    00468 { \
    00469    QSqlError err; \
    00470    if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
    00471    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
    00472    return err; \
    00473 } \
    00474 \
    00475 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \
    00476 { \
    00477    if (id.isValid()) \
    00478    { \
    00479       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00480       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00481       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00482       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00483       pDataMemberId->fromVariant(this, id); \
    00484    } \
    00485    return qx::dao::delete_by_id((* this), pDatabase); \
    00486 } \
    00487 \
    00488 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
    00489 { \
    00490    return qx::dao::delete_all< className >(pDatabase); \
    00491 } \
    00492 \
    00493 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00494 { \
    00495    return qx::dao::delete_by_query< className >(query, pDatabase); \
    00496 } \
    00497 \
    00498 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \
    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 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00505       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00506       pDataMemberId->fromVariant(this, id); \
    00507    } \
    00508    return qx::dao::destroy_by_id((* this), pDatabase); \
    00509 } \
    00510 \
    00511 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
    00512 { \
    00513    return qx::dao::destroy_all< className >(pDatabase); \
    00514 } \
    00515 \
    00516 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00517 { \
    00518    return qx::dao::destroy_by_query< className >(query, pDatabase); \
    00519 } \
    00520 \
    00521 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00522 { \
    00523    return qx::dao::execute_query(query, (* this), pDatabase); \
    00524 } \
    00525 \
    00526 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase) \
    00527 { \
    00528    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00529    if (! list_typed) { qDebug("[QxOrm] problem with 'qxExecuteQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >"); qAssert(false); } \
    00530    if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxExecuteQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, qx_shared_ptr<type> >'", "", QSqlError::UnknownError); } \
    00531    return qx::dao::execute_query(query, (* list_typed), pDatabase); \
    00532 } \
    00533 \
    00534 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
    00535 { \
    00536    if (id.isValid()) \
    00537    { \
    00538       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00539       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00540       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00541       if (! pDataMemberId) { return qx_bool(false); } \
    00542       pDataMemberId->fromVariant(this, id); \
    00543    } \
    00544    return qx::dao::exist((* this), pDatabase); \
    00545 } \
    00546 \
    00547 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
    00548 { \
    00549    return qx::validate((* this), groups); \
    00550 } \
    00551 \
    00552 qx::IxCollection_ptr className::qxNewPersistableCollection() const \
    00553 { \
    00554    qx::IxCollection_ptr coll; \
    00555    coll.reset(new qx::IxPersistableCollection< className >::type()); \
    00556    return coll; \
    00557 } \
    00558 \
    00559 qx::IxClass * className::qxClass() const \
    00560 { \
    00561    return qx::QxClass< className >::getSingleton(); \
    00562 } \
    00563
    00564 #endif // _IX_PERSISTABLE_H_