QxDao_Insert.inl 9.8 KB

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