QxSqlRelation_OneToMany.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 _QX_SQL_RELATION_ONE_TO_MANY_H_
  32. #define _QX_SQL_RELATION_ONE_TO_MANY_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxSqlRelation_OneToMany.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Manage a relationship one-to-many defined between 2 classes (or between 2 tables in database)
  41. */
  42. #include <QxDao/QxSqlRelation.h>
  43. namespace qx {
  44. /*!
  45. * \ingroup QxDao
  46. * \brief qx::QxSqlRelation_OneToMany<DataType, Owner> : manage a relationship one-to-many defined between 2 classes (or between 2 tables in database)
  47. */
  48. template <class DataType, class Owner>
  49. class QxSqlRelation_OneToMany : public QxSqlRelation<DataType, Owner>
  50. {
  51. private:
  52. typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
  53. typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
  54. typedef typename QxSqlRelation<DataType, Owner>::type_container type_container;
  55. typedef typename QxSqlRelation<DataType, Owner>::type_generic_container type_generic_container;
  56. typedef typename QxSqlRelation<DataType, Owner>::type_item type_item;
  57. typedef typename type_generic_container::type_iterator type_iterator;
  58. typedef typename type_item::type_value type_value;
  59. enum { is_data_container = QxSqlRelation<DataType, Owner>::is_data_container };
  60. public:
  61. QxSqlRelation_OneToMany(IxDataMember * p, const QString & sForeignKey) : QxSqlRelation<DataType, Owner>(p) { this->setRelationType(qx::IxSqlRelation::one_to_many); this->setForeignKey(sForeignKey); qAssert(! this->getForeignKey().isEmpty()); }
  62. virtual ~QxSqlRelation_OneToMany() { static_assert(is_data_container, "is_data_container"); }
  63. virtual QString getDescription() const { return "relation one-to-many"; }
  64. virtual QString createExtraTable() const { return ""; }
  65. virtual bool getCartesianProduct() const { return true; }
  66. virtual void createTable(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  67. virtual void lazySelect(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  68. virtual void lazyFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  69. virtual void eagerFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  70. virtual void lazyJoin(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  71. virtual void lazyWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  72. virtual void eagerWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  73. virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  74. virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  75. virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  76. virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  77. virtual void lazyInsert(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  78. virtual void lazyInsert_Values(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  79. virtual void lazyUpdate(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  80. virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  81. virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); }
  82. virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const { Q_UNUSED(params); return QSqlError(); }
  83. virtual QSqlError onAfterSave(QxSqlRelationParams & params) const
  84. {
  85. if (this->isNullData(params)) { return QSqlError(); }
  86. this->forceParentIdToAllChildren(params);
  87. if (! params.recursiveMode()) { return qx::dao::save(this->getContainer(params), (& params.database())); }
  88. else { return qx::dao::save_with_relation_recursive(this->getContainer(params), params.saveMode(), (& params.database()), (& params)); }
  89. }
  90. virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const
  91. { return this->getIdFromQuery_OneToMany(bEager, params); }
  92. virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
  93. { this->updateOffset_OneToMany(bEager, params); }
  94. virtual void eagerSelect(QxSqlRelationParams & params) const
  95. { this->eagerSelect_OneToMany(params); }
  96. virtual void eagerJoin(QxSqlRelationParams & params) const
  97. { this->eagerJoin_OneToMany(params); }
  98. virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const
  99. { this->eagerWhereSoftDelete_OneToMany(params); }
  100. virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
  101. {
  102. if (! this->verifyOffset(params, true)) { return NULL; }
  103. QSqlQuery & query = params.query();
  104. IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; }
  105. IxDataMember * pForeign = this->getDataByKey(this->getForeignKey()); qAssert(pForeign); if (! pForeign) { return NULL; }
  106. long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); long lOffsetForeign = (pForeign ? pForeign->getNameCount() : 0);
  107. long lOffsetOld = params.offset(); this->updateOffset(true, params);
  108. long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetForeign);
  109. long lRelation = 0; IxSqlRelation * pRelation = NULL;
  110. bool bValidId(false), bValidForeign(false);
  111. for (int i = 0; i < pId->getNameCount(); i++)
  112. { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
  113. for (int i = 0; i < pForeign->getNameCount(); i++)
  114. { QVariant vForeign = query.value(lOffsetOld + lOffsetId + i); bValidForeign = (bValidForeign || qx::trait::is_valid_primary_key(vForeign)); }
  115. if (! bValidId || ! bValidForeign) { return NULL; }
  116. type_item item = this->createItem();
  117. type_data & item_val = item.value_qx();
  118. if (! this->callTriggerBeforeFetch(item_val, params)) { return NULL; }
  119. for (int i = 0; i < pId->getNameCount(); i++)
  120. { QVariant v = query.value(lOffsetOld + i); qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database); }
  121. for (int i = 0; i < pId->getNameCount(); i++)
  122. { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& item_val), v, "", i, qx::cvt::context::e_database); }
  123. for (int i = 0; i < pForeign->getNameCount(); i++)
  124. { QVariant v = query.value(lOffsetOld + lOffsetId + i); pForeign->fromVariant((& item_val), v, "", i, qx::cvt::context::e_database); }
  125. while ((p = this->nextData(lIndex)))
  126. { if ((p != pForeign) && (params.checkColumns(p->getKey()))) { p->fromVariant((& item_val), query.value(lOffsetRelation++), -1, qx::cvt::context::e_database); } }
  127. if (params.relationX())
  128. {
  129. QString sOldCustomAliasOwner = params.getCustomAliasOwner(); params.setCustomAliasOwner(params.getCustomAlias());
  130. long lIndexOwnerOld = params.indexOwner(); params.setIndexOwner(params.index());
  131. void * pOwnerOld = params.owner(); params.setOwner(& item_val);
  132. lOffsetOld = params.offset(); params.setOffset(lOffsetRelation++);
  133. while ((pRelation = this->nextRelation(lRelation)))
  134. { if (this->addLazyRelation(params, pRelation)) { pRelation->lazyFetch_ResolveOutput(params); } }
  135. params.setOwner(pOwnerOld); params.setOffset(lOffsetOld);
  136. params.setCustomAliasOwner(sOldCustomAliasOwner);
  137. params.setIndexOwner(lIndexOwnerOld);
  138. }
  139. if (! this->callTriggerAfterFetch(item_val, params)) { return NULL; }
  140. type_value * pValue = type_generic_container::insertItem(this->getContainer(params), item);
  141. if (! type_item::is_value_pointer && pValue) { return pValue; }
  142. return (& item_val);
  143. }
  144. private:
  145. void forceParentIdToAllChildren(QxSqlRelationParams & params) const
  146. {
  147. bool bForce = qx::QxSqlDatabase::getSingleton()->getForceParentIdToAllChildren();
  148. if (! bForce || ! params.owner()) { return; }
  149. IxDataMember * pIdOwner = this->getDataIdOwner(); if (! pIdOwner) { return; }
  150. IxDataMember * pForeign = this->getDataByKey(this->getForeignKey()); if (! pForeign) { return; }
  151. if (pIdOwner->getNameCount() != pForeign->getNameCount()) { return; }
  152. QList<QVariant> vIdOwner;
  153. for (int i = 0; i < pIdOwner->getNameCount(); i++)
  154. { vIdOwner.append(pIdOwner->toVariant(params.owner(), i, qx::cvt::context::e_database)); }
  155. type_item item;
  156. type_container & container = this->getContainer(params);
  157. type_iterator itr = type_generic_container::begin(container, item);
  158. type_iterator itr_end = type_generic_container::end(container);
  159. while (itr != itr_end)
  160. {
  161. type_data & item_val = item.value_qx();
  162. for (int i = 0; i < vIdOwner.count(); i++)
  163. { pForeign->fromVariant((& item_val), vIdOwner.at(i), "", i, qx::cvt::context::e_database); }
  164. itr = type_generic_container::next(container, itr, item);
  165. }
  166. }
  167. };
  168. } // namespace qx
  169. #endif // _QX_SQL_RELATION_ONE_TO_MANY_H_