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