QxOrm  1.4.9
C++ Object Relational Mapping library
IxSqlQueryBuilder.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 _IX_SQL_QUERY_BUILDER_H_
00033 #define _IX_SQL_QUERY_BUILDER_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDataMember/IxDataMemberX.h>
00047 
00048 #include <QxDao/IxSqlRelation.h>
00049 #include <QxDao/QxSoftDelete.h>
00050 #include <QxDao/QxSqlRelationLinked.h>
00051 
00052 namespace qx {
00053 namespace dao {
00054 namespace detail {
00055 
00056 class IxDao_Helper;
00057 
00058 } // namespace detail
00059 } // namespace dao
00060 } // namespace qx
00061 
00062 namespace qx {
00063 
00068 class QX_DLL_EXPORT IxSqlQueryBuilder
00069 {
00070 
00071 private:
00072 
00073    struct IxSqlQueryBuilderImpl;
00074    std::unique_ptr<IxSqlQueryBuilderImpl> m_pImpl; 
00075 
00076 public:
00077 
00078    IxSqlQueryBuilder();
00079    virtual ~IxSqlQueryBuilder() = 0;
00080 
00081    IxDataMemberX * getDataMemberX() const;
00082    QxCollection<QString, IxDataMember *> * getLstDataMember() const;
00083    IxSqlRelationX * getLstRelation() const;
00084    qx::dao::detail::IxDao_Helper * getDaoHelper() const;
00085    void setDaoHelper(qx::dao::detail::IxDao_Helper * p);
00086 
00087    void setHashRelation(const QString & s);
00088    void setCartesianProduct(bool b);
00089    QString getSqlQuery() const;
00090    QString getHashRelation() const;
00091    QString table() const;
00092    QxSoftDelete getSoftDelete() const;
00093    bool getCartesianProduct() const;
00094    long getDataCount() const;
00095    long getRelationCount() const;
00096    IxDataMember * getDataId() const;
00097    IxDataMember * nextData(long & l) const;
00098    IxSqlRelation * nextRelation(long & l) const;
00099    QString & getCurrentBuildingSql() const;
00100 
00101    void initIdX(long lAllRelationCount);
00102    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00103    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00104    void setSqlQuery(const QString & sql, const QString & key = QString());
00105    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00106    bool getAddAutoIncrementIdToUpdateQuery() const;
00107    void replaceSqlQueryAlias(QString & sql) const;
00108 
00109    virtual void init();
00110    virtual void clone(const IxSqlQueryBuilder & other);
00111    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL) = 0;
00112 
00113    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00114 
00115    static void sql_CreateTable(QString & sql, IxSqlQueryBuilder & builder);
00116    static void sql_DeleteById(QString & sql, IxSqlQueryBuilder & builder, bool bSoftDelete);
00117    static void sql_Exist(QString & sql, IxSqlQueryBuilder & builder);
00118    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder);
00119    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00120    static void sql_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00121    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder);
00122    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00123    static void sql_FetchById_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00124    static void sql_Insert(QString & sql, IxSqlQueryBuilder & builder);
00125    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder);
00126    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00127    static void sql_Count_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00128 
00129    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00130    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00131    static void resolveOutput_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00132 
00133    static void resolveInput_Insert(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00134    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00135    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00136    static void resolveInput_DeleteById(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00137 
00138 protected:
00139 
00140    bool verifyColumns(const QStringList & columns) const QX_USED;
00141 
00142    bool isInitDone() const;
00143    QxSoftDelete & softDelete();
00144    const QxSoftDelete & softDelete() const;
00145    void setSoftDelete(const QxSoftDelete & o);
00146    void setDataMemberX(IxDataMemberX * p);
00147    bool findSqlQuery(const QString & key);
00148    bool findSqlAlias(const QString & key);
00149    void insertSqlAlias(const QString & key);
00150 
00151 };
00152 
00153 typedef std::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00154 
00155 } // namespace qx
00156 
00157 #include <QxDao/IxDao_Helper.h>
00158 
00159 #endif // _IX_SQL_QUERY_BUILDER_H_