QxDao_Save.inl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_Save_Generic
  36. {
  37. static QSqlError save(T & t, QSqlDatabase * pDatabase)
  38. {
  39. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "save", new qx::QxSqlQueryBuilder_Update<T>());
  40. if (! dao.isValid()) { return dao.error(); }
  41. if (! pDatabase) { dao.transaction(); }
  42. dao.quiet();
  43. qx_bool bExist = dao.isValidPrimaryKey(t);
  44. if (bExist) { bExist = qx::dao::exist(t, (& dao.database())); }
  45. if (bExist) { dao.updateError(qx::dao::update(t, (& dao.database()))); }
  46. else { dao.updateError(qx::dao::insert(t, (& dao.database()))); }
  47. return dao.error();
  48. }
  49. };
  50. template <class T>
  51. struct QxDao_Save_Container
  52. {
  53. static QSqlError save(T & t, QSqlDatabase * pDatabase)
  54. {
  55. typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
  56. if (qx::trait::generic_container<T>::size(t) <= 0) { return QSqlError(); }
  57. qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "save", new qx::QxSqlQueryBuilder_Update<type_item>());
  58. if (! dao.isValid()) { return dao.error(); }
  59. if (! pDatabase) { dao.transaction(); }
  60. dao.quiet();
  61. for (typename T::iterator it = t.begin(); it != t.end(); ++it)
  62. { if (! saveItem((* it), dao)) { return dao.error(); } }
  63. return dao.error();
  64. }
  65. private:
  66. template <typename U>
  67. static inline bool saveItem(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  68. {
  69. bool bSaveOk = saveItem_Helper<U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::save(item, dao);
  70. if (bSaveOk) { qx::dao::detail::QxDao_Keep_Original<U>::backup(item); }
  71. return bSaveOk;
  72. }
  73. template <typename U, bool bIsPointer /* = true */>
  74. struct saveItem_Helper
  75. {
  76. static inline bool save(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  77. { return (item ? qx::dao::detail::QxDao_Save_Container<T>::saveItem((* item), dao) : false); }
  78. };
  79. template <typename U1, typename U2>
  80. struct saveItem_Helper<std::pair<U1, U2>, false>
  81. {
  82. static inline bool save(std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  83. { return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao); }
  84. };
  85. template <typename U1, typename U2>
  86. struct saveItem_Helper<const std::pair<U1, U2>, false>
  87. {
  88. static inline bool save(const std::pair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  89. { return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao); }
  90. };
  91. template <typename U1, typename U2>
  92. struct saveItem_Helper<QPair<U1, U2>, false>
  93. {
  94. static inline bool save(QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  95. { return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao); }
  96. };
  97. template <typename U1, typename U2>
  98. struct saveItem_Helper<const QPair<U1, U2>, false>
  99. {
  100. static inline bool save(const QPair<U1, U2> & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  101. { return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao); }
  102. };
  103. template <typename U>
  104. struct saveItem_Helper<U, false>
  105. {
  106. static bool save(U & item, qx::dao::detail::QxDao_Helper_Container<T> & dao)
  107. {
  108. qx_bool bExist = dao.isValidPrimaryKey(item);
  109. if (bExist) { bExist = qx::dao::exist(item, (& dao.database())); }
  110. if (bExist) { dao.updateError(qx::dao::update(item, (& dao.database()))); }
  111. else { dao.updateError(qx::dao::insert(item, (& dao.database()))); }
  112. return dao.isValid();
  113. }
  114. };
  115. };
  116. template <class T>
  117. struct QxDao_Save_Ptr
  118. {
  119. static inline QSqlError save(T & t, QSqlDatabase * pDatabase)
  120. { return (t ? qx::dao::save((* t), pDatabase) : QSqlError()); }
  121. };
  122. template <class T>
  123. struct QxDao_Save
  124. {
  125. static inline QSqlError save(T & t, QSqlDatabase * pDatabase)
  126. {
  127. typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_Save_Ptr<T>, qx::dao::detail::QxDao_Save_Generic<T> >::type type_dao_1;
  128. typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Save_Ptr<T>, type_dao_1 >::type type_dao_2;
  129. typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Save_Container<T>, type_dao_2 >::type type_dao_3;
  130. QSqlError error = type_dao_3::save(t, pDatabase);
  131. if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original<T>::backup(t); }
  132. return error;
  133. }
  134. };
  135. } // namespace detail
  136. } // namespace dao
  137. } // namespace qx