QxSoftDelete.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_SOFT_DELETE_H_
  32. #define _QX_SOFT_DELETE_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxSoftDelete.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database
  41. */
  42. #define QX_DAO_SOFT_DELETE_QDATETIME_FORMAT "yyyyMMddhhmmsszzz"
  43. namespace qx {
  44. /*!
  45. * \ingroup QxDao
  46. * \brief qx::QxSoftDelete : soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database
  47. *
  48. * A soft delete doesn't remove rows from database (this is not a physical delete) : a new column is added to the table definition to flag a row as deleted or not.
  49. * This column can contain a boolean (1 means row deleted, 0 or NULL means row not deleted), or can contain deletion date-time (if empty or NULL, row is not deleted).
  50. * So you can reactivate a deleted row by setting NULL or empty value into database.
  51. *
  52. * To define a soft delete behavior with QxOrm library, you have to use the class qx::QxSoftDelete in function mapping by class qx::register_class<T>.
  53. * Here is an example with the class <i>Bar</i> containing 2 properties <i>m_id</i> and <i>m_desc</i> :
  54. * \code
  55. namespace qx {
  56. template <> void register_class(QxClass<Bar> & t)
  57. {
  58. t.setSoftDelete(qx::QxSoftDelete("deleted_at"));
  59. t.id(& Bar::m_id, "id");
  60. t.data(& Bar::m_desc, "desc");
  61. }}
  62. * \endcode
  63. *
  64. * SQL queries builded by QxOrm library will take into account this soft delete parameter to add conditions (don't fetch deleted item, don't delete physically a row, etc.).
  65. * For example, if you execute this code with the class <i>Bar</i> :
  66. * \code
  67. Bar_ptr pBar; pBar.reset(new Bar());
  68. pBar->setId(5);
  69. QSqlError daoError = qx::dao::delete_by_id(pBar); qAssert(! daoError.isValid());
  70. qx_bool bDaoExist = qx::dao::exist(pBar); qAssert(! bDaoExist);
  71. daoError = qx::dao::delete_all<Bar>(); qAssert(! daoError.isValid());
  72. long lBarCount = qx::dao::count<Bar>(); qAssert(lBarCount == 0);
  73. daoError = qx::dao::destroy_all<Bar>(); qAssert(! daoError.isValid());
  74. * \endcode
  75. *
  76. * You will obtain following output trace :
  77. * \code
  78. [QxOrm] sql query (93 ms) : UPDATE Bar SET deleted_at = '20110617115148615' WHERE id = :id
  79. [QxOrm] sql query (0 ms) : SELECT Bar.id AS Bar_id_0, Bar.deleted_at FROM Bar WHERE Bar.id = :id AND (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
  80. [QxOrm] sql query (78 ms) : UPDATE Bar SET deleted_at = '20110617115148724'
  81. [QxOrm] sql query (0 ms) : SELECT COUNT(*) FROM Bar WHERE (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
  82. [QxOrm] sql query (110 ms) : DELETE FROM Bar
  83. * \endcode
  84. *
  85. * <i>Note :</i> To delete physically a row from database, you have to use followings functions : qx::dao::destroy_by_id() and qx::dao::destroy_all().
  86. *
  87. * <i>Other note :</i> it is recommended to define into database an index on column <i>deleted_at</i> to optimize execution of SQL queries.
  88. */
  89. class QX_DLL_EXPORT QxSoftDelete
  90. {
  91. public:
  92. enum mode { mode_flag, mode_date_time };
  93. private:
  94. QString m_sTable; //!< Table name where soft delete behavior is applied
  95. QString m_sColumn; //!< Column name to store soft delete information
  96. QString m_sSqlQueryToFetch; //!< Overrided user SQL query to fetch an item, if empty QxOrm library builds a default SQL query
  97. QString m_sSqlQueryToUpdate; //!< Overrided user SQL query to update an item, if empty QxOrm library builds a default SQL query
  98. QString m_sSqlQueryToCreateTable; //!< Overrided user SQL query to create table, if empty QxOrm library builds a default SQL query
  99. mode m_eMode; //!< Soft delete mode : 'mode_flag' with a boolean column, 'mode_date_time' with a date-time column containing deletion date-time
  100. bool m_bFetchInJoin; //!< Add SQL condition to fetch in the JOIN part (default value), for backward compatibility with previous versions of QxOrm library set this value to false (means fetch in the WHERE part)
  101. public:
  102. QxSoftDelete();
  103. QxSoftDelete(const QString & sColumn);
  104. QxSoftDelete(const QString & sColumn, mode eMode);
  105. ~QxSoftDelete();
  106. QString getTableName() const;
  107. QString getColumnName() const;
  108. QString getSqlQueryToFetch() const;
  109. QString getSqlQueryToUpdate() const;
  110. QString getSqlQueryToCreateTable() const;
  111. mode getMode() const;
  112. bool getSqlFetchInJoin() const;
  113. void setTableName(const QString & sTable);
  114. void setColumnName(const QString & sColumn);
  115. void setSqlQueryToFetch(const QString & s);
  116. void setSqlQueryToUpdate(const QString & s);
  117. void setSqlQueryToCreateTable(const QString & s);
  118. void setMode(mode eMode);
  119. void setSqlFetchInJoin(bool b);
  120. bool isEmpty() const;
  121. QString buildSqlTablePointName(const QString & sTable = QString()) const;
  122. QString buildSqlQueryToFetch(const QString & sTable = QString()) const;
  123. QString buildSqlQueryToUpdate() const;
  124. QString buildSqlQueryToCreateTable() const;
  125. };
  126. } // namespace qx
  127. #endif // _QX_SOFT_DELETE_H_