| QxOrm 1.2.3 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 ** 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 <QxDao/QxSqlQuery.h>
    00041 #include <QxDao/QxDao.h>
    00042
    00043 #include <QxRegister/QxClass.h>
    00044
    00045 #include <QxCollection/QxCollection.h>
    00046
    00047 #include <QxTraits/get_primary_key.h>
    00048
    00049 #include <QxValidator/QxInvalidValueX.h>
    00050 #include <QxValidator/QxValidatorFct.h>
    00051
    00052 namespace qx {
    00053
    00072 class QX_DLL_EXPORT IxPersistable
    00073 {
    00074
    00075 public:
    00076
    00077    IxPersistable();
    00078    virtual ~IxPersistable();
    00079
    00089    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
    00090
    00103    virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00104
    00118    virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00119
    00134    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00135
    00146    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00147
    00160    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00161
    00174    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00175
    00189    virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00190
    00203    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
    00204
    00218    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
    00219
    00230    virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00231
    00241    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
    00242
    00253    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
    00254
    00265    virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0;
    00266
    00274    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
    00275
    00280    virtual qx::IxCollection_ptr qxNewPersistableCollection() const = 0;
    00281
    00286    virtual qx::IxClass * qxClass() const = 0;
    00287
    00288 public:
    00289
    00290    static qx::IxCollection_ptr qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
    00291    static qx::IxCollection_ptr qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
    00292
    00293 };
    00294
    00295 typedef boost::shared_ptr<qx::IxPersistable> IxPersistable_ptr;
    00296
    00301 template <typename T>
    00302 class IxPersistableCollection
    00303 {
    00304
    00305 private:
    00306
    00307    typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key;
    00308    typedef boost::shared_ptr<T> qx_type_ptr;
    00309
    00310 public:
    00311
    00312    typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type;
    00313
    00314 };
    00315
    00316 } // namespace qx
    00317
    00318 #define QX_PERSISTABLE_HPP(className) \
    00319 public: \
    00320 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
    00321 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00322 virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00323 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00324 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00325 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00326 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00327 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00328 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \
    00329 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00330 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00331 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \
    00332 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00333 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00334 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \
    00335 virtual qx::IxCollection_ptr qxNewPersistableCollection() const; \
    00336 virtual qx::IxClass * qxClass() const;
    00337
    00338 #define QX_PERSISTABLE_CPP(className) \
    00339 \
    00340 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00341 { \
    00342    return qx::dao::count< className >(query, pDatabase); \
    00343 } \
    00344 \
    00345 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00346 { \
    00347    if (id.isValid()) \
    00348    { \
    00349       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00350       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00351       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00352       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00353       pDataMemberId->fromVariant(this, id); \
    00354    } \
    00355    QSqlError err; \
    00356    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
    00357    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
    00358    return err; \
    00359 } \
    00360 \
    00361 QSqlError className::qxFetchAll(qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00362 { \
    00363    qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \
    00364    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); } \
    00365    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); } \
    00366    QSqlError err; \
    00367    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
    00368    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
    00369    return err; \
    00370 } \
    00371 \
    00372 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00373 { \
    00374    qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \
    00375    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); } \
    00376    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); } \
    00377    QSqlError err; \
    00378    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
    00379    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
    00380    return err; \
    00381 } \
    00382 \
    00383 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \
    00384 { \
    00385    QSqlError err; \
    00386    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \
    00387    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
    00388    return err; \
    00389 } \
    00390 \
    00391 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00392 { \
    00393    QSqlError err; \
    00394    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \
    00395    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
    00396    return err; \
    00397 } \
    00398 \
    00399 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \
    00400 { \
    00401    QSqlError err; \
    00402    if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
    00403    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
    00404    return err; \
    00405 } \
    00406 \
    00407 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \
    00408 { \
    00409    if (id.isValid()) \
    00410    { \
    00411       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00412       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00413       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00414       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00415       pDataMemberId->fromVariant(this, id); \
    00416    } \
    00417    return qx::dao::delete_by_id((* this), pDatabase); \
    00418 } \
    00419 \
    00420 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
    00421 { \
    00422    return qx::dao::delete_all< className >(pDatabase); \
    00423 } \
    00424 \
    00425 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00426 { \
    00427    return qx::dao::delete_by_query< className >(query, pDatabase); \
    00428 } \
    00429 \
    00430 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \
    00431 { \
    00432    if (id.isValid()) \
    00433    { \
    00434       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00435       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00436       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00437       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00438       pDataMemberId->fromVariant(this, id); \
    00439    } \
    00440    return qx::dao::destroy_by_id((* this), pDatabase); \
    00441 } \
    00442 \
    00443 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
    00444 { \
    00445    return qx::dao::destroy_all< className >(pDatabase); \
    00446 } \
    00447 \
    00448 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00449 { \
    00450    return qx::dao::destroy_by_query< className >(query, pDatabase); \
    00451 } \
    00452 \
    00453 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
    00454 { \
    00455    if (id.isValid()) \
    00456    { \
    00457       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00458       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00459       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00460       if (! pDataMemberId) { return qx_bool(false); } \
    00461       pDataMemberId->fromVariant(this, id); \
    00462    } \
    00463    return qx::dao::exist((* this), pDatabase); \
    00464 } \
    00465 \
    00466 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
    00467 { \
    00468    return qx::validate((* this), groups); \
    00469 } \
    00470 \
    00471 qx::IxCollection_ptr className::qxNewPersistableCollection() const \
    00472 { \
    00473    qx::IxCollection_ptr coll; \
    00474    coll.reset(new qx::IxPersistableCollection< className >::type()); \
    00475    return coll; \
    00476 } \
    00477 \
    00478 qx::IxClass * className::qxClass() const \
    00479 { \
    00480    return qx::QxClass< className >::getSingleton(); \
    00481 } \
    00482
    00483 #endif // _IX_PERSISTABLE_H_