resultset_view.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_RESULTSET_VIEW_HPP
  8. #define BOOST_MYSQL_RESULTSET_VIEW_HPP
  9. #include <boost/mysql/metadata_collection_view.hpp>
  10. #include <boost/mysql/rows_view.hpp>
  11. #include <boost/mysql/detail/access.hpp>
  12. #include <boost/mysql/detail/execution_processor/results_impl.hpp>
  13. #include <boost/assert.hpp>
  14. namespace boost {
  15. namespace mysql {
  16. /**
  17. * \brief A non-owning reference to a resultset.
  18. * \details
  19. * A `resultset_view` points to memory owned by an external object, usually a \ref results.
  20. * The view and any other reference type obtained from it are valid as long as the
  21. * object they point to is alive.
  22. */
  23. class resultset_view
  24. {
  25. public:
  26. /**
  27. * \brief Constructs a view with `this->has_value() == false`.
  28. * \par Exception safety
  29. * No-throw guarantee.
  30. */
  31. resultset_view() = default;
  32. /**
  33. * \brief Returns whether this is a null view or not.
  34. * \details
  35. * Only returns true for default-constructed views.
  36. *
  37. * \par Exception safety
  38. * No-throw guarantee.
  39. *
  40. * \par Complexity
  41. * Constant.
  42. */
  43. bool has_value() const noexcept { return impl_ != nullptr; }
  44. /**
  45. * \brief Returns the rows for this resultset.
  46. * \par Preconditions
  47. * `this->has_value() == true`
  48. *
  49. * \par Exception safety
  50. * No-throw guarantee.
  51. *
  52. * \par Object lifetimes
  53. * The returned reference and any other references obtained from it are valid as long as
  54. * the object that `*this` points to is alive.
  55. *
  56. * \par Complexity
  57. * Constant.
  58. */
  59. rows_view rows() const noexcept
  60. {
  61. BOOST_ASSERT(has_value());
  62. return impl_->get_rows(index_);
  63. }
  64. /**
  65. * \brief Returns metadata for this resultset.
  66. * \par Preconditions
  67. * `this->has_value() == true`
  68. *
  69. * \par Exception safety
  70. * No-throw guarantee.
  71. *
  72. * \par Object lifetimes
  73. * The returned reference and any other references obtained from it are valid as long as
  74. * the object that `*this` points to is alive.
  75. *
  76. * \par Complexity
  77. * Constant.
  78. */
  79. metadata_collection_view meta() const noexcept
  80. {
  81. BOOST_ASSERT(has_value());
  82. return impl_->get_meta(index_);
  83. }
  84. /**
  85. * \brief Returns the number of affected rows for this resultset.
  86. * \par Preconditions
  87. * `this->has_value() == true`
  88. *
  89. * \par Exception safety
  90. * No-throw guarantee.
  91. *
  92. * \par Complexity
  93. * Constant.
  94. */
  95. std::uint64_t affected_rows() const noexcept
  96. {
  97. BOOST_ASSERT(has_value());
  98. return impl_->get_affected_rows(index_);
  99. }
  100. /**
  101. * \brief Returns the last insert ID for this resultset.
  102. * \par Preconditions
  103. * `this->has_value() == true`
  104. *
  105. * \par Exception safety
  106. * No-throw guarantee.
  107. *
  108. * \par Complexity
  109. * Constant.
  110. */
  111. std::uint64_t last_insert_id() const noexcept
  112. {
  113. BOOST_ASSERT(has_value());
  114. return impl_->get_last_insert_id(index_);
  115. }
  116. /**
  117. * \brief Returns the number of warnings for this resultset.
  118. * \par Preconditions
  119. * `this->has_value() == true`
  120. *
  121. * \par Exception safety
  122. * No-throw guarantee.
  123. *
  124. * \par Complexity
  125. * Constant.
  126. */
  127. unsigned warning_count() const noexcept
  128. {
  129. BOOST_ASSERT(has_value());
  130. return impl_->get_warning_count(index_);
  131. }
  132. /**
  133. * \brief Returns additional information for this resultset.
  134. * \details
  135. * The format of this information is documented by MySQL <a
  136. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  137. * \n
  138. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  139. *
  140. * \par Preconditions
  141. * `this->has_value() == true`
  142. *
  143. * \par Exception safety
  144. * No-throw guarantee.
  145. *
  146. * \par Object lifetimes
  147. * The returned reference and any other references obtained from it are valid as long as
  148. * the object that `*this` points to is alive.
  149. *
  150. * \par Complexity
  151. * Constant.
  152. */
  153. string_view info() const noexcept
  154. {
  155. BOOST_ASSERT(has_value());
  156. return impl_->get_info(index_);
  157. }
  158. /**
  159. * \brief Returns whether this resultset represents a procedure OUT params.
  160. * \par Preconditions
  161. * `this->has_value() == true`
  162. *
  163. * \par Exception safety
  164. * No-throw guarantee.
  165. *
  166. * \par Complexity
  167. * Constant.
  168. */
  169. bool is_out_params() const noexcept
  170. {
  171. BOOST_ASSERT(has_value());
  172. return impl_->get_is_out_params(index_);
  173. }
  174. #ifndef BOOST_MYSQL_DOXYGEN
  175. const resultset_view* operator->() const noexcept { return this; }
  176. #endif
  177. private:
  178. const detail::results_impl* impl_{};
  179. std::size_t index_{};
  180. resultset_view(const detail::results_impl& impl, std::size_t index) noexcept : impl_(&impl), index_(index)
  181. {
  182. }
  183. #ifndef BOOST_MYSQL_DOXYGEN
  184. friend struct detail::access;
  185. #endif
  186. };
  187. } // namespace mysql
  188. } // namespace boost
  189. #endif