QxOrm  1.2.3
C++ Object Relational Mapping library
IxSqlQueryBuilder.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _IX_SQL_QUERY_BUILDER_H_
00027 #define _IX_SQL_QUERY_BUILDER_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <QxDataMember/IxDataMemberX.h>
00041 
00042 #include <QxDao/IxSqlRelation.h>
00043 #include <QxDao/QxSoftDelete.h>
00044 
00045 namespace qx {
00046 
00051 class QX_DLL_EXPORT IxSqlQueryBuilder
00052 {
00053 
00054 public:
00055 
00056    typedef QPair<QString, QString> type_id;
00057    typedef QHash<type_id, void *> type_ptr_by_id;
00058    typedef boost::shared_ptr<type_ptr_by_id> type_ptr_by_id_ptr;
00059    typedef QList<type_ptr_by_id_ptr> type_lst_ptr_by_id;
00060    typedef boost::shared_ptr<type_lst_ptr_by_id> type_lst_ptr_by_id_ptr;
00061 
00062 protected:
00063 
00064    QxCollection<QString, IxDataMember *> * m_lstDataMemberPtr;    
00065    IxSqlRelationX * m_lstSqlRelationPtr;                          
00066    IxDataMember * m_pDataMemberId;                                
00067    QString m_sSqlQuery;                                           
00068    QString m_sTableName;                                          
00069    QString m_sHashRelation;                                       
00070    bool m_bCartesianProduct;                                      
00071    type_lst_ptr_by_id_ptr m_pIdX;                                 
00072    QxSoftDelete m_oSoftDelete;                                    
00073    QHash<QString, QString> m_lstSqlQueryAlias;                    
00074 
00075 public:
00076 
00077    IxSqlQueryBuilder() : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_bCartesianProduct(false) { ; }
00078    IxSqlQueryBuilder(const QString & sql) : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_sSqlQuery(sql), m_bCartesianProduct(false) { ; }
00079    virtual ~IxSqlQueryBuilder() = 0;
00080 
00081    inline QxCollection<QString, IxDataMember *> * getLstDataMember() const    { return m_lstDataMemberPtr; }
00082    inline IxSqlRelationX * getLstRelation() const                             { return m_lstSqlRelationPtr; }
00083 
00084    inline void setSqlQuery(const QString & sql)          { m_sSqlQuery = sql; }
00085    inline void setHashRelation(const QString & s)        { m_sHashRelation = s; }
00086    inline void setCartesianProduct(bool b)               { m_bCartesianProduct = b; }
00087    inline QString getSqlQuery() const                    { return m_sSqlQuery; }
00088    inline QString getHashRelation() const                { return m_sHashRelation; }
00089    inline QString table() const                          { return m_sTableName; }
00090    inline QxSoftDelete getSoftDelete() const             { return m_oSoftDelete; }
00091    inline bool getCartesianProduct() const               { return m_bCartesianProduct; }
00092    inline long getDataCount() const                      { return (m_lstDataMemberPtr ? m_lstDataMemberPtr->count() : 0); }
00093    inline long getRelationCount() const                  { return (m_lstSqlRelationPtr ? m_lstSqlRelationPtr->count() : 0); }
00094    inline IxDataMember * getDataId() const               { return m_pDataMemberId; }
00095    inline IxDataMember * nextData(long & l) const        { if ((! m_lstDataMemberPtr) || (l < 0) || (l >= m_lstDataMemberPtr->count())) { return NULL; }; ++l; return m_lstDataMemberPtr->getByIndex(l - 1); }
00096    inline IxSqlRelation * nextRelation(long & l) const   { if ((! m_lstSqlRelationPtr) || (l < 0) || (l >= m_lstSqlRelationPtr->count())) { return NULL; }; ++l; return m_lstSqlRelationPtr->getByIndex(l - 1); }
00097 
00098    void displaySqlQuery(int time_ms = -1) const;
00099    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00100    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00101    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00102    void replaceSqlQueryAlias(QString & sql) const;
00103 
00104    virtual void init() = 0;
00105    virtual IxDataMemberX * getDataMemberX() const = 0;
00106 
00107    virtual IxSqlQueryBuilder & count() = 0;
00108    virtual IxSqlQueryBuilder & exist() = 0;
00109    virtual IxSqlQueryBuilder & fetchAll() = 0;
00110    virtual IxSqlQueryBuilder & fetchById() = 0;
00111    virtual IxSqlQueryBuilder & insert() = 0;
00112    virtual IxSqlQueryBuilder & update() = 0;
00113    virtual IxSqlQueryBuilder & deleteAll() = 0;
00114    virtual IxSqlQueryBuilder & softDeleteAll() = 0;
00115    virtual IxSqlQueryBuilder & deleteById() = 0;
00116    virtual IxSqlQueryBuilder & softDeleteById() = 0;
00117    virtual IxSqlQueryBuilder & createTable() = 0;
00118 
00119    virtual IxSqlQueryBuilder & fetchAll(const QStringList & columns) = 0;
00120    virtual IxSqlQueryBuilder & fetchById(const QStringList & columns) = 0;
00121    virtual IxSqlQueryBuilder & update(const QStringList & columns) = 0;
00122 
00123    virtual IxSqlQueryBuilder & fetchAll_WithRelation(IxSqlRelationX * pRelationX) = 0;
00124    virtual IxSqlQueryBuilder & fetchById_WithRelation(IxSqlRelationX * pRelationX) = 0;
00125 
00126    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00127 
00128 private:
00129 
00130    void initIdX();
00131 
00132 };
00133 
00134 typedef boost::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00135 
00136 } // namespace qx
00137 
00138 #endif // _IX_SQL_QUERY_BUILDER_H_