QxOrm  1.4.4
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 namespace qx {
00055 
00056 class IxClass;
00057 class IxDataMember;
00058 class IxDataMemberX;
00059 class IxSqlRelation;
00060 
00061 typedef QxCollection<QString, IxSqlRelation *> IxSqlRelationX;
00062 
00067 class QX_DLL_EXPORT IxSqlRelation : public qx::QxPropertyBag
00068 {
00069 
00070 private:
00071 
00072    struct IxSqlRelationImpl;
00073    std::unique_ptr<IxSqlRelationImpl> m_pImpl; 
00074 
00075 public:
00076 
00077    enum relation_type { no_relation, one_to_one, one_to_many, many_to_one, many_to_many };
00078 
00079    IxSqlRelation(IxDataMember * p);
00080    virtual ~IxSqlRelation() = 0;
00081 
00082    QxCollection<QString, IxDataMember *> * getLstDataMember() const;
00083    IxSqlRelationX * getLstRelation() const;
00084 
00085    void setSqlJoinType(qx::dao::sql_join::join_type e);
00086    qx::dao::sql_join::join_type getSqlJoinType() const;
00087    relation_type getRelationType() const;
00088    IxClass * getClass() const;
00089    IxClass * getClassOwner() const;
00090    IxDataMember * getDataMember() const;
00091    IxDataMemberX * getDataMemberX() const;
00092    IxDataMember * getDataId() const;
00093    IxDataMember * getDataIdOwner() const;
00094 
00095    QString getKey() const;
00096    QString getForeignKey() const;
00097    QString getForeignKeyOwner() const;
00098    QString getForeignKeyDataType() const;
00099    QString getExtraTable() const;
00100    long getDataCount() const;
00101    long getRelationCount() const;
00102    IxDataMember * getDataByKey(const QString & sKey) const;
00103    IxDataMember * nextData(long & lIndex) const;
00104    IxSqlRelation * nextRelation(long & lIndex) const;
00105    QString table() const;
00106    QString tableAlias(QxSqlRelationParams & params) const;
00107    QString tableAliasOwner(QxSqlRelationParams & params) const;
00108    QString getSqlJoin(qx::dao::sql_join::join_type e = qx::dao::sql_join::no_join) const;
00109    bool traceSqlQuery() const;
00110 
00111    virtual void init();
00112    virtual QString getDescription() const = 0;
00113    virtual QString createExtraTable() const = 0;
00114    virtual bool getCartesianProduct() const = 0;
00115    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const = 0;
00116    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const = 0;
00117    virtual void createTable(QxSqlRelationParams & params) const = 0;
00118    virtual void lazySelect(QxSqlRelationParams & params) const = 0;
00119    virtual void eagerSelect(QxSqlRelationParams & params) const = 0;
00120    virtual void lazyFrom(QxSqlRelationParams & params) const = 0;
00121    virtual void eagerFrom(QxSqlRelationParams & params) const = 0;
00122    virtual void lazyJoin(QxSqlRelationParams & params) const = 0;
00123    virtual void eagerJoin(QxSqlRelationParams & params) const = 0;
00124    virtual void lazyWhere(QxSqlRelationParams & params) const = 0;
00125    virtual void eagerWhere(QxSqlRelationParams & params) const = 0;
00126    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const = 0;
00127    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const = 0;
00128    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
00129    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
00130    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
00131    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
00132    virtual void lazyInsert(QxSqlRelationParams & params) const = 0;
00133    virtual void lazyInsert_Values(QxSqlRelationParams & params) const = 0;
00134    virtual void lazyUpdate(QxSqlRelationParams & params) const = 0;
00135    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const = 0;
00136    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const = 0;
00137    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const = 0;
00138    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const = 0;
00139 
00140    bool verifyOffset(QxSqlRelationParams & params, bool bId) const QX_USED;
00141 
00142    static void setTraceRelationInit(bool bTrace);
00143 
00144 protected:
00145 
00146    QVariant getIdFromQuery_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
00147    QVariant getIdFromQuery_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
00148    QVariant getIdFromQuery_OneToMany(bool bEager, QxSqlRelationParams & params) const;
00149    QVariant getIdFromQuery_OneToOne(bool bEager, QxSqlRelationParams & params) const;
00150 
00151    void updateOffset_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
00152    void updateOffset_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
00153    void updateOffset_OneToMany(bool bEager, QxSqlRelationParams & params) const;
00154    void updateOffset_OneToOne(bool bEager, QxSqlRelationParams & params) const;
00155 
00156    void eagerSelect_ManyToMany(QxSqlRelationParams & params) const;
00157    void eagerSelect_ManyToOne(QxSqlRelationParams & params) const;
00158    void eagerSelect_OneToMany(QxSqlRelationParams & params) const;
00159    void eagerSelect_OneToOne(QxSqlRelationParams & params) const;
00160 
00161    void eagerJoin_ManyToMany(QxSqlRelationParams & params) const;
00162    void eagerJoin_ManyToOne(QxSqlRelationParams & params) const;
00163    void eagerJoin_OneToMany(QxSqlRelationParams & params) const;
00164    void eagerJoin_OneToOne(QxSqlRelationParams & params) const;
00165 
00166    void eagerWhereSoftDelete_ManyToMany(QxSqlRelationParams & params) const;
00167    void eagerWhereSoftDelete_ManyToOne(QxSqlRelationParams & params) const;
00168    void eagerWhereSoftDelete_OneToMany(QxSqlRelationParams & params) const;
00169    void eagerWhereSoftDelete_OneToOne(QxSqlRelationParams & params) const;
00170 
00171    void lazySelect_ManyToOne(QxSqlRelationParams & params) const;
00172    void lazyInsert_ManyToOne(QxSqlRelationParams & params) const;
00173    void lazyInsert_Values_ManyToOne(QxSqlRelationParams & params) const;
00174    void lazyUpdate_ManyToOne(QxSqlRelationParams & params) const;
00175 
00176    void createTable_ManyToOne(QxSqlRelationParams & params) const;
00177    QSqlError deleteFromExtraTable_ManyToMany(QxSqlRelationParams & params) const;
00178    QString createExtraTable_ManyToMany() const;
00179 
00180    bool addLazyRelation(QxSqlRelationParams & params, IxSqlRelation * pRelation) const;
00181 
00182    bool canInit() const;
00183    void setIsSameDataOwner(int i);
00184    void setClass(IxClass * pClass, IxClass * pClassOwner);
00185    void setRelationType(relation_type e);
00186    void setForeignKey(const QString & s) const;
00187    void setForeignKeyOwner(const QString & s) const;
00188    void setForeignKeyDataType(const QString & s) const;
00189    void setExtraTable(const QString & s) const;
00190 
00191 };
00192 
00193 } // namespace qx
00194 
00195 #endif // _IX_SQL_RELATION_H_