| QxOrm 1.4.4 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 ** http://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 QString key = QxClass<type_sql>::getSingleton()->getKey() + oper; \
    00060 QString sql = IxSqlQueryBuilder::listSqlQuery().value(key); \
    00061 if (! sql.isEmpty()) { this->setSqlQuery(sql); return (* this); }
    00062
    00063 #define QX_SQL_BUILDER_INIT_FCT_WITH_RELATION(oper) \
    00064 QString key = QxClass<type_sql>::getSingleton()->getKey() + this->getHashRelation() + oper; \
    00065 QString sql = IxSqlQueryBuilder::listSqlQuery().value(key); \
    00066 if (! sql.isEmpty()) { this->setSqlQuery(sql); this->listSqlQueryAlias() = IxSqlQueryBuilder::listSqlAlias().value(key); return (* this); }
    00067
    00068 namespace qx {
    00069
    00074 template <class T>
    00075 class QxSqlQueryBuilder : public IxSqlQueryBuilder
    00076 {
    00077
    00078 private:
    00079
    00080    typedef typename qx::trait::remove_attr<T>::type type_sql_tmp_1;
    00081    typedef typename qx::trait::remove_smart_ptr<type_sql_tmp_1>::type type_sql_tmp_2;
    00082
    00083 public:
    00084
    00085    typedef typename qx::QxSqlQueryBuilder<T>::type_sql_tmp_2 type_sql;
    00086
    00087 public:
    00088
    00089    QxSqlQueryBuilder() : IxSqlQueryBuilder() { ; }
    00090    virtual ~QxSqlQueryBuilder() { static_assert(qx::trait::is_qx_registered<type_sql>::value, "qx::trait::is_qx_registered<type_sql>::value"); }
    00091
    00092    virtual void init()
    00093    {
    00094       if (isInitDone()) { return; }
    00095       setDataMemberX(QxClass<type_sql>::getSingleton()->dataMemberX());
    00096       setSoftDelete(QxClass<type_sql>::getSingleton()->getSoftDelete());
    00097       IxSqlQueryBuilder::init();
    00098    }
    00099
    00100 };
    00101
    00106 template <class T>
    00107 class QxSqlQueryBuilder_Count : public QxSqlQueryBuilder<T>
    00108 {
    00109
    00110 public:
    00111
    00112    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00113
    00114    QxSqlQueryBuilder_Count() : QxSqlQueryBuilder<T>() { ; }
    00115    virtual ~QxSqlQueryBuilder_Count() { ; }
    00116
    00117    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00118    {
    00119       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00120       QX_SQL_BUILDER_INIT_FCT("Count")
    00121       sql = "SELECT COUNT(*) FROM " + qx::IxDataMember::getSqlFromTable(this->table());
    00122       if (! this->softDelete().isEmpty()) { sql += " WHERE " + this->softDelete().buildSqlQueryToFetch(); }
    00123       this->setSqlQuery(sql, key);
    00124       return (* this);
    00125    }
    00126
    00127 };
    00128
    00133 template <class T>
    00134 class QxSqlQueryBuilder_Exist : public QxSqlQueryBuilder<T>
    00135 {
    00136
    00137 public:
    00138
    00139    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00140
    00141    QxSqlQueryBuilder_Exist() : QxSqlQueryBuilder<T>() { ; }
    00142    virtual ~QxSqlQueryBuilder_Exist() { ; }
    00143
    00144    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00145    {
    00146       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00147       QX_SQL_BUILDER_INIT_FCT("Exist")
    00148       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00149       qx::dao::detail::QxSqlQueryHelper_Exist<type_sql>::sql(sql, (* this));
    00150       this->setSqlQuery(sql, key);
    00151       return (* this);
    00152    }
    00153
    00154 };
    00155
    00160 template <class T>
    00161 class QxSqlQueryBuilder_FetchAll : public QxSqlQueryBuilder<T>
    00162 {
    00163
    00164 public:
    00165
    00166    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00167
    00168    QxSqlQueryBuilder_FetchAll() : QxSqlQueryBuilder<T>() { ; }
    00169    virtual ~QxSqlQueryBuilder_FetchAll() { ; }
    00170
    00171    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00172    {
    00173       Q_UNUSED(pRelationX);
    00174
    00175       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00176       {
    00177          QX_SQL_BUILDER_INIT_FCT("FetchAll")
    00178          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this));
    00179          this->setSqlQuery(sql, key);
    00180       }
    00181       else
    00182       {
    00183          QString sql;
    00184          if (! this->verifyColumns(columns)) { return (* this); }
    00185          qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (* this), columns);
    00186          this->setSqlQuery(sql);
    00187       }
    00188
    00189       return (* this);
    00190    }
    00191
    00192 };
    00193
    00198 template <class T>
    00199 class QxSqlQueryBuilder_FetchById : public QxSqlQueryBuilder<T>
    00200 {
    00201
    00202 public:
    00203
    00204    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00205
    00206    QxSqlQueryBuilder_FetchById() : QxSqlQueryBuilder<T>() { ; }
    00207    virtual ~QxSqlQueryBuilder_FetchById() { ; }
    00208
    00209    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00210    {
    00211       Q_UNUSED(pRelationX);
    00212       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00213       QxSqlQueryBuilder_FetchAll<type_sql> builder; builder.clone(* this);
    00214
    00215       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00216       {
    00217          QX_SQL_BUILDER_INIT_FCT("FetchById")
    00218          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder);
    00219          this->setSqlQuery(sql, key);
    00220       }
    00221       else
    00222       {
    00223          QString sql;
    00224          if (! this->verifyColumns(columns)) { return (* this); }
    00225          qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder, columns);
    00226          this->setSqlQuery(sql);
    00227       }
    00228
    00229       return (* this);
    00230    }
    00231
    00232 };
    00233
    00238 template <class T>
    00239 class QxSqlQueryBuilder_Insert : public QxSqlQueryBuilder<T>
    00240 {
    00241
    00242 public:
    00243
    00244    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00245
    00246    QxSqlQueryBuilder_Insert() : QxSqlQueryBuilder<T>() { ; }
    00247    virtual ~QxSqlQueryBuilder_Insert() { ; }
    00248
    00249    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00250    {
    00251       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00252       QX_SQL_BUILDER_INIT_FCT("Insert")
    00253       qx::dao::detail::QxSqlQueryHelper_Insert<type_sql>::sql(sql, (* this));
    00254       this->setSqlQuery(sql, key);
    00255       return (* this);
    00256    }
    00257
    00258 };
    00259
    00264 template <class T>
    00265 class QxSqlQueryBuilder_Update : public QxSqlQueryBuilder<T>
    00266 {
    00267
    00268 public:
    00269
    00270    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00271
    00272    QxSqlQueryBuilder_Update() : QxSqlQueryBuilder<T>() { ; }
    00273    virtual ~QxSqlQueryBuilder_Update() { ; }
    00274
    00275    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00276    {
    00277       Q_UNUSED(pRelationX);
    00278       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00279
    00280       if ((columns.count() <= 0) || (columns.at(0) == "*"))
    00281       {
    00282          QX_SQL_BUILDER_INIT_FCT("Update")
    00283          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this));
    00284          this->setSqlQuery(sql, key);
    00285       }
    00286       else
    00287       {
    00288          QString sql;
    00289          if (! this->verifyColumns(columns)) { return (* this); }
    00290          qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (* this), columns);
    00291          this->setSqlQuery(sql);
    00292       }
    00293
    00294       return (* this);
    00295    }
    00296
    00297 };
    00298
    00303 template <class T>
    00304 class QxSqlQueryBuilder_DeleteAll : public QxSqlQueryBuilder<T>
    00305 {
    00306
    00307 public:
    00308
    00309    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00310
    00311    QxSqlQueryBuilder_DeleteAll() : QxSqlQueryBuilder<T>() { ; }
    00312    virtual ~QxSqlQueryBuilder_DeleteAll() { ; }
    00313
    00314    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00315    {
    00316       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00317       QX_SQL_BUILDER_INIT_FCT("DeleteAll")
    00318       sql = "DELETE FROM " + this->table();
    00319       this->setSqlQuery(sql, key);
    00320       return (* this);
    00321    }
    00322
    00323 };
    00324
    00329 template <class T>
    00330 class QxSqlQueryBuilder_SoftDeleteAll : public QxSqlQueryBuilder<T>
    00331 {
    00332
    00333 public:
    00334
    00335    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00336
    00337    QxSqlQueryBuilder_SoftDeleteAll() : QxSqlQueryBuilder<T>() { ; }
    00338    virtual ~QxSqlQueryBuilder_SoftDeleteAll() { ; }
    00339
    00340    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00341    {
    00342       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00343       QX_SQL_BUILDER_INIT_FCT("SoftDeleteAll")
    00344       if (! this->softDelete().isEmpty()) { sql = "UPDATE " + this->table() + " SET " + this->softDelete().buildSqlQueryToUpdate(); }
    00345       else { qAssert(false); }
    00346       this->setSqlQuery(sql, key);
    00347       return (* this);
    00348    }
    00349
    00350 };
    00351
    00356 template <class T>
    00357 class QxSqlQueryBuilder_DeleteById : public QxSqlQueryBuilder<T>
    00358 {
    00359
    00360 public:
    00361
    00362    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00363
    00364    QxSqlQueryBuilder_DeleteById() : QxSqlQueryBuilder<T>() { ; }
    00365    virtual ~QxSqlQueryBuilder_DeleteById() { ; }
    00366
    00367    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00368    {
    00369       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00370       QX_SQL_BUILDER_INIT_FCT("DeleteById")
    00371       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00372       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), false);
    00373       this->setSqlQuery(sql, key);
    00374       return (* this);
    00375    }
    00376
    00377 };
    00378
    00383 template <class T>
    00384 class QxSqlQueryBuilder_SoftDeleteById : public QxSqlQueryBuilder<T>
    00385 {
    00386
    00387 public:
    00388
    00389    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00390
    00391    QxSqlQueryBuilder_SoftDeleteById() : QxSqlQueryBuilder<T>() { ; }
    00392    virtual ~QxSqlQueryBuilder_SoftDeleteById() { ; }
    00393
    00394    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00395    {
    00396       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00397       QX_SQL_BUILDER_INIT_FCT("SoftDeleteById")
    00398       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00399       if (this->softDelete().isEmpty()) { qAssert(false); return (* this); }
    00400       qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (* this), true);
    00401       this->setSqlQuery(sql, key);
    00402       return (* this);
    00403    }
    00404
    00405 };
    00406
    00411 template <class T>
    00412 class QxSqlQueryBuilder_CreateTable : public QxSqlQueryBuilder<T>
    00413 {
    00414
    00415 public:
    00416
    00417    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00418
    00419    QxSqlQueryBuilder_CreateTable() : QxSqlQueryBuilder<T>() { ; }
    00420    virtual ~QxSqlQueryBuilder_CreateTable() { ; }
    00421
    00422    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00423    {
    00424       Q_UNUSED(columns); Q_UNUSED(pRelationX);
    00425       QX_SQL_BUILDER_INIT_FCT("CreateTable")
    00426       qx::dao::detail::QxSqlQueryHelper_CreateTable<type_sql>::sql(sql, (* this));
    00427       this->setSqlQuery(sql, key);
    00428       return (* this);
    00429    }
    00430
    00431 };
    00432
    00437 template <class T>
    00438 class QxSqlQueryBuilder_FetchAll_WithRelation : public QxSqlQueryBuilder<T>
    00439 {
    00440
    00441 public:
    00442
    00443    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00444
    00445    QxSqlQueryBuilder_FetchAll_WithRelation() : QxSqlQueryBuilder<T>() { ; }
    00446    virtual ~QxSqlQueryBuilder_FetchAll_WithRelation() { ; }
    00447
    00448    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00449    {
    00450       Q_UNUSED(columns);
    00451       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchAll_WithRelation")
    00452       qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_sql>::sql(pRelationX, sql, (* this));
    00453       IxSqlQueryBuilder::listSqlAlias().insert(key, this->listSqlQueryAlias());
    00454       this->setSqlQuery(sql, key);
    00455       return (* this);
    00456    }
    00457
    00458 };
    00459
    00464 template <class T>
    00465 class QxSqlQueryBuilder_FetchById_WithRelation : public QxSqlQueryBuilder<T>
    00466 {
    00467
    00468 public:
    00469
    00470    typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
    00471
    00472    QxSqlQueryBuilder_FetchById_WithRelation() : QxSqlQueryBuilder<T>() { ; }
    00473    virtual ~QxSqlQueryBuilder_FetchById_WithRelation() { ; }
    00474
    00475    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL)
    00476    {
    00477       Q_UNUSED(columns);
    00478       QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchById_WithRelation")
    00479       if (! this->getDataId()) { qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED); qAssert(false); return (* this); }
    00480       QxSqlQueryBuilder_FetchAll_WithRelation<type_sql> builder; builder.clone(* this);
    00481       qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<type_sql>::sql(pRelationX, sql, builder);
    00482       IxSqlQueryBuilder::listSqlAlias().insert(key, this->listSqlQueryAlias());
    00483       this->setSqlQuery(sql, key);
    00484       return (* this);
    00485    }
    00486
    00487 };
    00488
    00489 } // namespace qx
    00490
    00491 #endif // _QX_SQL_QUERY_BUILDER_H_