QxOrm  1.4.3
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 public:
00094 
00095    QxSqlRelation(IxDataMember * p) : IxSqlRelation(p) { this->m_iIsSameDataOwner = static_cast<int>(is_same_data_owner); }
00096    virtual ~QxSqlRelation() { BOOST_STATIC_ASSERT(is_valid); }
00097 
00098    virtual void init()
00099    {
00100       if (m_bInitInEvent || m_bInitDone) { return; }
00101       m_pClass = QxClass<type_data>::getSingleton();
00102       m_pClassOwner = QxClass<type_owner>::getSingleton();
00103       IxSqlRelation::init();
00104    }
00105 
00106 protected:
00107 
00108    inline DataType * getDataTypePtr(QxSqlRelationParams & params) const
00109    { qAssert(params.owner() && m_pDataMember); return static_cast<DataType *>(m_pDataMember->getValueVoidPtr(params.owner())); }
00110 
00111    inline type_owner & getOwner(QxSqlRelationParams & params) const
00112    { qAssert(params.owner()); return (* static_cast<type_owner *>(params.owner())); }
00113 
00114    inline type_data & getData(QxSqlRelationParams & params) const
00115    { return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00116 
00117    inline type_container & getContainer(QxSqlRelationParams & params) const
00118    { return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00119 
00120    inline type_item createItem() const
00121    { return createItem_Helper<is_data_container, 0>::get(); }
00122 
00123    inline bool isNullData(QxSqlRelationParams & params) const
00124    { return isNullData_Helper<is_data_pointer, 0>::get(getDataTypePtr(params)); }
00125 
00126    bool callTriggerBeforeFetch(type_data & t, QxSqlRelationParams & params) const
00127    {
00128       if (! params.builder().getDaoHelper()) { return true; }
00129       qx::dao::on_before_fetch<type_data>((& t), params.builder().getDaoHelper());
00130       return params.builder().getDaoHelper()->isValid();
00131    }
00132 
00133    bool callTriggerAfterFetch(type_data & t, QxSqlRelationParams & params) const
00134    {
00135       if (! params.builder().getDaoHelper()) { return true; }
00136       qx::dao::on_after_fetch<type_data>((& t), params.builder().getDaoHelper());
00137       return params.builder().getDaoHelper()->isValid();
00138    }
00139 
00140 private:
00141 
00142    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00143    struct getData_Helper
00144    { static inline type_data & get(DataType * t) { return (* t); } };
00145 
00146    template <int dummy>
00147    struct getData_Helper<true, false, dummy>
00148    { static inline type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00149 
00150    template <int dummy>
00151    struct getData_Helper<false, true, dummy>
00152    { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00153 
00154    template <int dummy>
00155    struct getData_Helper<true, true, dummy>
00156    { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00157 
00158    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00159    struct getContainer_Helper
00160    { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00161 
00162    template <int dummy>
00163    struct getContainer_Helper<true, false, dummy>
00164    { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00165 
00166    template <int dummy>
00167    struct getContainer_Helper<false, true, dummy>
00168    { static inline type_container & get(DataType * t) { return (* t); } };
00169 
00170    template <int dummy>
00171    struct getContainer_Helper<true, true, dummy>
00172    { static inline type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00173 
00174    template <bool bIsContainer /* = false */, int dummy>
00175    struct createItem_Helper
00176    { static inline type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } };
00177 
00178    template <int dummy>
00179    struct createItem_Helper<true, dummy>
00180    { static inline type_item get() { return type_generic_container::createItem(); } };
00181 
00182    template <bool bIsPointer /* = false */, int dummy>
00183    struct isNullData_Helper
00184    { static inline bool get(DataType * t) { Q_UNUSED(t); return false; } };
00185 
00186    template <int dummy>
00187    struct isNullData_Helper<true, dummy>
00188    { static inline bool get(DataType * t) { return ((! (* t)) ? true : false); } };
00189 
00190 };
00191 
00192 } // namespace qx
00193 
00194 #include <QxDao/QxSqlRelation_OneToOne.h>
00195 #include <QxDao/QxSqlRelation_OneToMany.h>
00196 #include <QxDao/QxSqlRelation_ManyToOne.h>
00197 #include <QxDao/QxSqlRelation_ManyToMany.h>
00198 #include <QxDao/QxSqlRelation_RawData.h>
00199 
00200 #endif // _QX_SQL_RELATION_H_