| QxOrm 1.4.9 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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 ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
    00062 QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + ignoreSoftDeleteHash + oper; \
    00063 if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { return (* this); } \
    00064 QString & sql = this->getCurrentBuildingSql(); sql = "";
    00065
    00066 #define QX_SQL_BUILDER_INIT_FCT_WITH_RELATION(oper) \
    00067 qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
    00068 QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
    00069 QString ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
    00070 QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + this->getHashRelation() + ignoreSoftDeleteHash + oper; \
    00071 if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) { this->findSqlAlias(key); return (* this); } \
    00072 QString & sql = this->getCurrentBuildingSql(); sql = "";
    00073
    00074 namespace qx {
    00075
    00080 template <class T>
    00081 class QxSqlQueryBuilder : public IxSqlQueryBuilder
    00082 {
    00083
    00084 private:
    00085
    00086    typedef typename qx::trait::remove_attr<T>::type type_sql_tmp_1;
    00087    typedef typename qx::trait::remove_smart_ptr<type_sql_tmp_1>::type type_sql_tmp_2;
    00088
    00089 public:
    00090
    00091    typedef typename qx::QxSqlQueryBuilder<T>::type_sql_tmp_2 type_sql;
    00092
    00093 public:
    00094
    00095    QxSqlQueryBuilder() : IxSqlQueryBuilder() { ; }
    00096    virtual ~QxSqlQueryBuilder() { static_assert(qx::trait::is_qx_registered<type_sql>::value, "qx::trait::is_qx_registered<type_sql>::value"); }
    00097
    00098    virtual void init()
    00099    {
    00100       if (isInitDone()) { return; }
    00101       setDataMemberX(QxClass<type_sql>::getSingleton()->dataMemberX());
    00102       setSoftDelete(QxClass<type_sql>::getSingleton()->getSoftDelete());
    00103       IxSqlQueryBuilder::init();
    00104    }
    00105
    00106 };
    00107
    00112 template <class T>
    00113 class QxSqlQueryBuilder_Count : public QxSqlQueryBuilder<T>
    00114 {
    00115
    00116 public:
    00117
    00118    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00119
    00120    QxSqlQueryBuilder_Count() : QxSqlQueryBuilder<T>() { ; }
    00121    virtual ~QxSqlQueryBuilder_Count() { ; }
    00122
    00123    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00124    {
    00125       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00126       QX_SQL_BUILDER_INIT_FCT("Count")
    00127       sql = "SELECT COUNT(*) FROM " + qx::IxDataMember::getSqlFromTable(this->table());
    00128       if (! this->softDelete().isEmpty()) { sql += " WHERE " + this->softDelete().buildSqlQueryToFetch(); }
    00129       this->setSqlQuery(sql, key);
    00130       return (* this);
    00131    }
    00132
    00133 };
    00134
    00139 template <class T>
    00140 class QxSqlQueryBuilder_Exist : public QxSqlQueryBuilder<T>
    00141 {
    00142
    00143 public:
    00144
    00145    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00146
    00147    QxSqlQueryBuilder_Exist() : QxSqlQueryBuilder<T>() { ; }
    00148    virtual ~QxSqlQueryBuilder_Exist() { ; }
    00149
    00150    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00151    {
    00152       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00153       QX_SQL_BUILDER_INIT_FCT("Exist")
    00154       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00155       qx::dao::detail::QxSqlQueryHelper_Exist<type_sql>::sql(sql, (* this));
    00156       this->setSqlQuery(sql, key);
    00157       return (* this);
    00158    }
    00159
    00160 };
    00161
    00166 template <class T>
    00167 class QxSqlQueryBuilder_FetchAll : public QxSqlQueryBuilder<T>
    00168 {
    00169
    00170 public:
    00171
    00172    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00173
    00174    QxSqlQueryBuilder_FetchAll() : QxSqlQueryBuilder<T>() { ; }
    00175    virtual ~QxSqlQueryBuilder_FetchAll() { ; }
    00176
    00177    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00178    {
    00179       Q_UNUSED(pRelationX);
    00180
    00181       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00182       {
    00183          QX_SQL_BUILDER_INIT_FCT("FetchAll")
    00184          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this));
    00185          this->setSqlQuery(sql, key);
    00186       }
    00187       else
    00188       {
    00189          QString sql;
    00190          if (! this->verifyColumns(columns)) { return (* this); }
    00191          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this), columns);
    00192          this->setSqlQuery(sql);
    00193       }
    00194
    00195       return (* this);
    00196    }
    00197
    00198 };
    00199
    00204 template <class T>
    00205 class QxSqlQueryBuilder_FetchById : public QxSqlQueryBuilder<T>
    00206 {
    00207
    00208 public:
    00209
    00210    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00211
    00212    QxSqlQueryBuilder_FetchById() : QxSqlQueryBuilder<T>() { ; }
    00213    virtual ~QxSqlQueryBuilder_FetchById() { ; }
    00214
    00215    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00216    {
    00217       Q_UNUSED(pRelationX);
    00218       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00219       QxSqlQueryBuilder_FetchAll<type_sql> builder; builder.clone(* this);
    00220
    00221       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00222       {
    00223          QX_SQL_BUILDER_INIT_FCT("FetchById")
    00224          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder);
    00225          this->setSqlQuery(sql, key);
    00226       }
    00227       else
    00228       {
    00229          QString sql;
    00230          if (! this->verifyColumns(columns)) { return (* this); }
    00231          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder, columns);
    00232          this->setSqlQuery(sql);
    00233       }
    00234
    00235       return (* this);
    00236    }
    00237
    00238 };
    00239
    00244 template <class T>
    00245 class QxSqlQueryBuilder_Insert : public QxSqlQueryBuilder<T>
    00246 {
    00247
    00248 public:
    00249
    00250    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00251
    00252    QxSqlQueryBuilder_Insert() : QxSqlQueryBuilder<T>() { ; }
    00253    virtual ~QxSqlQueryBuilder_Insert() { ; }
    00254
    00255    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00256    {
    00257       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00258       QX_SQL_BUILDER_INIT_FCT("Insert")
    00259       qx::dao::detail::QxSqlQueryHelper_Insert<type_sql>::sql(sql, (* this));
    00260       this->setSqlQuery(sql, key);
    00261       return (* this);
    00262    }
    00263
    00264 };
    00265
    00270 template <class T>
    00271 class QxSqlQueryBuilder_Update : public QxSqlQueryBuilder<T>
    00272 {
    00273
    00274 public:
    00275
    00276    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00277
    00278    QxSqlQueryBuilder_Update() : QxSqlQueryBuilder<T>() { ; }
    00279    virtual ~QxSqlQueryBuilder_Update() { ; }
    00280
    00281    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00282    {
    00283       Q_UNUSED(pRelationX);
    00284       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00285
    00286       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00287       {
    00288          QX_SQL_BUILDER_INIT_FCT("Update")
    00289          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this));
    00290          this->setSqlQuery(sql, key);
    00291       }
    00292       else
    00293       {
    00294          QString sql;
    00295          if (! this->verifyColumns(columns)) { return (* this); }
    00296          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this), columns);
    00297          this->setSqlQuery(sql);
    00298       }
    00299
    00300       return (* this);
    00301    }
    00302
    00303 };
    00304
    00309 template <class T>
    00310 class QxSqlQueryBuilder_DeleteAll : public QxSqlQueryBuilder<T>
    00311 {
    00312
    00313 public:
    00314
    00315    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00316
    00317    QxSqlQueryBuilder_DeleteAll() : QxSqlQueryBuilder<T>() { ; }
    00318    virtual ~QxSqlQueryBuilder_DeleteAll() { ; }
    00319
    00320    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00321    {
    00322       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00323       QX_SQL_BUILDER_INIT_FCT("DeleteAll")
    00324       sql = "DELETE FROM " + this->table();
    00325       this->setSqlQuery(sql, key);
    00326       return (* this);
    00327    }
    00328
    00329 };
    00330
    00335 template <class T>
    00336 class QxSqlQueryBuilder_SoftDeleteAll : public QxSqlQueryBuilder<T>
    00337 {
    00338
    00339 public:
    00340
    00341    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00342
    00343    QxSqlQueryBuilder_SoftDeleteAll() : QxSqlQueryBuilder<T>() { ; }
    00344    virtual ~QxSqlQueryBuilder_SoftDeleteAll() { ; }
    00345
    00346    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00347    {
    00348       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00349       QX_SQL_BUILDER_INIT_FCT("SoftDeleteAll")
    00350       if (! this->softDelete().isEmpty()) { sql = "UPDATE " + this->table() + " SET " + this->softDelete().buildSqlQueryToUpdate(); }
    00351       else { qAssert(false); }
    00352       this->setSqlQuery(sql, key);
    00353       return (* this);
    00354    }
    00355
    00356 };
    00357
    00362 template <class T>
    00363 class QxSqlQueryBuilder_DeleteById : public QxSqlQueryBuilder<T>
    00364 {
    00365
    00366 public:
    00367
    00368    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00369
    00370    QxSqlQueryBuilder_DeleteById() : QxSqlQueryBuilder<T>() { ; }
    00371    virtual ~QxSqlQueryBuilder_DeleteById() { ; }
    00372
    00373    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00374    {
    00375       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00376       QX_SQL_BUILDER_INIT_FCT("DeleteById")
    00377       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00378       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), false);
    00379       this->setSqlQuery(sql, key);
    00380       return (* this);
    00381    }
    00382
    00383 };
    00384
    00389 template <class T>
    00390 class QxSqlQueryBuilder_SoftDeleteById : public QxSqlQueryBuilder<T>
    00391 {
    00392
    00393 public:
    00394
    00395    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00396
    00397    QxSqlQueryBuilder_SoftDeleteById() : QxSqlQueryBuilder<T>() { ; }
    00398    virtual ~QxSqlQueryBuilder_SoftDeleteById() { ; }
    00399
    00400    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00401    {
    00402       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00403       QX_SQL_BUILDER_INIT_FCT("SoftDeleteById")
    00404       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00405       if (this->softDelete().isEmpty()) { qAssert(false); return (* this); }
    00406       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), true);
    00407       this->setSqlQuery(sql, key);
    00408       return (* this);
    00409    }
    00410
    00411 };
    00412
    00417 template <class T>
    00418 class QxSqlQueryBuilder_CreateTable : public QxSqlQueryBuilder<T>
    00419 {
    00420
    00421 public:
    00422
    00423    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00424
    00425    QxSqlQueryBuilder_CreateTable() : QxSqlQueryBuilder<T>() { ; }
    00426    virtual ~QxSqlQueryBuilder_CreateTable() { ; }
    00427
    00428    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00429    {
    00430       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00431       QX_SQL_BUILDER_INIT_FCT("CreateTable")
    00432       qx::dao::detail::QxSqlQueryHelper_CreateTable<type_sql>::sql(sql, (* this));
    00433       this->setSqlQuery(sql, key);
    00434       return (* this);
    00435    }
    00436
    00437 };
    00438
    00443 template <class T>
    00444 class QxSqlQueryBuilder_Count_WithRelation : public QxSqlQueryBuilder<T>
    00445 {
    00446
    00447 public:
    00448
    00449    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00450
    00451    QxSqlQueryBuilder_Count_WithRelation() : QxSqlQueryBuilder<T>() { ; }
    00452    virtual ~QxSqlQueryBuilder_Count_WithRelation() { ; }
    00453
    00454    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00455    {
    00456       Q_UNUSED(columns);
    00457       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("Count_WithRelation")
    00458       IxSqlQueryBuilder::sql_Count_WithRelation(pRelationX, sql, (* this));
    00459       this->setSqlQuery(sql, key);
    00460       this->insertSqlAlias(key);
    00461       return (* this);
    00462    }
    00463
    00464 };
    00465
    00470 template <class T>
    00471 class QxSqlQueryBuilder_FetchAll_WithRelation : public QxSqlQueryBuilder<T>
    00472 {
    00473
    00474 public:
    00475
    00476    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00477
    00478    QxSqlQueryBuilder_FetchAll_WithRelation() : QxSqlQueryBuilder<T>() { ; }
    00479    virtual ~QxSqlQueryBuilder_FetchAll_WithRelation() { ; }
    00480
    00481    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00482    {
    00483       Q_UNUSED(columns);
    00484       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchAll_WithRelation")
    00485       qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_sql>::sql(pRelationX, sql, (* this));
    00486       this->setSqlQuery(sql, key);
    00487       this->insertSqlAlias(key);
    00488       return (* this);
    00489    }
    00490
    00491 };
    00492
    00497 template <class T>
    00498 class QxSqlQueryBuilder_FetchById_WithRelation : public QxSqlQueryBuilder<T>
    00499 {
    00500
    00501 public:
    00502
    00503    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00504
    00505    QxSqlQueryBuilder_FetchById_WithRelation() : QxSqlQueryBuilder<T>() { ; }
    00506    virtual ~QxSqlQueryBuilder_FetchById_WithRelation() { ; }
    00507
    00508    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00509    {
    00510       Q_UNUSED(columns);
    00511       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchById_WithRelation")
    00512       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00513       QxSqlQueryBuilder_FetchAll_WithRelation<type_sql> builder; builder.clone(* this);
    00514       qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<type_sql>::sql(pRelationX, sql, builder);
    00515       this->setSqlQuery(sql, key);
    00516       this->insertSqlAlias(key);
    00517       return (* this);
    00518    }
    00519
    00520 };
    00521
    00522 } // namespace qx
    00523
    00524 #endif // _QX_SQL_QUERY_BUILDER_H_