QxDao_FetchAll_WithRelation.inl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_FetchAll_WithRelation_Generic
  36. {
  37. typedef qx::dao::detail::QxDao_Helper<T> type_dao_helper;
  38. typedef qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<T> type_query_helper;
  39. static QSqlError fetchAll(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  40. {
  41. type_dao_helper dao(t, pDatabase, "fetch all with relation", new qx::QxSqlQueryBuilder_FetchAll_WithRelation<T>(), (& query));
  42. if (! dao.isValid()) { return dao.error(); }
  43. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  44. #ifdef _QX_ENABLE_MONGODB
  45. if (dao.isMongoDB())
  46. {
  47. qx::dao::on_before_fetch<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  48. QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
  49. qx::dao::mongodb::QxMongoDB_Helper::findOne((& dao), dao.getDataMemberX()->getClass(), json, (& query)); if (! dao.isValid()) { return dao.error(); }
  50. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  51. qx::serialization::json::from_string(t, json, 1, "mongodb");
  52. qx::dao::on_after_fetch<T>((& t), (& dao)); if (! dao.isValid()) { return dao.error(); }
  53. return dao.error();
  54. }
  55. #endif // _QX_ENABLE_MONGODB
  56. QStringList columns;
  57. QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
  58. if (sql.isEmpty()) { return dao.errEmpty(); }
  59. if (! query.isEmpty()) { dao.addQuery(true); sql = dao.builder().getSqlQuery(); }
  60. if (! dao.exec()) { return dao.errFailed(); }
  61. {
  62. qx::dao::on_before_fetch<T>((& t), (& dao));
  63. if (! dao.isValid()) { return dao.error(); }
  64. if (dao.getCartesianProduct()) { fetchAll_Complex(t, dao); }
  65. else { fetchAll_Simple(t, dao); }
  66. if (! dao.isValid()) { return dao.error(); }
  67. qx::dao::on_after_fetch<T>((& t), (& dao));
  68. }
  69. return dao.error();
  70. }
  71. private:
  72. static inline void fetchAll_Simple(T & t, type_dao_helper & dao)
  73. {
  74. if (! dao.nextRecord()) { return; }
  75. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  76. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
  77. }
  78. static inline void fetchAll_Complex(T & t, type_dao_helper & dao)
  79. {
  80. while (dao.nextRecord())
  81. {
  82. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  83. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
  84. if (! dao.isValid()) { return; }
  85. }
  86. }
  87. };
  88. #ifdef _QX_ENABLE_MONGODB
  89. template <class T>
  90. struct QxDao_FetchAll_WithRelation_MongoDB_Fetcher : public qx::dao::mongodb::QxMongoDB_Fetcher
  91. {
  92. typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
  93. typedef qx::trait::generic_container<T> type_generic_container;
  94. typedef typename type_generic_container::type_item type_item;
  95. typedef typename type_item::type_value_qx type_value_qx;
  96. typedef typename type_item::type_value type_value;
  97. T & m_t;
  98. type_dao_helper & m_dao;
  99. QxDao_FetchAll_WithRelation_MongoDB_Fetcher(T & t, type_dao_helper & dao) : qx::dao::mongodb::QxMongoDB_Fetcher(), m_t(t), m_dao(dao) { ; }
  100. virtual ~QxDao_FetchAll_WithRelation_MongoDB_Fetcher() { ; }
  101. virtual void fetch(const QString & json)
  102. { fetcherHelper<type_item::is_value_pointer, 0>::insertNewItem(m_t, m_dao, json); }
  103. private:
  104. template <bool bIsPointer /* = false */, int dummy>
  105. struct fetcherHelper
  106. {
  107. static void insertNewItem(T & t, type_dao_helper & dao, const QString & json)
  108. {
  109. type_item item = type_generic_container::createItem();
  110. qx::IxDataMember * pId = dao.getDataId(); qAssert(pId);
  111. if (json.isEmpty()) { return; }
  112. type_value_qx & item_val_tmp = item.value_qx();
  113. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  114. qx::serialization::json::from_string(item_val_tmp, json, 1, "mongodb:columns{,_id," + pId->getKey() + ",}");
  115. for (int i = 0; i < (pId ? pId->getNameCount() : 0); i++)
  116. {
  117. QVariant id = pId->toVariant((& item_val_tmp), "", i, qx::cvt::context::e_database);
  118. qx::cvt::from_variant(id, item.key(), "", i, qx::cvt::context::e_database);
  119. }
  120. type_value * pValue = type_generic_container::insertItem(t, item);
  121. type_value_qx & item_val = (pValue ? (* static_cast<type_value_qx *>(pValue)) : item.value_qx());
  122. qx::dao::on_before_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  123. qx::serialization::json::from_string(item_val, json, 1, "mongodb");
  124. qx::dao::on_after_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  125. qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
  126. }
  127. };
  128. template <int dummy>
  129. struct fetcherHelper<true, dummy>
  130. {
  131. static void insertNewItem(T & t, type_dao_helper & dao, const QString & json)
  132. {
  133. type_item item = type_generic_container::createItem();
  134. type_value_qx & item_val = item.value_qx();
  135. qx::IxDataMember * pId = dao.getDataId(); qAssert(pId);
  136. if (json.isEmpty()) { return; }
  137. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  138. qx::dao::on_before_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  139. qx::serialization::json::from_string(item_val, json, 1, "mongodb");
  140. for (int i = 0; i < (pId ? pId->getNameCount() : 0); i++)
  141. {
  142. QVariant id = pId->toVariant((& item_val), "", i, qx::cvt::context::e_database);
  143. qx::cvt::from_variant(id, item.key(), "", i, qx::cvt::context::e_database);
  144. }
  145. qx::dao::on_after_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  146. qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
  147. qx::trait::generic_container<T>::insertItem(t, item);
  148. }
  149. };
  150. };
  151. #endif // _QX_ENABLE_MONGODB
  152. template <class T>
  153. struct QxDao_FetchAll_WithRelation_Container
  154. {
  155. typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
  156. typedef qx::dao::detail::QxDao_FetchAll_WithRelation_Container<T> type_this;
  157. typedef qx::trait::generic_container<T> type_generic_container;
  158. typedef typename type_generic_container::type_item type_item;
  159. typedef typename type_item::type_value_qx type_value_qx;
  160. typedef typename type_item::type_value type_value;
  161. typedef qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_value_qx> type_query_helper;
  162. static QSqlError fetchAll(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  163. {
  164. type_generic_container::clear(t);
  165. type_dao_helper dao(t, pDatabase, "fetch all with relation", new qx::QxSqlQueryBuilder_FetchAll_WithRelation<type_value_qx>(), (& query));
  166. if (! dao.isValid()) { return dao.error(); }
  167. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  168. #ifdef _QX_ENABLE_MONGODB
  169. if (dao.isMongoDB())
  170. {
  171. QStringList & itemsAsJson = dao.itemsAsJson();
  172. QxDao_FetchAll_WithRelation_MongoDB_Fetcher<T> fetcher(t, dao);
  173. qx::dao::mongodb::QxMongoDB_Helper::findMany((& dao), dao.getDataMemberX()->getClass(), itemsAsJson, (& query), (& fetcher));
  174. return dao.error();
  175. }
  176. #endif // _QX_ENABLE_MONGODB
  177. bool bComplex = dao.getCartesianProduct(); QVariant vId; QStringList columns;
  178. QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
  179. if (sql.isEmpty()) { return dao.errEmpty(); }
  180. if (! query.isEmpty()) { dao.addQuery(true); sql = dao.builder().getSqlQuery(); }
  181. if (! dao.exec()) { return dao.errFailed(); }
  182. bool bSize = (dao.hasFeature(QSqlDriver::QuerySize) && (dao.query().size() > 0));
  183. if (bSize) { type_generic_container::reserve(t, dao.query().size()); }
  184. while (dao.nextRecord())
  185. {
  186. if (! dao.isValid()) { return dao.error(); }
  187. qx::IxDataMember * pId = dao.getDataId(); qAssert(pId);
  188. if (pId) { QString sId; for (int i = 0; i < pId->getNameCount(); i++) { sId += dao.query().value(i).toString() + "|"; }; vId = sId; }
  189. void * pItemTmp = (bComplex ? dao.builder().existIdX(0, vId, vId) : NULL);
  190. if (! pItemTmp) { insertHelper<type_item::is_value_pointer, 0>::insertNewItem(t, dao); continue; }
  191. type_value_qx * pItem = static_cast<type_value_qx *>(pItemTmp);
  192. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), (* pItem), dao.query(), dao.builder());
  193. }
  194. if (bSize) { type_generic_container::reserve(t, type_generic_container::size(t)); }
  195. return dao.error();
  196. }
  197. private:
  198. template <bool bIsPointer /* = false */, int dummy>
  199. struct insertHelper
  200. {
  201. static void insertNewItem(T & t, type_dao_helper & dao)
  202. {
  203. type_item item = type_generic_container::createItem();
  204. qx::IxDataMember * pId = dao.getDataId(); qAssert(pId);
  205. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  206. if (pId) { for (int i = 0; i < pId->getNameCount(); i++) { QVariant v = dao.query().value(i); qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database); } }
  207. type_value * pValue = type_generic_container::insertItem(t, item);
  208. type_value_qx & item_val = (pValue ? (* static_cast<type_value_qx *>(pValue)) : item.value_qx());
  209. qx::dao::on_before_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  210. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item_val, dao.query(), dao.builder()); if (! dao.isValid()) { return; }
  211. qx::dao::on_after_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  212. qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
  213. }
  214. };
  215. template <int dummy>
  216. struct insertHelper<true, dummy>
  217. {
  218. static void insertNewItem(T & t, type_dao_helper & dao)
  219. {
  220. type_item item = type_generic_container::createItem();
  221. type_value_qx & item_val = item.value_qx();
  222. qx::IxDataMember * pId = dao.getDataId(); qAssert(pId);
  223. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
  224. if (pId) { for (int i = 0; i < pId->getNameCount(); i++) { QVariant v = dao.query().value(i); qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database); } }
  225. qx::dao::on_before_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  226. type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item_val, dao.query(), dao.builder()); if (! dao.isValid()) { return; }
  227. qx::dao::on_after_fetch<type_value_qx>((& item_val), (& dao)); if (! dao.isValid()) { return; }
  228. type_generic_container::insertItem(t, item);
  229. qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
  230. }
  231. };
  232. };
  233. template <class T>
  234. struct QxDao_FetchAll_WithRelation_Ptr
  235. {
  236. static inline QSqlError fetchAll(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  237. { return (t ? qx::dao::fetch_by_query_with_relation(relation, query, (* t), pDatabase) : QSqlError()); }
  238. };
  239. template <class T>
  240. struct QxDao_FetchAll_WithRelation
  241. {
  242. static inline QSqlError fetchAll(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  243. {
  244. QStringList lst;
  245. if (! relation.isEmpty()) { lst = relation.split("|"); }
  246. return QxDao_FetchAll_WithRelation<T>::fetchAll(lst, query, t, pDatabase);
  247. }
  248. static inline QSqlError fetchAll(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase)
  249. {
  250. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Ptr<T>, qx::dao::detail::QxDao_FetchAll_WithRelation_Generic<T> >::type type_dao_1;
  251. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Ptr<T>, type_dao_1 >::type type_dao_2;
  252. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Container<T>, type_dao_2 >::type type_dao_3;
  253. QSqlError error = type_dao_3::fetchAll(relation, query, t, pDatabase);
  254. if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original<T>::backup(t); }
  255. return error;
  256. }
  257. };
  258. } // namespace detail
  259. } // namespace dao
  260. } // namespace qx