QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches

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
 
bool getSqlFetchInJoin () 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)
 
void setSqlFetchInJoin (bool b)
 
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.
 
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)
 

Detailed Description

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");
}}
void setSoftDelete(const qx::QxSoftDelete &o)
qx::QxClass<T> : concrete class of type T registered into QxOrm context (this class is a singleton an...
Definition QxClass.h:79
IxDataMember * id(type_primary_key T::*pDataMemberId, const QString &sKey, long lVersion=0)
IxDataMember * data(const QString &sKey, long lVersion)
qx::QxSoftDelete : soft delete (or logical delete) behavior to update a row into database (flag it as...
void register_class(T &t)
qx::register_class<T>(T & t) : specialize this template to register a class of type T into QxOrm cont...
Definition QxClass.h:71
Root namespace for all QxOrm library features.

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());
#define qAssert(x)
Definition QxMacro.h:52
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
QSqlError destroy_all(QSqlDatabase *pDatabase=NULL)
Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context),...
Definition QxDao.h:220
QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered i...
Definition QxDao.h:176
qx_bool exist(T &t, QSqlDatabase *pDatabase=NULL)
Search if an element (or list of elements) already exists into database.
Definition QxDao.h:278
QSqlError delete_all(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
Definition QxDao.h:207
long count(const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL)
Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm c...
Definition QxDao.h:98

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.

Member Enumeration Documentation

◆ mode

Enumerator
mode_flag 
mode_date_time 

Definition at line 101 of file QxSoftDelete.h.

Constructor & Destructor Documentation

◆ QxSoftDelete() [1/3]

qx::QxSoftDelete::QxSoftDelete ( )

◆ QxSoftDelete() [2/3]

qx::QxSoftDelete::QxSoftDelete ( const QString & sColumn)

◆ QxSoftDelete() [3/3]

qx::QxSoftDelete::QxSoftDelete ( const QString & sColumn,
mode eMode )

◆ ~QxSoftDelete()

qx::QxSoftDelete::~QxSoftDelete ( )

Member Function Documentation

◆ buildSqlQueryToCreateTable()

QString qx::QxSoftDelete::buildSqlQueryToCreateTable ( ) const

◆ buildSqlQueryToFetch()

QString qx::QxSoftDelete::buildSqlQueryToFetch ( const QString & sTable = QString()) const

◆ buildSqlQueryToUpdate()

QString qx::QxSoftDelete::buildSqlQueryToUpdate ( ) const

◆ buildSqlTablePointName()

QString qx::QxSoftDelete::buildSqlTablePointName ( const QString & sTable = QString()) const

◆ getColumnName()

QString qx::QxSoftDelete::getColumnName ( ) const

◆ getMode()

mode qx::QxSoftDelete::getMode ( ) const

◆ getSqlFetchInJoin()

bool qx::QxSoftDelete::getSqlFetchInJoin ( ) const

◆ getSqlQueryToCreateTable()

QString qx::QxSoftDelete::getSqlQueryToCreateTable ( ) const

◆ getSqlQueryToFetch()

QString qx::QxSoftDelete::getSqlQueryToFetch ( ) const

◆ getSqlQueryToUpdate()

QString qx::QxSoftDelete::getSqlQueryToUpdate ( ) const

◆ getTableName()

QString qx::QxSoftDelete::getTableName ( ) const

◆ isEmpty()

bool qx::QxSoftDelete::isEmpty ( ) const

◆ setColumnName()

void qx::QxSoftDelete::setColumnName ( const QString & sColumn)

◆ setMode()

void qx::QxSoftDelete::setMode ( mode eMode)

◆ setSqlFetchInJoin()

void qx::QxSoftDelete::setSqlFetchInJoin ( bool b)

◆ setSqlQueryToCreateTable()

void qx::QxSoftDelete::setSqlQueryToCreateTable ( const QString & s)

◆ setSqlQueryToFetch()

void qx::QxSoftDelete::setSqlQueryToFetch ( const QString & s)

◆ setSqlQueryToUpdate()

void qx::QxSoftDelete::setSqlQueryToUpdate ( const QString & s)

◆ setTableName()

void qx::QxSoftDelete::setTableName ( const QString & sTable)

Member Data Documentation

◆ m_bFetchInJoin

bool qx::QxSoftDelete::m_bFetchInJoin
private

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)

Definition at line 111 of file QxSoftDelete.h.

◆ m_eMode

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.

◆ m_sColumn

QString qx::QxSoftDelete::m_sColumn
private

Column name to store soft delete information.

Definition at line 106 of file QxSoftDelete.h.

◆ m_sSqlQueryToCreateTable

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.

◆ m_sSqlQueryToFetch

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.

◆ m_sSqlQueryToUpdate

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.

◆ m_sTable

QString qx::QxSoftDelete::m_sTable
private

Table name where soft delete behavior is applied.

Definition at line 105 of file QxSoftDelete.h.


The documentation for this class was generated from the following file: