QxDao_FetchById_WithRelation.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_FetchById_WithRelation_Generic
  36. {
  37. typedef qx::dao::detail::QxDao_Helper<T> type_dao_helper;
  38. typedef qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<T> type_query_helper;
  39. static QSqlError fetchById(const QStringList & relation, T & t, QSqlDatabase * pDatabase)
  40. {
  41. type_dao_helper dao(t, pDatabase, "fetch by id with relation", new qx::QxSqlQueryBuilder_FetchById_WithRelation<T>());
  42. if (! dao.isValid()) { return dao.error(); }
  43. if (! dao.isValidPrimaryKey(t)) { return dao.errInvalidId(); }
  44. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  45. #ifdef _QX_ENABLE_MONGODB
  46. if (dao.isMongoDB())
  47. {
  48. qx::dao::on_before_fetch<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  49. QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
  50. qx::dao::mongodb::QxMongoDB_Helper::findOne((& dao), dao.getDataMemberX()->getClass(), json, NULL); if (! dao.isValid()) { return dao.error(); }
  51. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  52. qx::serialization::json::from_string(t, json, 1, "mongodb");
  53. qx::dao::on_after_fetch<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  54. return dao.error();
  55. }
  56. #endif // _QX_ENABLE_MONGODB
  57. QStringList columns;
  58. QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
  59. if (! dao.getDataId() || sql.isEmpty()) { return dao.errEmpty(); }
  60. if (! dao.prepare(sql)) { return dao.errFailed(true); }
  61. {
  62. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  63. type_query_helper::resolveInput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
  64. }
  65. if (! dao.exec(true)) { return dao.errFailed(); }
  66. {
  67. qx::dao::on_before_fetch<T>((& t), (& dao));
  68. if (! dao.isValid()) { return dao.error(); }
  69. if (dao.getCartesianProduct()) { fetchById_Complex(t, dao); }
  70. else { fetchById_Simple(t, dao); }
  71. if (! dao.isValid()) { return dao.error(); }
  72. qx::dao::on_after_fetch<T>((& t), (& dao));
  73. }
  74. return dao.error();
  75. }
  76. private:
  77. static inline void fetchById_Simple(T & t, type_dao_helper & dao)
  78. {
  79. if (! dao.nextRecord()) { dao.errNoData(); return; }
  80. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  81. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
  82. }
  83. static inline void fetchById_Complex(T & t, type_dao_helper & dao)
  84. {
  85. if (! dao.nextRecord()) { dao.errNoData(); return; }
  86. do
  87. {
  88. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  89. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
  90. if (! dao.isValid()) { return; }
  91. }
  92. while (dao.nextRecord());
  93. }
  94. };
  95. template <class T>
  96. struct QxDao_FetchById_WithRelation_Container
  97. {
  98. typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
  99. typedef qx::dao::detail::QxDao_FetchById_WithRelation_Container<T> type_this;
  100. typedef qx::trait::generic_container<T> type_generic_container;
  101. typedef typename type_generic_container::type_item type_item;
  102. typedef typename type_item::type_value_qx type_value_qx;
  103. typedef typename type_item::type_value type_value;
  104. static QSqlError fetchById(const QStringList & relation, T & t, QSqlDatabase * pDatabase)
  105. {
  106. if (qx::trait::generic_container<T>::size(t) <= 0) { return QSqlError(); }
  107. type_dao_helper dao(t, pDatabase, "fetch by id with relation", new qx::QxSqlQueryBuilder_FetchById_WithRelation<type_value_qx>());
  108. if (! dao.isValid()) { return dao.error(); }
  109. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  110. #ifdef _QX_ENABLE_MONGODB
  111. if (dao.isMongoDB())
  112. {
  113. for (typename T::iterator it = t.begin(); it != t.end(); ++it) { if (! fetchItem((* it), dao)) { return dao.error(); } }
  114. QStringList & itemsAsJson = dao.itemsAsJson();
  115. qx::dao::mongodb::QxMongoDB_Helper::findMany((& dao), dao.getDataMemberX()->getClass(), itemsAsJson, NULL, NULL); if (! dao.isValid()) { return dao.error(); }
  116. dao.qxQuery().queryAt(2, "<done>");
  117. for (typename T::iterator it = t.begin(); it != t.end(); ++it) { if (! fetchItem((* it), dao)) { return dao.error(); } }
  118. return dao.error();
  119. }
  120. #endif // _QX_ENABLE_MONGODB
  121. QStringList columns;
  122. QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
  123. if (! dao.getDataId() || sql.isEmpty()) { return dao.errEmpty(); }
  124. if (! dao.prepare(sql)) { return dao.errFailed(true); }
  125. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  126. { if (! fetchItem((* it), dao)) { return dao.error(); } }
  127. return dao.error();
  128. }
  129. private:
  130. template <typename U>
  131. static inline bool fetchItem(U & item, type_dao_helper & dao)
  132. {
  133. bool bFetchOk = fetchItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::fetch(item, dao);
  134. if (bFetchOk) { qx::dao::detail::QxDao_Keep_Original<U>::backup(item); }
  135. return bFetchOk;
  136. }
  137. template <typename U, bool bIsPointer /* = true */>
  138. struct fetchItem_Helper
  139. {
  140. static inline bool fetch(U & item, type_dao_helper & dao)
  141. { return (item ? type_this::fetchItem((* item), dao) : true); }
  142. };
  143. template <typename U1, typename U2>
  144. struct fetchItem_Helper<std::pair<U1, U2>, false>
  145. {
  146. static inline bool fetch(std::pair<U1, U2> & item, type_dao_helper & dao)
  147. { return type_this::fetchItem(item.second, dao); }
  148. };
  149. template <typename U1, typename U2>
  150. struct fetchItem_Helper<const std::pair<U1, U2>, false>
  151. {
  152. static inline bool fetch(const std::pair<U1, U2> & item, type_dao_helper & dao)
  153. { return type_this::fetchItem(item.second, dao); }
  154. };
  155. template <typename U1, typename U2>
  156. struct fetchItem_Helper<QPair<U1, U2>, false>
  157. {
  158. static inline bool fetch(QPair<U1, U2> & item, type_dao_helper & dao)
  159. { return type_this::fetchItem(item.second, dao); }
  160. };
  161. template <typename U1, typename U2>
  162. struct fetchItem_Helper<const QPair<U1, U2>, false>
  163. {
  164. static inline bool fetch(const QPair<U1, U2> & item, type_dao_helper & dao)
  165. { return type_this::fetchItem(item.second, dao); }
  166. };
  167. template <typename U>
  168. struct fetchItem_Helper<U, false>
  169. {
  170. typedef qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<U> type_query_helper;
  171. static bool fetch(U & item, type_dao_helper & dao)
  172. {
  173. if (! dao.isValidPrimaryKey(item)) { dao.errInvalidId(); return false; }
  174. #ifdef _QX_ENABLE_MONGODB
  175. if (dao.isMongoDB())
  176. {
  177. if (dao.qxQuery().queryAt(2) == "<done>")
  178. {
  179. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  180. if (! dao.itemsAsJson().isEmpty()) { QString json = dao.itemsAsJson().takeFirst(); if (! json.isEmpty()) { qx::serialization::json::from_string(item, json, 1, "mongodb"); } }
  181. qx::dao::on_after_fetch<U>((& item), (& dao));
  182. return dao.isValid();
  183. }
  184. {
  185. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  186. qx::dao::on_before_fetch<U>((& item), (& dao)); if (! dao.isValid()) { return false; }
  187. QVariant id = (dao.getDataId() ? dao.getDataId()->toVariant(& item) : QVariant());
  188. if (! id.isNull() && ! id.toString().isEmpty()) { dao.itemsAsJson().append(id.toString()); }
  189. }
  190. return dao.isValid();
  191. }
  192. #endif // _QX_ENABLE_MONGODB
  193. {
  194. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  195. type_query_helper::resolveInput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
  196. }
  197. if (! dao.exec(true)) { dao.errFailed(); return false; }
  198. {
  199. qx::dao::on_before_fetch<U>((& item), (& dao)); if (! dao.isValid()) { return false; }
  200. if (dao.getCartesianProduct()) { fetch_Complex(item, dao); }
  201. else { fetch_Simple(item, dao); }
  202. if (! dao.isValid()) { return false; }
  203. qx::dao::on_after_fetch<U>((& item), (& dao));
  204. }
  205. return dao.isValid();
  206. }
  207. static inline void fetch_Simple(U & item, type_dao_helper & dao)
  208. {
  209. if (! dao.nextRecord()) { dao.errNoData(); return; }
  210. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  211. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
  212. }
  213. static inline void fetch_Complex(U & item, type_dao_helper & dao)
  214. {
  215. if (! dao.nextRecord()) { dao.errNoData(); return; }
  216. do
  217. {
  218. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  219. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
  220. if (! dao.isValid()) { return; }
  221. }
  222. while (dao.nextRecord());
  223. }
  224. };
  225. };
  226. template <class T>
  227. struct QxDao_FetchById_WithRelation_Ptr
  228. {
  229. static inline QSqlError fetchById(const QStringList & relation, T & t, QSqlDatabase * pDatabase)
  230. { if (! t) { qx::trait::construct_ptr<T>::get(t); }; return qx::dao::fetch_by_id_with_relation(relation, (* t), pDatabase); }
  231. };
  232. template <class T>
  233. struct QxDao_FetchById_WithRelation
  234. {
  235. static inline QSqlError fetchById(const QString & relation, T & t, QSqlDatabase * pDatabase)
  236. {
  237. QStringList lst;
  238. if (! relation.isEmpty()) { lst = relation.split("|"); }
  239. return QxDao_FetchById_WithRelation<T>::fetchById(lst, t, pDatabase);
  240. }
  241. static inline QSqlError fetchById(const QStringList & relation, T & t, QSqlDatabase * pDatabase)
  242. {
  243. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Ptr<T>, qx::dao::detail::QxDao_FetchById_WithRelation_Generic<T> >::type type_dao_1;
  244. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Ptr<T>, type_dao_1 >::type type_dao_2;
  245. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Container<T>, type_dao_2 >::type type_dao_3;
  246. QSqlError error = type_dao_3::fetchById(relation, t, pDatabase);
  247. if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original<T>::backup(t); }
  248. return error;
  249. }
  250. };
  251. } // namespace detail
  252. } // namespace dao
  253. } // namespace qx