![]() |
QxOrm
1.4.4
C++ Object Relational Mapping library
|
qx::QxSoftDelete : soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database More...
#include <QxSoftDelete.h>
Public Types | |
enum | mode { mode_flag, mode_date_time } |
Public Member Functions | |
QxSoftDelete () | |
QxSoftDelete (const QString &sColumn) | |
QxSoftDelete (const QString &sColumn, mode eMode) | |
~QxSoftDelete () | |
QString | getTableName () const |
QString | getColumnName () const |
QString | getSqlQueryToFetch () const |
QString | getSqlQueryToUpdate () const |
QString | getSqlQueryToCreateTable () const |
mode | getMode () const |
void | setTableName (const QString &sTable) |
void | setColumnName (const QString &sColumn) |
void | setSqlQueryToFetch (const QString &s) |
void | setSqlQueryToUpdate (const QString &s) |
void | setSqlQueryToCreateTable (const QString &s) |
void | setMode (mode eMode) |
bool | isEmpty () const |
QString | buildSqlTablePointName (const QString &sTable=QString()) const |
QString | buildSqlQueryToFetch (const QString &sTable=QString()) const |
QString | buildSqlQueryToUpdate () const |
QString | buildSqlQueryToCreateTable () const |
Private Attributes | |
QString | m_sTable |
Table name where soft delete behavior is applied. | |
QString | m_sColumn |
Column name to store soft delete information. | |
QString | m_sSqlQueryToFetch |
Overrided user SQL query to fetch an item, if empty QxOrm library builds a default SQL query. | |
QString | m_sSqlQueryToUpdate |
Overrided user SQL query to update an item, if empty QxOrm library builds a default SQL query. | |
QString | m_sSqlQueryToCreateTable |
Overrided user SQL query to create table, if empty QxOrm library builds a default SQL query. | |
mode | m_eMode |
Soft delete mode : 'mode_flag' with a boolean column, 'mode_date_time' with a date-time column containing deletion date-time. |
qx::QxSoftDelete : soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database
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. 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). So you can reactivate a deleted row by setting NULL or empty value into database.
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>. Here is an example with the class Bar containing 2 properties m_id and m_desc :
namespace qx { template <> void register_class(QxClass<Bar> & t) { t.setSoftDelete(qx::QxSoftDelete("deleted_at")); t.id(& Bar::m_id, "id"); t.data(& Bar::m_desc, "desc"); }}
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.). For example, if you execute this code with the class Bar :
Bar_ptr pBar; pBar.reset(new Bar()); pBar->setId(5); QSqlError daoError = qx::dao::delete_by_id(pBar); qAssert(! daoError.isValid()); qx_bool bDaoExist = qx::dao::exist(pBar); qAssert(! bDaoExist); daoError = qx::dao::delete_all<Bar>(); qAssert(! daoError.isValid()); long lBarCount = qx::dao::count<Bar>(); qAssert(lBarCount == 0); daoError = qx::dao::destroy_all<Bar>(); qAssert(! daoError.isValid());
You will obtain following output trace :
[QxOrm] sql query (93 ms) : UPDATE Bar SET deleted_at = '20110617115148615' WHERE id = :id [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 = '') [QxOrm] sql query (78 ms) : UPDATE Bar SET deleted_at = '20110617115148724' [QxOrm] sql query (0 ms) : SELECT COUNT(*) FROM Bar WHERE (Bar.deleted_at IS NULL OR Bar.deleted_at = '') [QxOrm] sql query (110 ms) : DELETE FROM Bar
Note : To delete physically a row from database, you have to use followings functions : qx::dao::destroy_by_id() and qx::dao::destroy_all().
Other note : it is recommended to define into database an index on column deleted_at to optimize execution of SQL queries.
Definition at line 96 of file QxSoftDelete.h.
Definition at line 101 of file QxSoftDelete.h.
qx::QxSoftDelete::QxSoftDelete | ( | ) |
qx::QxSoftDelete::QxSoftDelete | ( | const QString & | sColumn | ) |
qx::QxSoftDelete::QxSoftDelete | ( | const QString & | sColumn, |
mode | eMode | ||
) |
qx::QxSoftDelete::~QxSoftDelete | ( | ) |
QString qx::QxSoftDelete::buildSqlQueryToCreateTable | ( | ) | const |
QString qx::QxSoftDelete::buildSqlQueryToFetch | ( | const QString & | sTable = QString() | ) | const |
QString qx::QxSoftDelete::buildSqlQueryToUpdate | ( | ) | const |
QString qx::QxSoftDelete::buildSqlTablePointName | ( | const QString & | sTable = QString() | ) | const |
QString qx::QxSoftDelete::getColumnName | ( | ) | const |
mode qx::QxSoftDelete::getMode | ( | ) | const |
QString qx::QxSoftDelete::getSqlQueryToCreateTable | ( | ) | const |
QString qx::QxSoftDelete::getSqlQueryToFetch | ( | ) | const |
QString qx::QxSoftDelete::getSqlQueryToUpdate | ( | ) | const |
QString qx::QxSoftDelete::getTableName | ( | ) | const |
bool qx::QxSoftDelete::isEmpty | ( | ) | const |
void qx::QxSoftDelete::setColumnName | ( | const QString & | sColumn | ) |
void qx::QxSoftDelete::setMode | ( | mode | eMode | ) |
void qx::QxSoftDelete::setSqlQueryToCreateTable | ( | const QString & | s | ) |
void qx::QxSoftDelete::setSqlQueryToFetch | ( | const QString & | s | ) |
void qx::QxSoftDelete::setSqlQueryToUpdate | ( | const QString & | s | ) |
void qx::QxSoftDelete::setTableName | ( | const QString & | sTable | ) |
mode qx::QxSoftDelete::m_eMode [private] |
Soft delete mode : 'mode_flag' with a boolean column, 'mode_date_time' with a date-time column containing deletion date-time.
Definition at line 110 of file QxSoftDelete.h.
QString qx::QxSoftDelete::m_sColumn [private] |
Column name to store soft delete information.
Definition at line 106 of file QxSoftDelete.h.
QString qx::QxSoftDelete::m_sSqlQueryToCreateTable [private] |
Overrided user SQL query to create table, if empty QxOrm library builds a default SQL query.
Definition at line 109 of file QxSoftDelete.h.
QString qx::QxSoftDelete::m_sSqlQueryToFetch [private] |
Overrided user SQL query to fetch an item, if empty QxOrm library builds a default SQL query.
Definition at line 107 of file QxSoftDelete.h.
QString qx::QxSoftDelete::m_sSqlQueryToUpdate [private] |
Overrided user SQL query to update an item, if empty QxOrm library builds a default SQL query.
Definition at line 108 of file QxSoftDelete.h.
QString qx::QxSoftDelete::m_sTable [private] |
Table name where soft delete behavior is applied.
Definition at line 105 of file QxSoftDelete.h.