QxOrm 1.1.8
C++ Object Relational Mapping library
QxDataMemberX.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_DATA_MEMBER_X_H_
00027 #define _QX_DATA_MEMBER_X_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/type_traits/is_integral.hpp>
00041 #include <boost/type_traits/is_base_of.hpp>
00042 
00043 #include <QxDataMember/IxDataMemberX.h>
00044 #include <QxDataMember/QxDataMember.h>
00045 
00046 #include <QxDao/QxSqlRelation.h>
00047 
00048 #include <QxSingleton/QxSingleton.h>
00049 
00050 #include <QxFactory/QxFactory.h>
00051 
00052 #include <QxTraits/get_base_class.h>
00053 #include <QxTraits/get_primary_key.h>
00054 #include <QxTraits/get_sql_type.h>
00055 
00056 namespace qx {
00057 
00062 template <class T>
00063 class QxDataMemberX : public IxDataMemberX, public QxSingleton< QxDataMemberX<T> >
00064 {
00065 
00066    friend class QxSingleton< QxDataMemberX<T> >;
00067 
00068 public:
00069 
00070    typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
00071    typedef typename qx::trait::get_base_class<T>::type type_base_class;
00072 
00073 protected:
00074 
00075    QxDataMember<type_primary_key, T> * m_pDataMemberId;  
00076 
00077 protected:
00078 
00079    QxDataMemberX() : IxDataMemberX(), QxSingleton< QxDataMemberX<T> >(QString("qx::QxDataMemberX_") + qx::trait::get_class_name<T>::get_xml_tag()), m_pDataMemberId(NULL) { ; }
00080    virtual ~QxDataMemberX() { ; }
00081 
00082 public:
00083 
00084    virtual IxDataMember * getId() const { return m_pDataMemberId; }
00085 
00086    virtual long count_WithDaoStrategy() const                              { return count_WithDaoStrategy_Helper(); }
00087    virtual bool exist_WithDaoStrategy(const QString & sKey) const          { return exist_WithDaoStrategy_Helper(sKey); }
00088    virtual IxDataMember * get_WithDaoStrategy(long lIndex) const           { return get_WithDaoStrategy_Helper(lIndex); }
00089    virtual IxDataMember * get_WithDaoStrategy(const QString & sKey) const  { return get_WithDaoStrategy_Helper(sKey); }
00090    virtual IxDataMember * getId_WithDaoStrategy() const                    { return getId_WithDaoStrategy_Helper(); }
00091 
00092    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey);
00093    IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey, long lVersion);
00094 
00095    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey);
00096    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion);
00097    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion, bool bSerialize);
00098    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao);
00099 
00100    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey);
00101    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion);
00102    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey);
00103    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion);
00104    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey);
00105    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion);
00106    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType);
00107    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType, long lVersion);
00108 
00109    template <class Archive> inline void toArchive(const T * pOwner, Archive & ar, const unsigned int file_version) const;
00110    template <class Archive> inline void fromArchive(T * pOwner, Archive & ar, const unsigned int file_version);
00111 
00112 private:
00113 
00114    inline IxDataMemberX * getBaseClass_Helper() const { return QxDataMemberX<type_base_class>::getSingleton(); }
00115 
00116    long count_WithDaoStrategy_Helper() const
00117    {
00118       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00119       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? count() : getBaseClass_Helper()->count_WithDaoStrategy()); }
00120       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00121       { return (count() + ((! m_pDataMemberId && getId_WithDaoStrategy()) ? 1 : 0)); }
00122       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00123       { return (count() + getBaseClass_Helper()->count_WithDaoStrategy()); }
00124       qAssert(false); return 0;
00125    }
00126 
00127    bool exist_WithDaoStrategy_Helper(const QString & sKey) const
00128    {
00129       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00130       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? exist(sKey) : getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
00131       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00132       { return (exist(sKey) || (getId_WithDaoStrategy() ? (getId_WithDaoStrategy()->getKey() == sKey) : false)); }
00133       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00134       { return (exist(sKey) || getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
00135       qAssert(false); return false;
00136    }
00137 
00138    IxDataMember * get_WithDaoStrategy_Helper(long lIndex) const
00139    {
00140       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00141       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex)); }
00142       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00143       { return ((! m_pDataMemberId && (lIndex == count())) ? getId_WithDaoStrategy() : get(lIndex)); }
00144       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00145       { return (((lIndex >= 0) && (lIndex < count())) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex - count())); }
00146       qAssert(false); return NULL;
00147    }
00148 
00149    IxDataMember * get_WithDaoStrategy_Helper(const QString & sKey) const
00150    {
00151       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00152       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
00153       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00154       { return ((getId_WithDaoStrategy() && (getId_WithDaoStrategy()->getKey() == sKey)) ? getId_WithDaoStrategy() : get(sKey)); }
00155       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00156       { return (exist(sKey) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
00157       qAssert(false); return NULL;
00158    }
00159 
00160    IxDataMember * getId_WithDaoStrategy_Helper() const
00161    {
00162       if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
00163       { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00164       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00165       { return (m_pDataMemberId ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00166       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00167       { return (m_pDataMemberId ? m_pDataMemberId : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00168       qAssert(false); return NULL;
00169    }
00170 
00171 };
00172 
00173 } // namespace qx
00174 
00175 #include "../../inl/QxDataMember/QxDataMemberX.inl"
00176 
00177 #endif // _QX_DATA_MEMBER_X_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines