QxOrm  1.4.8
C++ Object Relational Mapping library
QxDataMemberX.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_DATA_MEMBER_X_H_
00033 #define _QX_DATA_MEMBER_X_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDataMember/IxDataMemberX.h>
00047 #include <QxDataMember/QxDataMember.h>
00048 #include <QxDataMember/QxDataMember_QObject.h>
00049 #include <QxDataMember/QxDataMember_PImpl.h>
00050 
00051 #include <QxDao/QxSqlRelation.h>
00052 
00053 #include <QxSingleton/QxSingleton.h>
00054 
00055 #include <QxFactory/QxFactory.h>
00056 
00057 #include <QxTraits/get_base_class.h>
00058 #include <QxTraits/get_primary_key.h>
00059 #include <QxTraits/get_sql_type.h>
00060 #include <QxTraits/qt_meta_object.h>
00061 
00062 namespace qx {
00063 
00068 template <class T>
00069 class QxDataMemberX : public IxDataMemberX, public QxSingleton< QxDataMemberX<T> >
00070 {
00071 
00072    friend class QxSingleton< QxDataMemberX<T> >;
00073 
00074 public:
00075 
00076    typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
00077    typedef typename qx::trait::get_base_class<T>::type type_base_class;
00078 
00079 protected:
00080 
00081    QxDataMemberX() : IxDataMemberX(), QxSingleton< QxDataMemberX<T> >(QString("qx::QxDataMemberX_") + qx::trait::get_class_name<T>::get_xml_tag()) { ; }
00082    virtual ~QxDataMemberX() { ; }
00083 
00084 public:
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, long lVersion = 0);
00093    IxDataMember * id(const QString & sKey, long lVersion);
00094    IxDataMember * add(const QString & sKey, long lVersion);
00095 
00096    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion = 0, bool bSerialize = true, bool bDao = true);
00097    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion = 0);
00098    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion = 0);
00099    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion = 0);
00100    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType, long lVersion = 0);
00101 
00102    template <typename V, typename U> IxDataMember * pimpl(V U::* pData, const QString & sKey);
00103    template <typename U> IxDataMember * id(type_primary_key U::* pDataMemberId, const QString & sKey, long lVersion, IxDataMember * pImpl);
00104    template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion, bool bSerialize, bool bDao, IxDataMember * pImpl);
00105    template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion, IxDataMember * pImpl);
00106    template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion, IxDataMember * pImpl);
00107    template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion, IxDataMember * pImpl);
00108    template <typename V, typename U> IxSqlRelation * relationManyToMany(V U::* pData, const QString & sKey, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType, long lVersion, IxDataMember * pImpl);
00109 
00110 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00111    template <class Archive> inline void toArchive(const T * pOwner, Archive & ar, const unsigned int file_version) const;
00112    template <class Archive> inline void fromArchive(T * pOwner, Archive & ar, const unsigned int file_version);
00113 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00114 
00115 private:
00116 
00117    IxDataMember * initId(IxDataMember * pId, long lVersion);
00118    IxDataMember * initData(IxDataMember * pData, long lVersion);
00119    IxDataMember * initPImpl(IxDataMember * pImpl);
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() + ((! getId() && 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 ((! getId() && (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()) ? getId() : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00171       else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
00172       { return (getId() ? getId() : getBaseClass_Helper()->getId_WithDaoStrategy()); }
00173       else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
00174       { return (getId() ? getId() : 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_