QxOrm  1.4.3
C++ Object Relational Mapping library
IxSqlRelation.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_RELATION_H_
00033 #define _IX_SQL_RELATION_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxCommon/QxPropertyBag.h>
00047 
00048 #include <QxDao/QxSqlRelationParams.h>
00049 #include <QxDao/QxSoftDelete.h>
00050 #include <QxDao/QxSqlJoin.h>
00051 
00052 #include <QxCollection/QxCollection.h>
00053 
00054 #define QX_CONSTRUCT_IX_RELATION() \
00055 m_pClass(NULL), m_pClassOwner(NULL), m_pDataMember(p), m_pDataMemberX(NULL), \
00056 m_pDataMemberId(NULL), m_pDataMemberIdOwner(NULL), m_lOffsetRelation(100), \
00057 m_eJoinType(qx::dao::sql_join::left_outer_join), m_eRelationType(no_relation), \
00058 m_bInitInEvent(false), m_bInitDone(false), m_iIsSameDataOwner(0)
00059 
00060 namespace qx {
00061 
00062 class IxClass;
00063 class IxDataMember;
00064 class IxDataMemberX;
00065 class IxSqlRelation;
00066 
00067 typedef QxCollection<QString, IxSqlRelation *> IxSqlRelationX;
00068 
00073 class QX_DLL_EXPORT IxSqlRelation : public qx::QxPropertyBag
00074 {
00075 
00076 public:
00077 
00078    enum relation_type { no_relation, one_to_one, one_to_many, many_to_one, many_to_many };
00079 
00080    typedef QxCollection<QString, IxDataMember *> type_lst_data_member;
00081    typedef qx_shared_ptr<type_lst_data_member> type_lst_data_member_ptr;
00082 
00083 protected:
00084 
00085    IxClass *                        m_pClass;               
00086    IxClass *                        m_pClassOwner;          
00087    IxDataMember *                   m_pDataMember;          
00088    IxDataMemberX *                  m_pDataMemberX;         
00089    IxDataMember *                   m_pDataMemberId;        
00090    IxDataMember *                   m_pDataMemberIdOwner;   
00091    long                             m_lOffsetRelation;      
00092    qx::dao::sql_join::join_type     m_eJoinType;            
00093    relation_type                    m_eRelationType;        
00094    QxSoftDelete                     m_oSoftDelete;          
00095    QString                          m_sForeignKey;          
00096    QString                          m_sExtraTable;          
00097    QString                          m_sForeignKeyOwner;     
00098    QString                          m_sForeignKeyDataType;  
00099    bool                             m_bInitInEvent;         
00100    bool                             m_bInitDone;            
00101    int                              m_iIsSameDataOwner;     
00102 
00103    type_lst_data_member_ptr m_lstDataMemberPtr;             
00104    qx_shared_ptr<IxSqlRelationX> m_lstSqlRelationPtr;       
00105 
00106    static bool m_bTraceRelationInit;   
00107 
00108 public:
00109 
00110    IxSqlRelation(IxDataMember * p) : qx::QxPropertyBag(), QX_CONSTRUCT_IX_RELATION() { ; }
00111    virtual ~IxSqlRelation() = 0;
00112 
00113    inline QxCollection<QString, IxDataMember *> * getLstDataMember() const { return m_lstDataMemberPtr.get(); }
00114    inline IxSqlRelationX * getLstRelation() const                          { return m_lstSqlRelationPtr.get(); }
00115 
00116    inline void setSqlJoinType(qx::dao::sql_join::join_type e)     { m_eJoinType = e; }
00117    inline qx::dao::sql_join::join_type getSqlJoinType() const     { return m_eJoinType; }
00118    inline relation_type getRelationType() const                   { return m_eRelationType; }
00119    inline IxClass * getClass() const                              { return m_pClass; }
00120    inline IxClass * getClassOwner() const                         { return m_pClassOwner; }
00121    inline IxDataMember * getDataMember() const                    { return m_pDataMember; }
00122    inline IxDataMemberX * getDataMemberX() const                  { return m_pDataMemberX; }
00123    inline IxDataMember * getDataId() const                        { return m_pDataMemberId; }
00124    inline IxDataMember * getDataIdOwner() const                   { return m_pDataMemberIdOwner; }
00125 
00126    QString getKey() const;
00127    long getDataCount() const;
00128    long getRelationCount() const;
00129    IxDataMember * getDataByKey(const QString & sKey) const;
00130    IxDataMember * nextData(long & lIndex) const;
00131    IxSqlRelation * nextRelation(long & lIndex) const;
00132    QString table() const;
00133    QString tableAlias(QxSqlRelationParams & params) const;
00134    QString tableAliasOwner(QxSqlRelationParams & params) const;
00135    QString getSqlJoin(qx::dao::sql_join::join_type e = qx::dao::sql_join::no_join) const;
00136    bool traceSqlQuery() const;
00137 
00138    virtual void init();
00139    virtual QString getDescription() const = 0;
00140    virtual QString getExtraTable() const = 0;
00141    virtual QString createExtraTable() const = 0;
00142    virtual bool getCartesianProduct() const = 0;
00143    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const = 0;
00144    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const = 0;
00145    virtual void createTable(QxSqlRelationParams & params) const = 0;
00146    virtual void lazySelect(QxSqlRelationParams & params) const = 0;
00147    virtual void eagerSelect(QxSqlRelationParams & params) const = 0;
00148    virtual void lazyFrom(QxSqlRelationParams & params) const = 0;
00149    virtual void eagerFrom(QxSqlRelationParams & params) const = 0;
00150    virtual void lazyJoin(QxSqlRelationParams & params) const = 0;
00151    virtual void eagerJoin(QxSqlRelationParams & params) const = 0;
00152    virtual void lazyWhere(QxSqlRelationParams & params) const = 0;
00153    virtual void eagerWhere(QxSqlRelationParams & params) const = 0;
00154    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const = 0;
00155    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const = 0;
00156    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
00157    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
00158    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
00159    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
00160    virtual void lazyInsert(QxSqlRelationParams & params) const = 0;
00161    virtual void lazyInsert_Values(QxSqlRelationParams & params) const = 0;
00162    virtual void lazyUpdate(QxSqlRelationParams & params) const = 0;
00163    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const = 0;
00164    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const = 0;
00165    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const = 0;
00166    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const = 0;
00167 
00168    bool verifyOffset(QxSqlRelationParams & params, bool bId) const BOOST_USED;
00169 
00170    static void setTraceRelationInit(bool bTrace);
00171 
00172 protected:
00173 
00174    QVariant getIdFromQuery_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
00175    QVariant getIdFromQuery_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
00176    QVariant getIdFromQuery_OneToMany(bool bEager, QxSqlRelationParams & params) const;
00177    QVariant getIdFromQuery_OneToOne(bool bEager, QxSqlRelationParams & params) const;
00178 
00179    void updateOffset_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
00180    void updateOffset_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
00181    void updateOffset_OneToMany(bool bEager, QxSqlRelationParams & params) const;
00182    void updateOffset_OneToOne(bool bEager, QxSqlRelationParams & params) const;
00183 
00184    void eagerSelect_ManyToMany(QxSqlRelationParams & params) const;
00185    void eagerSelect_ManyToOne(QxSqlRelationParams & params) const;
00186    void eagerSelect_OneToMany(QxSqlRelationParams & params) const;
00187    void eagerSelect_OneToOne(QxSqlRelationParams & params) const;
00188 
00189    void eagerJoin_ManyToMany(QxSqlRelationParams & params) const;
00190    void eagerJoin_ManyToOne(QxSqlRelationParams & params) const;
00191    void eagerJoin_OneToMany(QxSqlRelationParams & params) const;
00192    void eagerJoin_OneToOne(QxSqlRelationParams & params) const;
00193 
00194    void eagerWhereSoftDelete_ManyToMany(QxSqlRelationParams & params) const;
00195    void eagerWhereSoftDelete_ManyToOne(QxSqlRelationParams & params) const;
00196    void eagerWhereSoftDelete_OneToMany(QxSqlRelationParams & params) const;
00197    void eagerWhereSoftDelete_OneToOne(QxSqlRelationParams & params) const;
00198 
00199    void lazySelect_ManyToOne(QxSqlRelationParams & params) const;
00200    void lazyInsert_ManyToOne(QxSqlRelationParams & params) const;
00201    void lazyInsert_Values_ManyToOne(QxSqlRelationParams & params) const;
00202    void lazyUpdate_ManyToOne(QxSqlRelationParams & params) const;
00203 
00204    void createTable_ManyToOne(QxSqlRelationParams & params) const;
00205    QSqlError deleteFromExtraTable_ManyToMany(QxSqlRelationParams & params) const;
00206    QString createExtraTable_ManyToMany() const;
00207 
00208    bool addLazyRelation(QxSqlRelationParams & params, IxSqlRelation * pRelation) const;
00209 
00210 private:
00211 
00212    IxDataMember * isValid_DataMember(long lIndex) const;
00213    IxDataMember * isValid_SqlRelation(long lIndex) const;
00214 
00215 };
00216 
00217 } // namespace qx
00218 
00219 #endif // _IX_SQL_RELATION_H_