QxOrm  1.4.2
C++ Object Relational Mapping library
QxDataMemberX.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_DATA_MEMBER_X_H_
00033 #define _QX_DATA_MEMBER_X_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/type_traits/is_integral.hpp>
00047 #include <boost/type_traits/is_base_of.hpp>
00048 
00049 #include <QxDataMember/IxDataMemberX.h>
00050 #include <QxDataMember/QxDataMember.h>
00051 #include <QxDataMember/QxDataMember_QObject.h>
00052 
00053 #include <QxDao/QxSqlRelation.h>
00054 
00055 #include <QxSingleton/QxSingleton.h>
00056 
00057 #include <QxFactory/QxFactory.h>
00058 
00059 #include <QxTraits/get_base_class.h>
00060 #include <QxTraits/get_primary_key.h>
00061 #include <QxTraits/get_sql_type.h>
00062 #include <QxTraits/qt_meta_object.h>
00063 
00064 namespace qx {
00065 
00070 template <class T>
00071 class QxDataMemberX : public IxDataMemberX, public QxSingleton< QxDataMemberX<T> >
00072 {
00073 
00074    friend class QxSingleton< QxDataMemberX<T> >;
00075 
00076 public:
00077 
00078    typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
00079    typedef typename qx::trait::get_base_class<T>::type type_base_class;
00080 
00081 protected:
00082 
00083    IxDataMember * m_pDataMemberId;  
00084 
00085 protected:
00086 
00087    QxDataMemberX() : IxDataMemberX(), QxSingleton< QxDataMemberX<T> >(QString("qx::QxDataMemberX_") + qx::trait::get_class_name<T>::get_xml_tag()), m_pDataMemberId(NULL) { ; }
00088    virtual ~QxDataMemberX() { ; }
00089 
00090 public:
00091 
00092    virtual IxDataMember * getId() const { return m_pDataMemberId; }
00093 
00094    virtual long count_WithDaoStrategy() const                              { return count_WithDaoStrategy_Helper(); }
00095    virtual bool exist_WithDaoStrategy(const QString & sKey) const          { return exist_WithDaoStrategy_Helper(sKey); }
00096    virtual IxDataMember * get_WithDaoStrategy(long lIndex) const           { return get_WithDaoStrategy_Helper(lIndex); }
00097    virtual IxDataMember * get_WithDaoStrategy(const QString & sKey) const  { return get_WithDaoStrategy_Helper(sKey); }
00098    virtual IxDataMember * getId_WithDaoStrategy() const                    { return getId_WithDaoStrategy_Helper(); }
00099 
00100    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey);
00101    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey, long lVersion);
00102    IxDataMember * id(const QString & sKey, long lVersion);
00103    IxDataMember * add(const QString & sKey, long lVersion);
00104 
00105    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey);
00106    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion);
00107    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion, bool bSerialize);
00108    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao);
00109 
00110    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey);
00111    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion);
00112    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey);
00113    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion);
00114    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey);
00115    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion);
00116    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType);
00117    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType, long lVersion);
00118 
00119 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00120    template <class Archive> inline void toArchive(const T * pOwner, Archive & ar, const unsigned int file_version) const;
00121    template <class Archive> inline void fromArchive(T * pOwner, Archive & ar, const unsigned int file_version);
00122 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00123 
00124 private:
00125 
00126    IxDataMember * initId(IxDataMember * pId, long lVersion);
00127    IxDataMember * initData(IxDataMember * pData, long lVersion);
00128 
00129    inline IxDataMemberX * getBaseClass_Helper() const { return QxDataMemberX<type_base_class>::getSingleton(); }
00130 
00131    long count_WithDaoStrategy_Helper() const
00132    {
00133       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00134       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? count() : getBaseClass_Helper()->count_WithDaoStrategy()); }
00135       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00136       { return (count() + ((! m_pDataMemberId && getId_WithDaoStrategy()) ? 1 : 0)); }
00137       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00138       { return (count() + getBaseClass_Helper()->count_WithDaoStrategy()); }
00139       qAssert(false); return 0;
00140    }
00141 
00142    bool exist_WithDaoStrategy_Helper(const QString & sKey) const
00143    {
00144       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00145       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? exist(sKey) : getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
00146       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00147       { return (exist(sKey) || (getId_WithDaoStrategy() ? (getId_WithDaoStrategy()->getKey() == sKey) : false)); }
00148       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00149       { return (exist(sKey) || getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
00150       qAssert(false); return false;
00151    }
00152 
00153    IxDataMember * get_WithDaoStrategy_Helper(long lIndex) const
00154    {
00155       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00156       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex)); }
00157       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00158       { return ((! m_pDataMemberId && (lIndex == count())) ? getId_WithDaoStrategy() : get(lIndex)); }
00159       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00160       { return (((lIndex >= 0) && (lIndex < count())) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex - count())); }
00161       qAssert(false); return NULL;
00162    }
00163 
00164    IxDataMember * get_WithDaoStrategy_Helper(const QString & sKey) const
00165    {
00166       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00167       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
00168       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00169       { return ((getId_WithDaoStrategy() && (getId_WithDaoStrategy()->getKey() == sKey)) ? getId_WithDaoStrategy() : get(sKey)); }
00170       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00171       { return (exist(sKey) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
00172       qAssert(false); return NULL;
00173    }
00174 
00175    IxDataMember * getId_WithDaoStrategy_Helper() const
00176    {
00177       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00178       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00179       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00180       { return (m_pDataMemberId ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00181       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00182       { return (m_pDataMemberId ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00183       qAssert(false); return NULL;
00184    }
00185 
00186 };
00187 
00188 } // namespace qx
00189 
00190 #include "../../inl/QxDataMember/QxDataMemberX.inl"
00191 
00192 #endif // _QX_DATA_MEMBER_X_H_