QxDaoPointer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef _QX_DAO_POINTER_H_
  32. #define _QX_DAO_POINTER_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDaoPointer.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief qx::dao::ptr<T> : provide a classic smart-pointer (like boost::shared_ptr<T> or QSharedPointer<T>) with some features associated with QxDao module of QxOrm library
  41. */
  42. #include <QtCore/qsharedpointer.h>
  43. #include <QtCore/qstringlist.h>
  44. #include <QtCore/qdatastream.h>
  45. #include <QxSerialize/QDataStream/QxSerializeQDataStream_QSharedPointer.h>
  46. namespace qx {
  47. template <class T> QSharedPointer<T> clone_to_qt_shared_ptr(const T & obj);
  48. } // namespace qx
  49. namespace qx {
  50. namespace dao {
  51. template <typename T> class ptr;
  52. } // namespace dao
  53. } // namespace qx
  54. template <typename T> QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t);
  55. template <typename T> QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t);
  56. namespace qx {
  57. namespace dao {
  58. namespace detail {
  59. template <class T> struct QxDao_IsDirty;
  60. } // namespace detail
  61. /*!
  62. * \ingroup QxDao
  63. * \brief qx::dao::ptr<T> : provide a classic smart-pointer (like boost::shared_ptr<T> or QSharedPointer<T>) with some features associated with QxDao module of QxOrm library
  64. *
  65. * QxOrm can be used with smart-pointers of boost and Qt libraries.
  66. * QxOrm smart-pointer is based on QSharedPointer and provides new features with qx::dao::xxx functions of QxDao module.
  67. * qx::dao::ptr<T> keeps automatically values from database.
  68. * So it's possible to detect if an instance has been modified using the method isDirty() : this method can return list of properties changed.
  69. * qx::dao::ptr<T> can also be used with the function qx::dao::update_optimized() to update in database only properties changed.
  70. * qx::dao::ptr<T> can be used with a simple object and with many containers : stl, boost, Qt and qx::QxCollection<Key, Value>.
  71. *
  72. * Quick sample using qx::dao::ptr<T> smart-pointer :
  73. * \code
  74. // Test 'isDirty()' method
  75. qx::dao::ptr<blog> blog_isdirty = qx::dao::ptr<blog>(new blog());
  76. blog_isdirty->m_id = blog_1->m_id;
  77. daoError = qx::dao::fetch_by_id(blog_isdirty);
  78. qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());
  79. blog_isdirty->m_text = "blog property 'text' modified => blog is dirty !!!";
  80. QStringList lstDiff; bool bDirty = blog_isdirty.isDirty(lstDiff);
  81. qAssert(bDirty && (lstDiff.count() == 1) && (lstDiff.at(0) == "blog_text"));
  82. if (bDirty) { qDebug("[QxOrm] test dirty 1 : blog is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
  83. // Update only property 'm_text' of 'blog_isdirty'
  84. daoError = qx::dao::update_optimized(blog_isdirty);
  85. qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());
  86. qx::dump(blog_isdirty);
  87. // Test 'isDirty()' method with a container
  88. typedef qx::dao::ptr< QList<author_ptr> > type_lst_author_test_is_dirty;
  89. type_lst_author_test_is_dirty container_isdirty = type_lst_author_test_is_dirty(new QList<author_ptr>());
  90. daoError = qx::dao::fetch_all(container_isdirty);
  91. qAssert(! daoError.isValid() && ! container_isdirty.isDirty() && (container_isdirty->count() == 3));
  92. author_ptr author_ptr_dirty = container_isdirty->at(1);
  93. author_ptr_dirty->m_name = "author name modified at index 1 => container is dirty !!!";
  94. bDirty = container_isdirty.isDirty(lstDiff);
  95. qAssert(bDirty && (lstDiff.count() == 1));
  96. if (bDirty) { qDebug("[QxOrm] test dirty 2 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
  97. author_ptr_dirty = container_isdirty->at(2);
  98. author_ptr_dirty->m_birthdate = QDate(1998, 03, 06);
  99. bDirty = container_isdirty.isDirty(lstDiff);
  100. qAssert(bDirty && (lstDiff.count() == 2));
  101. if (bDirty) { qDebug("[QxOrm] test dirty 3 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
  102. // Update only property 'm_name' at position 1, only property 'm_birthdate' at position 2 and nothing at position 0
  103. daoError = qx::dao::update_optimized(container_isdirty);
  104. qAssert(! daoError.isValid() && ! container_isdirty.isDirty());
  105. qx::dump(container_isdirty);
  106. // Fetch only property 'm_dt_creation' of blog
  107. QStringList lstColumns = QStringList() << "date_creation";
  108. list_blog lst_blog_with_only_date_creation;
  109. daoError = qx::dao::fetch_all(lst_blog_with_only_date_creation, NULL, lstColumns);
  110. qAssert(! daoError.isValid() && (lst_blog_with_only_date_creation.size() > 0));
  111. if ((lst_blog_with_only_date_creation.size() > 0) && (lst_blog_with_only_date_creation[0] != NULL))
  112. { qAssert(lst_blog_with_only_date_creation[0]->m_text.isEmpty()); }
  113. qx::dump(lst_blog_with_only_date_creation);
  114. * \endcode
  115. */
  116. template <typename T>
  117. class ptr
  118. {
  119. template <class U> friend QDataStream & ::operator<< (QDataStream & stream, const qx::dao::ptr<U> & t);
  120. template <class U> friend QDataStream & ::operator>> (QDataStream & stream, qx::dao::ptr<U> & t);
  121. private:
  122. QSharedPointer<T> m_pWork; //!< Default pointer => user works with this pointer
  123. QSharedPointer<T> m_pOriginal; //!< Keep original pointer containing all values from database
  124. public:
  125. ptr() { ; }
  126. explicit ptr(T * ptr) : m_pWork(ptr) { ; }
  127. explicit ptr(T * ptr, T * original) : m_pWork(ptr), m_pOriginal(original) { ; }
  128. ptr(const qx::dao::ptr<T> & other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal) { ; }
  129. ptr(const QSharedPointer<T> & other) : m_pWork(other) { ; }
  130. ptr(const QSharedPointer<T> & other, const QSharedPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; }
  131. ptr(const QWeakPointer<T> & other) : m_pWork(other) { ; }
  132. ptr(const QWeakPointer<T> & other, const QWeakPointer<T> & original) : m_pWork(other), m_pOriginal(original) { ; }
  133. virtual ~ptr() { ; }
  134. template <typename Deleter> ptr(T * ptr, Deleter deleter) : m_pWork(ptr, deleter) { ; }
  135. template <typename Deleter> ptr(T * ptr, T * original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original) { ; }
  136. template <class X> ptr(const qx::dao::ptr<X> & other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal)) { ; }
  137. template <class X> ptr(const QSharedPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other)) { ; }
  138. template <class X> ptr(const QSharedPointer<X> & other, const QSharedPointer<T> & original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original)) { ; }
  139. template <class X> ptr(const QWeakPointer<X> & other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())) { ; }
  140. template <class X> ptr(const QWeakPointer<X> & other, const QWeakPointer<X> & original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef())) { ; }
  141. qx::dao::ptr<T> & operator=(const qx::dao::ptr<T> & other) { m_pWork = other.m_pWork; m_pOriginal = other.m_pOriginal; return (* this); }
  142. qx::dao::ptr<T> & operator=(const QSharedPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); }
  143. qx::dao::ptr<T> & operator=(const QWeakPointer<T> & other) { m_pWork = other; m_pOriginal.clear(); return (* this); }
  144. template <class X> qx::dao::ptr<T> & operator=(const qx::dao::ptr<X> & other) { m_pWork = qSharedPointerCast<T>(other.m_pWork); m_pOriginal = qSharedPointerCast<T>(other.m_pOriginal); return (* this); }
  145. template <class X> qx::dao::ptr<T> & operator=(const QSharedPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other); m_pOriginal.clear(); return (* this); }
  146. template <class X> qx::dao::ptr<T> & operator=(const QWeakPointer<X> & other) { m_pWork = qSharedPointerCast<T>(other.toStrongRef()); m_pOriginal.clear(); return (* this); }
  147. inline T * get() const { return m_pWork.data(); }
  148. inline T * getOriginal() const { return m_pOriginal.data(); }
  149. inline T * data() const { return m_pWork.data(); }
  150. inline T * dataOriginal() const { return m_pOriginal.data(); }
  151. inline bool isNull() const { return m_pWork.isNull(); }
  152. inline operator bool() const { return (! m_pWork.isNull()); }
  153. inline bool operator!() const { return m_pWork.isNull(); }
  154. inline T & operator*() const { return (* m_pWork.data()); }
  155. inline T * operator->() const { return m_pWork.data(); }
  156. inline void clear() { m_pWork.clear(); m_pOriginal.clear(); }
  157. inline void reset() { m_pWork.clear(); m_pOriginal.clear(); }
  158. inline void reset(const QSharedPointer<T> & ptr) { m_pWork = ptr; m_pOriginal.clear(); }
  159. inline void resetOriginal(const QSharedPointer<T> & ptr) { m_pOriginal = ptr; }
  160. inline bool isDirty() const { QStringList lstDiff; return isDirty(lstDiff); }
  161. inline QSharedPointer<T> toQtSharedPointer() const { return m_pWork; }
  162. inline void saveToOriginal() { m_pOriginal.clear(); if (m_pWork) { m_pOriginal = qx::clone_to_qt_shared_ptr(* m_pWork); } }
  163. inline void restoreFromOriginal() { m_pWork.clear(); if (m_pOriginal) { m_pWork = qx::clone_to_qt_shared_ptr(* m_pOriginal); } }
  164. template <class X> qx::dao::ptr<X> staticCast() const { return qx::dao::ptr<X>(m_pWork.template staticCast<X>(), m_pOriginal.template staticCast<X>()); }
  165. template <class X> qx::dao::ptr<X> dynamicCast() const { return qx::dao::ptr<X>(m_pWork.template dynamicCast<X>(), m_pOriginal.template dynamicCast<X>()); }
  166. template <class X> qx::dao::ptr<X> constCast() const { return qx::dao::ptr<X>(m_pWork.template constCast<X>(), m_pOriginal.template constCast<X>()); }
  167. bool isDirty(QStringList & lstDiff) const
  168. {
  169. lstDiff.clear();
  170. if (m_pWork.isNull() || m_pOriginal.isNull()) { lstDiff.append("*"); return true; }
  171. if (m_pWork == m_pOriginal) { return false; }
  172. qx::dao::detail::QxDao_IsDirty<T>::compare((* m_pWork), (* m_pOriginal), lstDiff);
  173. return (! lstDiff.isEmpty());
  174. }
  175. };
  176. } // namespace dao
  177. } // namespace qx
  178. template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); }
  179. template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); }
  180. template<class T, class X> bool operator==(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() == ptr2); }
  181. template<class T, class X> bool operator!=(const qx::dao::ptr<T> & ptr1, const X * ptr2) { return (ptr1.toQtSharedPointer() != ptr2); }
  182. template<class T, class X> bool operator==(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 == ptr2.toQtSharedPointer()); }
  183. template<class T, class X> bool operator!=(const T * ptr1, const qx::dao::ptr<X> & ptr2) { return (ptr1 != ptr2.toQtSharedPointer()); }
  184. template <typename T>
  185. QDataStream & operator<< (QDataStream & stream, const qx::dao::ptr<T> & t)
  186. {
  187. stream << t.m_pWork;
  188. stream << t.m_pOriginal;
  189. return stream;
  190. }
  191. template <typename T>
  192. QDataStream & operator>> (QDataStream & stream, qx::dao::ptr<T> & t)
  193. {
  194. stream >> t.m_pWork;
  195. stream >> t.m_pOriginal;
  196. return stream;
  197. }
  198. #endif // _QX_DAO_POINTER_H_