results.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // Copyright (c) 2019-2025 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/metadata_collection_view.hpp>
  10. #include <boost/mysql/resultset.hpp>
  11. #include <boost/mysql/resultset_view.hpp>
  12. #include <boost/mysql/rows_view.hpp>
  13. #include <boost/mysql/string_view.hpp>
  14. #include <boost/mysql/detail/access.hpp>
  15. #include <boost/mysql/detail/execution_processor/results_impl.hpp>
  16. #include <boost/mysql/detail/results_iterator.hpp>
  17. #include <boost/assert.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <stdexcept>
  20. namespace boost {
  21. namespace mysql {
  22. /**
  23. * \brief Holds the results of a SQL query (dynamic interface).
  24. * \details
  25. * This object can store the results of single and multi resultset queries.
  26. * For the former, you use \ref meta, \ref rows, \ref affected_rows and so on.
  27. * For the latter, this class is a random-access collection of \ref resultset objects.
  28. * \n
  29. * \par Thread safety
  30. * Distinct objects: safe. \n
  31. * Shared objects: unsafe. \n
  32. */
  33. class results
  34. {
  35. public:
  36. #ifdef BOOST_MYSQL_DOXYGEN
  37. /**
  38. * \brief A random access iterator to an element.
  39. * \details The exact type of the iterator is unspecified.
  40. */
  41. using iterator = __see_below__;
  42. #else
  43. using iterator = detail::results_iterator;
  44. #endif
  45. /// \copydoc iterator
  46. using const_iterator = iterator;
  47. /// A type that can hold elements in this collection with value semantics.
  48. using value_type = resultset;
  49. /// The reference type.
  50. using reference = resultset_view;
  51. /// \copydoc reference
  52. using const_reference = resultset_view;
  53. /// An unsigned integer type to represent sizes.
  54. using size_type = std::size_t;
  55. /// A signed integer type used to represent differences.
  56. using difference_type = std::ptrdiff_t;
  57. /**
  58. * \brief Default constructor.
  59. * \details Constructs an empty results object, with `this->has_value() == false`.
  60. *
  61. * \par Exception safety
  62. * No-throw guarantee.
  63. */
  64. results() = default;
  65. /**
  66. * \brief Copy constructor.
  67. * \par Exception safety
  68. * Strong guarantee. Internal allocations may throw.
  69. */
  70. results(const results& other) = default;
  71. /**
  72. * \brief Move constructor.
  73. * \par Exception safety
  74. * No-throw guarantee.
  75. *
  76. * \par Object lifetimes
  77. * View objects obtained from `other` using \ref rows and \ref meta remain valid.
  78. * Any other views and iterators referencing `other` are invalidated.
  79. */
  80. results(results&& other) = default;
  81. /**
  82. * \brief Copy assignment.
  83. * \par Exception safety
  84. * Basic guarantee. Internal allocations may throw.
  85. *
  86. * \par Object lifetimes
  87. * Views and iterators referencing `*this` are invalidated.
  88. */
  89. results& operator=(const results& other) = default;
  90. /**
  91. * \brief Move assignment.
  92. * \par Exception safety
  93. * Basic guarantee. Internal allocations may throw.
  94. *
  95. * \par Object lifetimes
  96. * View objects obtained from `other` using \ref rows and \ref meta remain valid.
  97. * Any other views and iterators referencing `other` are invalidated. Views and iterators
  98. * referencing `*this` are invalidated.
  99. */
  100. results& operator=(results&& other) = default;
  101. /// Destructor
  102. ~results() = default;
  103. /**
  104. * \brief Returns whether the object holds a valid result.
  105. * \details Having `this->has_value()` is a precondition to call all data accessors.
  106. * Objects populated by \ref connection::execute and \ref connection::async_execute
  107. * are guaranteed to have `this->has_value() == true`.
  108. *
  109. * \par Exception safety
  110. * No-throw guarantee.
  111. *
  112. * \par Complexity
  113. * Constant.
  114. */
  115. bool has_value() const noexcept { return impl_.is_complete(); }
  116. /**
  117. * \brief Returns the rows retrieved by the SQL query.
  118. * \details
  119. * For operations returning more than one resultset, returns the rows
  120. * for the first resultset.
  121. *
  122. * \par Preconditions
  123. * `this->has_value() == true`
  124. *
  125. * \par Exception safety
  126. * No-throw guarantee.
  127. *
  128. * \par Object lifetimes
  129. * This function returns a view object, with reference semantics. The returned view points into
  130. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  131. * from `*this` are alive.
  132. *
  133. * \par Complexity
  134. * Constant.
  135. */
  136. rows_view rows() const noexcept
  137. {
  138. BOOST_ASSERT(has_value());
  139. return impl_.get_rows(0);
  140. }
  141. /**
  142. * \brief Returns metadata about the columns in the query.
  143. * \details
  144. * The returned collection will have as many \ref metadata objects as columns retrieved by
  145. * the SQL query, and in the same order.
  146. * \n
  147. * For operations returning more than one resultset, returns metadata
  148. * for the first resultset.
  149. *
  150. * \par Preconditions
  151. * `this->has_value() == true`
  152. *
  153. * \par Exception safety
  154. * No-throw guarantee.
  155. *
  156. * \par Object lifetimes
  157. * This function returns a view object, with reference semantics. The returned view points into
  158. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  159. * from `*this` are alive.
  160. *
  161. * \par Complexity
  162. * Constant.
  163. */
  164. metadata_collection_view meta() const noexcept
  165. {
  166. BOOST_ASSERT(has_value());
  167. return impl_.get_meta(0);
  168. }
  169. /**
  170. * \brief Returns the number of rows affected by the executed SQL statement.
  171. * \details
  172. * Note that this is NOT the number of matched rows. If a row
  173. * is matched but not affected, it won't be accounted for here.
  174. *
  175. * For operations returning more than one resultset, returns the
  176. * first resultset's affected rows.
  177. *
  178. * \par Preconditions
  179. * `this->has_value() == true`
  180. *
  181. * \par Exception safety
  182. * No-throw guarantee.
  183. *
  184. * \par Complexity
  185. * Constant.
  186. */
  187. std::uint64_t affected_rows() const noexcept
  188. {
  189. BOOST_ASSERT(has_value());
  190. return impl_.get_affected_rows(0);
  191. }
  192. /**
  193. * \brief Returns the last insert ID produced by the executed SQL statement.
  194. * \details
  195. * For operations returning more than one resultset, returns the
  196. * first resultset's last insert ID.
  197. *
  198. * \par Preconditions
  199. * `this->has_value() == true`
  200. *
  201. * \par Exception safety
  202. * No-throw guarantee.
  203. *
  204. * \par Complexity
  205. * Constant.
  206. */
  207. std::uint64_t last_insert_id() const noexcept
  208. {
  209. BOOST_ASSERT(has_value());
  210. return impl_.get_last_insert_id(0);
  211. }
  212. /**
  213. * \brief Returns the number of warnings produced by the executed SQL statement.
  214. * \details
  215. * For operations returning more than one resultset, returns the
  216. * first resultset's warning count.
  217. *
  218. * \par Preconditions
  219. * `this->has_value() == true`
  220. *
  221. * \par Exception safety
  222. * No-throw guarantee.
  223. *
  224. * \par Complexity
  225. * Constant.
  226. */
  227. unsigned warning_count() const noexcept
  228. {
  229. BOOST_ASSERT(has_value());
  230. return impl_.get_warning_count(0);
  231. }
  232. /**
  233. * \brief Returns additional text information about the execution of the SQL statement.
  234. * \details
  235. * The format of this information is documented by MySQL <a
  236. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  237. * \n
  238. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  239. * \n
  240. * For operations returning more than one resultset, returns the
  241. * first resultset's info.
  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. string_view info() const noexcept
  258. {
  259. BOOST_ASSERT(has_value());
  260. return impl_.get_info(0);
  261. }
  262. /**
  263. * \brief Returns an iterator pointing to the first resultset that this object contains.
  264. * \par Preconditions
  265. * `this->has_value() == true`
  266. *
  267. * \par Exception safety
  268. * No-throw guarantee.
  269. *
  270. * \par Object lifetimes
  271. * The returned iterator and any reference obtained from it are valid as long as
  272. * `*this` is alive. Move operations invalidate iterators.
  273. *
  274. * \par Complexity
  275. * Constant.
  276. */
  277. iterator begin() const noexcept
  278. {
  279. BOOST_ASSERT(has_value());
  280. return iterator(&impl_, 0);
  281. }
  282. /**
  283. * \brief Returns an iterator pointing to one-past-the-last resultset that this object contains.
  284. * \par Preconditions
  285. * `this->has_value() == true`
  286. *
  287. * \par Exception safety
  288. * No-throw guarantee.
  289. *
  290. * \par Object lifetimes
  291. * The returned iterator and any reference obtained from it are valid as long as
  292. * `*this` is alive. Move operations invalidate iterators.
  293. *
  294. * \par Complexity
  295. * Constant.
  296. */
  297. iterator end() const noexcept
  298. {
  299. BOOST_ASSERT(has_value());
  300. return iterator(&impl_, size());
  301. }
  302. /**
  303. * \brief Returns the i-th resultset or throws an exception.
  304. * \par Preconditions
  305. * `this->has_value() == true`
  306. *
  307. * \par Exception safety
  308. * Strong guranatee. Throws on invalid input.
  309. * \throws std::out_of_range `i >= this->size()`
  310. *
  311. * \par Object lifetimes
  312. * The returned reference and any other references obtained from it are valid as long as
  313. * `*this` is alive. Move operations invalidate references.
  314. *
  315. * \par Complexity
  316. * Constant.
  317. */
  318. inline resultset_view at(std::size_t i) const
  319. {
  320. BOOST_ASSERT(has_value());
  321. if (i >= size())
  322. BOOST_THROW_EXCEPTION(std::out_of_range("results::at: out of range"));
  323. return detail::access::construct<resultset_view>(impl_, i);
  324. }
  325. /**
  326. * \brief Returns the i-th resultset (unchecked access).
  327. * \par Preconditions
  328. * `this->has_value() == true && i < this->size()`
  329. *
  330. * \par Exception safety
  331. * No-throw guarantee.
  332. *
  333. * \par Object lifetimes
  334. * The returned reference and any other references obtained from it are valid as long as
  335. * `*this` is alive. Move operations invalidate references.
  336. *
  337. * \par Complexity
  338. * Constant.
  339. */
  340. resultset_view operator[](std::size_t i) const noexcept
  341. {
  342. BOOST_ASSERT(has_value());
  343. BOOST_ASSERT(i < size());
  344. return detail::access::construct<resultset_view>(impl_, i);
  345. }
  346. /**
  347. * \brief Returns the first resultset.
  348. * \par Preconditions
  349. * `this->has_value() == true`
  350. *
  351. * \par Exception safety
  352. * No-throw guarantee.
  353. *
  354. * \par Object lifetimes
  355. * The returned reference and any other references obtained from it are valid as long as
  356. * `*this` is alive. Move operations invalidate references.
  357. *
  358. * \par Complexity
  359. * Constant.
  360. */
  361. resultset_view front() const noexcept { return (*this)[0]; }
  362. /**
  363. * \brief Returns the last resultset.
  364. * \par Preconditions
  365. * `this->has_value() == true`
  366. *
  367. * \par Exception safety
  368. * No-throw guarantee.
  369. *
  370. * \par Object lifetimes
  371. * The returned reference and any other references obtained from it are valid as long as
  372. * `*this` is alive. Move operations invalidate references.
  373. *
  374. * \par Complexity
  375. * Constant.
  376. */
  377. resultset_view back() const noexcept { return (*this)[size() - 1]; }
  378. /**
  379. * \brief Returns whether the collection contains any resultset.
  380. * \details
  381. * This function is provided for compatibility with standard collections,
  382. * and always returns false, since any valid `results` contains at least one resultset.
  383. *
  384. * \par Preconditions
  385. * `this->has_value() == true`
  386. *
  387. * \par Exception safety
  388. * No-throw guarantee.
  389. *
  390. * \par Complexity
  391. * Constant.
  392. */
  393. bool empty() const noexcept
  394. {
  395. BOOST_ASSERT(has_value());
  396. return false;
  397. }
  398. /**
  399. * \brief Returns the number of resultsets that this collection contains.
  400. * \par Preconditions
  401. * `this->has_value() == true`
  402. *
  403. * \par Exception safety
  404. * No-throw guarantee.
  405. *
  406. * \par Complexity
  407. * Constant.
  408. */
  409. std::size_t size() const noexcept
  410. {
  411. BOOST_ASSERT(has_value());
  412. return impl_.num_resultsets();
  413. }
  414. /**
  415. * \brief Returns the output parameters of a stored procedure call.
  416. * \details
  417. * Relevant for `CALL` operations performed using prepared statements that
  418. * bind placeholders to `OUT` or `INOUT` parameters. Returns a row containing a field per
  419. * bound output parameter.
  420. * \n
  421. * If this operation had no output parameters (e.g. it wasn't a `CALL`), returns an empty row.
  422. *
  423. * \par Preconditions
  424. * `this->has_value() == true`
  425. *
  426. * \par Exception safety
  427. * No-throw guarantee.
  428. *
  429. * \par Object lifetimes
  430. * The returned reference and any other references obtained from it are valid as long as
  431. * `*this` is alive. Move operations invalidate references.
  432. *
  433. * \par Complexity
  434. * Linear on `this->size()`.
  435. */
  436. row_view out_params() const noexcept
  437. {
  438. BOOST_ASSERT(has_value());
  439. return impl_.get_out_params();
  440. }
  441. private:
  442. detail::results_impl impl_;
  443. #ifndef BOOST_MYSQL_DOXYGEN
  444. friend struct detail::access;
  445. #endif
  446. };
  447. } // namespace mysql
  448. } // namespace boost
  449. #endif