resultset.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_HPP
  8. #define BOOST_MYSQL_RESULTSET_HPP
  9. #include <boost/mysql/metadata.hpp>
  10. #include <boost/mysql/metadata_collection_view.hpp>
  11. #include <boost/mysql/resultset_view.hpp>
  12. #include <boost/mysql/row_view.hpp>
  13. #include <boost/mysql/rows.hpp>
  14. #include <boost/mysql/detail/config.hpp>
  15. #include <boost/assert.hpp>
  16. namespace boost {
  17. namespace mysql {
  18. /**
  19. * \brief An owning resultset, containing metadata, rows and additional info.
  20. * \details
  21. * Similar to \ref results, but can only represent a single resultset (while `results` can hold
  22. * multiple resultsets). Can be used to take ownership of a \ref resultset_view.
  23. */
  24. class resultset
  25. {
  26. public:
  27. /**
  28. * \brief Constructs an empty resultset.
  29. * \details
  30. * The constructed object has `this->has_value() == false`.
  31. *
  32. * \par Exception safety
  33. * No-throw guarantee.
  34. */
  35. resultset() = default;
  36. /**
  37. * \brief Copy constructor.
  38. * \par Exception safety
  39. * Strong guarantee. Internal allocations may throw.
  40. *
  41. * \par Object lifetimes
  42. * `*this` lifetime will be independent of `other`'s.
  43. *
  44. * \par Complexity
  45. * Linear on rows and metadata size for `other`.
  46. */
  47. resultset(const resultset& other) = default;
  48. /**
  49. * \brief Move constructor.
  50. * \par Exception safety
  51. * No-throw guarantee.
  52. *
  53. * \par Object lifetimes
  54. * Views obtained from `other` remain valid.
  55. *
  56. * \par Complexity
  57. * Constant.
  58. */
  59. resultset(resultset&& other) = default;
  60. /**
  61. * \brief Copy assignment.
  62. * \par Exception safety
  63. * Basic guarantee. Internal allocations may throw.
  64. *
  65. * \par Object lifetimes
  66. * `*this` lifetime will be independent of `other`'s. Views obtained from `*this`
  67. * are invalidated.
  68. *
  69. * \par Complexity
  70. * Linear on rows and metadata size for `other`.
  71. */
  72. resultset& operator=(const resultset& other) = default;
  73. /**
  74. * \brief Move assignment.
  75. * \par Exception safety
  76. * No-throw guarantee.
  77. *
  78. * \par Object lifetimes
  79. * Views obtained from `*this` are invalidated. Views obtained from `other` remain valid.
  80. *
  81. * \par Complexity
  82. * Constant.
  83. */
  84. resultset& operator=(resultset&& other) = default;
  85. /**
  86. * \brief Destructor.
  87. */
  88. ~resultset() = default;
  89. /**
  90. * \brief Constructs a resultset object by taking ownership of a view.
  91. * \par Exception safety
  92. * Strong guarantee. Internal allocations may throw.
  93. *
  94. * \par Object lifetimes
  95. * `*this` lifetime will be independent of `v`'s (the contents of `v` will be copied
  96. * into `*this`).
  97. *
  98. * \par Complexity
  99. * Linear on rows and metadata size for `v`.
  100. */
  101. resultset(resultset_view v) { assign(v); }
  102. /**
  103. * \brief Replaces the contents of `*this` with a \ref resultset_view.
  104. * \par Exception safety
  105. * Basic guarantee. Internal allocations may throw.
  106. *
  107. * \par Object lifetimes
  108. * `*this` lifetime will be independent of `v`'s (the contents of `v` will be copied
  109. * into `*this`). Views obtained from `*this` are invalidated.
  110. *
  111. * \par Complexity
  112. * Linear on rows and metadata size for `v` and `*this`.
  113. */
  114. resultset& operator=(resultset_view v)
  115. {
  116. assign(v);
  117. return *this;
  118. }
  119. /**
  120. * \brief Returns whether this object contains actual data or not.
  121. * \details
  122. * Only returns true for default-constructed objects.
  123. *
  124. * \par Exception safety
  125. * No-throw guarantee.
  126. *
  127. * \par Complexity
  128. * Constant.
  129. */
  130. bool has_value() const noexcept { return has_value_; }
  131. /**
  132. * \brief Returns the rows that this resultset contains.
  133. * \par Preconditions
  134. * `this->has_value() == true`
  135. *
  136. * \par Exception safety
  137. * No-throw guarantee.
  138. *
  139. * \par Object lifetimes
  140. * The returned reference and any other references obtained from it are valid as long as
  141. * `*this` or an object move-constructed from `*this` are alive.
  142. *
  143. * \par Complexity
  144. * Constant.
  145. */
  146. rows_view rows() const noexcept
  147. {
  148. BOOST_ASSERT(has_value_);
  149. return rws_;
  150. }
  151. /**
  152. * \brief Returns metadata for this resultset.
  153. * \par Preconditions
  154. * `this->has_value() == true`
  155. *
  156. * \par Exception safety
  157. * No-throw guarantee.
  158. *
  159. * \par Object lifetimes
  160. * The returned reference and any other references obtained from it are valid as long as
  161. * `*this` or an object move-constructed from `*this` are alive.
  162. *
  163. * \par Complexity
  164. * Constant.
  165. */
  166. metadata_collection_view meta() const noexcept
  167. {
  168. BOOST_ASSERT(has_value_);
  169. return meta_;
  170. }
  171. /**
  172. * \brief Returns the number of affected rows for this resultset.
  173. * \par Preconditions
  174. * `this->has_value() == true`
  175. *
  176. * \par Exception safety
  177. * No-throw guarantee.
  178. *
  179. * \par Complexity
  180. * Constant.
  181. */
  182. std::uint64_t affected_rows() const noexcept
  183. {
  184. BOOST_ASSERT(has_value_);
  185. return affected_rows_;
  186. }
  187. /**
  188. * \brief Returns the last insert ID for this resultset.
  189. * \par Preconditions
  190. * `this->has_value() == true`
  191. *
  192. * \par Exception safety
  193. * No-throw guarantee.
  194. *
  195. * \par Complexity
  196. * Constant.
  197. */
  198. std::uint64_t last_insert_id() const noexcept
  199. {
  200. BOOST_ASSERT(has_value_);
  201. return last_insert_id_;
  202. }
  203. /**
  204. * \brief Returns the number of warnings for this resultset.
  205. * \par Preconditions
  206. * `this->has_value() == true`
  207. *
  208. * \par Exception safety
  209. * No-throw guarantee.
  210. *
  211. * \par Complexity
  212. * Constant.
  213. */
  214. unsigned warning_count() const noexcept
  215. {
  216. BOOST_ASSERT(has_value_);
  217. return warnings_;
  218. }
  219. /**
  220. * \brief Returns additional information for this resultset.
  221. * \details
  222. * The format of this information is documented by MySQL <a
  223. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  224. * \n
  225. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  226. *
  227. * \par Preconditions
  228. * `this->has_value() == true`
  229. *
  230. * \par Exception safety
  231. * No-throw guarantee.
  232. *
  233. * \par Object lifetimes
  234. * The returned reference and any other references obtained from it are valid as long as
  235. * `*this` or an object move-constructed from `*this` are alive.
  236. *
  237. * \par Complexity
  238. * Constant.
  239. */
  240. string_view info() const noexcept
  241. {
  242. BOOST_ASSERT(has_value_);
  243. return string_view(info_.data(), info_.size());
  244. }
  245. /**
  246. * \brief Returns whether this resultset represents a procedure OUT params.
  247. * \par Preconditions
  248. * `this->has_value() == true`
  249. *
  250. * \par Exception safety
  251. * No-throw guarantee.
  252. *
  253. * \par Complexity
  254. * Constant.
  255. */
  256. bool is_out_params() const noexcept
  257. {
  258. BOOST_ASSERT(has_value_);
  259. return is_out_params_;
  260. }
  261. private:
  262. bool has_value_{false};
  263. std::vector<metadata> meta_;
  264. ::boost::mysql::rows rws_;
  265. std::uint64_t affected_rows_{};
  266. std::uint64_t last_insert_id_{};
  267. std::uint16_t warnings_{};
  268. std::vector<char> info_;
  269. bool is_out_params_{false};
  270. BOOST_MYSQL_DECL
  271. void assign(resultset_view v);
  272. };
  273. } // namespace mysql
  274. } // namespace boost
  275. #ifdef BOOST_MYSQL_HEADER_ONLY
  276. #include <boost/mysql/impl/resultset.ipp>
  277. #endif
  278. #endif