IxSqlRelation.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _IX_SQL_RELATION_H_
  32. #define _IX_SQL_RELATION_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxSqlRelation.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Common interface for all relationships defined between 2 classes (or between 2 tables in database)
  41. */
  42. #include <QxCommon/QxPropertyBag.h>
  43. #include <QxDao/QxSqlRelationParams.h>
  44. #include <QxDao/QxSoftDelete.h>
  45. #include <QxDao/QxSqlJoin.h>
  46. #include <QxCollection/QxCollection.h>
  47. namespace qx {
  48. class IxClass;
  49. class IxDataMember;
  50. class IxDataMemberX;
  51. class IxSqlRelation;
  52. typedef QxCollection<QString, IxSqlRelation *> IxSqlRelationX;
  53. typedef std::shared_ptr<IxSqlRelationX> IxSqlRelationX_ptr;
  54. /*!
  55. * \ingroup QxDao
  56. * \brief qx::IxSqlRelation : common interface for all relationships defined between 2 classes (or between 2 tables in database)
  57. */
  58. class QX_DLL_EXPORT IxSqlRelation : public qx::QxPropertyBag
  59. {
  60. private:
  61. struct IxSqlRelationImpl;
  62. std::unique_ptr<IxSqlRelationImpl> m_pImpl; //!< Private implementation idiom
  63. public:
  64. enum relation_type { no_relation, one_to_one, one_to_many, many_to_one, many_to_many };
  65. IxSqlRelation(IxDataMember * p);
  66. virtual ~IxSqlRelation() = 0;
  67. QxCollection<QString, IxDataMember *> * getLstDataMember() const;
  68. IxSqlRelationX * getLstRelation() const;
  69. void setSqlJoinType(qx::dao::sql_join::join_type e);
  70. qx::dao::sql_join::join_type getSqlJoinType() const;
  71. relation_type getRelationType() const;
  72. IxClass * getClass() const;
  73. IxClass * getClassOwner() const;
  74. IxDataMember * getDataMember() const;
  75. IxDataMemberX * getDataMemberX() const;
  76. IxDataMember * getDataId() const;
  77. IxDataMember * getDataIdOwner() const;
  78. QString getKey() const;
  79. QString getForeignKey() const;
  80. QString getForeignKeyOwner() const;
  81. QString getForeignKeyDataType() const;
  82. QString getExtraTable() const;
  83. long getDataCount() const;
  84. long getRelationCount() const;
  85. IxDataMember * getDataByKey(const QString & sKey) const;
  86. IxDataMember * nextData(long & lIndex) const;
  87. IxSqlRelation * nextRelation(long & lIndex) const;
  88. QString table() const;
  89. QString tableAlias(QxSqlRelationParams & params) const;
  90. QString tableAliasOwner(QxSqlRelationParams & params) const;
  91. QString getSqlJoin(qx::dao::sql_join::join_type e = qx::dao::sql_join::no_join) const;
  92. bool traceSqlQuery() const;
  93. virtual void init();
  94. virtual QString getDescription() const = 0;
  95. virtual QString createExtraTable() const = 0;
  96. virtual bool getCartesianProduct() const = 0;
  97. virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const = 0;
  98. virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const = 0;
  99. virtual void createTable(QxSqlRelationParams & params) const = 0;
  100. virtual void lazySelect(QxSqlRelationParams & params) const = 0;
  101. virtual void eagerSelect(QxSqlRelationParams & params) const = 0;
  102. virtual void lazyFrom(QxSqlRelationParams & params) const = 0;
  103. virtual void eagerFrom(QxSqlRelationParams & params) const = 0;
  104. virtual void lazyJoin(QxSqlRelationParams & params) const = 0;
  105. virtual void eagerJoin(QxSqlRelationParams & params) const = 0;
  106. virtual void lazyWhere(QxSqlRelationParams & params) const = 0;
  107. virtual void eagerWhere(QxSqlRelationParams & params) const = 0;
  108. virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const = 0;
  109. virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const = 0;
  110. virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
  111. virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const = 0;
  112. virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
  113. virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const = 0;
  114. virtual void lazyInsert(QxSqlRelationParams & params) const = 0;
  115. virtual void lazyInsert_Values(QxSqlRelationParams & params) const = 0;
  116. virtual void lazyUpdate(QxSqlRelationParams & params) const = 0;
  117. virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const = 0;
  118. virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const = 0;
  119. virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const = 0;
  120. virtual QSqlError onAfterSave(QxSqlRelationParams & params) const = 0;
  121. bool verifyOffset(QxSqlRelationParams & params, bool bId) const QX_USED;
  122. static void setTraceRelationInit(bool bTrace);
  123. protected:
  124. QVariant getIdFromQuery_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
  125. QVariant getIdFromQuery_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
  126. QVariant getIdFromQuery_OneToMany(bool bEager, QxSqlRelationParams & params) const;
  127. QVariant getIdFromQuery_OneToOne(bool bEager, QxSqlRelationParams & params) const;
  128. void updateOffset_ManyToMany(bool bEager, QxSqlRelationParams & params) const;
  129. void updateOffset_ManyToOne(bool bEager, QxSqlRelationParams & params) const;
  130. void updateOffset_OneToMany(bool bEager, QxSqlRelationParams & params) const;
  131. void updateOffset_OneToOne(bool bEager, QxSqlRelationParams & params) const;
  132. void eagerSelect_ManyToMany(QxSqlRelationParams & params) const;
  133. void eagerSelect_ManyToOne(QxSqlRelationParams & params) const;
  134. void eagerSelect_OneToMany(QxSqlRelationParams & params) const;
  135. void eagerSelect_OneToOne(QxSqlRelationParams & params) const;
  136. void eagerJoin_ManyToMany(QxSqlRelationParams & params) const;
  137. void eagerJoin_ManyToOne(QxSqlRelationParams & params) const;
  138. void eagerJoin_OneToMany(QxSqlRelationParams & params) const;
  139. void eagerJoin_OneToOne(QxSqlRelationParams & params) const;
  140. void eagerWhereSoftDelete_ManyToMany(QxSqlRelationParams & params) const;
  141. void eagerWhereSoftDelete_ManyToOne(QxSqlRelationParams & params) const;
  142. void eagerWhereSoftDelete_OneToMany(QxSqlRelationParams & params) const;
  143. void eagerWhereSoftDelete_OneToOne(QxSqlRelationParams & params) const;
  144. void lazySelect_ManyToOne(QxSqlRelationParams & params) const;
  145. void lazyInsert_ManyToOne(QxSqlRelationParams & params) const;
  146. void lazyInsert_Values_ManyToOne(QxSqlRelationParams & params) const;
  147. void lazyUpdate_ManyToOne(QxSqlRelationParams & params) const;
  148. void createTable_ManyToOne(QxSqlRelationParams & params) const;
  149. QSqlError deleteFromExtraTable_ManyToMany(QxSqlRelationParams & params) const;
  150. QString createExtraTable_ManyToMany() const;
  151. bool addLazyRelation(QxSqlRelationParams & params, IxSqlRelation * pRelation) const;
  152. bool canInit() const;
  153. void setIsSameDataOwner(int i);
  154. void setClass(IxClass * pClass, IxClass * pClassOwner);
  155. void setRelationType(relation_type e);
  156. void setForeignKey(const QString & s) const;
  157. void setForeignKeyOwner(const QString & s) const;
  158. void setForeignKeyDataType(const QString & s) const;
  159. void setExtraTable(const QString & s) const;
  160. };
  161. } // namespace qx
  162. #endif // _IX_SQL_RELATION_H_