QxDao_Update.inl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_Generic
  36. {
  37. static QSqlError update(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase, const QStringList & columns)
  38. {
  39. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "update", new qx::QxSqlQueryBuilder_Update<T>(), (& query));
  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.validateInstance(t)) { return dao.error(); }
  44. #ifdef _QX_ENABLE_MONGODB
  45. if (dao.isMongoDB())
  46. {
  47. qx::dao::on_before_update<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  48. QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
  49. qx::dao::mongodb::QxMongoDB_Helper::updateOne((& dao), dao.getDataMemberX()->getClass(), qx::serialization::json::to_string(t, 1, ctx), (& query)); if (! dao.isValid()) { return dao.error(); }
  50. qx::dao::on_after_update<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  51. return dao.error();
  52. }
  53. #endif // _QX_ENABLE_MONGODB
  54. QString sql = dao.builder().buildSql(columns).getSqlQuery();
  55. if (! dao.getDataId() || sql.isEmpty()) { return dao.errEmpty(); }
  56. if (! query.isEmpty()) { dao.addQuery(false); sql = dao.builder().getSqlQuery(); }
  57. if (! pDatabase) { dao.transaction(); }
  58. if (! dao.prepare(sql)) { return dao.errFailed(true); }
  59. IxSqlGenerator * pSqlGenerator = dao.getSqlGenerator();
  60. if (pSqlGenerator) { pSqlGenerator->onBeforeUpdate((& dao), (& t)); }
  61. qx::dao::on_before_update<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  62. {
  63. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  64. qx::dao::detail::QxSqlQueryHelper_Update<T>::resolveInput(t, dao.query(), dao.builder(), columns);
  65. }
  66. if (! query.isEmpty()) { query.resolve(dao.query()); }
  67. if (! dao.exec(true)) { return dao.errFailed(); }
  68. if (pSqlGenerator) { pSqlGenerator->onAfterUpdate((& dao), (& t)); }
  69. qx::dao::on_after_update<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  70. return dao.error();
  71. }
  72. };
  73. template <class T>
  74. struct QxDao_Update_Container
  75. {
  76. static QSqlError update(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase, const QStringList & columns)
  77. {
  78. typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
  79. if (qx::trait::generic_container<T>::size(t) <= 0) { return QSqlError(); }
  80. qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "update", new qx::QxSqlQueryBuilder_Update<type_item>(), (& query));
  81. if (! dao.isValid()) { return dao.error(); }
  82. if (dao.isReadOnly()) { return dao.errReadOnly(); }
  83. if (! dao.validateInstance(t)) { return dao.error(); }
  84. #ifdef _QX_ENABLE_MONGODB
  85. if (dao.isMongoDB())
  86. {
  87. dao.setSqlColumns(columns);
  88. for (typename T::iterator it = t.begin(); it != t.end(); ++it) { if (! updateItem((* it), dao)) { return dao.error(); } }
  89. QStringList & itemsAsJson = dao.itemsAsJson();
  90. qx::dao::mongodb::QxMongoDB_Helper::updateMany((& dao), dao.getDataMemberX()->getClass(), itemsAsJson, (& query)); if (! dao.isValid()) { return dao.error(); }
  91. dao.qxQuery().queryAt(2, "<done>"); dao.itemsAsJson().clear();
  92. for (typename T::iterator it = t.begin(); it != t.end(); ++it) { if (! updateItem((* it), dao)) { return dao.error(); } }
  93. return dao.error();
  94. }
  95. #endif // _QX_ENABLE_MONGODB
  96. QString sql = dao.builder().buildSql(columns).getSqlQuery();
  97. if (! dao.getDataId() || sql.isEmpty()) { return dao.errEmpty(); }
  98. if (! query.isEmpty()) { dao.addQuery(false); sql = dao.builder().getSqlQuery(); }
  99. if (! pDatabase) { dao.transaction(); }
  100. if (! dao.prepare(sql)) { return dao.errFailed(true); }
  101. dao.setSqlColumns(columns);
  102. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  103. { if (! updateItem((* it), dao)) { return dao.error(); } }
  104. return dao.error();
  105. }
  106. private:
  107. template <typename U>
  108. static inline bool updateItem(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  109. {
  110. bool bUpdateOk = updateItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::update(item, dao);
  111. if (bUpdateOk) { qx::dao::detail::QxDao_Keep_Original<U>::backup(item); }
  112. return bUpdateOk;
  113. }
  114. template <typename U, bool bIsPointer /* = true */>
  115. struct updateItem_Helper
  116. {
  117. static inline bool update(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  118. { return (item ? qx::dao::detail::QxDao_Update_Container<T>::updateItem((* item), dao) : true); }
  119. };
  120. template <typename U1, typename U2>
  121. struct updateItem_Helper<std::pair<U1, U2>, false>
  122. {
  123. static inline bool update(std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  124. { return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao); }
  125. };
  126. template <typename U1, typename U2>
  127. struct updateItem_Helper<const std::pair<U1, U2>, false>
  128. {
  129. static inline bool update(const std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  130. { return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao); }
  131. };
  132. template <typename U1, typename U2>
  133. struct updateItem_Helper<QPair<U1, U2>, false>
  134. {
  135. static inline bool update(QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  136. { return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao); }
  137. };
  138. template <typename U1, typename U2>
  139. struct updateItem_Helper<const QPair<U1, U2>, false>
  140. {
  141. static inline bool update(const QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  142. { return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao); }
  143. };
  144. template <typename U>
  145. struct updateItem_Helper<U, false>
  146. {
  147. static bool update(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  148. {
  149. QStringList columns = dao.getSqlColumns();
  150. #ifdef _QX_ENABLE_MONGODB
  151. if (dao.isMongoDB())
  152. {
  153. if (dao.qxQuery().queryAt(2) == "<done>") { qx::dao::on_after_update<U>((& item), (& dao)); return dao.isValid(); }
  154. QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
  155. qx::dao::on_before_update<U>((& item), (& dao)); if (! dao.isValid()) { return false; }
  156. dao.itemsAsJson().append(qx::serialization::json::to_string(item, 1, ctx));
  157. return dao.isValid();
  158. }
  159. #endif // _QX_ENABLE_MONGODB
  160. if (! dao.isValidPrimaryKey(item)) { dao.errInvalidId(); return false; }
  161. IxSqlGenerator * pSqlGenerator = dao.getSqlGenerator();
  162. if (pSqlGenerator) { pSqlGenerator->onBeforeUpdate((& dao), (& item)); }
  163. qx::dao::on_before_update<U>((& item), (& dao)); if (! dao.isValid()) { return false; }
  164. {
  165. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  166. qx::dao::detail::QxSqlQueryHelper_Update<U>::resolveInput(item, dao.query(), dao.builder(), columns);
  167. }
  168. if (! dao.qxQuery().isEmpty()) { dao.qxQuery().resolve(dao.query()); }
  169. if (! dao.exec(true)) { dao.errFailed(); return false; }
  170. if (pSqlGenerator) { pSqlGenerator->onAfterUpdate((& dao), (& item)); }
  171. qx::dao::on_after_update<U>((& item), (& dao)); if (! dao.isValid()) { return false; }
  172. return dao.isValid();
  173. }
  174. };
  175. };
  176. template <class T>
  177. struct QxDao_Update_Ptr
  178. {
  179. static inline QSqlError update(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase, const QStringList & columns)
  180. { return (t ? qx::dao::update_by_query(query, (* t), pDatabase, columns) : QSqlError()); }
  181. };
  182. template <class T>
  183. struct QxDao_Update
  184. {
  185. static inline QSqlError update(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase, const QStringList & columns)
  186. {
  187. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_Update_Ptr<T>, qx::dao::detail::QxDao_Update_Generic<T> >::type type_dao_1;
  188. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Update_Ptr<T>, type_dao_1 >::type type_dao_2;
  189. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Update_Container<T>, type_dao_2 >::type type_dao_3;
  190. QSqlError error = type_dao_3::update(query, t, pDatabase, columns);
  191. if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original<T>::backup(t); }
  192. return error;
  193. }
  194. };
  195. } // namespace detail
  196. } // namespace dao
  197. } // namespace qx