QxDao_Update_WithRelation.inl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. namespace qx {
  32. namespace dao {
  33. namespace detail {
  34. template <class T>
  35. struct QxDao_Update_WithRelation_Generic
  36. {
  37. static QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  38. {
  39. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update<T>());
  40. if (! dao.isValid()) { return dao.error(); }
  41. if (dao.isReadOnly()) { return dao.errReadOnly(); }
  42. if (! dao.isValidPrimaryKey(t)) { return dao.errInvalidId(); }
  43. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  44. if (! pDatabase) { dao.transaction(); }
  45. dao.quiet();
  46. qx::QxSqlRelationParams params(0, 0, NULL, (& dao.builder()), (& dao.query()), (& t));
  47. params.setDatabase((& dao.database()));
  48. qx::QxSqlRelationLinked * pRelationLinked = dao.getSqlRelationLinked();
  49. if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params)); }
  50. if (! dao.isValid()) { return dao.error(); }
  51. dao.updateError(qx::dao::update_by_query(query, t, (& dao.database())));
  52. if (! dao.isValid()) { return dao.error(); }
  53. if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnAfterSave(params)); }
  54. if (! dao.isValid()) { return dao.error(); }
  55. return dao.error();
  56. }
  57. };
  58. template <class T>
  59. struct QxDao_Update_WithRelation_Container
  60. {
  61. static QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  62. {
  63. typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
  64. if (qx::trait::generic_container<T>::size(t) <= 0) { return QSqlError(); }
  65. qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update<type_item>());
  66. if (! dao.isValid()) { return dao.error(); }
  67. if (dao.isReadOnly()) { return dao.errReadOnly(); }
  68. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  69. if (! pDatabase) { dao.transaction(); }
  70. dao.quiet();
  71. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  72. { if (! updateItem(query, (* it), dao)) { return dao.error(); } }
  73. return dao.error();
  74. }
  75. private:
  76. template <typename U>
  77. static inline bool updateItem(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  78. {
  79. bool bUpdateOk = updateItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::update(query, item, dao);
  80. if (bUpdateOk) { qx::dao::detail::QxDao_Keep_Original<U>::backup(item); }
  81. return bUpdateOk;
  82. }
  83. template <typename U, bool bIsPointer /* = true */>
  84. struct updateItem_Helper
  85. {
  86. static inline bool update(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  87. { return (item ? qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, (* item), dao) : true); }
  88. };
  89. template <typename U1, typename U2>
  90. struct updateItem_Helper<std::pair<U1, U2>, false>
  91. {
  92. static inline bool update(const qx::QxSqlQuery & query, std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  93. { return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao); }
  94. };
  95. template <typename U1, typename U2>
  96. struct updateItem_Helper<const std::pair<U1, U2>, false>
  97. {
  98. static inline bool update(const qx::QxSqlQuery & query, const std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  99. { return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao); }
  100. };
  101. template <typename U1, typename U2>
  102. struct updateItem_Helper<QPair<U1, U2>, false>
  103. {
  104. static inline bool update(const qx::QxSqlQuery & query, QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  105. { return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao); }
  106. };
  107. template <typename U1, typename U2>
  108. struct updateItem_Helper<const QPair<U1, U2>, false>
  109. {
  110. static inline bool update(const qx::QxSqlQuery & query, const QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  111. { return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao); }
  112. };
  113. template <typename U>
  114. struct updateItem_Helper<U, false>
  115. {
  116. static bool update(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  117. {
  118. qx::QxSqlRelationParams params(0, 0, NULL, (& dao.builder()), (& dao.query()), (& item));
  119. params.setDatabase((& dao.database()));
  120. qx::QxSqlRelationLinked * pRelationLinked = dao.getSqlRelationLinked();
  121. if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params)); }
  122. if (! dao.isValid()) { return false; }
  123. dao.updateError(qx::dao::update_by_query(query, item, (& dao.database())));
  124. if (! dao.isValid()) { return false; }
  125. if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnAfterSave(params)); }
  126. if (! dao.isValid()) { return false; }
  127. return dao.isValid();
  128. }
  129. };
  130. };
  131. template <class T>
  132. struct QxDao_Update_WithRelation_Ptr
  133. {
  134. static inline QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  135. { return (t ? qx::dao::update_by_query_with_relation(relation, query, (* t), pDatabase) : QSqlError()); }
  136. };
  137. template <class T>
  138. struct QxDao_Update_WithRelation
  139. {
  140. static inline QSqlError update(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  141. {
  142. QStringList lst;
  143. if (! relation.isEmpty()) { lst = relation.split("|"); }
  144. return QxDao_Update_WithRelation<T>::update(lst, query, t, pDatabase);
  145. }
  146. static inline QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  147. {
  148. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr<T>, qx::dao::detail::QxDao_Update_WithRelation_Generic<T> >::type type_dao_1;
  149. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr<T>, type_dao_1 >::type type_dao_2;
  150. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Container<T>, type_dao_2 >::type type_dao_3;
  151. QSqlError error = type_dao_3::update(relation, query, t, pDatabase);
  152. if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original<T>::backup(t); }
  153. return error;
  154. }
  155. };
  156. } // namespace detail
  157. } // namespace dao
  158. } // namespace qx