QxDao_Insert_WithRelation.inl 7.6 KB

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