| QxOrm 1.3.2 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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