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