QxOrm  1.4.3
C++ Object Relational Mapping library
IxSqlQueryBuilder.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 _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 public:
00072 
00073    typedef QPair<QString, QString> type_id;
00074    typedef QHash<type_id, void *> type_ptr_by_id;
00075    typedef qx_shared_ptr<type_ptr_by_id> type_ptr_by_id_ptr;
00076    typedef QList<type_ptr_by_id_ptr> type_lst_ptr_by_id;
00077    typedef qx_shared_ptr<type_lst_ptr_by_id> type_lst_ptr_by_id_ptr;
00078    typedef QxCollection<QString, IxDataMember *> type_lst_data_member;
00079    typedef qx_shared_ptr<type_lst_data_member> type_lst_data_member_ptr;
00080 
00081 protected:
00082 
00083    type_lst_data_member_ptr m_lstDataMemberPtr;                      
00084    qx_shared_ptr<IxSqlRelationX> m_lstSqlRelationPtr;                
00085    IxDataMember * m_pDataMemberId;                                   
00086    QString m_sSqlQuery;                                              
00087    QString m_sTableName;                                             
00088    QString m_sHashRelation;                                          
00089    bool m_bCartesianProduct;                                         
00090    type_lst_ptr_by_id_ptr m_pIdX;                                    
00091    QxSoftDelete m_oSoftDelete;                                       
00092    QHash<QString, QString> m_lstSqlQueryAlias;                       
00093    qx::dao::detail::IxDao_Helper * m_pDaoHelper;                     
00094    IxDataMemberX * m_pDataMemberX;                                   
00095    bool m_bInitDone;                                                 
00096 
00097    static QHash<QString, QString> m_lstSqlQuery;                     
00098    static QHash<QString, QHash<QString, QString> > m_lstSqlAlias;    
00099 
00100 public:
00101 
00102    IxSqlQueryBuilder() : m_pDataMemberId(NULL), m_bCartesianProduct(false), m_pDaoHelper(NULL), m_pDataMemberX(NULL), m_bInitDone(false) { ; }
00103    virtual ~IxSqlQueryBuilder() = 0;
00104 
00105    inline IxDataMemberX * getDataMemberX() const                              { return m_pDataMemberX; }
00106    inline QxCollection<QString, IxDataMember *> * getLstDataMember() const    { return m_lstDataMemberPtr.get(); }
00107    inline IxSqlRelationX * getLstRelation() const                             { return m_lstSqlRelationPtr.get(); }
00108    inline qx::dao::detail::IxDao_Helper * getDaoHelper() const                { return m_pDaoHelper; }
00109    inline void setDaoHelper(qx::dao::detail::IxDao_Helper * p)                { m_pDaoHelper = p; }
00110 
00111    inline void setHashRelation(const QString & s)        { m_sHashRelation = s; }
00112    inline void setCartesianProduct(bool b)               { m_bCartesianProduct = b; }
00113    inline QString getSqlQuery() const                    { return m_sSqlQuery; }
00114    inline QString getHashRelation() const                { return m_sHashRelation; }
00115    inline QString table() const                          { return m_sTableName; }
00116    inline QxSoftDelete getSoftDelete() const             { return m_oSoftDelete; }
00117    inline bool getCartesianProduct() const               { return m_bCartesianProduct; }
00118    inline long getDataCount() const                      { return (m_lstDataMemberPtr ? m_lstDataMemberPtr->count() : 0); }
00119    inline long getRelationCount() const                  { return (m_lstSqlRelationPtr ? m_lstSqlRelationPtr->count() : 0); }
00120    inline IxDataMember * getDataId() const               { return m_pDataMemberId; }
00121    inline IxDataMember * nextData(long & l) const        { if ((! m_lstDataMemberPtr) || (l < 0) || (l >= m_lstDataMemberPtr->count())) { return NULL; }; ++l; return m_lstDataMemberPtr->getByIndex(l - 1); }
00122    inline IxSqlRelation * nextRelation(long & l) const   { if ((! m_lstSqlRelationPtr) || (l < 0) || (l >= m_lstSqlRelationPtr->count())) { return NULL; }; ++l; return m_lstSqlRelationPtr->getByIndex(l - 1); }
00123 
00124    void initIdX(long lAllRelationCount);
00125    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00126    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00127    void setSqlQuery(const QString & sql, const QString & key = QString());
00128    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00129    bool getAddAutoIncrementIdToUpdateQuery() const;
00130    void replaceSqlQueryAlias(QString & sql) const;
00131    void displaySqlQuery(int time_ms = -1) const;
00132 
00133    virtual void init();
00134    virtual void clone(const IxSqlQueryBuilder & other);
00135    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL) = 0;
00136 
00137    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00138 
00139    static void sql_CreateTable(QString & sql, IxSqlQueryBuilder & builder);
00140    static void sql_DeleteById(QString & sql, IxSqlQueryBuilder & builder, bool bSoftDelete);
00141    static void sql_Exist(QString & sql, IxSqlQueryBuilder & builder);
00142    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder);
00143    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00144    static void sql_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00145    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder);
00146    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00147    static void sql_FetchById_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00148    static void sql_Insert(QString & sql, IxSqlQueryBuilder & builder);
00149    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder);
00150    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00151 
00152    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00153    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00154    static void resolveOutput_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00155 
00156    static void resolveInput_Insert(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00157    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00158    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00159 
00160 protected:
00161 
00162    bool verifyColumns(const QStringList & columns) const BOOST_USED;
00163 
00164    IxDataMember * isValid_DataMember(long lIndex) const;
00165    IxDataMember * isValid_SqlRelation(long lIndex) const;
00166 
00167 };
00168 
00169 typedef qx_shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00170 
00171 } // namespace qx
00172 
00173 #include <QxDao/IxDao_Helper.h>
00174 
00175 #endif // _IX_SQL_QUERY_BUILDER_H_