results.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (c) 2019-2023 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_RESULTS_HPP
  8. #define BOOST_MYSQL_RESULTS_HPP
  9. #include <boost/mysql/execution_state.hpp>
  10. #include <boost/mysql/metadata_collection_view.hpp>
  11. #include <boost/mysql/rows.hpp>
  12. #include <boost/mysql/rows_view.hpp>
  13. #include <boost/mysql/string_view.hpp>
  14. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  15. #include <cassert>
  16. namespace boost {
  17. namespace mysql {
  18. /**
  19. * \brief Holds the results of a SQL query.
  20. * \details The results (rows, metadata and additional info) are held in-memory.
  21. * \n
  22. * \par Thread safety
  23. * Distinct objects: safe. \n
  24. * Shared objects: unsafe. \n
  25. */
  26. class results
  27. {
  28. public:
  29. /**
  30. * \brief Default constructor.
  31. * \details Constructs an empty results object, with `this->has_value() == false`.
  32. *
  33. * \par Exception safety
  34. * No-throw guarantee.
  35. */
  36. results() = default;
  37. /**
  38. * \brief Copy constructor.
  39. * \par Exception safety
  40. * Strong guarantee. Internal allocations may throw.
  41. */
  42. results(const results& other) = default;
  43. /**
  44. * \brief Move constructor.
  45. * \par Exception safety
  46. * No-throw guarantee.
  47. *
  48. * \par Object lifetimes
  49. * View objects obtained from `other` remain valid.
  50. */
  51. results(results&& other) = default;
  52. /**
  53. * \brief Copy assignment.
  54. * \par Exception safety
  55. * Basic guarantee. Internal allocations may throw.
  56. *
  57. * \par Object lifetimes
  58. * View objects referencing `*this` are invalidated.
  59. */
  60. results& operator=(const results& other) = default;
  61. /**
  62. * \brief Move assignment.
  63. * \par Exception safety
  64. * Basic guarantee. Internal allocations may throw.
  65. *
  66. * \par Object lifetimes
  67. * View objects referencing `other` remain valid. View objects
  68. * referencing `*this` are invalidated.
  69. */
  70. results& operator=(results&& other) = default;
  71. /// Destructor
  72. ~results() = default;
  73. /**
  74. * \brief Returns whether the object holds a valid result.
  75. * \details Having `this->has_value()` is a precondition to call all data accessors.
  76. * Objects populated by \ref connection::query, \ref connection::execute_statement or their async
  77. * counterparts are guaranteed to have `this->has_value() == true`.
  78. *
  79. * \par Exception safety
  80. * No-throw guarantee.
  81. */
  82. bool has_value() const noexcept { return st_.complete(); }
  83. /**
  84. * \brief Returns the rows retrieved by the SQL query.
  85. * \par Preconditions
  86. * `this->has_value() == true`
  87. *
  88. * \par Exception safety
  89. * No-throw guarantee.
  90. *
  91. * \par Object lifetimes
  92. * This function returns a view object, with reference semantics. The returned view points into
  93. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  94. * from `*this` are alive.
  95. */
  96. rows_view rows() const noexcept
  97. {
  98. assert(has_value());
  99. return rows_;
  100. }
  101. /**
  102. * \brief Returns metadata about the columns in the query.
  103. * \details
  104. * The returned collection will have as many \ref metadata objects as columns retrieved by
  105. * the SQL query, and in the same order.
  106. *
  107. * \par Preconditions
  108. * `this->has_value() == true`
  109. *
  110. * \par Exception safety
  111. * No-throw guarantee.
  112. *
  113. * \par Object lifetimes
  114. * This function returns a view object, with reference semantics. The returned view points into
  115. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  116. * from `*this` are alive.
  117. */
  118. metadata_collection_view meta() const noexcept
  119. {
  120. assert(has_value());
  121. return st_.meta();
  122. }
  123. /**
  124. * \brief Returns the number of rows affected by the executed SQL statement.
  125. * \par Preconditions
  126. * `this->has_value() == true`
  127. *
  128. * \par Exception safety
  129. * No-throw guarantee.
  130. */
  131. std::uint64_t affected_rows() const noexcept
  132. {
  133. assert(has_value());
  134. return st_.affected_rows();
  135. }
  136. /**
  137. * \brief Returns the last insert ID produced by the executed SQL statement.
  138. * \par Preconditions
  139. * `this->has_value() == true`
  140. *
  141. * \par Exception safety
  142. * No-throw guarantee.
  143. */
  144. std::uint64_t last_insert_id() const noexcept
  145. {
  146. assert(has_value());
  147. return st_.last_insert_id();
  148. }
  149. /**
  150. * \brief Returns the number of warnings produced by the executed SQL statement.
  151. * \par Preconditions
  152. * `this->has_value() == true`
  153. *
  154. * \par Exception safety
  155. * No-throw guarantee.
  156. */
  157. unsigned warning_count() const noexcept
  158. {
  159. assert(has_value());
  160. return st_.warning_count();
  161. }
  162. /**
  163. * \brief Returns additionat text information about the execution of the SQL statement.
  164. * \details
  165. * The format of this information is documented by MySQL <a
  166. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  167. * \n
  168. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  169. *
  170. * \par Preconditions
  171. * `this->has_value() == true`
  172. *
  173. * \par Exception safety
  174. * No-throw guarantee.
  175. *
  176. * \par Object lifetimes
  177. * This function returns a view object, with reference semantics. The returned view points into
  178. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  179. * from `*this` are alive.
  180. */
  181. string_view info() const noexcept
  182. {
  183. assert(has_value());
  184. return st_.info();
  185. }
  186. private:
  187. execution_state st_;
  188. ::boost::mysql::rows rows_;
  189. #ifndef BOOST_MYSQL_DOXYGEN
  190. friend struct detail::results_access;
  191. #endif
  192. };
  193. } // namespace mysql
  194. } // namespace boost
  195. #include <boost/mysql/impl/results.hpp>
  196. #endif