QxOrm  1.2.4
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 #include <QxDao/QxSqlRelationLinked.h>
00045 
00046 namespace qx {
00047 
00052 class QX_DLL_EXPORT IxSqlQueryBuilder
00053 {
00054 
00055 public:
00056 
00057    typedef QPair<QString, QString> type_id;
00058    typedef QHash<type_id, void *> type_ptr_by_id;
00059    typedef boost::shared_ptr<type_ptr_by_id> type_ptr_by_id_ptr;
00060    typedef QList<type_ptr_by_id_ptr> type_lst_ptr_by_id;
00061    typedef boost::shared_ptr<type_lst_ptr_by_id> type_lst_ptr_by_id_ptr;
00062 
00063 protected:
00064 
00065    QxCollection<QString, IxDataMember *> * m_lstDataMemberPtr;    
00066    IxSqlRelationX * m_lstSqlRelationPtr;                          
00067    IxDataMember * m_pDataMemberId;                                
00068    QString m_sSqlQuery;                                           
00069    QString m_sTableName;                                          
00070    QString m_sHashRelation;                                       
00071    bool m_bCartesianProduct;                                      
00072    type_lst_ptr_by_id_ptr m_pIdX;                                 
00073    QxSoftDelete m_oSoftDelete;                                    
00074    QHash<QString, QString> m_lstSqlQueryAlias;                    
00075 
00076 public:
00077 
00078    IxSqlQueryBuilder() : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_bCartesianProduct(false) { ; }
00079    IxSqlQueryBuilder(const QString & sql) : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_sSqlQuery(sql), m_bCartesianProduct(false) { ; }
00080    virtual ~IxSqlQueryBuilder() = 0;
00081 
00082    inline QxCollection<QString, IxDataMember *> * getLstDataMember() const    { return m_lstDataMemberPtr; }
00083    inline IxSqlRelationX * getLstRelation() const                             { return m_lstSqlRelationPtr; }
00084 
00085    inline void setSqlQuery(const QString & sql)          { m_sSqlQuery = sql; }
00086    inline void setHashRelation(const QString & s)        { m_sHashRelation = s; }
00087    inline void setCartesianProduct(bool b)               { m_bCartesianProduct = b; }
00088    inline QString getSqlQuery() const                    { return m_sSqlQuery; }
00089    inline QString getHashRelation() const                { return m_sHashRelation; }
00090    inline QString table() const                          { return m_sTableName; }
00091    inline QxSoftDelete getSoftDelete() const             { return m_oSoftDelete; }
00092    inline bool getCartesianProduct() const               { return m_bCartesianProduct; }
00093    inline long getDataCount() const                      { return (m_lstDataMemberPtr ? m_lstDataMemberPtr->count() : 0); }
00094    inline long getRelationCount() const                  { return (m_lstSqlRelationPtr ? m_lstSqlRelationPtr->count() : 0); }
00095    inline IxDataMember * getDataId() const               { return m_pDataMemberId; }
00096    inline IxDataMember * nextData(long & l) const        { if ((! m_lstDataMemberPtr) || (l < 0) || (l >= m_lstDataMemberPtr->count())) { return NULL; }; ++l; return m_lstDataMemberPtr->getByIndex(l - 1); }
00097    inline IxSqlRelation * nextRelation(long & l) const   { if ((! m_lstSqlRelationPtr) || (l < 0) || (l >= m_lstSqlRelationPtr->count())) { return NULL; }; ++l; return m_lstSqlRelationPtr->getByIndex(l - 1); }
00098 
00099    void initIdX(long lAllRelationCount);
00100    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00101    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00102    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00103    void replaceSqlQueryAlias(QString & sql) const;
00104    void displaySqlQuery(int time_ms = -1) const;
00105 
00106    virtual void init() = 0;
00107    virtual IxDataMemberX * getDataMemberX() const = 0;
00108    virtual IxSqlQueryBuilder & count() = 0;
00109    virtual IxSqlQueryBuilder & exist() = 0;
00110    virtual IxSqlQueryBuilder & fetchAll() = 0;
00111    virtual IxSqlQueryBuilder & fetchById() = 0;
00112    virtual IxSqlQueryBuilder & insert() = 0;
00113    virtual IxSqlQueryBuilder & update() = 0;
00114    virtual IxSqlQueryBuilder & deleteAll() = 0;
00115    virtual IxSqlQueryBuilder & softDeleteAll() = 0;
00116    virtual IxSqlQueryBuilder & deleteById() = 0;
00117    virtual IxSqlQueryBuilder & softDeleteById() = 0;
00118    virtual IxSqlQueryBuilder & createTable() = 0;
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    virtual IxSqlQueryBuilder & fetchAll_WithRelation(QxSqlRelationLinked * pRelationX) = 0;
00123    virtual IxSqlQueryBuilder & fetchById_WithRelation(QxSqlRelationLinked * pRelationX) = 0;
00124 
00125    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00126 
00127 };
00128 
00129 typedef boost::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00130 
00131 } // namespace qx
00132 
00133 #endif // _IX_SQL_QUERY_BUILDER_H_