QxSqlRelationLinked.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_LINKED_H_
  32. #define _QX_SQL_RELATION_LINKED_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxSqlRelationLinked.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Hierarchy of relationships to build SQL query
  41. */
  42. #include <QtSql/qsqlerror.h>
  43. #include <QxCommon/QxBool.h>
  44. #include <QxDao/IxSqlRelation.h>
  45. #include <QxCollection/QxCollection.h>
  46. namespace qx {
  47. namespace dao {
  48. namespace detail {
  49. class IxDao_Helper;
  50. } // namespace detail
  51. } // namespace dao
  52. } // namespace qx
  53. namespace qx {
  54. class IxClass;
  55. /*!
  56. * \ingroup QxDao
  57. * \brief qx::QxSqlRelationLinked : hierarchy of relationships to build SQL query
  58. *
  59. * Here is the structure, each real relation has a relation linked associated to build the hierarchy, like this :
  60. * \code
  61. (<root>, <relation_linked>)
  62. ("blog", blog_relation)
  63. ("blog", <relation_linked>)
  64. ("author", author_relation)
  65. ("author", <relation_linked>)
  66. ("list_blog", list_blog_relation)
  67. ("list_blog", <relation_linked>)
  68. (etc...)
  69. ("comment", comment_relation)
  70. ("comment", <relation_linked>)
  71. ("blog_id", blog_id_relation)
  72. ("blog_id", <relation_linked>)
  73. (etc...)
  74. ("category", category_relation)
  75. ("category", <relation_linked>)
  76. ("list_blog", list_blog_relation)
  77. ("list_blog", <relation_linked>)
  78. (etc...)
  79. * \endcode
  80. */
  81. class QX_DLL_EXPORT QxSqlRelationLinked
  82. {
  83. public:
  84. typedef std::shared_ptr<QxSqlRelationLinked> type_ptr;
  85. typedef std::tuple<qx::dao::sql_join::join_type, IxSqlRelation *, QPair<QSet<QString>, long>, QString> type_relation;
  86. typedef qx::QxCollection<QString, type_relation> type_lst_relation;
  87. typedef QHash<QString, type_ptr> type_lst_relation_linked;
  88. private:
  89. struct QxSqlRelationLinkedImpl;
  90. std::unique_ptr<QxSqlRelationLinkedImpl> m_pImpl; //!< Private implementation idiom
  91. public:
  92. QxSqlRelationLinked();
  93. QxSqlRelationLinked(bool bRoot);
  94. virtual ~QxSqlRelationLinked();
  95. static type_ptr getHierarchy(IxClass * pClass, const QStringList & sRelationX, qx_bool & bOk, qx::dao::detail::IxDao_Helper * pDaoHelper = NULL);
  96. void hierarchySelect(QxSqlRelationParams & params);
  97. void hierarchyFrom(QxSqlRelationParams & params);
  98. void hierarchyJoin(QxSqlRelationParams & params);
  99. void hierarchyWhereSoftDelete(QxSqlRelationParams & params);
  100. void hierarchyResolveOutput(QxSqlRelationParams & params);
  101. QSqlError hierarchyOnBeforeSave(QxSqlRelationParams & params);
  102. QSqlError hierarchyOnAfterSave(QxSqlRelationParams & params);
  103. void updateOffset(QxSqlRelationParams & params);
  104. bool getCartesianProduct() const;
  105. long getAllRelationCount() const;
  106. long getRelationCount() const;
  107. bool existRelation(const QString & sKey) const;
  108. type_lst_relation_linked getRelationLinkedX() const;
  109. type_lst_relation getRelationX() const;
  110. bool isRoot() const;
  111. bool checkRootColumns(const QString & s) const;
  112. long getRootColumnsCount() const;
  113. long getRootColumnsOffset() const;
  114. void setRootColumnsOffset(long l);
  115. QString getRootCustomAlias() const;
  116. protected:
  117. bool isValidDaoHelper(QxSqlRelationParams & params) const;
  118. };
  119. typedef std::shared_ptr<QxSqlRelationLinked> QxSqlRelationLinked_ptr;
  120. } // namespace qx
  121. #endif // _QX_SQL_RELATION_LINKED_H_