![]() |
QxOrm
1.4.1
C++ Object Relational Mapping library
|
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 static QxCollection<QString, IxDataMember *> m_lstDataMember; 00096 static IxSqlRelationX m_lstSqlRelation; 00097 static QMutex m_oMutex; 00098 bool m_bInitInEvent; 00099 00100 public: 00101 00102 QxSqlRelation(IxDataMember * p) : IxSqlRelation(p), m_bInitInEvent(false) { ; } 00103 virtual ~QxSqlRelation() { BOOST_STATIC_ASSERT(is_valid); } 00104 00105 virtual void init() 00106 { 00107 if (m_bInitInEvent) { return; }; m_bInitInEvent = true; 00108 QMutexLocker locker(& QxSqlRelation<DataType, Owner>::m_oMutex); 00109 m_pClass = QxClass<type_data>::getSingleton(); 00110 m_pClassOwner = QxClass<type_owner>::getSingleton(); 00111 m_pDataMemberX = (m_pClass ? m_pClass->getDataMemberX() : NULL); 00112 m_pDataMemberId = (m_pDataMemberX ? m_pDataMemberX->getId_WithDaoStrategy() : NULL); 00113 m_pDataMemberIdOwner = ((m_pClassOwner && m_pClassOwner->getDataMemberX()) ? m_pClassOwner->getDataMemberX()->getId_WithDaoStrategy() : NULL); 00114 m_lstDataMemberPtr = (& QxSqlRelation<DataType, Owner>::m_lstDataMember); 00115 m_lstSqlRelationPtr = (& QxSqlRelation<DataType, Owner>::m_lstSqlRelation); 00116 if (m_pClass) { m_oSoftDelete = m_pClass->getSoftDelete(); } 00117 qAssert(m_pClass && m_pClassOwner && m_pDataMember && m_pDataMemberX && m_pDataMemberId); 00118 if (getDataCount() > 0 || getRelationCount() > 0) { m_bInitInEvent = false; return; } 00119 IxDataMember * p = NULL; long lCount = m_pDataMemberX->count_WithDaoStrategy(); 00120 for (long l = 0; l < lCount; ++l) { if ((p = isValid_DataMember(l))) { m_lstDataMember.insert(p->getKey(), p); } } 00121 for (long l = 0; l < lCount; ++l) { if ((p = isValid_SqlRelation(l))) { m_lstSqlRelation.insert(p->getKey(), p->getSqlRelation()); } } 00122 m_bInitInEvent = false; 00123 } 00124 00125 private: 00126 00127 inline IxDataMember * isValid_DataMember(long lIndex) const 00128 { 00129 IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex); 00130 bool bValid = (p && p->getDao() && ! p->hasSqlRelation()); 00131 bValid = (bValid && (p != m_pDataMemberId)); 00132 return (bValid ? p : NULL); 00133 } 00134 00135 inline IxDataMember * isValid_SqlRelation(long lIndex) const 00136 { 00137 IxDataMember * p = m_pDataMemberX->get_WithDaoStrategy(lIndex); 00138 bool bIsValid = (p && p->getDao() && p->hasSqlRelation()); 00139 if (bIsValid && (! is_same_data_owner) && (p != m_pDataMember)) { p->getSqlRelation()->init(); } 00140 return (bIsValid ? p : NULL); 00141 } 00142 00143 protected: 00144 00145 inline DataType * getDataTypePtr(QxSqlRelationParams & params) const 00146 { qAssert(params.owner() && m_pDataMember); return static_cast<DataType *>(m_pDataMember->getValueVoidPtr(params.owner())); } 00147 00148 inline type_owner & getOwner(QxSqlRelationParams & params) const 00149 { qAssert(params.owner()); return (* static_cast<type_owner *>(params.owner())); } 00150 00151 inline type_data & getData(QxSqlRelationParams & params) const 00152 { return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); } 00153 00154 inline type_container & getContainer(QxSqlRelationParams & params) const 00155 { return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); } 00156 00157 inline type_item createItem() const 00158 { return createItem_Helper<is_data_container, 0>::get(); } 00159 00160 inline bool isNullData(QxSqlRelationParams & params) const 00161 { return isNullData_Helper<is_data_pointer, 0>::get(getDataTypePtr(params)); } 00162 00163 bool callTriggerBeforeFetch(type_data & t, QxSqlRelationParams & params) const 00164 { 00165 if (! params.builder().getDaoHelper()) { return true; } 00166 qx::dao::on_before_fetch<type_data>((& t), params.builder().getDaoHelper()); 00167 return params.builder().getDaoHelper()->isValid(); 00168 } 00169 00170 bool callTriggerAfterFetch(type_data & t, QxSqlRelationParams & params) const 00171 { 00172 if (! params.builder().getDaoHelper()) { return true; } 00173 qx::dao::on_after_fetch<type_data>((& t), params.builder().getDaoHelper()); 00174 return params.builder().getDaoHelper()->isValid(); 00175 } 00176 00177 private: 00178 00179 template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy> 00180 struct getData_Helper 00181 { static inline type_data & get(DataType * t) { return (* t); } }; 00182 00183 template <int dummy> 00184 struct getData_Helper<true, false, dummy> 00185 { static inline type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } }; 00186 00187 template <int dummy> 00188 struct getData_Helper<false, true, dummy> 00189 { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; 00190 00191 template <int dummy> 00192 struct getData_Helper<true, true, dummy> 00193 { static inline type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; 00194 00195 template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy> 00196 struct getContainer_Helper 00197 { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; 00198 00199 template <int dummy> 00200 struct getContainer_Helper<true, false, dummy> 00201 { static inline type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; 00202 00203 template <int dummy> 00204 struct getContainer_Helper<false, true, dummy> 00205 { static inline type_container & get(DataType * t) { return (* t); } }; 00206 00207 template <int dummy> 00208 struct getContainer_Helper<true, true, dummy> 00209 { static inline type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } }; 00210 00211 template <bool bIsContainer /* = false */, int dummy> 00212 struct createItem_Helper 00213 { static inline type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } }; 00214 00215 template <int dummy> 00216 struct createItem_Helper<true, dummy> 00217 { static inline type_item get() { return type_generic_container::createItem(); } }; 00218 00219 template <bool bIsPointer /* = false */, int dummy> 00220 struct isNullData_Helper 00221 { static inline bool get(DataType * t) { Q_UNUSED(t); return false; } }; 00222 00223 template <int dummy> 00224 struct isNullData_Helper<true, dummy> 00225 { static inline bool get(DataType * t) { return ((! (* t)) ? true : false); } }; 00226 00227 }; 00228 00229 template <class DataType, class Owner> QxCollection<QString, IxDataMember *> QxSqlRelation<DataType, Owner>::m_lstDataMember; 00230 template <class DataType, class Owner> IxSqlRelationX QxSqlRelation<DataType, Owner>::m_lstSqlRelation; 00231 template <class DataType, class Owner> QMutex QxSqlRelation<DataType, Owner>::m_oMutex(QMutex::Recursive); 00232 00233 } // namespace qx 00234 00235 #include <QxDao/QxSqlRelation_OneToOne.h> 00236 #include <QxDao/QxSqlRelation_OneToMany.h> 00237 #include <QxDao/QxSqlRelation_ManyToOne.h> 00238 #include <QxDao/QxSqlRelation_ManyToMany.h> 00239 #include <QxDao/QxSqlRelation_RawData.h> 00240 00241 #endif // _QX_SQL_RELATION_H_