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