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