123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- /****************************************************************************
- **
- ** https://www.qxorm.com/
- ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
- **
- ** This file is part of the QxOrm library
- **
- ** This software is provided 'as-is', without any express or implied
- ** warranty. In no event will the authors be held liable for any
- ** damages arising from the use of this software
- **
- ** Commercial Usage
- ** Licensees holding valid commercial QxOrm licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and Lionel Marty
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3.0 as published by the Free Software
- ** Foundation and appearing in the file 'license.gpl3.txt' included in the
- ** packaging of this file. Please review the following information to
- ** ensure the GNU General Public License version 3.0 requirements will be
- ** met : http://www.gnu.org/copyleft/gpl.html
- **
- ** If you are unsure which license is appropriate for your use, or
- ** if you have questions regarding the use of this file, please contact :
- ** contact@qxorm.com
- **
- ****************************************************************************/
- #ifndef _QX_SQL_QUERY_BUILDER_H_
- #define _QX_SQL_QUERY_BUILDER_H_
- #ifdef _MSC_VER
- #pragma once
- #endif
- /*!
- * \file QxSqlQueryBuilder.h
- * \author Lionel Marty
- * \ingroup QxDao
- * \brief Concrete SQL query builder by class with a cache mechanism to backup and restore queries already built by the program
- */
- #include <QxDao/IxSqlQueryBuilder.h>
- #include <QxDao/QxSqlQueryHelper.h>
- #include <QxRegister/QxClass.h>
- #include <QxTraits/remove_attr.h>
- #include <QxTraits/remove_smart_ptr.h>
- #include <QxTraits/is_qx_registered.h>
- #define QX_SQL_ERR_NO_DATA_MEMBER_REGISTERED "'QxSqlQueryBuilder<T>' error : 'qx::register_class()' not called or no data member registered"
- #define QX_SQL_ERR_NO_ID_REGISTERED "'QxSqlQueryBuilder<T>' error : no id registered"
- #define QX_SQL_BUILDER_INIT_FCT(oper) \
- qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
- QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
- QString ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
- QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + ignoreSoftDeleteHash + oper; \
- if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { return (* this); } \
- QString sql; Q_UNUSED(sql);
- #define QX_SQL_BUILDER_INIT_FCT_WITH_RELATION(oper) \
- qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
- QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
- QString ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
- QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + this->getHashRelation() + ignoreSoftDeleteHash + oper; \
- if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { this->findSqlAlias(key); return (* this); } \
- QString sql; Q_UNUSED(sql);
- namespace qx {
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder<T> : concrete SQL query builder for class T with a cache mechanism to backup and restore queries already built by the program
- */
- template <class T>
- class QxSqlQueryBuilder : public IxSqlQueryBuilder
- {
- private:
- typedef typename qx::trait::remove_attr<T>::type type_sql_tmp_1;
- typedef typename qx::trait::remove_smart_ptr<type_sql_tmp_1>::type type_sql_tmp_2;
- public:
- typedef typename qx::QxSqlQueryBuilder<T>::type_sql_tmp_2 type_sql;
- public:
- QxSqlQueryBuilder() : IxSqlQueryBuilder() { ; }
- virtual ~QxSqlQueryBuilder() { static_assert(qx::trait::is_qx_registered<type_sql>::value, "qx::trait::is_qx_registered<type_sql>::value"); }
- virtual void init()
- {
- if (isInitDone()) { return; }
- setDataMemberX(QxClass<type_sql>::getSingleton()->dataMemberX());
- setSoftDelete(QxClass<type_sql>::getSingleton()->getSoftDelete());
- IxSqlQueryBuilder::init();
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_Count<T> : concrete SQL query builder for class T to build a COUNT SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_Count : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_Count() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_Count() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("Count")
- sql = "SELECT COUNT(*) FROM " + qx::IxDataMember::getSqlFromTable(this->table());
- if (! this->softDelete().isEmpty()) { sql += " WHERE " + this->softDelete().buildSqlQueryToFetch(); }
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_Exist<T> : concrete SQL query builder for class T to build an EXIST SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_Exist : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_Exist() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_Exist() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("Exist")
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- qx::dao::detail::QxSqlQueryHelper_Exist<type_sql>::sql(sql, (* this));
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_FetchAll<T> : concrete SQL query builder for class T to build a FETCH ALL SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_FetchAll : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_FetchAll() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_FetchAll() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(pRelationX);
- if ((columns.count() <= 0) || (columns.at(0) == "*"))
- {
- QX_SQL_BUILDER_INIT_FCT("FetchAll")
- qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this));
- this->setSqlQuery(sql, key);
- }
- else
- {
- QString sql;
- if (! this->verifyColumns(columns)) { return (* this); }
- qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this), columns);
- this->setSqlQuery(sql);
- }
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_FetchById<T> : concrete SQL query builder for class T to build a FETCH BY ID SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_FetchById : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_FetchById() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_FetchById() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(pRelationX);
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- QxSqlQueryBuilder_FetchAll<type_sql> builder; builder.clone(* this);
- if ((columns.count() <= 0) || (columns.at(0) == "*"))
- {
- QX_SQL_BUILDER_INIT_FCT("FetchById")
- qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder);
- this->setSqlQuery(sql, key);
- }
- else
- {
- QString sql;
- if (! this->verifyColumns(columns)) { return (* this); }
- qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder, columns);
- this->setSqlQuery(sql);
- }
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_Insert<T> : concrete SQL query builder for class T to build an INSERT SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_Insert : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_Insert() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_Insert() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("Insert")
- qx::dao::detail::QxSqlQueryHelper_Insert<type_sql>::sql(sql, (* this));
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_Update<T> : concrete SQL query builder for class T to build an UPDATE SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_Update : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_Update() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_Update() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(pRelationX);
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- if ((columns.count() <= 0) || (columns.at(0) == "*"))
- {
- QX_SQL_BUILDER_INIT_FCT("Update")
- qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this));
- this->setSqlQuery(sql, key);
- }
- else
- {
- QString sql;
- if (! this->verifyColumns(columns)) { return (* this); }
- qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this), columns);
- this->setSqlQuery(sql);
- }
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_DeleteAll<T> : concrete SQL query builder for class T to build a DELETE ALL SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_DeleteAll : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_DeleteAll() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_DeleteAll() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("DeleteAll")
- sql = "DELETE FROM " + this->table();
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_SoftDeleteAll<T> : concrete SQL query builder for class T to build a SOFT DELETE ALL SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_SoftDeleteAll : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_SoftDeleteAll() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_SoftDeleteAll() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("SoftDeleteAll")
- if (! this->softDelete().isEmpty()) { sql = "UPDATE " + this->table() + " SET " + this->softDelete().buildSqlQueryToUpdate(); }
- else { qAssert(false); }
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_DeleteById<T> : concrete SQL query builder for class T to build a DELETE BY ID SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_DeleteById : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_DeleteById() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_DeleteById() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("DeleteById")
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), false);
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_SoftDeleteById<T> : concrete SQL query builder for class T to build a SOFT DELETE BY ID SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_SoftDeleteById : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_SoftDeleteById() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_SoftDeleteById() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("SoftDeleteById")
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- if (this->softDelete().isEmpty()) { qAssert(false); return (* this); }
- qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), true);
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_CreateTable<T> : concrete SQL query builder for class T to build a CREATE TABLE SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_CreateTable : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_CreateTable() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_CreateTable() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns); Q_UNUSED(pRelationX);
- QX_SQL_BUILDER_INIT_FCT("CreateTable")
- qx::dao::detail::QxSqlQueryHelper_CreateTable<type_sql>::sql(sql, (* this));
- this->setSqlQuery(sql, key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_Count_WithRelation<T> : concrete SQL query builder for class T to build a COUNT WITH RELATION SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_Count_WithRelation : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_Count_WithRelation() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_Count_WithRelation() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns);
- QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("Count_WithRelation")
- IxSqlQueryBuilder::sql_Count_WithRelation(pRelationX, sql, (* this));
- this->setSqlQuery(sql, key);
- this->insertSqlAlias(key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_FetchAll_WithRelation<T> : concrete SQL query builder for class T to build a FETCH ALL WITH RELATION SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_FetchAll_WithRelation : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_FetchAll_WithRelation() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_FetchAll_WithRelation() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns);
- QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchAll_WithRelation")
- qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_sql>::sql(pRelationX, sql, (* this));
- this->setSqlQuery(sql, key);
- this->insertSqlAlias(key);
- return (* this);
- }
- };
- /*!
- * \ingroup QxDao
- * \brief qx::QxSqlQueryBuilder_FetchById_WithRelation<T> : concrete SQL query builder for class T to build a FETCH BY ID WITH RELATION SQL query
- */
- template <class T>
- class QxSqlQueryBuilder_FetchById_WithRelation : public QxSqlQueryBuilder<T>
- {
- public:
- typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
- QxSqlQueryBuilder_FetchById_WithRelation() : QxSqlQueryBuilder<T>() { ; }
- virtual ~QxSqlQueryBuilder_FetchById_WithRelation() { ; }
- virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
- {
- Q_UNUSED(columns);
- QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchById_WithRelation")
- if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
- QxSqlQueryBuilder_FetchAll_WithRelation<type_sql> builder; builder.clone(* this);
- qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<type_sql>::sql(pRelationX, sql, builder);
- this->setSqlQuery(sql, key);
- this->insertSqlAlias(key);
- return (* this);
- }
- };
- } // namespace qx
- #endif // _QX_SQL_QUERY_BUILDER_H_
|