| QxOrm 1.4.5 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 ** 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
    00049 #include <QxRegister/QxClass.h>
    00050
    00051 #include <QxCollection/QxCollection.h>
    00052
    00053 #include <QxValidator/QxInvalidValueX.h>
    00054 #include <QxValidator/QxValidatorFct.h>
    00055
    00056 #include <QxTraits/get_class_name.h>
    00057
    00058 namespace qx {
    00059
    00060 class IxPersistableCollection;
    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::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
    00139
    00154    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, 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::IxPersistableCollection * list = NULL, 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 std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection() const = 0;
    00318
    00323    virtual qx::IxClass * qxClass() const = 0;
    00324
    00325 #ifndef _QX_NO_JSON
    00326    virtual QString toJson(const QString & format = QString()) const = 0;
    00327    virtual qx_bool fromJson(const QString & json, const QString & format = QString()) = 0;
    00328 #endif // _QX_NO_JSON
    00329
    00330 public:
    00331
    00332    static std::shared_ptr<qx::IxPersistableCollection> qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL);
    00333    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);
    00334    static std::shared_ptr<qx::IxPersistableCollection> qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
    00335
    00336 };
    00337
    00338 typedef std::shared_ptr<qx::IxPersistable> IxPersistable_ptr;
    00339
    00340 } // namespace qx
    00341
    00342 namespace qx {
    00343 namespace trait {
    00344
    00349 template <typename T>
    00350 struct is_ix_persistable
    00351 { enum { value = std::is_base_of<qx::IxPersistable, T>::value }; };
    00352
    00353 } // namespace trait
    00354 } // namespace qx
    00355
    00356 #ifndef _QX_NO_JSON
    00357 #define QX_PERSISTABLE_JSON_HPP(className) \
    00358 virtual QString toJson(const QString & format = QString()) const; \
    00359 virtual qx_bool fromJson(const QString & json, const QString & format = QString());
    00360 #else // _QX_NO_JSON
    00361 #define QX_PERSISTABLE_JSON_HPP(className) /* Nothing */
    00362 #endif // _QX_NO_JSON
    00363
    00364 #define QX_PERSISTABLE_HPP(className) \
    00365 public: \
    00366 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
    00367 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); \
    00368 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00369 virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00370 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00371 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00372 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00373 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \
    00374 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00375 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \
    00376 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00377 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00378 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \
    00379 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00380 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \
    00381 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL); \
    00382 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \
    00383 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \
    00384 virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection() const; \
    00385 virtual qx::IxClass * qxClass() const; \
    00386 QX_PERSISTABLE_JSON_HPP(className)
    00387
    00388 #ifdef _QX_NO_RTTI
    00389 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
    00390 if (! list) { return QSqlError(); } \
    00391 qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = static_cast< qx::QxPersistableCollectionHelper< className >::type * >(list);
    00392 #else // _QX_NO_RTTI
    00393 #define QX_PERSISTABLE_CAST_COLLECTION(className) \
    00394 if (! list) { return QSqlError(); } \
    00395 qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = dynamic_cast< qx::QxPersistableCollectionHelper< className >::type * >(list);
    00396 #endif // _QX_NO_RTTI
    00397
    00398 #ifndef _QX_NO_JSON
    00399 #define QX_PERSISTABLE_JSON_CPP(className) \
    00400 QString className::toJson(const QString & format) const { return qx::serialization::json::to_string((* this), 1, format); } \
    00401 qx_bool className::fromJson(const QString & json, const QString & format) { return qx::serialization::json::from_string((* this), json, 1, format); }
    00402 #else // _QX_NO_JSON
    00403 #define QX_PERSISTABLE_JSON_CPP(className) /* Nothing */
    00404 #endif // _QX_NO_JSON
    00405
    00406 #define QX_PERSISTABLE_CPP(className) \
    00407 \
    00408 long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00409 { \
    00410    return qx::dao::count< className >(query, pDatabase); \
    00411 } \
    00412 \
    00413 QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00414 { \
    00415    return qx::dao::count< className >(lCount, query, pDatabase); \
    00416 } \
    00417 \
    00418 QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00419 { \
    00420    if (id.isValid()) \
    00421    { \
    00422       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00423       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00424       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00425       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00426       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
    00427    } \
    00428    QSqlError err; \
    00429    if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \
    00430    else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \
    00431    return err; \
    00432 } \
    00433 \
    00434 QSqlError className::qxFetchAll(qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00435 { \
    00436    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00437    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); } \
    00438    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); } \
    00439    QSqlError err; \
    00440    if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \
    00441    else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \
    00442    return err; \
    00443 } \
    00444 \
    00445 QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00446 { \
    00447    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00448    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); } \
    00449    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); } \
    00450    QSqlError err; \
    00451    if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \
    00452    else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \
    00453    return err; \
    00454 } \
    00455 \
    00456 QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \
    00457 { \
    00458    QSqlError err; \
    00459    if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \
    00460    else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \
    00461    return err; \
    00462 } \
    00463 \
    00464 QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \
    00465 { \
    00466    QSqlError err; \
    00467    if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \
    00468    else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \
    00469    return err; \
    00470 } \
    00471 \
    00472 QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase) \
    00473 { \
    00474    QSqlError err; \
    00475    if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \
    00476    else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \
    00477    return err; \
    00478 } \
    00479 \
    00480 QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \
    00481 { \
    00482    if (id.isValid()) \
    00483    { \
    00484       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00485       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00486       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00487       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00488       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
    00489    } \
    00490    return qx::dao::delete_by_id((* this), pDatabase); \
    00491 } \
    00492 \
    00493 QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \
    00494 { \
    00495    return qx::dao::delete_all< className >(pDatabase); \
    00496 } \
    00497 \
    00498 QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00499 { \
    00500    return qx::dao::delete_by_query< className >(query, pDatabase); \
    00501 } \
    00502 \
    00503 QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \
    00504 { \
    00505    if (id.isValid()) \
    00506    { \
    00507       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00508       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00509       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00510       if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \
    00511       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
    00512    } \
    00513    return qx::dao::destroy_by_id((* this), pDatabase); \
    00514 } \
    00515 \
    00516 QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \
    00517 { \
    00518    return qx::dao::destroy_all< className >(pDatabase); \
    00519 } \
    00520 \
    00521 QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00522 { \
    00523    return qx::dao::destroy_by_query< className >(query, pDatabase); \
    00524 } \
    00525 \
    00526 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \
    00527 { \
    00528    return qx::dao::execute_query(query, (* this), pDatabase); \
    00529 } \
    00530 \
    00531 QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list, QSqlDatabase * pDatabase) \
    00532 { \
    00533    QX_PERSISTABLE_CAST_COLLECTION(className) \
    00534    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); } \
    00535    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); } \
    00536    return qx::dao::execute_query(query, (* list_typed), pDatabase); \
    00537 } \
    00538 \
    00539 qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \
    00540 { \
    00541    if (id.isValid()) \
    00542    { \
    00543       qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \
    00544       qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \
    00545       if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \
    00546       if (! pDataMemberId) { return qx_bool(false); } \
    00547       pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \
    00548    } \
    00549    return qx::dao::exist((* this), pDatabase); \
    00550 } \
    00551 \
    00552 qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \
    00553 { \
    00554    return qx::validate((* this), groups); \
    00555 } \
    00556 \
    00557 std::shared_ptr<qx::IxPersistableCollection> className::qxNewPersistableCollection() const \
    00558 { \
    00559    std::shared_ptr<qx::IxPersistableCollection> coll; \
    00560    coll.reset(new qx::QxPersistableCollectionHelper< className >::type()); \
    00561    return coll; \
    00562 } \
    00563 \
    00564 qx::IxClass * className::qxClass() const \
    00565 { \
    00566    return qx::QxClass< className >::getSingleton(); \
    00567 } \
    00568 \
    00569 QX_PERSISTABLE_JSON_CPP(className)
    00570
    00571 QX_REGISTER_CLASS_NAME(qx::IxPersistable)
    00572
    00573 #include <QxDao/IxPersistableCollection.h>
    00574
    00575 #endif // _IX_PERSISTABLE_H_