static_execution_state.hpp 8.2 KB

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