/**************************************************************************** ** ** https://www.qxorm.com/ ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) ** ** This file is part of the QxOrm library ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any ** damages arising from the use of this software ** ** Commercial Usage ** Licensees holding valid commercial QxOrm licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Lionel Marty ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file 'license.gpl3.txt' included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met : http://www.gnu.org/copyleft/gpl.html ** ** If you are unsure which license is appropriate for your use, or ** if you have questions regarding the use of this file, please contact : ** contact@qxorm.com ** ****************************************************************************/ #ifndef _QX_SQL_RELATION_H_ #define _QX_SQL_RELATION_H_ #ifdef _MSC_VER #pragma once #endif /*! * \file QxSqlRelation.h * \author Lionel Marty * \ingroup QxDao * \brief Base class for all relationships defined between 2 classes (or between 2 tables in database) */ #include #include #include #include #include #include #include #include #include #include namespace qx { template class QxClass; /*! * \ingroup QxDao * \brief qx::QxSqlRelation : base class for all relationships defined between 2 classes (or between 2 tables in database) */ template class QxSqlRelation : public IxSqlRelation { protected: typedef typename qx::trait::remove_attr::type type_tmp_1; typedef typename qx::trait::remove_smart_ptr::type type_tmp_2; typedef type_tmp_2 type_container; typedef qx::trait::generic_container type_generic_container; typedef typename type_generic_container::type_item type_item; typedef typename std::conditional::value, typename type_generic_container::type_value_qx, type_container>::type type_tmp_3; typedef typename QxSqlRelation::type_tmp_3 type_data; typedef Owner type_owner; enum { is_valid = (qx::trait::is_qx_registered::value && qx::trait::is_qx_registered::value) }; enum { is_data_pointer = (std::is_pointer::value || qx::trait::is_smart_ptr::value) }; enum { is_data_container = qx::trait::is_container::value }; enum { is_same_data_owner = std::is_same::value }; public: QxSqlRelation(IxDataMember * p) : IxSqlRelation(p) { this->setIsSameDataOwner(static_cast(is_same_data_owner)); } virtual ~QxSqlRelation() { static_assert(is_valid, "is_valid"); } virtual void init() { if (! this->canInit()) { return; } this->setClass(QxClass::getSingleton(), QxClass::getSingleton()); IxSqlRelation::init(); } protected: DataType * getDataTypePtr(QxSqlRelationParams & params) const { qAssert(params.owner() && this->getDataMember()); return static_cast(this->getDataMember()->getValueVoidPtr(params.owner())); } type_owner & getOwner(QxSqlRelationParams & params) const { qAssert(params.owner()); return (* static_cast(params.owner())); } type_data & getData(QxSqlRelationParams & params) const { return getData_Helper::get(getDataTypePtr(params)); } type_container & getContainer(QxSqlRelationParams & params) const { return getContainer_Helper::get(getDataTypePtr(params)); } type_item createItem() const { return createItem_Helper::get(); } bool isNullData(QxSqlRelationParams & params) const { return isNullData_Helper::get(getDataTypePtr(params)); } bool callTriggerBeforeFetch(type_data & t, QxSqlRelationParams & params) const { if (! params.builder().getDaoHelper()) { return true; } qx::dao::on_before_fetch((& t), params.builder().getDaoHelper()); return params.builder().getDaoHelper()->isValid(); } bool callTriggerAfterFetch(type_data & t, QxSqlRelationParams & params) const { if (! params.builder().getDaoHelper()) { return true; } qx::dao::on_after_fetch((& t), params.builder().getDaoHelper()); return params.builder().getDaoHelper()->isValid(); } private: template struct getData_Helper { static type_data & get(DataType * t) { return (* t); } }; template struct getData_Helper { static type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr::get(* t); }; return (** t); } }; template struct getData_Helper { static type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; template struct getData_Helper { static type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } }; template struct getContainer_Helper { static type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; template struct getContainer_Helper { static type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } }; template struct getContainer_Helper { static type_container & get(DataType * t) { return (* t); } }; template struct getContainer_Helper { static type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr::get(* t); }; return (** t); } }; template struct createItem_Helper { static type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } }; template struct createItem_Helper { static type_item get() { return type_generic_container::createItem(); } }; template struct isNullData_Helper { static bool get(DataType * t) { Q_UNUSED(t); return false; } }; template struct isNullData_Helper { static bool get(DataType * t) { return ((! (* t)) ? true : false); } }; }; } // namespace qx #include #include #include #include #include #endif // _QX_SQL_RELATION_H_