QxDao_Save_WithRelation.inl 7.0 KB

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