IxSqlQueryBuilder.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_QUERY_BUILDER_H_
  32. #define _IX_SQL_QUERY_BUILDER_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxSqlQueryBuilder.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Common interface to build SQL queries to communicate with database
  41. */
  42. #include <QxDataMember/IxDataMemberX.h>
  43. #include <QxDao/IxSqlRelation.h>
  44. #include <QxDao/QxSoftDelete.h>
  45. #include <QxDao/QxSqlRelationLinked.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. /*!
  55. * \ingroup QxDao
  56. * \brief qx::IxSqlQueryBuilder : common interface to build SQL queries to communicate with database
  57. */
  58. class QX_DLL_EXPORT IxSqlQueryBuilder
  59. {
  60. private:
  61. struct IxSqlQueryBuilderImpl;
  62. std::unique_ptr<IxSqlQueryBuilderImpl> m_pImpl; //!< Private implementation idiom
  63. public:
  64. IxSqlQueryBuilder();
  65. virtual ~IxSqlQueryBuilder() = 0;
  66. IxDataMemberX * getDataMemberX() const;
  67. QxCollection<QString, IxDataMember *> * getLstDataMember() const;
  68. IxSqlRelationX * getLstRelation() const;
  69. qx::dao::detail::IxDao_Helper * getDaoHelper() const;
  70. void setDaoHelper(qx::dao::detail::IxDao_Helper * p);
  71. void setHashRelation(const QString & s);
  72. void setCartesianProduct(bool b);
  73. QString getSqlQuery() const;
  74. QString getHashRelation() const;
  75. QString table() const;
  76. QxSoftDelete getSoftDelete() const;
  77. bool getCartesianProduct() const;
  78. long getDataCount() const;
  79. long getRelationCount() const;
  80. IxDataMember * getDataId() const;
  81. IxDataMember * nextData(long & l) const;
  82. IxSqlRelation * nextRelation(long & l) const;
  83. void initIdX(long lAllRelationCount);
  84. bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
  85. void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
  86. void setSqlQuery(const QString & sql, const QString & key = QString());
  87. void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
  88. bool getAddAutoIncrementIdToUpdateQuery() const;
  89. void replaceSqlQueryAlias(QString & sql) const;
  90. virtual void init();
  91. virtual void clone(const IxSqlQueryBuilder & other);
  92. virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL) = 0;
  93. static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
  94. static void sql_CreateTable(QString & sql, IxSqlQueryBuilder & builder);
  95. static void sql_DeleteById(QString & sql, IxSqlQueryBuilder & builder, bool bSoftDelete);
  96. static void sql_Exist(QString & sql, IxSqlQueryBuilder & builder);
  97. static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder);
  98. static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
  99. static void sql_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
  100. static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder);
  101. static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
  102. static void sql_FetchById_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
  103. static void sql_Insert(QString & sql, IxSqlQueryBuilder & builder);
  104. static void sql_Update(QString & sql, IxSqlQueryBuilder & builder);
  105. static void sql_Update(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
  106. static void sql_Count_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
  107. static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
  108. static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
  109. static void resolveOutput_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
  110. static void resolveInput_Insert(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
  111. static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
  112. static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
  113. protected:
  114. bool verifyColumns(const QStringList & columns) const QX_USED;
  115. bool isInitDone() const;
  116. QxSoftDelete & softDelete();
  117. const QxSoftDelete & softDelete() const;
  118. void setSoftDelete(const QxSoftDelete & o);
  119. void setDataMemberX(IxDataMemberX * p);
  120. bool findSqlQuery(const QString & key);
  121. bool findSqlAlias(const QString & key);
  122. void insertSqlAlias(const QString & key);
  123. };
  124. typedef std::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
  125. } // namespace qx
  126. #include <QxDao/IxDao_Helper.h>
  127. #endif // _IX_SQL_QUERY_BUILDER_H_