execution_state.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_EXECUTION_STATE_HPP
  8. #define BOOST_MYSQL_EXECUTION_STATE_HPP
  9. #include <boost/mysql/metadata_collection_view.hpp>
  10. #include <boost/mysql/string_view.hpp>
  11. #include <boost/mysql/detail/access.hpp>
  12. #include <boost/mysql/detail/execution_processor/execution_state_impl.hpp>
  13. #include <cstddef>
  14. #include <cstdint>
  15. namespace boost {
  16. namespace mysql {
  17. /**
  18. * \brief Holds state for multi-function SQL execution operations (dynamic interface).
  19. * \details
  20. * This class behaves like a state machine. The current state can be accessed using
  21. * \ref should_start_op, \ref should_read_rows, \ref should_read_head
  22. * and \ref complete. They are mutually exclusive.
  23. * More states may be added in the future as more protocol features are implemented.
  24. *
  25. * \par Thread safety
  26. * Distinct objects: safe. \n
  27. * Shared objects: unsafe.
  28. */
  29. class execution_state
  30. {
  31. public:
  32. /**
  33. * \brief Default constructor.
  34. * \details The constructed object is guaranteed to have
  35. * `should_start_op() == true`.
  36. *
  37. * \par Exception safety
  38. * No-throw guarantee.
  39. */
  40. execution_state() = default;
  41. /**
  42. * \brief Copy constructor.
  43. * \par Exception safety
  44. * Strong guarantee. Internal allocations may throw.
  45. *
  46. * \par Object lifetimes
  47. * `*this` lifetime will be independent of `other`'s.
  48. */
  49. execution_state(const execution_state& other) = default;
  50. /**
  51. * \brief Move constructor.
  52. * \par Exception safety
  53. * No-throw guarantee.
  54. *
  55. * \par Object lifetimes
  56. * Views obtained from `other` remain valid.
  57. */
  58. execution_state(execution_state&& other) = default;
  59. /**
  60. * \brief Copy assignment.
  61. * \par Exception safety
  62. * Basic guarantee. Internal allocations may throw.
  63. *
  64. * \par Object lifetimes
  65. * `*this` lifetime will be independent of `other`'s. Views obtained from `*this`
  66. * are invalidated.
  67. */
  68. execution_state& operator=(const execution_state& other) = default;
  69. /**
  70. * \brief Move assignment.
  71. * \par Exception safety
  72. * No-throw guarantee.
  73. *
  74. * \par Object lifetimes
  75. * Views obtained from `*this` are invalidated. Views obtained from `other` remain valid.
  76. */
  77. execution_state& operator=(execution_state&& other) = default;
  78. /**
  79. * \brief Returns whether `*this` is in the initial state.
  80. * \details
  81. * Call \ref connection::start_execution or \ref connection::async_start_execution to move
  82. * forward. No data is available in this state.
  83. *
  84. * \par Exception safety
  85. * No-throw guarantee.
  86. */
  87. bool should_start_op() const noexcept { return impl_.is_reading_first(); }
  88. /**
  89. * \brief Returns whether the next operation should be read resultset head.
  90. * \details
  91. * Call \ref connection::read_resultset_head or its async counterpart to move forward.
  92. * Metadata and OK data for the previous resultset is available in this state.
  93. *
  94. * \par Exception safety
  95. * No-throw guarantee.
  96. */
  97. bool should_read_head() const noexcept { return impl_.is_reading_first_subseq(); }
  98. /**
  99. * \brief Returns whether the next operation should be read some rows.
  100. * \details
  101. * Call \ref connection::read_some_rows or its async counterpart to move forward.
  102. * Metadata for the current resultset is available in this state.
  103. *
  104. * \par Exception safety
  105. * No-throw guarantee.
  106. */
  107. bool should_read_rows() const noexcept { return impl_.is_reading_rows(); }
  108. /**
  109. * \brief Returns whether all the messages generated by this operation have been read.
  110. * \details
  111. * No further network calls are required to move forward. Metadata and OK data for the last
  112. * resultset are available in this state.
  113. *
  114. * \par Exception safety
  115. * No-throw guarantee.
  116. */
  117. bool complete() const noexcept { return impl_.is_complete(); }
  118. /**
  119. * \brief Returns metadata about the columns in the query.
  120. * \details
  121. * The returned collection will have as many \ref metadata objects as columns retrieved by
  122. * the SQL query, and in the same order.
  123. *
  124. * \par Exception safety
  125. * No-throw guarantee.
  126. *
  127. * \par Object lifetimes
  128. * This function returns a view object, with reference semantics. The returned view points into
  129. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  130. * from `*this` are alive.
  131. */
  132. metadata_collection_view meta() const noexcept { return impl_.meta(); }
  133. /**
  134. * \brief Returns the number of rows affected by the SQL statement associated to this resultset.
  135. * \par Exception safety
  136. * No-throw guarantee.
  137. *
  138. * \par Preconditions
  139. * `this->complete() == true || this->should_read_head() == true`
  140. */
  141. std::uint64_t affected_rows() const noexcept { return impl_.get_affected_rows(); }
  142. /**
  143. * \brief Returns the last insert ID produced by the SQL statement associated to this resultset.
  144. * \par Exception safety
  145. * No-throw guarantee.
  146. *
  147. * \par Preconditions
  148. * `this->complete() == true || this->should_read_head() == true`
  149. */
  150. std::uint64_t last_insert_id() const noexcept { return impl_.get_last_insert_id(); }
  151. /**
  152. * \brief Returns the number of warnings produced by the SQL statement associated to this resultset.
  153. * \par Exception safety
  154. * No-throw guarantee.
  155. *
  156. * \par Preconditions
  157. * `this->complete() == true || this->should_read_head() == true`
  158. */
  159. unsigned warning_count() const noexcept { return impl_.get_warning_count(); }
  160. /**
  161. * \brief Returns additional text information about this resultset.
  162. * \details
  163. * The format of this information is documented by MySQL <a
  164. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  165. * \n
  166. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  167. *
  168. * \par Exception safety
  169. * No-throw guarantee.
  170. *
  171. * \par Preconditions
  172. * `this->complete() == true || this->should_read_head() == true`
  173. *
  174. * \par Object lifetimes
  175. * This function returns a view object, with reference semantics. The returned view points into
  176. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  177. * from `*this` are alive.
  178. */
  179. string_view info() const noexcept { return impl_.get_info(); }
  180. /**
  181. * \brief Returns whether the current resultset represents a procedure OUT params.
  182. * \par Preconditions
  183. * `this->complete() == true || this->should_read_head() == true`
  184. *
  185. * \par Exception safety
  186. * No-throw guarantee.
  187. */
  188. bool is_out_params() const noexcept { return impl_.get_is_out_params(); }
  189. private:
  190. detail::execution_state_impl impl_;
  191. #ifndef BOOST_MYSQL_DOXYGEN
  192. friend struct detail::access;
  193. #endif
  194. };
  195. } // namespace mysql
  196. } // namespace boost
  197. #endif