| QxOrm 1.3.1 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 boost::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 boost::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 #define QX_PERSISTABLE_CPP(className) \
    00394 \
    00395 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00396 { \
    00397    return qx::dao::count< className >(query, pDatabase); \
    00398 } \
    00399 \
    00400 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00401 { \
    00402    return qx::dao::count< className >(lCount, query, pDatabase); \
    00403 } \
    00404 \
    00405 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00406 { \
    00407    if (id.isValid()) \
    00408    { \
    00409       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00410       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00411       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00412       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00413       pDataMemberId->fromVariant(this, id); \
    00414    } \
    00415    QSqlError err; \
    00416    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
    00417    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
    00418    return err; \
    00419 } \
    00420 \
    00421 QSqlError className::qxFetchAll(qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00422 { \
    00423    qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \
    00424    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); } \
    00425    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); } \
    00426    QSqlError err; \
    00427    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
    00428    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
    00429    return err; \
    00430 } \
    00431 \
    00432 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00433 { \
    00434    qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \
    00435    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); } \
    00436    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); } \
    00437    QSqlError err; \
    00438    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
    00439    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
    00440    return err; \
    00441 } \
    00442 \
    00443 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \
    00444 { \
    00445    QSqlError err; \
    00446    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \
    00447    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
    00448    return err; \
    00449 } \
    00450 \
    00451 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00452 { \
    00453    QSqlError err; \
    00454    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \
    00455    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
    00456    return err; \
    00457 } \
    00458 \
    00459 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \
    00460 { \
    00461    QSqlError err; \
    00462    if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
    00463    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
    00464    return err; \
    00465 } \
    00466 \
    00467 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \
    00468 { \
    00469    if (id.isValid()) \
    00470    { \
    00471       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00472       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00473       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00474       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00475       pDataMemberId->fromVariant(this, id); \
    00476    } \
    00477    return qx::dao::delete_by_id((* this), pDatabase); \
    00478 } \
    00479 \
    00480 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
    00481 { \
    00482    return qx::dao::delete_all< className >(pDatabase); \
    00483 } \
    00484 \
    00485 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00486 { \
    00487    return qx::dao::delete_by_query< className >(query, pDatabase); \
    00488 } \
    00489 \
    00490 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \
    00491 { \
    00492    if (id.isValid()) \
    00493    { \
    00494       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00495       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00496       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00497       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00498       pDataMemberId->fromVariant(this, id); \
    00499    } \
    00500    return qx::dao::destroy_by_id((* this), pDatabase); \
    00501 } \
    00502 \
    00503 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
    00504 { \
    00505    return qx::dao::destroy_all< className >(pDatabase); \
    00506 } \
    00507 \
    00508 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00509 { \
    00510    return qx::dao::destroy_by_query< className >(query, pDatabase); \
    00511 } \
    00512 \
    00513 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00514 { \
    00515    return qx::dao::execute_query(query, (* this), pDatabase); \
    00516 } \
    00517 \
    00518 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxCollection & list, QSqlDatabase * pDatabase) \
    00519 { \
    00520    qx::IxPersistableCollection< className >::type * list_typed = dynamic_cast< qx::IxPersistableCollection< className >::type * >(& list); \
    00521    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); } \
    00522    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); } \
    00523    return qx::dao::execute_query(query, (* list_typed), pDatabase); \
    00524 } \
    00525 \
    00526 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
    00527 { \
    00528    if (id.isValid()) \
    00529    { \
    00530       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00531       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00532       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00533       if (! pDataMemberId) { return qx_bool(false); } \
    00534       pDataMemberId->fromVariant(this, id); \
    00535    } \
    00536    return qx::dao::exist((* this), pDatabase); \
    00537 } \
    00538 \
    00539 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
    00540 { \
    00541    return qx::validate((* this), groups); \
    00542 } \
    00543 \
    00544 qx::IxCollection_ptr className::qxNewPersistableCollection() const \
    00545 { \
    00546    qx::IxCollection_ptr coll; \
    00547    coll.reset(new qx::IxPersistableCollection< className >::type()); \
    00548    return coll; \
    00549 } \
    00550 \
    00551 qx::IxClass * className::qxClass() const \
    00552 { \
    00553    return qx::QxClass< className >::getSingleton(); \
    00554 } \
    00555
    00556 #endif // _IX_PERSISTABLE_H_