QxDao_IsDirty.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_DAO_IS_DIRTY_H_
  32. #define _QX_DAO_IS_DIRTY_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. #include <QtCore/qstringlist.h>
  37. #include <QxDao/QxSqlQueryBuilder.h>
  38. #include <QxDao/IxSqlRelation.h>
  39. #include <QxTraits/is_qx_registered.h>
  40. #include <QxTraits/is_container.h>
  41. #include <QxTraits/is_smart_ptr.h>
  42. #include <QxTraits/generic_container.h>
  43. namespace qx {
  44. namespace dao {
  45. namespace detail {
  46. template <class T>
  47. inline void is_dirty(const T & obj1, const T & obj2, QStringList & lstDiff);
  48. template <class T>
  49. struct QxDao_IsDirty_Generic
  50. {
  51. static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
  52. {
  53. static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
  54. qx::QxSqlQueryBuilder_Count<T> builder; builder.init();
  55. qx::IxDataMember * pId = builder.getDataId();
  56. if (pId && (! pId->isEqual((& obj1), (& obj2)))) { lstDiff.append(pId->getKey()); }
  57. long l = 0;
  58. qx::IxDataMember * p = NULL;
  59. while ((p = builder.nextData(l)))
  60. { if (p && (! p->isEqual((& obj1), (& obj2)))) { lstDiff.append(p->getKey()); } }
  61. }
  62. };
  63. template <class T>
  64. struct QxDao_IsDirty_Container
  65. {
  66. static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
  67. {
  68. if (qx::trait::generic_container<T>::size(obj1) <= 0) { return; }
  69. if (qx::trait::generic_container<T>::size(obj1) != qx::trait::generic_container<T>::size(obj2)) { lstDiff.append("*"); return; }
  70. long lCurrIndex = 0;
  71. typename T::const_iterator it2 = obj2.begin();
  72. for (typename T::const_iterator it1 = obj1.begin(); it1 != obj1.end(); ++it1)
  73. {
  74. QStringList lstDiffItem;
  75. qx::dao::detail::is_dirty((* it1), (* it2), lstDiffItem);
  76. if (lstDiffItem.count() > 0) { lstDiff.append(QString::number(lCurrIndex) + "|" + lstDiffItem.join("|")); }
  77. ++lCurrIndex; ++it2;
  78. }
  79. }
  80. };
  81. template <class T>
  82. struct QxDao_IsDirty_Ptr
  83. {
  84. static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
  85. { qx::dao::detail::is_dirty((* obj1), (* obj2), lstDiff); }
  86. };
  87. template <class T>
  88. struct QxDao_IsDirty
  89. {
  90. static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
  91. {
  92. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, qx::dao::detail::QxDao_IsDirty_Generic<T> >::type type_dao_1;
  93. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, type_dao_1 >::type type_dao_2;
  94. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_IsDirty_Container<T>, type_dao_2 >::type type_dao_3;
  95. type_dao_3::compare(obj1, obj2, lstDiff);
  96. }
  97. };
  98. template <class T>
  99. inline void is_dirty(const T & obj1, const T & obj2, QStringList & lstDiff)
  100. { return qx::dao::detail::QxDao_IsDirty<T>::compare(obj1, obj2, lstDiff); }
  101. } // namespace detail
  102. } // namespace dao
  103. } // namespace qx
  104. #endif // _QX_DAO_IS_DIRTY_H_