QxOrm  1.4.7
C++ Object Relational Mapping library
QxSqlQueryBuilder.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_SQL_QUERY_BUILDER_H_
00033 #define _QX_SQL_QUERY_BUILDER_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDao/IxSqlQueryBuilder.h>
00047 #include <QxDao/QxSqlQueryHelper.h>
00048 
00049 #include <QxRegister/QxClass.h>
00050 
00051 #include <QxTraits/remove_attr.h>
00052 #include <QxTraits/remove_smart_ptr.h>
00053 #include <QxTraits/is_qx_registered.h>
00054 
00055 #define QX_SQL_ERR_NO_DATA_MEMBER_REGISTERED    "'QxSqlQueryBuilder<T>' error : 'qx::register_class()' not called or no data member registered"
00056 #define QX_SQL_ERR_NO_ID_REGISTERED             "'QxSqlQueryBuilder<T>' error : no id registered"
00057 
00058 #define QX_SQL_BUILDER_INIT_FCT(oper) \
00059 qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
00060 QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
00061 QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + oper; \
00062 if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { return (* this); } \
00063 QString sql; Q_UNUSED(sql);
00064 
00065 #define QX_SQL_BUILDER_INIT_FCT_WITH_RELATION(oper) \
00066 qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
00067 QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
00068 QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + this->getHashRelation() + oper; \
00069 if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { this->findSqlAlias(key); return (* this); } \
00070 QString sql; Q_UNUSED(sql);
00071 
00072 namespace qx {
00073 
00078 template <class T>
00079 class QxSqlQueryBuilder : public IxSqlQueryBuilder
00080 {
00081 
00082 private:
00083 
00084    typedef typename qx::trait::remove_attr<T>::type type_sql_tmp_1;
00085    typedef typename qx::trait::remove_smart_ptr<type_sql_tmp_1>::type type_sql_tmp_2;
00086 
00087 public:
00088 
00089    typedef typename qx::QxSqlQueryBuilder<T>::type_sql_tmp_2 type_sql;
00090 
00091 public:
00092 
00093    QxSqlQueryBuilder() : IxSqlQueryBuilder() { ; }
00094    virtual ~QxSqlQueryBuilder() { static_assert(qx::trait::is_qx_registered<type_sql>::value, "qx::trait::is_qx_registered<type_sql>::value"); }
00095 
00096    virtual void init()
00097    {
00098       if (isInitDone()) { return; }
00099       setDataMemberX(QxClass<type_sql>::getSingleton()->dataMemberX());
00100       setSoftDelete(QxClass<type_sql>::getSingleton()->getSoftDelete());
00101       IxSqlQueryBuilder::init();
00102    }
00103 
00104 };
00105 
00110 template <class T>
00111 class QxSqlQueryBuilder_Count : public QxSqlQueryBuilder<T>
00112 {
00113 
00114 public:
00115 
00116    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00117 
00118    QxSqlQueryBuilder_Count() : QxSqlQueryBuilder<T>() { ; }
00119    virtual ~QxSqlQueryBuilder_Count() { ; }
00120 
00121    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00122    {
00123       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00124       QX_SQL_BUILDER_INIT_FCT("Count")
00125       sql = "SELECT COUNT(*) FROM " + qx::IxDataMember::getSqlFromTable(this->table());
00126       if (! this->softDelete().isEmpty()) { sql += " WHERE " + this->softDelete().buildSqlQueryToFetch(); }
00127       this->setSqlQuery(sql, key);
00128       return (* this);
00129    }
00130 
00131 };
00132 
00137 template <class T>
00138 class QxSqlQueryBuilder_Exist : public QxSqlQueryBuilder<T>
00139 {
00140 
00141 public:
00142 
00143    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00144 
00145    QxSqlQueryBuilder_Exist() : QxSqlQueryBuilder<T>() { ; }
00146    virtual ~QxSqlQueryBuilder_Exist() { ; }
00147 
00148    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00149    {
00150       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00151       QX_SQL_BUILDER_INIT_FCT("Exist")
00152       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00153       qx::dao::detail::QxSqlQueryHelper_Exist<type_sql>::sql(sql, (* this));
00154       this->setSqlQuery(sql, key);
00155       return (* this);
00156    }
00157 
00158 };
00159 
00164 template <class T>
00165 class QxSqlQueryBuilder_FetchAll : public QxSqlQueryBuilder<T>
00166 {
00167 
00168 public:
00169 
00170    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00171 
00172    QxSqlQueryBuilder_FetchAll() : QxSqlQueryBuilder<T>() { ; }
00173    virtual ~QxSqlQueryBuilder_FetchAll() { ; }
00174 
00175    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00176    {
00177       Q_UNUSED(pRelationX);
00178 
00179       if ((columns.count() <= 0) || (columns.at(0) == "*"))
00180       {
00181          QX_SQL_BUILDER_INIT_FCT("FetchAll")
00182          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this));
00183          this->setSqlQuery(sql, key);
00184       }
00185       else
00186       {
00187          QString sql;
00188          if (! this->verifyColumns(columns)) { return (* this); }
00189          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this), columns);
00190          this->setSqlQuery(sql);
00191       }
00192 
00193       return (* this);
00194    }
00195 
00196 };
00197 
00202 template <class T>
00203 class QxSqlQueryBuilder_FetchById : public QxSqlQueryBuilder<T>
00204 {
00205 
00206 public:
00207 
00208    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00209 
00210    QxSqlQueryBuilder_FetchById() : QxSqlQueryBuilder<T>() { ; }
00211    virtual ~QxSqlQueryBuilder_FetchById() { ; }
00212 
00213    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00214    {
00215       Q_UNUSED(pRelationX);
00216       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00217       QxSqlQueryBuilder_FetchAll<type_sql> builder; builder.clone(* this);
00218 
00219       if ((columns.count() <= 0) || (columns.at(0) == "*"))
00220       {
00221          QX_SQL_BUILDER_INIT_FCT("FetchById")
00222          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder);
00223          this->setSqlQuery(sql, key);
00224       }
00225       else
00226       {
00227          QString sql;
00228          if (! this->verifyColumns(columns)) { return (* this); }
00229          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder, columns);
00230          this->setSqlQuery(sql);
00231       }
00232 
00233       return (* this);
00234    }
00235 
00236 };
00237 
00242 template <class T>
00243 class QxSqlQueryBuilder_Insert : public QxSqlQueryBuilder<T>
00244 {
00245 
00246 public:
00247 
00248    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00249 
00250    QxSqlQueryBuilder_Insert() : QxSqlQueryBuilder<T>() { ; }
00251    virtual ~QxSqlQueryBuilder_Insert() { ; }
00252 
00253    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00254    {
00255       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00256       QX_SQL_BUILDER_INIT_FCT("Insert")
00257       qx::dao::detail::QxSqlQueryHelper_Insert<type_sql>::sql(sql, (* this));
00258       this->setSqlQuery(sql, key);
00259       return (* this);
00260    }
00261 
00262 };
00263 
00268 template <class T>
00269 class QxSqlQueryBuilder_Update : public QxSqlQueryBuilder<T>
00270 {
00271 
00272 public:
00273 
00274    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00275 
00276    QxSqlQueryBuilder_Update() : QxSqlQueryBuilder<T>() { ; }
00277    virtual ~QxSqlQueryBuilder_Update() { ; }
00278 
00279    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00280    {
00281       Q_UNUSED(pRelationX);
00282       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00283 
00284       if ((columns.count() <= 0) || (columns.at(0) == "*"))
00285       {
00286          QX_SQL_BUILDER_INIT_FCT("Update")
00287          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this));
00288          this->setSqlQuery(sql, key);
00289       }
00290       else
00291       {
00292          QString sql;
00293          if (! this->verifyColumns(columns)) { return (* this); }
00294          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this), columns);
00295          this->setSqlQuery(sql);
00296       }
00297 
00298       return (* this);
00299    }
00300 
00301 };
00302 
00307 template <class T>
00308 class QxSqlQueryBuilder_DeleteAll : public QxSqlQueryBuilder<T>
00309 {
00310 
00311 public:
00312 
00313    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00314 
00315    QxSqlQueryBuilder_DeleteAll() : QxSqlQueryBuilder<T>() { ; }
00316    virtual ~QxSqlQueryBuilder_DeleteAll() { ; }
00317 
00318    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00319    {
00320       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00321       QX_SQL_BUILDER_INIT_FCT("DeleteAll")
00322       sql = "DELETE FROM " + this->table();
00323       this->setSqlQuery(sql, key);
00324       return (* this);
00325    }
00326 
00327 };
00328 
00333 template <class T>
00334 class QxSqlQueryBuilder_SoftDeleteAll : public QxSqlQueryBuilder<T>
00335 {
00336 
00337 public:
00338 
00339    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00340 
00341    QxSqlQueryBuilder_SoftDeleteAll() : QxSqlQueryBuilder<T>() { ; }
00342    virtual ~QxSqlQueryBuilder_SoftDeleteAll() { ; }
00343 
00344    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00345    {
00346       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00347       QX_SQL_BUILDER_INIT_FCT("SoftDeleteAll")
00348       if (! this->softDelete().isEmpty()) { sql = "UPDATE " + this->table() + " SET " + this->softDelete().buildSqlQueryToUpdate(); }
00349       else { qAssert(false); }
00350       this->setSqlQuery(sql, key);
00351       return (* this);
00352    }
00353 
00354 };
00355 
00360 template <class T>
00361 class QxSqlQueryBuilder_DeleteById : public QxSqlQueryBuilder<T>
00362 {
00363 
00364 public:
00365 
00366    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00367 
00368    QxSqlQueryBuilder_DeleteById() : QxSqlQueryBuilder<T>() { ; }
00369    virtual ~QxSqlQueryBuilder_DeleteById() { ; }
00370 
00371    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00372    {
00373       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00374       QX_SQL_BUILDER_INIT_FCT("DeleteById")
00375       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00376       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), false);
00377       this->setSqlQuery(sql, key);
00378       return (* this);
00379    }
00380 
00381 };
00382 
00387 template <class T>
00388 class QxSqlQueryBuilder_SoftDeleteById : public QxSqlQueryBuilder<T>
00389 {
00390 
00391 public:
00392 
00393    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00394 
00395    QxSqlQueryBuilder_SoftDeleteById() : QxSqlQueryBuilder<T>() { ; }
00396    virtual ~QxSqlQueryBuilder_SoftDeleteById() { ; }
00397 
00398    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00399    {
00400       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00401       QX_SQL_BUILDER_INIT_FCT("SoftDeleteById")
00402       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00403       if (this->softDelete().isEmpty()) { qAssert(false); return (* this); }
00404       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), true);
00405       this->setSqlQuery(sql, key);
00406       return (* this);
00407    }
00408 
00409 };
00410 
00415 template <class T>
00416 class QxSqlQueryBuilder_CreateTable : public QxSqlQueryBuilder<T>
00417 {
00418 
00419 public:
00420 
00421    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00422 
00423    QxSqlQueryBuilder_CreateTable() : QxSqlQueryBuilder<T>() { ; }
00424    virtual ~QxSqlQueryBuilder_CreateTable() { ; }
00425 
00426    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00427    {
00428       Q_UNUSED(columns); Q_UNUSED(pRelationX);
00429       QX_SQL_BUILDER_INIT_FCT("CreateTable")
00430       qx::dao::detail::QxSqlQueryHelper_CreateTable<type_sql>::sql(sql, (* this));
00431       this->setSqlQuery(sql, key);
00432       return (* this);
00433    }
00434 
00435 };
00436 
00441 template <class T>
00442 class QxSqlQueryBuilder_Count_WithRelation : public QxSqlQueryBuilder<T>
00443 {
00444 
00445 public:
00446 
00447    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00448 
00449    QxSqlQueryBuilder_Count_WithRelation() : QxSqlQueryBuilder<T>() { ; }
00450    virtual ~QxSqlQueryBuilder_Count_WithRelation() { ; }
00451 
00452    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00453    {
00454       Q_UNUSED(columns);
00455       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("Count_WithRelation")
00456       IxSqlQueryBuilder::sql_Count_WithRelation(pRelationX, sql, (* this));
00457       this->setSqlQuery(sql, key);
00458       this->insertSqlAlias(key);
00459       return (* this);
00460    }
00461 
00462 };
00463 
00468 template <class T>
00469 class QxSqlQueryBuilder_FetchAll_WithRelation : public QxSqlQueryBuilder<T>
00470 {
00471 
00472 public:
00473 
00474    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00475 
00476    QxSqlQueryBuilder_FetchAll_WithRelation() : QxSqlQueryBuilder<T>() { ; }
00477    virtual ~QxSqlQueryBuilder_FetchAll_WithRelation() { ; }
00478 
00479    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00480    {
00481       Q_UNUSED(columns);
00482       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchAll_WithRelation")
00483       qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_sql>::sql(pRelationX, sql, (* this));
00484       this->setSqlQuery(sql, key);
00485       this->insertSqlAlias(key);
00486       return (* this);
00487    }
00488 
00489 };
00490 
00495 template <class T>
00496 class QxSqlQueryBuilder_FetchById_WithRelation : public QxSqlQueryBuilder<T>
00497 {
00498 
00499 public:
00500 
00501    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
00502 
00503    QxSqlQueryBuilder_FetchById_WithRelation() : QxSqlQueryBuilder<T>() { ; }
00504    virtual ~QxSqlQueryBuilder_FetchById_WithRelation() { ; }
00505 
00506    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
00507    {
00508       Q_UNUSED(columns);
00509       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchById_WithRelation")
00510       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
00511       QxSqlQueryBuilder_FetchAll_WithRelation<type_sql> builder; builder.clone(* this);
00512       qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<type_sql>::sql(pRelationX, sql, builder);
00513       this->setSqlQuery(sql, key);
00514       this->insertSqlAlias(key);
00515       return (* this);
00516    }
00517 
00518 };
00519 
00520 } // namespace qx
00521 
00522 #endif // _QX_SQL_QUERY_BUILDER_H_