QxOrm  1.3.2
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 boost::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 boost::shared_ptr<type_lst_ptr_by_id> type_lst_ptr_by_id_ptr;
00078 
00079 protected:
00080 
00081    QxCollection<QString, IxDataMember *> * m_lstDataMemberPtr;    
00082    IxSqlRelationX * m_lstSqlRelationPtr;                          
00083    IxDataMember * m_pDataMemberId;                                
00084    QString m_sSqlQuery;                                           
00085    QString m_sTableName;                                          
00086    QString m_sHashRelation;                                       
00087    bool m_bCartesianProduct;                                      
00088    type_lst_ptr_by_id_ptr m_pIdX;                                 
00089    QxSoftDelete m_oSoftDelete;                                    
00090    QHash<QString, QString> m_lstSqlQueryAlias;                    
00091    qx::dao::detail::IxDao_Helper * m_pDaoHelper;                  
00092 
00093 public:
00094 
00095    IxSqlQueryBuilder() : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_bCartesianProduct(false), m_pDaoHelper(NULL) { ; }
00096    IxSqlQueryBuilder(const QString & sql) : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_sSqlQuery(sql), m_bCartesianProduct(false), m_pDaoHelper(NULL) { ; }
00097    virtual ~IxSqlQueryBuilder() = 0;
00098 
00099    inline QxCollection<QString, IxDataMember *> * getLstDataMember() const    { return m_lstDataMemberPtr; }
00100    inline IxSqlRelationX * getLstRelation() const                             { return m_lstSqlRelationPtr; }
00101    inline qx::dao::detail::IxDao_Helper * getDaoHelper() const                { return m_pDaoHelper; }
00102    inline void setDaoHelper(qx::dao::detail::IxDao_Helper * p)                { m_pDaoHelper = p; }
00103 
00104    inline void setSqlQuery(const QString & sql)          { m_sSqlQuery = sql; }
00105    inline void setHashRelation(const QString & s)        { m_sHashRelation = s; }
00106    inline void setCartesianProduct(bool b)               { m_bCartesianProduct = b; }
00107    inline QString getSqlQuery() const                    { return m_sSqlQuery; }
00108    inline QString getHashRelation() const                { return m_sHashRelation; }
00109    inline QString table() const                          { return m_sTableName; }
00110    inline QxSoftDelete getSoftDelete() const             { return m_oSoftDelete; }
00111    inline bool getCartesianProduct() const               { return m_bCartesianProduct; }
00112    inline long getDataCount() const                      { return (m_lstDataMemberPtr ? m_lstDataMemberPtr->count() : 0); }
00113    inline long getRelationCount() const                  { return (m_lstSqlRelationPtr ? m_lstSqlRelationPtr->count() : 0); }
00114    inline IxDataMember * getDataId() const               { return m_pDataMemberId; }
00115    inline IxDataMember * nextData(long & l) const        { if ((! m_lstDataMemberPtr) || (l < 0) || (l >= m_lstDataMemberPtr->count())) { return NULL; }; ++l; return m_lstDataMemberPtr->getByIndex(l - 1); }
00116    inline IxSqlRelation * nextRelation(long & l) const   { if ((! m_lstSqlRelationPtr) || (l < 0) || (l >= m_lstSqlRelationPtr->count())) { return NULL; }; ++l; return m_lstSqlRelationPtr->getByIndex(l - 1); }
00117 
00118    void initIdX(long lAllRelationCount);
00119    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00120    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00121    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00122    bool getAddAutoIncrementIdToUpdateQuery() const;
00123    void replaceSqlQueryAlias(QString & sql) const;
00124    void displaySqlQuery(int time_ms = -1) const;
00125 
00126    virtual void init() = 0;
00127    virtual IxDataMemberX * getDataMemberX() const = 0;
00128    virtual IxSqlQueryBuilder & count() = 0;
00129    virtual IxSqlQueryBuilder & exist() = 0;
00130    virtual IxSqlQueryBuilder & fetchAll() = 0;
00131    virtual IxSqlQueryBuilder & fetchById() = 0;
00132    virtual IxSqlQueryBuilder & insert() = 0;
00133    virtual IxSqlQueryBuilder & update() = 0;
00134    virtual IxSqlQueryBuilder & deleteAll() = 0;
00135    virtual IxSqlQueryBuilder & softDeleteAll() = 0;
00136    virtual IxSqlQueryBuilder & deleteById() = 0;
00137    virtual IxSqlQueryBuilder & softDeleteById() = 0;
00138    virtual IxSqlQueryBuilder & createTable() = 0;
00139    virtual IxSqlQueryBuilder & fetchAll(const QStringList & columns) = 0;
00140    virtual IxSqlQueryBuilder & fetchById(const QStringList & columns) = 0;
00141    virtual IxSqlQueryBuilder & update(const QStringList & columns) = 0;
00142    virtual IxSqlQueryBuilder & fetchAll_WithRelation(QxSqlRelationLinked * pRelationX) = 0;
00143    virtual IxSqlQueryBuilder & fetchById_WithRelation(QxSqlRelationLinked * pRelationX) = 0;
00144 
00145    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00146 
00147 };
00148 
00149 typedef boost::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00150 
00151 } // namespace qx
00152 
00153 #include <QxDao/IxDao_Helper.h>
00154 
00155 #endif // _IX_SQL_QUERY_BUILDER_H_