static_results.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_RESULTS_HPP
  8. #define BOOST_MYSQL_STATIC_RESULTS_HPP
  9. #include <boost/mysql/detail/config.hpp>
  10. #ifdef BOOST_MYSQL_CXX14
  11. #include <boost/mysql/metadata_collection_view.hpp>
  12. #include <boost/mysql/string_view.hpp>
  13. #include <boost/mysql/underlying_row.hpp>
  14. #include <boost/mysql/detail/access.hpp>
  15. #include <boost/mysql/detail/execution_processor/static_results_impl.hpp>
  16. #include <boost/assert.hpp>
  17. namespace boost {
  18. namespace mysql {
  19. /**
  20. * \brief Holds the results of a SQL query (static interface).
  21. * \details
  22. * This object can store the results of single and multi resultset queries
  23. * in a type-safe manner.
  24. *
  25. * \tparam StaticRow The row or row types that will be returned by the server.
  26. * There must be one for every resultset returned by the query, and always at least one.
  27. * All the passed types must fulfill the `StaticRow` concept.
  28. *
  29. * \par Thread safety
  30. * Distinct objects: safe. \n
  31. * Shared objects: unsafe. \n
  32. */
  33. template <class... StaticRow>
  34. class static_results
  35. {
  36. public:
  37. /**
  38. * \brief Default constructor.
  39. * \details Constructs an empty results object, with `this->has_value() == false`.
  40. *
  41. * \par Exception safety
  42. * No-throw guarantee.
  43. */
  44. static_results() = default;
  45. /**
  46. * \brief Copy constructor.
  47. * \par Exception safety
  48. * Strong guarantee. Internal allocations may throw.
  49. */
  50. static_results(const static_results& other) = default;
  51. /**
  52. * \brief Move constructor.
  53. * \par Exception safety
  54. * No-throw guarantee.
  55. *
  56. * \par Object lifetimes
  57. * View objects obtained from `other` remain valid.
  58. */
  59. static_results(static_results&& other) = default;
  60. /**
  61. * \brief Copy assignment.
  62. * \par Exception safety
  63. * Basic guarantee. Internal allocations may throw.
  64. *
  65. * \par Object lifetimes
  66. * Views referencing `*this` are invalidated.
  67. */
  68. static_results& operator=(const static_results& other) = default;
  69. /**
  70. * \brief Move assignment.
  71. * \par Exception safety
  72. * Basic guarantee. Internal allocations may throw.
  73. *
  74. * \par Object lifetimes
  75. * View objects obtained from `other` remain valid.
  76. * Views and referencing `*this` are invalidated.
  77. */
  78. static_results& operator=(static_results&& other) = default;
  79. /// Destructor
  80. ~static_results() = default;
  81. /**
  82. * \brief Returns whether the object holds a valid result.
  83. * \details Having `this->has_value()` is a precondition to call all data accessors.
  84. * Objects populated by \ref connection::execute and \ref connection::async_execute
  85. * are guaranteed to have `this->has_value() == true`.
  86. *
  87. * \par Exception safety
  88. * No-throw guarantee.
  89. *
  90. * \par Complexity
  91. * Constant.
  92. */
  93. bool has_value() const noexcept { return impl_.get_interface().is_complete(); }
  94. /**
  95. * \brief Returns the rows retrieved by the SQL query.
  96. * \details
  97. *
  98. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  99. * specify this parameter to obtain the rows contained in the i-th resultset. If left unspecified,
  100. * rows for the first resultset are returned.
  101. *
  102. * \return Returns a read-only span of the `I`-th row type.
  103. *
  104. * \par Preconditions
  105. * `this->has_value() == true`
  106. *
  107. * \par Exception safety
  108. * No-throw guarantee.
  109. *
  110. * \par Object lifetimes
  111. * This function returns a view object, with reference semantics. The returned view points into
  112. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  113. * from `*this` are alive.
  114. *
  115. * \par Complexity
  116. * Constant.
  117. */
  118. template <std::size_t I = 0>
  119. #ifdef BOOST_MYSQL_DOXYGEN
  120. boost::span<const StaticRow... [I]>
  121. #else
  122. detail::rows_span_t<I, StaticRow...>
  123. #endif
  124. rows() const noexcept {
  125. static_assert(I < sizeof...(StaticRow), "Index I out of range");
  126. BOOST_ASSERT(has_value());
  127. return impl_.template get_rows<I>();
  128. }
  129. /**
  130. * \brief Returns metadata about the columns in the query.
  131. * \details
  132. * The returned collection will have as many \ref metadata objects as columns retrieved by
  133. * the SQL query, and in the same order. Note that this may not be the same order as in the `StaticRow`
  134. * type, since columns may be mapped by name or discarded. This function returns the representation that
  135. * was retrieved from the database.
  136. *
  137. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  138. * specify this parameter to obtain metadata for the i-th resultset. If left unspecified,
  139. * metadata for the first resultset is returned.
  140. *
  141. * \par Preconditions
  142. * `this->has_value() == true`
  143. *
  144. * \par Exception safety
  145. * No-throw guarantee.
  146. *
  147. * \par Object lifetimes
  148. * This function returns a view object, with reference semantics. The returned view points into
  149. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  150. * from `*this` are alive.
  151. *
  152. * \par Complexity
  153. * Constant.
  154. */
  155. template <std::size_t I = 0>
  156. metadata_collection_view meta() const noexcept
  157. {
  158. static_assert(I < sizeof...(StaticRow), "Index I out of range");
  159. BOOST_ASSERT(has_value());
  160. return impl_.get_interface().get_meta(I);
  161. }
  162. /**
  163. * \brief Returns the number of rows affected by the executed SQL statement.
  164. * \details
  165. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  166. * specify this parameter to obtain the number of affected rows by the i-th resultset. If left
  167. * unspecified, the number of affected rows by the first resultset is returned.
  168. *
  169. * \par Preconditions
  170. * `this->has_value() == true`
  171. *
  172. * \par Exception safety
  173. * No-throw guarantee.
  174. *
  175. * \par Complexity
  176. * Constant.
  177. */
  178. template <std::size_t I = 0>
  179. std::uint64_t affected_rows() const noexcept
  180. {
  181. static_assert(I < sizeof...(StaticRow), "Index I out of range");
  182. BOOST_ASSERT(has_value());
  183. return impl_.get_interface().get_affected_rows(I);
  184. }
  185. /**
  186. * \brief Returns the last insert ID produced by the executed SQL statement.
  187. * \details
  188. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  189. * specify this parameter to obtain the last insert ID for the i-th resultset. If left unspecified,
  190. * the last insert ID for the first resultset is returned.
  191. *
  192. * \par Preconditions
  193. * `this->has_value() == true`
  194. *
  195. * \par Exception safety
  196. * No-throw guarantee.
  197. *
  198. * \par Complexity
  199. * Constant.
  200. */
  201. template <std::size_t I = 0>
  202. std::uint64_t last_insert_id() const noexcept
  203. {
  204. static_assert(I < sizeof...(StaticRow), "I index out of range");
  205. BOOST_ASSERT(has_value());
  206. return impl_.get_interface().get_last_insert_id(I);
  207. }
  208. /**
  209. * \brief Returns the number of warnings produced by the executed SQL statement.
  210. * \details
  211. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  212. * specify this parameter to obtain the warning count for the i-th resultset. If left unspecified,
  213. * the warning count for the first resultset is returned.
  214. *
  215. * \par Preconditions
  216. * `this->has_value() == true`
  217. *
  218. * \par Exception safety
  219. * No-throw guarantee.
  220. *
  221. * \par Complexity
  222. * Constant.
  223. */
  224. template <std::size_t I = 0>
  225. unsigned warning_count() const noexcept
  226. {
  227. static_assert(I < sizeof...(StaticRow), "I index out of range");
  228. BOOST_ASSERT(has_value());
  229. return impl_.get_interface().get_warning_count(I);
  230. }
  231. /**
  232. * \brief Returns additional text information about the execution of the SQL statement.
  233. * \details
  234. * The format of this information is documented by MySQL <a
  235. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  236. * \n
  237. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  238. *
  239. * \tparam I Resultset index. For operations returning more than one resultset, you can explicitly
  240. * specify this parameter to obtain the value for the i-th resultset. If left unspecified,
  241. * the value for the first resultset is returned.
  242. *
  243. * \par Preconditions
  244. * `this->has_value() == true`
  245. *
  246. * \par Exception safety
  247. * No-throw guarantee.
  248. *
  249. * \par Object lifetimes
  250. * This function returns a view object, with reference semantics. The returned view points into
  251. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  252. * from `*this` are alive.
  253. *
  254. * \par Complexity
  255. * Constant.
  256. */
  257. template <std::size_t I = 0>
  258. string_view info() const noexcept
  259. {
  260. static_assert(I < sizeof...(StaticRow), "I index out of range");
  261. BOOST_ASSERT(has_value());
  262. return impl_.get_interface().get_info(I);
  263. }
  264. private:
  265. detail::static_results_impl<StaticRow...> impl_;
  266. #ifndef BOOST_MYSQL_DOXYGEN
  267. friend struct detail::access;
  268. #endif
  269. };
  270. } // namespace mysql
  271. } // namespace boost
  272. #endif // BOOST_MYSQL_CXX14
  273. #endif