execution_state.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Copyright (c) 2019-2023 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_EXECUTION_STATE_HPP
  8. #define BOOST_MYSQL_EXECUTION_STATE_HPP
  9. #include <boost/mysql/metadata.hpp>
  10. #include <boost/mysql/metadata_collection_view.hpp>
  11. #include <boost/mysql/string_view.hpp>
  12. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  13. #include <boost/mysql/detail/protocol/common_messages.hpp>
  14. #include <boost/mysql/detail/protocol/resultset_encoding.hpp>
  15. #include <cassert>
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <vector>
  19. namespace boost {
  20. namespace mysql {
  21. /**
  22. * \brief Holds state for multi-function SQL execution operations.
  23. * \par Thread safety
  24. * Distinct objects: safe. \n
  25. * Shared objects: unsafe.
  26. */
  27. class execution_state
  28. {
  29. public:
  30. /**
  31. * \brief Default constructor.
  32. * \details The constructed object is guaranteed to have `meta().empty()` and
  33. * `!complete()`.
  34. *
  35. * \par Exception safety
  36. * No-throw guarantee.
  37. */
  38. execution_state() = default;
  39. /**
  40. * \brief Returns whether all the messages generated by this operation have been read.
  41. * \details
  42. * Once `complete`, you may access extra information about the operation, like
  43. * \ref affected_rows or \ref last_insert_id.
  44. *
  45. * \par Exception safety
  46. * No-throw guarantee.
  47. */
  48. bool complete() const noexcept { return eof_received_; }
  49. /**
  50. * \brief Returns metadata about the columns in the query.
  51. * \details
  52. * The returned collection will have as many \ref metadata objects as columns retrieved by
  53. * the SQL query, and in the same order.
  54. *
  55. * \par Exception safety
  56. * No-throw guarantee.
  57. *
  58. * \par Object lifetimes
  59. * This function returns a view object, with reference semantics. The returned view points into
  60. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  61. * from `*this` are alive.
  62. */
  63. metadata_collection_view meta() const noexcept
  64. {
  65. return metadata_collection_view(meta_.data(), meta_.size());
  66. }
  67. /**
  68. * \brief Returns the number of rows affected by the executed SQL statement.
  69. * \par Exception safety
  70. * No-throw guarantee.
  71. *
  72. * \par Preconditions
  73. * `this->complete() == true`
  74. */
  75. std::uint64_t affected_rows() const noexcept
  76. {
  77. assert(complete());
  78. return affected_rows_;
  79. }
  80. /**
  81. * \brief Returns the last insert ID produced by the executed SQL statement.
  82. * \par Exception safety
  83. * No-throw guarantee.
  84. *
  85. * \par Preconditions
  86. * `this->complete() == true`
  87. */
  88. std::uint64_t last_insert_id() const noexcept
  89. {
  90. assert(complete());
  91. return last_insert_id_;
  92. }
  93. /**
  94. * \brief Returns the number of warnings produced by the executed SQL statement.
  95. * \par Exception safety
  96. * No-throw guarantee.
  97. *
  98. * \par Preconditions
  99. * `this->complete() == true`
  100. */
  101. unsigned warning_count() const noexcept
  102. {
  103. assert(complete());
  104. return warnings_;
  105. }
  106. /**
  107. * \brief Returns additionat text information about the execution of the SQL statement.
  108. * \details
  109. * The format of this information is documented by MySQL <a
  110. * href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html">here</a>.
  111. * \n
  112. * The returned string always uses ASCII encoding, regardless of the connection's character set.
  113. *
  114. * \par Exception safety
  115. * No-throw guarantee.
  116. *
  117. * \par Preconditions
  118. * `this->complete() == true`
  119. *
  120. * \par Object lifetimes
  121. * This function returns a view object, with reference semantics. The returned view points into
  122. * memory owned by `*this`, and will be valid as long as `*this` or an object move-constructed
  123. * from `*this` are alive.
  124. */
  125. string_view info() const noexcept
  126. {
  127. assert(complete());
  128. return string_view(info_.data(), info_.size());
  129. }
  130. private:
  131. bool eof_received_{false};
  132. std::uint8_t seqnum_{};
  133. detail::resultset_encoding encoding_{detail::resultset_encoding::text};
  134. std::vector<metadata> meta_;
  135. std::uint64_t affected_rows_{};
  136. std::uint64_t last_insert_id_{};
  137. std::uint16_t warnings_{};
  138. std::vector<char> info_; // guarantee that no SBO is used
  139. #ifndef BOOST_MYSQL_DOXYGEN
  140. friend struct detail::execution_state_access;
  141. #endif
  142. };
  143. } // namespace mysql
  144. } // namespace boost
  145. #include <boost/mysql/impl/execution_state.hpp>
  146. #endif