results.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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_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. * For operations returning more than one resultset, returns the
  173. * first resultset's affected rows.
  174. *
  175. * \par Preconditions
  176. * `this->has_value() == true`
  177. *
  178. * \par Exception safety
  179. * No-throw guarantee.
  180. *
  181. * \par Complexity
  182. * Constant.
  183. */
  184. std::uint64_t affected_rows() const noexcept
  185. {
  186. BOOST_ASSERT(has_value());
  187. return impl_.get_affected_rows(0);
  188. }
  189. /**
  190. * \brief Returns the last insert ID produced by the executed SQL statement.
  191. * \details
  192. * For operations returning more than one resultset, returns the
  193. * first resultset's last insert ID.
  194. *
  195. * \par Preconditions
  196. * `this->has_value() == true`
  197. *
  198. * \par Exception safety
  199. * No-throw guarantee.
  200. *
  201. * \par Complexity
  202. * Constant.
  203. */
  204. std::uint64_t last_insert_id() const noexcept
  205. {
  206. BOOST_ASSERT(has_value());
  207. return impl_.get_last_insert_id(0);
  208. }
  209. /**
  210. * \brief Returns the number of warnings produced by the executed SQL statement.
  211. * \details
  212. * For operations returning more than one resultset, returns the
  213. * first resultset's warning count.
  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. unsigned warning_count() const noexcept
  225. {
  226. BOOST_ASSERT(has_value());
  227. return impl_.get_warning_count(0);
  228. }
  229. /**
  230. * \brief Returns additional text information about the execution of the SQL statement.
  231. * \details
  232. * The format of this information is documented by MySQL <a
  233. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  234. * \n
  235. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  236. * \n
  237. * For operations returning more than one resultset, returns the
  238. * first resultset's info.
  239. *
  240. * \par Preconditions
  241. * `this->has_value() == true`
  242. *
  243. * \par Exception safety
  244. * No-throw guarantee.
  245. *
  246. * \par Object lifetimes
  247. * This function returns a view object, with reference semantics. The returned view points into
  248. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  249. * from `*this` are alive.
  250. *
  251. * \par Complexity
  252. * Constant.
  253. */
  254. string_view info() const noexcept
  255. {
  256. BOOST_ASSERT(has_value());
  257. return impl_.get_info(0);
  258. }
  259. /**
  260. * \brief Returns an iterator pointing to the first resultset that this object contains.
  261. * \par Preconditions
  262. * `this->has_value() == true`
  263. *
  264. * \par Exception safety
  265. * No-throw guarantee.
  266. *
  267. * \par Object lifetimes
  268. * The returned iterator and any reference obtained from it are valid as long as
  269. * `*this` is alive. Move operations invalidate iterators.
  270. *
  271. * \par Complexity
  272. * Constant.
  273. */
  274. iterator begin() const noexcept
  275. {
  276. BOOST_ASSERT(has_value());
  277. return iterator(&impl_, 0);
  278. }
  279. /**
  280. * \brief Returns an iterator pointing to one-past-the-last resultset that this object contains.
  281. * \par Preconditions
  282. * `this->has_value() == true`
  283. *
  284. * \par Exception safety
  285. * No-throw guarantee.
  286. *
  287. * \par Object lifetimes
  288. * The returned iterator and any reference obtained from it are valid as long as
  289. * `*this` is alive. Move operations invalidate iterators.
  290. *
  291. * \par Complexity
  292. * Constant.
  293. */
  294. iterator end() const noexcept
  295. {
  296. BOOST_ASSERT(has_value());
  297. return iterator(&impl_, size());
  298. }
  299. /**
  300. * \brief Returns the i-th resultset or throws an exception.
  301. * \par Preconditions
  302. * `this->has_value() == true`
  303. *
  304. * \par Exception safety
  305. * Strong guranatee. Throws on invalid input.
  306. * \throws std::out_of_range `i >= this->size()`
  307. *
  308. * \par Object lifetimes
  309. * The returned reference and any other references obtained from it are valid as long as
  310. * `*this` is alive. Move operations invalidate references.
  311. *
  312. * \par Complexity
  313. * Constant.
  314. */
  315. inline resultset_view at(std::size_t i) const
  316. {
  317. BOOST_ASSERT(has_value());
  318. if (i >= size())
  319. BOOST_THROW_EXCEPTION(std::out_of_range("results::at: out of range"));
  320. return detail::access::construct<resultset_view>(impl_, i);
  321. }
  322. /**
  323. * \brief Returns the i-th resultset (unchecked access).
  324. * \par Preconditions
  325. * `this->has_value() == true && i < this->size()`
  326. *
  327. * \par Exception safety
  328. * No-throw guarantee.
  329. *
  330. * \par Object lifetimes
  331. * The returned reference and any other references obtained from it are valid as long as
  332. * `*this` is alive. Move operations invalidate references.
  333. *
  334. * \par Complexity
  335. * Constant.
  336. */
  337. resultset_view operator[](std::size_t i) const noexcept
  338. {
  339. BOOST_ASSERT(has_value());
  340. BOOST_ASSERT(i < size());
  341. return detail::access::construct<resultset_view>(impl_, i);
  342. }
  343. /**
  344. * \brief Returns the first resultset.
  345. * \par Preconditions
  346. * `this->has_value() == true`
  347. *
  348. * \par Exception safety
  349. * No-throw guarantee.
  350. *
  351. * \par Object lifetimes
  352. * The returned reference and any other references obtained from it are valid as long as
  353. * `*this` is alive. Move operations invalidate references.
  354. *
  355. * \par Complexity
  356. * Constant.
  357. */
  358. resultset_view front() const noexcept { return (*this)[0]; }
  359. /**
  360. * \brief Returns the last resultset.
  361. * \par Preconditions
  362. * `this->has_value() == true`
  363. *
  364. * \par Exception safety
  365. * No-throw guarantee.
  366. *
  367. * \par Object lifetimes
  368. * The returned reference and any other references obtained from it are valid as long as
  369. * `*this` is alive. Move operations invalidate references.
  370. *
  371. * \par Complexity
  372. * Constant.
  373. */
  374. resultset_view back() const noexcept { return (*this)[size() - 1]; }
  375. /**
  376. * \brief Returns whether the collection contains any resultset.
  377. * \details
  378. * This function is provided for compatibility with standard collections,
  379. * and always returns false, since any valid `results` contains at least one resultset.
  380. *
  381. * \par Preconditions
  382. * `this->has_value() == true`
  383. *
  384. * \par Exception safety
  385. * No-throw guarantee.
  386. *
  387. * \par Complexity
  388. * Constant.
  389. */
  390. bool empty() const noexcept
  391. {
  392. BOOST_ASSERT(has_value());
  393. return false;
  394. }
  395. /**
  396. * \brief Returns the number of resultsets that this collection contains.
  397. * \par Preconditions
  398. * `this->has_value() == true`
  399. *
  400. * \par Exception safety
  401. * No-throw guarantee.
  402. *
  403. * \par Complexity
  404. * Constant.
  405. */
  406. std::size_t size() const noexcept
  407. {
  408. BOOST_ASSERT(has_value());
  409. return impl_.num_resultsets();
  410. }
  411. /**
  412. * \brief Returns the output parameters of a stored procedure call.
  413. * \details
  414. * Relevant for `CALL` operations performed using prepared statements that
  415. * bind placeholders to `OUT` or `INOUT` parameters. Returns a row containing a field per
  416. * bound output parameter.
  417. * \n
  418. * If this operation had no output parameters (e.g. it wasn't a `CALL`), returns an empty row.
  419. *
  420. * \par Preconditions
  421. * `this->has_value() == true`
  422. *
  423. * \par Exception safety
  424. * No-throw guarantee.
  425. *
  426. * \par Object lifetimes
  427. * The returned reference and any other references obtained from it are valid as long as
  428. * `*this` is alive. Move operations invalidate references.
  429. *
  430. * \par Complexity
  431. * Linear on `this->size()`.
  432. */
  433. row_view out_params() const noexcept
  434. {
  435. BOOST_ASSERT(has_value());
  436. return impl_.get_out_params();
  437. }
  438. private:
  439. detail::results_impl impl_;
  440. #ifndef BOOST_MYSQL_DOXYGEN
  441. friend struct detail::access;
  442. #endif
  443. };
  444. } // namespace mysql
  445. } // namespace boost
  446. #endif