QxDao_Exist.inl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_Exist_Generic
  36. {
  37. static qx_bool exist(T & t, QSqlDatabase * pDatabase)
  38. {
  39. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "exist", new qx::QxSqlQueryBuilder_Exist<T>());
  40. if (! dao.isValid()) { return qx_bool(false); }
  41. #ifdef _QX_ENABLE_MONGODB
  42. if (dao.isMongoDB())
  43. {
  44. long cnt = 0; QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id"); qx_query query(json);
  45. qx::dao::mongodb::QxMongoDB_Helper::count((& dao), dao.getDataMemberX()->getClass(), cnt, (& query)); if (! dao.isValid()) { return qx_bool(false); }
  46. return qx_bool(cnt >= 1);
  47. }
  48. #endif // _QX_ENABLE_MONGODB
  49. QString sql = dao.builder().buildSql().getSqlQuery();
  50. if (! dao.getDataId() || sql.isEmpty()) { dao.errEmpty(); return qx_bool(false); }
  51. if (! dao.prepare(sql)) { dao.errFailed(true); return qx_bool(false); }
  52. {
  53. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  54. qx::dao::detail::QxSqlQueryHelper_Exist<T>::resolveInput(t, dao.query(), dao.builder());
  55. }
  56. if (! dao.exec(true)) { dao.errFailed(); return qx_bool(false); }
  57. return qx_bool(dao.nextRecord());
  58. }
  59. };
  60. template <class T>
  61. struct QxDao_Exist_Container
  62. {
  63. static qx_bool exist(T & t, QSqlDatabase * pDatabase)
  64. {
  65. typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
  66. if (qx::trait::generic_container<T>::size(t) <= 0) { return qx_bool(false); }
  67. qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "exist", new qx::QxSqlQueryBuilder_Exist<type_item>());
  68. if (! dao.isValid()) { return qx_bool(false); }
  69. #ifdef _QX_ENABLE_MONGODB
  70. if (dao.isMongoDB())
  71. {
  72. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  73. { if (! existItem((* it), dao)) { return qx_bool(false); } }
  74. return qx_bool(true);
  75. }
  76. #endif // _QX_ENABLE_MONGODB
  77. QString sql = dao.builder().buildSql().getSqlQuery();
  78. if (sql.isEmpty()) { dao.errEmpty(); return qx_bool(false); }
  79. if (! dao.prepare(sql)) { dao.errFailed(true); return qx_bool(false); }
  80. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  81. { if (! existItem((* it), dao)) { return qx_bool(false); } }
  82. return qx_bool(true);
  83. }
  84. private:
  85. template <typename U>
  86. static inline bool existItem(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  87. { return existItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::exist(item, dao); }
  88. template <typename U, bool bIsPointer /* = true */>
  89. struct existItem_Helper
  90. {
  91. static inline bool exist(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  92. { return (item ? qx::dao::detail::QxDao_Exist_Container<T>::existItem((* item), dao) : false); }
  93. };
  94. template <typename U1, typename U2>
  95. struct existItem_Helper<std::pair<U1, U2>, false>
  96. {
  97. static inline bool exist(std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  98. { return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao); }
  99. };
  100. template <typename U1, typename U2>
  101. struct existItem_Helper<const std::pair<U1, U2>, false>
  102. {
  103. static inline bool exist(const std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  104. { return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao); }
  105. };
  106. template <typename U1, typename U2>
  107. struct existItem_Helper<QPair<U1, U2>, false>
  108. {
  109. static inline bool exist(QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  110. { return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao); }
  111. };
  112. template <typename U1, typename U2>
  113. struct existItem_Helper<const QPair<U1, U2>, false>
  114. {
  115. static inline bool exist(const QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  116. { return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao); }
  117. };
  118. template <typename U>
  119. struct existItem_Helper<U, false>
  120. {
  121. static bool exist(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  122. {
  123. #ifdef _QX_ENABLE_MONGODB
  124. if (dao.isMongoDB())
  125. {
  126. long cnt = 0; QString json = qx::serialization::json::to_string(item, 1, "mongodb:only_id"); qx_query query(json);
  127. qx::dao::mongodb::QxMongoDB_Helper::count((& dao), dao.getDataMemberX()->getClass(), cnt, (& query)); if (! dao.isValid()) { return false; }
  128. return (cnt >= 1);
  129. }
  130. #endif // _QX_ENABLE_MONGODB
  131. {
  132. qx::dao::detail::IxDao_Timer timer((& dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
  133. qx::dao::detail::QxSqlQueryHelper_Exist<U>::resolveInput(item, dao.query(), dao.builder());
  134. }
  135. if (! dao.exec(true)) { dao.errFailed(); return false; }
  136. return dao.nextRecord();
  137. }
  138. };
  139. };
  140. template <class T>
  141. struct QxDao_Exist_Ptr
  142. {
  143. static inline qx_bool exist(T & t, QSqlDatabase * pDatabase)
  144. { return (t ? qx::dao::exist((* t), pDatabase) : qx_bool(false)); }
  145. };
  146. template <class T>
  147. struct QxDao_Exist
  148. {
  149. static inline qx_bool exist(T & t, QSqlDatabase * pDatabase)
  150. {
  151. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_Exist_Ptr<T>, qx::dao::detail::QxDao_Exist_Generic<T> >::type type_dao_1;
  152. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Exist_Ptr<T>, type_dao_1 >::type type_dao_2;
  153. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Exist_Container<T>, type_dao_2 >::type type_dao_3;
  154. return type_dao_3::exist(t, pDatabase);
  155. }
  156. };
  157. } // namespace detail
  158. } // namespace dao
  159. } // namespace qx