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