QxDataMemberX.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_DATA_MEMBER_X_H_
  32. #define _QX_DATA_MEMBER_X_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDataMemberX.h
  38. * \author Lionel Marty
  39. * \ingroup QxDataMember
  40. * \brief Concrete list of class properties registered into QxOrm context
  41. */
  42. #include <QxDataMember/IxDataMemberX.h>
  43. #include <QxDataMember/QxDataMember.h>
  44. #include <QxDataMember/QxDataMember_QObject.h>
  45. #include <QxDao/QxSqlRelation.h>
  46. #include <QxSingleton/QxSingleton.h>
  47. #include <QxFactory/QxFactory.h>
  48. #include <QxTraits/get_base_class.h>
  49. #include <QxTraits/get_primary_key.h>
  50. #include <QxTraits/get_sql_type.h>
  51. #include <QxTraits/qt_meta_object.h>
  52. namespace qx {
  53. /*!
  54. * \ingroup QxDataMember
  55. * \brief qx::QxDataMemberX<T> : concrete list of properties registered into QxOrm context for the class T
  56. */
  57. template <class T>
  58. class QxDataMemberX : public IxDataMemberX, public QxSingleton< QxDataMemberX<T> >
  59. {
  60. friend class QxSingleton< QxDataMemberX<T> >;
  61. public:
  62. typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
  63. typedef typename qx::trait::get_base_class<T>::type type_base_class;
  64. protected:
  65. QxDataMemberX() : IxDataMemberX(), QxSingleton< QxDataMemberX<T> >(QString("qx::QxDataMemberX_") + qx::trait::get_class_name<T>::get_xml_tag()) { ; }
  66. virtual ~QxDataMemberX() { ; }
  67. public:
  68. virtual long count_WithDaoStrategy() const { return count_WithDaoStrategy_Helper(); }
  69. virtual bool exist_WithDaoStrategy(const QString & sKey) const { return exist_WithDaoStrategy_Helper(sKey); }
  70. virtual IxDataMember * get_WithDaoStrategy(long lIndex) const { return get_WithDaoStrategy_Helper(lIndex); }
  71. virtual IxDataMember * get_WithDaoStrategy(const QString & sKey) const { return get_WithDaoStrategy_Helper(sKey); }
  72. virtual IxDataMember * getId_WithDaoStrategy() const { return getId_WithDaoStrategy_Helper(); }
  73. IxDataMember * id(type_primary_key T::* pDataMemberId, const QString & sKey, long lVersion = 0);
  74. IxDataMember * id(const QString & sKey, long lVersion);
  75. IxDataMember * add(const QString & sKey, long lVersion);
  76. template <typename V, typename U> IxDataMember * add(V U::* pData, const QString & sKey, long lVersion = 0, bool bSerialize = true, bool bDao = true);
  77. template <typename V, typename U> IxSqlRelation * relationOneToOne(V U::* pData, const QString & sKey, long lVersion = 0);
  78. template <typename V, typename U> IxSqlRelation * relationManyToOne(V U::* pData, const QString & sKey, long lVersion = 0);
  79. template <typename V, typename U> IxSqlRelation * relationOneToMany(V U::* pData, const QString & sKey, const QString & sForeignKey, long lVersion = 0);
  80. 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);
  81. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  82. template <class Archive> inline void toArchive(const T * pOwner, Archive & ar, const unsigned int file_version) const;
  83. template <class Archive> inline void fromArchive(T * pOwner, Archive & ar, const unsigned int file_version);
  84. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  85. private:
  86. IxDataMember * initId(IxDataMember * pId, long lVersion);
  87. IxDataMember * initData(IxDataMember * pData, long lVersion);
  88. inline IxDataMemberX * getBaseClass_Helper() const { return QxDataMemberX<type_base_class>::getSingleton(); }
  89. long count_WithDaoStrategy_Helper() const
  90. {
  91. if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
  92. { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? count() : getBaseClass_Helper()->count_WithDaoStrategy()); }
  93. else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
  94. { return (count() + ((! getId() && getId_WithDaoStrategy()) ? 1 : 0)); }
  95. else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
  96. { return (count() + getBaseClass_Helper()->count_WithDaoStrategy()); }
  97. qAssert(false); return 0;
  98. }
  99. bool exist_WithDaoStrategy_Helper(const QString & sKey) const
  100. {
  101. if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
  102. { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? exist(sKey) : getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
  103. else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
  104. { return (exist(sKey) || (getId_WithDaoStrategy() ? (getId_WithDaoStrategy()->getKey() == sKey) : false)); }
  105. else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
  106. { return (exist(sKey) || getBaseClass_Helper()->exist_WithDaoStrategy(sKey)); }
  107. qAssert(false); return false;
  108. }
  109. IxDataMember * get_WithDaoStrategy_Helper(long lIndex) const
  110. {
  111. if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
  112. { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex)); }
  113. else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
  114. { return ((! getId() && (lIndex == count())) ? getId_WithDaoStrategy() : get(lIndex)); }
  115. else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
  116. { return (((lIndex >= 0) && (lIndex < count())) ? get(lIndex) : getBaseClass_Helper()->get_WithDaoStrategy(lIndex - count())); }
  117. qAssert(false); return NULL;
  118. }
  119. IxDataMember * get_WithDaoStrategy_Helper(const QString & sKey) const
  120. {
  121. if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
  122. { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
  123. else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
  124. { return ((getId_WithDaoStrategy() && (getId_WithDaoStrategy()->getKey() == sKey)) ? getId_WithDaoStrategy() : get(sKey)); }
  125. else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
  126. { return (exist(sKey) ? get(sKey) : getBaseClass_Helper()->get_WithDaoStrategy(sKey)); }
  127. qAssert(false); return NULL;
  128. }
  129. IxDataMember * getId_WithDaoStrategy_Helper() const
  130. {
  131. if (getDaoStrategy() == qx::dao::strategy::single_table_inheritance)
  132. { return ((getBaseClass_Helper()->getDaoStrategy() != getDaoStrategy()) ? getId() : getBaseClass_Helper()->getId_WithDaoStrategy()); }
  133. else if (getDaoStrategy() == qx::dao::strategy::class_table_inheritance)
  134. { return (getId() ? getId() : getBaseClass_Helper()->getId_WithDaoStrategy()); }
  135. else if (getDaoStrategy() == qx::dao::strategy::concrete_table_inheritance)
  136. { return (getId() ? getId() : getBaseClass_Helper()->getId_WithDaoStrategy()); }
  137. qAssert(false); return NULL;
  138. }
  139. };
  140. } // namespace qx
  141. #include "../../inl/QxDataMember/QxDataMemberX.inl"
  142. #endif // _QX_DATA_MEMBER_X_H_