statement.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_STATEMENT_HPP
  8. #define BOOST_MYSQL_STATEMENT_HPP
  9. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  10. #include <cassert>
  11. #include <cstdint>
  12. namespace boost {
  13. namespace mysql {
  14. /**
  15. * \brief Represents a server-side prepared statement.
  16. * \details
  17. * This is a lightweight class, holding a handle to a server-side prepared statement.
  18. * \n
  19. * Note that statement's destructor doesn't deallocate the statement from the
  20. * server, as this implies a network transfer that may fail.
  21. *
  22. * \par Thread safety
  23. * Distinct objects: safe. \n
  24. * Shared objects: unsafe. \n
  25. */
  26. class statement
  27. {
  28. public:
  29. /**
  30. * \brief Default constructor.
  31. * \details Default constructed statements have `this->valid() == false`.
  32. *
  33. * \par Exception safety
  34. * No-throw guarantee.
  35. */
  36. statement() = default;
  37. /**
  38. * \brief Returns `true` if the object represents an actual server statement.
  39. * \details Calling any function other than assignment on a statement for which
  40. * this function returns `false` results in undefined behavior.
  41. * \n
  42. * Returns `false` for default-constructed statements.
  43. *
  44. * \par Exception safety
  45. * No-throw guarantee.
  46. */
  47. bool valid() const noexcept { return valid_; }
  48. /**
  49. * \brief Returns a server-side identifier for the statement (unique in a per-connection basis).
  50. * \details Note that, once a statement is closed, the server may recycle its ID.
  51. *
  52. * \par Preconditions
  53. * `this->valid() == true`
  54. *
  55. * \par Exception safety
  56. * No-throw guarantee.
  57. */
  58. std::uint32_t id() const noexcept
  59. {
  60. assert(valid());
  61. return id_;
  62. }
  63. /**
  64. * \brief Returns the number of parameters that should be provided when executing the statement.
  65. * \par Preconditions
  66. * `this->valid() == true`
  67. *
  68. * \par Exception safety
  69. * No-throw guarantee.
  70. */
  71. unsigned num_params() const noexcept
  72. {
  73. assert(valid());
  74. return num_params_;
  75. }
  76. private:
  77. bool valid_{false};
  78. std::uint32_t id_{0};
  79. std::uint16_t num_params_{0};
  80. #ifndef BOOST_MYSQL_DOXYGEN
  81. friend struct detail::statement_access;
  82. #endif
  83. };
  84. } // namespace mysql
  85. } // namespace boost
  86. #include <boost/mysql/impl/statement.hpp>
  87. #endif