QxOrm  1.4.9
C++ Object Relational Mapping library
QxSqlRelation.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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 <QxDao/QxDao.h>
00047 #include <QxDao/IxSqlRelation.h>
00048 #include <QxDao/IxSqlQueryBuilder.h>
00049 
00050 #include <QxTraits/remove_attr.h>
00051 #include <QxTraits/remove_smart_ptr.h>
00052 #include <QxTraits/generic_container.h>
00053 #include <QxTraits/is_container.h>
00054 #include <QxTraits/is_valid_primary_key.h>
00055 #include <QxTraits/is_qx_registered.h>
00056 
00057 #include <QxRegister/IxClass.h>
00058 
00059 namespace qx {
00060 
00061 template <class T>
00062 class QxClass;
00063 
00068 template <class DataType, class Owner>
00069 class QxSqlRelation : public IxSqlRelation
00070 {
00071 
00072 protected:
00073 
00074    typedef typename qx::trait::remove_attr<DataType>::type type_tmp_1;
00075    typedef typename qx::trait::remove_smart_ptr<type_tmp_1>::type type_tmp_2;
00076    typedef type_tmp_2 type_container;
00077    typedef qx::trait::generic_container<type_container> type_generic_container;
00078    typedef typename type_generic_container::type_item type_item;
00079    typedef typename std::conditional<qx::trait::is_container<type_container>::value, typename type_generic_container::type_value_qx, type_container>::type type_tmp_3;
00080    typedef typename QxSqlRelation<DataType, Owner>::type_tmp_3 type_data;
00081    typedef Owner type_owner;
00082 
00083    enum { is_valid = (qx::trait::is_qx_registered<type_data>::value && qx::trait::is_qx_registered<type_owner>::value) };
00084    enum { is_data_pointer = (std::is_pointer<DataType>::value || qx::trait::is_smart_ptr<DataType>::value) };
00085    enum { is_data_container = qx::trait::is_container<type_container>::value };
00086    enum { is_same_data_owner = std::is_same<type_data, type_owner>::value };
00087 
00088 public:
00089 
00090    QxSqlRelation(IxDataMember * p) : IxSqlRelation(p) { this->setIsSameDataOwner(static_cast<int>(is_same_data_owner)); }
00091    virtual ~QxSqlRelation() { static_assert(is_valid, "is_valid"); }
00092 
00093    virtual void init()
00094    {
00095       if (! this->canInit()) { return; }
00096       this->setClass(QxClass<type_data>::getSingleton(), QxClass<type_owner>::getSingleton());
00097       IxSqlRelation::init();
00098    }
00099 
00100 protected:
00101 
00102    DataType * getDataTypePtr(QxSqlRelationParams & params) const
00103    { qAssert(params.owner() && this->getDataMember()); return static_cast<DataType *>(this->getDataMember()->getValueVoidPtr(params.owner())); }
00104 
00105    type_owner & getOwner(QxSqlRelationParams & params) const
00106    { qAssert(params.owner()); return (* static_cast<type_owner *>(params.owner())); }
00107 
00108    type_data & getData(QxSqlRelationParams & params) const
00109    { return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00110 
00111    type_container & getContainer(QxSqlRelationParams & params) const
00112    { return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params)); }
00113 
00114    type_item createItem() const
00115    { return createItem_Helper<is_data_container, 0>::get(); }
00116 
00117    bool isNullData(QxSqlRelationParams & params) const
00118    { return isNullData_Helper<is_data_pointer, 0>::get(getDataTypePtr(params)); }
00119 
00120    bool callTriggerBeforeFetch(type_data & t, QxSqlRelationParams & params) const
00121    {
00122       if (! params.builder().getDaoHelper()) { return true; }
00123       qx::dao::on_before_fetch<type_data>((& t), params.builder().getDaoHelper());
00124       return params.builder().getDaoHelper()->isValid();
00125    }
00126 
00127    bool callTriggerAfterFetch(type_data & t, QxSqlRelationParams & params) const
00128    {
00129       if (! params.builder().getDaoHelper()) { return true; }
00130       qx::dao::on_after_fetch<type_data>((& t), params.builder().getDaoHelper());
00131       return params.builder().getDaoHelper()->isValid();
00132    }
00133 
00134 private:
00135 
00136    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00137    struct getData_Helper
00138    { static type_data & get(DataType * t) { return (* t); } };
00139 
00140    template <int dummy>
00141    struct getData_Helper<true, false, dummy>
00142    { static type_data & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00143 
00144    template <int dummy>
00145    struct getData_Helper<false, true, dummy>
00146    { static type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00147 
00148    template <int dummy>
00149    struct getData_Helper<true, true, dummy>
00150    { static type_data & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_data * pDummy(NULL); return (* pDummy); } };
00151 
00152    template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
00153    struct getContainer_Helper
00154    { static type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00155 
00156    template <int dummy>
00157    struct getContainer_Helper<true, false, dummy>
00158    { static type_container & get(DataType * t) { qAssert(false); Q_UNUSED(t); type_container * pDummy(NULL); return (* pDummy); } };
00159 
00160    template <int dummy>
00161    struct getContainer_Helper<false, true, dummy>
00162    { static type_container & get(DataType * t) { return (* t); } };
00163 
00164    template <int dummy>
00165    struct getContainer_Helper<true, true, dummy>
00166    { static type_container & get(DataType * t) { if (! (* t)) { qx::trait::construct_ptr<DataType>::get(* t); }; return (** t); } };
00167 
00168    template <bool bIsContainer /* = false */, int dummy>
00169    struct createItem_Helper
00170    { static type_item get() { qAssert(false); type_item * pDummy(NULL); return (* pDummy); } };
00171 
00172    template <int dummy>
00173    struct createItem_Helper<true, dummy>
00174    { static type_item get() { return type_generic_container::createItem(); } };
00175 
00176    template <bool bIsPointer /* = false */, int dummy>
00177    struct isNullData_Helper
00178    { static bool get(DataType * t) { Q_UNUSED(t); return false; } };
00179 
00180    template <int dummy>
00181    struct isNullData_Helper<true, dummy>
00182    { static bool get(DataType * t) { return ((! (* t)) ? true : false); } };
00183 
00184 };
00185 
00186 } // namespace qx
00187 
00188 #include <QxDao/QxSqlRelation_OneToOne.h>
00189 #include <QxDao/QxSqlRelation_OneToMany.h>
00190 #include <QxDao/QxSqlRelation_ManyToOne.h>
00191 #include <QxDao/QxSqlRelation_ManyToMany.h>
00192 #include <QxDao/QxSqlRelation_RawData.h>
00193 
00194 #endif // _QX_SQL_RELATION_H_