QxOrm  1.4.2
C++ Object Relational Mapping library
QxSqlRelation.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 _QX_SQL_RELATION_H_
00033 #define _QX_SQL_RELATION_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/mpl/if.hpp>
00047 #include <boost/mpl/logical.hpp>
00048 #include <boost/type_traits/is_same.hpp>
00049 #include <boost/type_traits/is_pointer.hpp>
00050 
00051 #include <QxDao/QxDao.h>
00052 #include <QxDao/IxSqlRelation.h>
00053 #include <QxDao/IxSqlQueryBuilder.h>
00054 
00055 #include <QxTraits/remove_attr.h>
00056 #include <QxTraits/remove_smart_ptr.h>
00057 #include <QxTraits/generic_container.h>
00058 #include <QxTraits/is_container.h>
00059 #include <QxTraits/is_valid_primary_key.h>
00060 #include <QxTraits/is_qx_registered.h>
00061 
00062 #include <QxRegister/IxClass.h>
00063 
00064 namespace qx {
00065 
00066 template <class T>
00067 class QxClass;
00068 
00073 template <class DataType, class Owner>
00074 class QxSqlRelation : public IxSqlRelation
00075 {
00076 
00077 protected:
00078 
00079    typedef typename qx::trait::remove_attr<DataType>::type type_tmp_1;
00080    typedef typename qx::trait::remove_smart_ptr<type_tmp_1>::type type_tmp_2;
00081    typedef type_tmp_2 type_container;
00082    typedef qx::trait::generic_container<type_container> type_generic_container;
00083    typedef typename type_generic_container::type_item type_item;
00084    typedef typename boost::mpl::if_c<qx::trait::is_container<type_container>::value, typename type_generic_container::type_value_qx, type_container>::type type_tmp_3;
00085    typedef typename QxSqlRelation<DataType, Owner>::type_tmp_3 type_data;
00086    typedef Owner type_owner;
00087 
00088    enum { is_valid = (qx::trait::is_qx_registered<type_data>::value && qx::trait::is_qx_registered<type_owner>::value) };
00089    enum { is_data_pointer = (boost::is_pointer<DataType>::value || qx::trait::is_smart_ptr<DataType>::value) };
00090    enum { is_data_container = qx::trait::is_container<type_container>::value };
00091    enum { is_same_data_owner = boost::is_same<type_data, type_owner>::value };
00092 
00093 protected:
00094 
00095    QMutex m_oMutex;        
00096    bool m_bInitInEvent;    
00097 
00098 public:
00099 
00100    QxSqlRelation(IxDataMember * p) : IxSqlRelation(p), m_bInitInEvent(false) { ; }
00101    virtual ~QxSqlRelation() { BOOST_STATIC_ASSERT(is_valid); }
00102 
00103    virtual void init()
00104    {
00105       if (m_bInitInEvent) { return; }; m_bInitInEvent = true;
00106       QMutexLocker locker(& this->m_oMutex);
00107       m_pClass = QxClass<type_data>::getSingleton();
00108       m_pClassOwner = QxClass<type_owner>::getSingleton();
00109       m_pDataMemberX = (m_pClass ? m_pClass->getDataMemberX() : NULL);
00110       m_pDataMemberId = (m_pDataMemberX ? m_pDataMemberX->getId_WithDaoStrategy() : NULL);
00111       m_pDataMemberIdOwner = ((m_pClassOwner && m_pClassOwner->getDataMemberX()) ? m_pClassOwner->getDataMemberX()->getId_WithDaoStrategy() : NULL);
00112       if (m_lstDataMemberPtr || m_lstSqlRelationPtr) { m_bInitInEvent = false; return; }
00113       if (m_pClass) { m_oSoftDelete = m_pClass->getSoftDelete(); }
00114 
00115 #ifdef _QX_MODE_DEBUG
00116       QString sCheckMsg = "Check relationship '" + this->getKey() + "' from '" + (m_pClassOwner ? m_pClassOwner->getKey() : QString()) + "' to '" + (m_pClass ? m_pClass->getKey() : QString()) + "'";
00117       if (! m_pClass) { QString sAssertMsg = sCheckMsg + " : m_pClass is equal to NULL"; qAssertMsg(false, "[QxOrm] qx::QxSqlRelation::init()", qPrintable(sAssertMsg)); }
00118       if (! m_pClassOwner) { QString sAssertMsg = sCheckMsg + " : m_pClassOwner is equal to NULL"; qAssertMsg(false, "[QxOrm] qx::QxSqlRelation::init()", qPrintable(sAssertMsg)); }
00119       if (! m_pDataMember) { QString sAssertMsg = sCheckMsg + " : m_pDataMember is equal to NULL"; qAssertMsg(false, "[QxOrm] qx::QxSqlRelation::init()", qPrintable(sAssertMsg)); }
00120       if (! m_pDataMemberX) { QString sAssertMsg = sCheckMsg + " : m_pDataMemberX is equal to NULL"; qAssertMsg(false, "[QxOrm] qx::QxSqlRelation::init()", qPrintable(sAssertMsg)); }
00121       if (! m_pDataMemberId) { QString sAssertMsg = sCheckMsg + " : m_pDataMemberId is equal to NULL"; qAssertMsg(false, "[QxOrm] qx::QxSqlRelation::init()", qPrintable(sAssertMsg)); }
00122 #endif // _QX_MODE_DEBUG
00123 
00124       if (IxSqlRelation::m_bTraceRelationInit)
00125       { QString sTraceMsg = "[QxOrm] Init relationship '" + this->getKey() + "' from '" + (m_pClassOwner ? m_pClassOwner->getKey() : QString()) + "' to '" + (m_pClass ? m_pClass->getKey() : QString()) + "'"; qDebug() << sTraceMsg; }
00126 
00127       m_lstSqlRelationPtr = new IxSqlRelationX();
00128       m_lstDataMemberPtr = new QxCollection<QString, IxDataMember *>();
00129       if ((getDataCount() > 0) || (getRelationCount() > 0)) { m_bInitInEvent = false; return; }
00130       IxDataMember * p = NULL; long lCount = m_pDataMemberX->count_WithDaoStrategy();
00131 
00132       for (long l = 0; l < lCount; ++l)
00133       {
00134          p = isValid_DataMember(l); if (! p) { continue; }
00135 #ifdef _QX_MODE_DEBUG
00136          if (m_lstDataMemberPtr->exist(p->getKey()))
00137          { QString sDebugMsg = "[QxOrm] Relationship '" + this->getKey() + "' from '" + (m_pClassOwner ? m_pClassOwner->getKey() : QString()) + "' to '" + (m_pClass ? m_pClass->getKey() : QString()) + "' : data member '" + p->getKey() + "' already exists in the collection"; qDebug() << sDebugMsg; }
00138 #endif // _QX_MODE_DEBUG
00139          m_lstDataMemberPtr->insert(p->getKey(), p);
00140       }
00141 
00142       for (long l = 0; l < lCount; ++l)
00143       {
00144          p = isValid_SqlRelation(l); if (! p) { continue; }
00145 #ifdef _QX_MODE_DEBUG
00146          if (m_lstSqlRelationPtr->exist(p->getKey()))
00147          { QString sDebugMsg = "[QxOrm] Relationship '" + this->getKey() + "' from '" + (m_pClassOwner ? m_pClassOwner->getKey() : QString()) + "' to '" + (m_pClass ? m_pClass->getKey() : QString()) + "' : relation '" + p->getKey() + "' already exists in the collection"; qDebug() << sDebugMsg; }
00148 #endif // _QX_MODE_DEBUG
00149          m_lstSqlRelationPtr->insert(p->getKey(), p->getSqlRelation());
00150       }
00151 
00152       m_bInitInEvent = false;
00153    }
00154 
00155 private:
00156 
00157    inline IxDataMember * isValid_DataMember(long lIndex) const
00158    {
00159       IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex);
00160       bool bValid = (p && p->getDao() && ! p->hasSqlRelation());
00161       bValid = (bValid && (p != m_pDataMemberId));
00162       return (bValid ? p : NULL);
00163    }
00164 
00165    inline IxDataMember * isValid_SqlRelation(long lIndex) const
00166    {
00167       IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex);
00168       bool bIsValid = (p && p->getDao() && p->hasSqlRelation());
00169       if (bIsValid && (! is_same_data_owner) && (p != m_pDataMember)) { p->getSqlRelation()->init(); }
00170       return (bIsValid ? p : NULL);
00171    }
00172 
00173 protected:
00174 
00175    inline DataType * getDataTypePtr(QxSqlRelationParams & params) const
00176    { qAssert(params.owner() && m_pDataMember); return static_cast<DataType *>(m_pDataMember->getValueVoidPtr(params.owner())); }
00177 
00178    inline type_owner & getOwner(QxSqlRelationParams & params) const
00179    { qAssert(params.owner()); return (* static_cast<type_owner *>(params.owner())); }
00180 
00181    inline type_data & getData(QxSqlRelationParams & params) const
00182    { return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00183 
00184    inline type_container & getContainer(QxSqlRelationParams & params) const
00185    { return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00186 
00187    inline type_item createItem() const
00188    { return createItem_Helper<is_data_container, 0>::get(); }
00189 
00190    inline bool isNullData(QxSqlRelationParams & params) const
00191    { return isNullData_Helper<is_data_pointer, 0>::get(getDataTypePtr(params)); }
00192 
00193    bool callTriggerBeforeFetch(type_data & t, QxSqlRelationParams & params) const
00194    {
00195       if (! params.builder().getDaoHelper()) { return true; }
00196       qx::dao::on_before_fetch<type_data>((& t), params.builder().getDaoHelper());
00197       return params.builder().getDaoHelper()->isValid();
00198    }
00199 
00200    bool callTriggerAfterFetch(type_data & t, QxSqlRelationParams & params) const
00201    {
00202       if (! params.builder().getDaoHelper()) { return true; }
00203       qx::dao::on_after_fetch<type_data>((& t), params.builder().getDaoHelper());
00204       return params.builder().getDaoHelper()->isValid();
00205    }
00206 
00207 private:
00208 
00209    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00210    struct getData_Helper
00211    { static inline type_data & get(DataType * t) { return (* t); } };
00212 
00213    template <int dummy>
00214    struct getData_Helper<true, false, dummy>
00215    { static inline type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00216 
00217    template <int dummy>
00218    struct getData_Helper<false, true, dummy>
00219    { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00220 
00221    template <int dummy>
00222    struct getData_Helper<true, true, dummy>
00223    { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00224 
00225    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00226    struct getContainer_Helper
00227    { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00228 
00229    template <int dummy>
00230    struct getContainer_Helper<true, false, dummy>
00231    { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00232 
00233    template <int dummy>
00234    struct getContainer_Helper<false, true, dummy>
00235    { static inline type_container & get(DataType * t) { return (* t); } };
00236 
00237    template <int dummy>
00238    struct getContainer_Helper<true, true, dummy>
00239    { static inline type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00240 
00241    template <bool bIsContainer /* = false */, int dummy>
00242    struct createItem_Helper
00243    { static inline type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } };
00244 
00245    template <int dummy>
00246    struct createItem_Helper<true, dummy>
00247    { static inline type_item get() { return type_generic_container::createItem(); } };
00248 
00249    template <bool bIsPointer /* = false */, int dummy>
00250    struct isNullData_Helper
00251    { static inline bool get(DataType * t) { Q_UNUSED(t); return false; } };
00252 
00253    template <int dummy>
00254    struct isNullData_Helper<true, dummy>
00255    { static inline bool get(DataType * t) { return ((! (* t)) ? true : false); } };
00256 
00257 };
00258 
00259 } // namespace qx
00260 
00261 #include <QxDao/QxSqlRelation_OneToOne.h>
00262 #include <QxDao/QxSqlRelation_OneToMany.h>
00263 #include <QxDao/QxSqlRelation_ManyToOne.h>
00264 #include <QxDao/QxSqlRelation_ManyToMany.h>
00265 #include <QxDao/QxSqlRelation_RawData.h>
00266 
00267 #endif // _QX_SQL_RELATION_H_