![]() |
QxOrm 1.1.9
C++ Object Relational Mapping library
|
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 _QX_SQL_RELATION_H_ 00027 #define _QX_SQL_RELATION_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <boost/mpl/if.hpp> 00041 #include <boost/mpl/logical.hpp> 00042 #include <boost/type_traits/is_same.hpp> 00043 #include <boost/type_traits/is_pointer.hpp> 00044 00045 #include <QxDao/QxDao.h> 00046 #include <QxDao/IxSqlRelation.h> 00047 #include <QxDao/IxSqlQueryBuilder.h> 00048 00049 #include <QxTraits/remove_attr.h> 00050 #include <QxTraits/remove_smart_ptr.h> 00051 #include <QxTraits/generic_container.h> 00052 #include <QxTraits/is_container.h> 00053 #include <QxTraits/is_valid_primary_key.h> 00054 #include <QxTraits/is_qx_registered.h> 00055 00056 namespace qx { 00057 00058 template <class T> 00059 class QxClass; 00060 00065 template <class DataType, class Owner> 00066 class QxSqlRelation : public IxSqlRelation 00067 { 00068 00069 protected: 00070 00071 typedef typename qx::trait::remove_attr<DataType>::type type_tmp_1; 00072 typedef typename qx::trait::remove_smart_ptr<type_tmp_1>::type type_tmp_2; 00073 typedef type_tmp_2 type_container; 00074 typedef qx::trait::generic_container<type_container> type_generic_container; 00075 typedef typename type_generic_container::type_item type_item; 00076 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; 00077 typedef typename QxSqlRelation<DataType, Owner>::type_tmp_3 type_data; 00078 typedef Owner type_owner; 00079 00080 enum { is_valid = (qx::trait::is_qx_registered<type_data>::value && qx::trait::is_qx_registered<type_owner>::value) }; 00081 enum { is_data_pointer = (boost::is_pointer<DataType>::value || qx::trait::is_smart_ptr<DataType>::value) }; 00082 enum { is_data_container = qx::trait::is_container<type_container>::value }; 00083 enum { is_same_data_owner = boost::is_same<type_data, type_owner>::value }; 00084 00085 protected: 00086 00087 static QxCollection<QString, IxDataMember *> m_lstDataMember; 00088 static IxSqlRelationX m_lstSqlRelation; 00089 static QMutex m_oMutex; 00090 bool m_bInitInEvent; 00091 00092 public: 00093 00094 QxSqlRelation(IxDataMember * p) : IxSqlRelation(p), m_bInitInEvent(false) { ; } 00095 virtual ~QxSqlRelation() { BOOST_STATIC_ASSERT(is_valid); } 00096 00097 virtual void init() 00098 { 00099 if (m_bInitInEvent) { return; }; m_bInitInEvent = true; 00100 QMutexLocker locker(& QxSqlRelation<DataType, Owner>::m_oMutex); 00101 m_pDataMemberX = QxClass<type_data>::getSingleton()->dataMemberX(); 00102 m_pDataMemberId = (m_pDataMemberX ? m_pDataMemberX->getId_WithDaoStrategy() : NULL); 00103 m_lstDataMemberPtr = (& QxSqlRelation<DataType, Owner>::m_lstDataMember); 00104 m_lstSqlRelationPtr = (& QxSqlRelation<DataType, Owner>::m_lstSqlRelation); 00105 m_oSoftDelete = QxClass<type_data>::getSingleton()->getSoftDelete(); 00106 qAssert(m_pDataMember && m_pDataMemberX && m_pDataMemberId); 00107 if (getDataCount() > 0 || getRelationCount() > 0) { m_bInitInEvent = false; return; } 00108 IxDataMember * p = NULL; long lCount = m_pDataMemberX->count_WithDaoStrategy(); 00109 for (long l = 0; l < lCount; ++l) { if ((p = isValid_DataMember(l))) { m_lstDataMember.insert(p->getKey(), p); } } 00110 for (long l = 0; l < lCount; ++l) { if ((p = isValid_SqlRelation(l))) { m_lstSqlRelation.insert(p->getKey(), p->getSqlRelation()); } } 00111 m_bInitInEvent = false; 00112 } 00113 00114 private: 00115 00116 inline IxDataMember * isValid_DataMember(long lIndex) const 00117 { 00118 IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex); 00119 bool bValid = (p && p->getDao() && ! p->hasSqlRelation()); 00120 bValid = (bValid && (p != m_pDataMemberId)); 00121 return (bValid ? p : NULL); 00122 } 00123 00124 inline IxDataMember * isValid_SqlRelation(long lIndex) const 00125 { 00126 IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex); 00127 bool bIsValid = (p && p->getDao() && p->hasSqlRelation()); 00128 if (bIsValid && (! is_same_data_owner) && (p != m_pDataMember)) { p->getSqlRelation()->init(); } 00129 return (bIsValid ? p : NULL); 00130 } 00131 00132 protected: 00133 00134 inline DataType * getDataTypePtr(QxSqlRelationParams & params) const 00135 { qAssert(params.owner() && m_pDataMember); return static_cast<DataType *>(m_pDataMember->getValueVoidPtr(params.owner())); } 00136 00137 inline type_owner & getOwner(QxSqlRelationParams & params) const 00138 { qAssert(params.owner()); return (* static_cast<type_owner *>(params.owner())); } 00139 00140 inline type_data & getData(QxSqlRelationParams & params) const 00141 { return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); } 00142 00143 inline type_container & getContainer(QxSqlRelationParams & params) const 00144 { return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); } 00145 00146 inline type_item createItem() const 00147 { return createItem_Helper<is_data_container, 0>::get(); } 00148 00149 private: 00150 00151 template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy> 00152 struct getData_Helper 00153 { static inline type_data & get(DataType * t) { return (* t); } }; 00154 00155 template <int dummy> 00156 struct getData_Helper<true, false, dummy> 00157 { static inline type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } }; 00158 00159 template <int dummy> 00160 struct getData_Helper<false, true, dummy> 00161 { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; 00162 00163 template <int dummy> 00164 struct getData_Helper<true, true, dummy> 00165 { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; 00166 00167 template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy> 00168 struct getContainer_Helper 00169 { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; 00170 00171 template <int dummy> 00172 struct getContainer_Helper<true, false, dummy> 00173 { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; 00174 00175 template <int dummy> 00176 struct getContainer_Helper<false, true, dummy> 00177 { static inline type_container & get(DataType * t) { return (* t); } }; 00178 00179 template <int dummy> 00180 struct getContainer_Helper<true, true, dummy> 00181 { static inline type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } }; 00182 00183 template <bool bIsContainer /* = false */, int dummy> 00184 struct createItem_Helper 00185 { static inline type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } }; 00186 00187 template <int dummy> 00188 struct createItem_Helper<true, dummy> 00189 { static inline type_item get() { return type_generic_container::createItem(); } }; 00190 00191 }; 00192 00193 template <class DataType, class Owner> QxCollection<QString, IxDataMember *> QxSqlRelation<DataType, Owner>::m_lstDataMember; 00194 template <class DataType, class Owner> IxSqlRelationX QxSqlRelation<DataType, Owner>::m_lstSqlRelation; 00195 template <class DataType, class Owner> QMutex QxSqlRelation<DataType, Owner>::m_oMutex; 00196 00197 } // namespace qx 00198 00199 #include <QxDao/QxSqlRelation_OneToOne.h> 00200 #include <QxDao/QxSqlRelation_OneToMany.h> 00201 #include <QxDao/QxSqlRelation_ManyToOne.h> 00202 #include <QxDao/QxSqlRelation_ManyToMany.h> 00203 #include <QxDao/QxSqlRelation_RawData.h> 00204 00205 #endif // _QX_SQL_RELATION_H_