QxDao_Count.inl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace qx {
  32. namespace dao {
  33. namespace detail {
  34. template <class T>
  35. struct QxDao_Count
  36. {
  37. static long count(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase)
  38. {
  39. T t; Q_UNUSED(t);
  40. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count", new qx::QxSqlQueryBuilder_Count<T>(), (& query));
  41. if (! dao.isValid()) { return 0; }
  42. #ifdef _QX_ENABLE_MONGODB
  43. if (dao.isMongoDB())
  44. {
  45. long cnt = 0;
  46. qx::dao::mongodb::QxMongoDB_Helper::count((& dao), dao.getDataMemberX()->getClass(), cnt, (& query)); if (! dao.isValid()) { return 0; }
  47. return cnt;
  48. }
  49. #endif // _QX_ENABLE_MONGODB
  50. QString sql = dao.builder().buildSql().getSqlQuery();
  51. if (sql.isEmpty()) { dao.errEmpty(); return 0; }
  52. if (! query.isEmpty()) { dao.addQuery(true); sql = dao.builder().getSqlQuery(); }
  53. if (! dao.exec()) { dao.errFailed(); return 0; }
  54. if (! dao.nextRecord()) { dao.errNoData(); return 0; }
  55. return static_cast<long>(dao.query().value(0).toLongLong());
  56. }
  57. static QSqlError count(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase)
  58. {
  59. T t; Q_UNUSED(t); lCount = 0;
  60. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count", new qx::QxSqlQueryBuilder_Count<T>(), (& query));
  61. if (! dao.isValid()) { return dao.error(); }
  62. #ifdef _QX_ENABLE_MONGODB
  63. if (dao.isMongoDB())
  64. {
  65. qx::dao::mongodb::QxMongoDB_Helper::count((& dao), dao.getDataMemberX()->getClass(), lCount, (& query)); if (! dao.isValid()) { return dao.error(); }
  66. return dao.error();
  67. }
  68. #endif // _QX_ENABLE_MONGODB
  69. QString sql = dao.builder().buildSql().getSqlQuery();
  70. if (sql.isEmpty()) { return dao.errEmpty(); }
  71. if (! query.isEmpty()) { dao.addQuery(true); sql = dao.builder().getSqlQuery(); }
  72. if (! dao.exec()) { return dao.errFailed(); }
  73. if (! dao.nextRecord()) { return dao.errNoData(); }
  74. lCount = static_cast<long>(dao.query().value(0).toLongLong());
  75. return dao.error();
  76. }
  77. };
  78. template <class T>
  79. struct QxDao_Count_WithRelation
  80. {
  81. static QSqlError count(long & lCount, const QStringList & relation, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase)
  82. {
  83. T t; Q_UNUSED(t); lCount = 0;
  84. qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count with relation", new qx::QxSqlQueryBuilder_Count_WithRelation<T>(), (& query));
  85. if (! dao.isValid()) { return dao.error(); }
  86. if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); }
  87. #ifdef _QX_ENABLE_MONGODB
  88. if (dao.isMongoDB())
  89. {
  90. qx::dao::mongodb::QxMongoDB_Helper::count((& dao), dao.getDataMemberX()->getClass(), lCount, (& query)); if (! dao.isValid()) { return dao.error(); }
  91. return dao.error();
  92. }
  93. #endif // _QX_ENABLE_MONGODB
  94. QStringList columns;
  95. QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
  96. if (sql.isEmpty()) { return dao.errEmpty(); }
  97. if (! query.isEmpty()) { dao.addQuery(true); sql = dao.builder().getSqlQuery(); }
  98. if (! dao.exec()) { return dao.errFailed(); }
  99. if (! dao.nextRecord()) { return dao.errNoData(); }
  100. lCount = static_cast<long>(dao.query().value(0).toLongLong());
  101. return dao.error();
  102. }
  103. };
  104. } // namespace detail
  105. } // namespace dao
  106. } // namespace qx