any_connection.hpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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_ANY_CONNECTION_HPP
  8. #define BOOST_MYSQL_ANY_CONNECTION_HPP
  9. #include <boost/mysql/any_address.hpp>
  10. #include <boost/mysql/character_set.hpp>
  11. #include <boost/mysql/connect_params.hpp>
  12. #include <boost/mysql/defaults.hpp>
  13. #include <boost/mysql/diagnostics.hpp>
  14. #include <boost/mysql/error_code.hpp>
  15. #include <boost/mysql/execution_state.hpp>
  16. #include <boost/mysql/metadata_mode.hpp>
  17. #include <boost/mysql/rows_view.hpp>
  18. #include <boost/mysql/statement.hpp>
  19. #include <boost/mysql/string_view.hpp>
  20. #include <boost/mysql/with_diagnostics.hpp>
  21. #include <boost/mysql/detail/access.hpp>
  22. #include <boost/mysql/detail/algo_params.hpp>
  23. #include <boost/mysql/detail/config.hpp>
  24. #include <boost/mysql/detail/connect_params_helpers.hpp>
  25. #include <boost/mysql/detail/connection_impl.hpp>
  26. #include <boost/mysql/detail/engine.hpp>
  27. #include <boost/mysql/detail/execution_concepts.hpp>
  28. #include <boost/mysql/detail/ssl_fwd.hpp>
  29. #include <boost/mysql/detail/throw_on_error_loc.hpp>
  30. #include <boost/asio/any_io_executor.hpp>
  31. #include <boost/asio/deferred.hpp>
  32. #include <boost/assert.hpp>
  33. #include <boost/optional/optional.hpp>
  34. #include <boost/system/result.hpp>
  35. #include <cstddef>
  36. #include <cstdint>
  37. #include <memory>
  38. #include <type_traits>
  39. #include <utility>
  40. #include <vector>
  41. namespace boost {
  42. namespace mysql {
  43. // Forward declarations
  44. template <class... StaticRow>
  45. class static_execution_state;
  46. class pipeline_request;
  47. class stage_response;
  48. /**
  49. * \brief Configuration parameters that can be passed to \ref any_connection's constructor.
  50. */
  51. struct any_connection_params
  52. {
  53. /**
  54. * \brief An external SSL context containing options to configure TLS.
  55. * \details
  56. * Relevant only for SSL connections (those that result on \ref
  57. * any_connection::uses_ssl returning `true`).
  58. * \n
  59. * If the connection is configured to use TLS, an internal `asio::ssl::stream`
  60. * object will be created. If this member is set to a non-null value,
  61. * this internal object will be initialized using the passed context.
  62. * This is the only way to configure TLS options in `any_connection`.
  63. * \n
  64. * If the connection is configured to use TLS and this member is `nullptr`,
  65. * an internal `asio::ssl::context` object with suitable default options
  66. * will be created.
  67. *
  68. * \par Object lifetimes
  69. * If set to non-null, the pointee object must be kept alive until
  70. * all \ref any_connection objects constructed from `*this` are destroyed.
  71. */
  72. asio::ssl::context* ssl_context{};
  73. /**
  74. * \brief The initial size of the connection's buffer, in bytes.
  75. * \details A bigger read buffer can increase the number of rows
  76. * returned by \ref any_connection::read_some_rows.
  77. */
  78. std::size_t initial_buffer_size{default_initial_read_buffer_size};
  79. /**
  80. * \brief The maximum size of the connection's buffer, in bytes (64MB by default).
  81. * \details
  82. * Attempting to read or write a protocol packet bigger than this size
  83. * will fail with a \ref client_errc::max_buffer_size_exceeded error.
  84. * \n
  85. * This effectively means: \n
  86. * - Each request sent to the server must be smaller than this value.
  87. * - Each individual row received from the server must be smaller than this value.
  88. * Note that when using `execute` or `async_execute`, results objects may
  89. * allocate memory beyond this limit if the total number of rows is high.
  90. * \n
  91. * If you need to send or receive larger packets, you may need to adjust
  92. * your server's <a
  93. * href="https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_max_allowed_packet">`max_allowed_packet`</a>
  94. * system variable, too.
  95. */
  96. std::size_t max_buffer_size{0x4000000};
  97. };
  98. /**
  99. * \brief A connection to a MySQL server.
  100. * \details
  101. * Represents a connection to a MySQL server.
  102. * This is the main I/O object that this library implements. It's logically comprised
  103. * of session state and an internal stream (usually a socket). The stream is not directly
  104. * accessible. It's constructed using the executor passed to the constructor.
  105. *
  106. * This class supports establishing connections
  107. * with servers using TCP, TCP over TLS and UNIX sockets.
  108. *
  109. * The class is named `any_connection` because it's not templated on a `Stream`
  110. * type, as opposed to \ref connection. New code should prefer using `any_connection`
  111. * whenever possible.
  112. *
  113. * Compared to \ref connection, this class:
  114. *
  115. * - Is type-erased. The type of the connection doesn't depend on the transport being used.
  116. * - Is easier to connect, as \ref connect and \ref async_connect handle hostname resolution.
  117. * - Can always be re-connected after being used or encountering an error.
  118. * - Always uses `asio::any_io_executor`.
  119. * - Has the same level of performance.
  120. *
  121. * This is a move-only type.
  122. *
  123. * \par Single outstanding async operation per connection
  124. * At any given point in time, only one async operation can be outstanding
  125. * per connection. If an async operation is initiated while another one is in progress,
  126. * it will fail with \ref client_errc::operation_in_progress.
  127. *
  128. * \par Default completion tokens
  129. * The default completion token for all async operations in this class is
  130. * `with_diagnostics(asio::deferred)`, which allows you to use `co_await`
  131. * and have the expected exceptions thrown on error.
  132. *
  133. * \par Thread safety
  134. * Distinct objects: safe. \n
  135. * Shared objects: unsafe. \n
  136. * This class is <b>not thread-safe</b>: for a single object, if you
  137. * call its member functions concurrently from separate threads, you will get a race condition.
  138. */
  139. class any_connection
  140. {
  141. detail::connection_impl impl_;
  142. #ifndef BOOST_MYSQL_DOXYGEN
  143. friend struct detail::access;
  144. #endif
  145. BOOST_MYSQL_DECL
  146. static std::unique_ptr<detail::engine> create_engine(asio::any_io_executor ex, asio::ssl::context* ctx);
  147. // Used by tests
  148. any_connection(std::unique_ptr<detail::engine> eng, any_connection_params params)
  149. : impl_(params.initial_buffer_size, params.max_buffer_size, std::move(eng))
  150. {
  151. }
  152. public:
  153. /**
  154. * \brief Constructs a connection object from an executor and an optional set of parameters.
  155. * \details
  156. * The resulting connection has `this->get_executor() == ex`. Any internally required I/O objects
  157. * will be constructed using this executor.
  158. * \n
  159. * You can configure extra parameters, like the SSL context and buffer sizes, by passing
  160. * an \ref any_connection_params object to this constructor.
  161. */
  162. any_connection(boost::asio::any_io_executor ex, any_connection_params params = {})
  163. : any_connection(create_engine(std::move(ex), params.ssl_context), params)
  164. {
  165. }
  166. /**
  167. * \brief Constructs a connection object from an execution context and an optional set of parameters.
  168. * \details
  169. * The resulting connection has `this->get_executor() == ctx.get_executor()`.
  170. * Any internally required I/O objects will be constructed using this executor.
  171. * \n
  172. * You can configure extra parameters, like the SSL context and buffer sizes, by passing
  173. * an \ref any_connection_params object to this constructor.
  174. * \n
  175. * This function participates in overload resolution only if `ExecutionContext`
  176. * satisfies the `ExecutionContext` requirements imposed by Boost.Asio.
  177. */
  178. template <
  179. class ExecutionContext
  180. #ifndef BOOST_MYSQL_DOXYGEN
  181. ,
  182. class = typename std::enable_if<std::is_convertible<
  183. decltype(std::declval<ExecutionContext&>().get_executor()),
  184. asio::any_io_executor>::value>::type
  185. #endif
  186. >
  187. any_connection(ExecutionContext& ctx, any_connection_params params = {})
  188. : any_connection(ctx.get_executor(), params)
  189. {
  190. }
  191. /**
  192. * \brief Move constructor.
  193. */
  194. any_connection(any_connection&& other) = default;
  195. /**
  196. * \brief Move assignment.
  197. */
  198. any_connection& operator=(any_connection&& rhs) = default;
  199. #ifndef BOOST_MYSQL_DOXYGEN
  200. any_connection(const any_connection&) = delete;
  201. any_connection& operator=(const any_connection&) = delete;
  202. #endif
  203. /**
  204. * \brief Destructor.
  205. * \details
  206. * Closes the connection at the transport layer (by closing any underlying socket objects).
  207. * If you require a clean close, call \ref close or \ref async_close before the connection
  208. * is destroyed.
  209. */
  210. ~any_connection() = default;
  211. /// The executor type associated to this object.
  212. using executor_type = asio::any_io_executor;
  213. /**
  214. * \brief Retrieves the executor associated to this object.
  215. * \par Exception safety
  216. * No-throw guarantee.
  217. */
  218. executor_type get_executor() noexcept { return impl_.get_engine().get_executor(); }
  219. /**
  220. * \brief Returns whether the connection negotiated the use of SSL or not.
  221. * \details
  222. * This function can be used to determine whether you are using a SSL
  223. * connection or not when using SSL negotiation.
  224. * \n
  225. * This function always returns `false`
  226. * for connections that haven't been established yet. If the connection establishment fails,
  227. * the return value is undefined.
  228. *
  229. * \par Exception safety
  230. * No-throw guarantee.
  231. */
  232. bool uses_ssl() const noexcept { return impl_.ssl_active(); }
  233. /**
  234. * \brief Returns whether backslashes are being treated as escape sequences.
  235. * \details
  236. * By default, the server treats backslashes in string values as escape characters.
  237. * This behavior can be disabled by activating the <a
  238. * href="https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_no_backslash_escapes">`NO_BACKSLASH_ESCAPES`</a>
  239. * SQL mode.
  240. * \n
  241. * Every time an operation involving server communication completes, the server reports whether
  242. * this mode was activated or not as part of the response. Connections store this information
  243. * and make it available through this function.
  244. * \n
  245. * \li If backslash are treated like escape characters, returns `true`.
  246. * \li If `NO_BACKSLASH_ESCAPES` has been activated, returns `false`.
  247. * \li If connection establishment hasn't happened yet, returns `true`.
  248. * \li Calling this function while an async operation that changes backslash behavior
  249. * is outstanding may return `true` or `false`.
  250. * \n
  251. * This function does not involve server communication.
  252. *
  253. * \par Exception safety
  254. * No-throw guarantee.
  255. */
  256. bool backslash_escapes() const noexcept { return impl_.backslash_escapes(); }
  257. /**
  258. * \brief Returns the character set used by this connection.
  259. * \details
  260. * Connections attempt to keep track of the current character set.
  261. * Deficiencies in the protocol can cause the character set to be unknown, though.
  262. * When the character set is known, this function returns
  263. * the character set currently in use. Otherwise, returns \ref client_errc::unknown_character_set.
  264. * \n
  265. * The following functions can modify the return value of this function: \n
  266. * \li Prior to connection, the character set is always unknown.
  267. * \li \ref connect and \ref async_connect may set the current character set
  268. * to a known value, depending on the requested collation.
  269. * \li \ref set_character_set always and \ref async_set_character_set always
  270. * set the current character set to the passed value.
  271. * \li \ref reset_connection and \ref async_reset_connection always makes the current character
  272. * unknown.
  273. *
  274. * \par Avoid changing the character set directly
  275. * If you change the connection's character set directly using SQL statements
  276. * like `"SET NAMES utf8mb4"`, the client has no way to track this change,
  277. * and this function will return incorrect results.
  278. *
  279. * \par Errors
  280. * \li \ref client_errc::unknown_character_set if the current character set is unknown.
  281. *
  282. * \par Exception safety
  283. * No-throw guarantee.
  284. */
  285. system::result<character_set> current_character_set() const noexcept
  286. {
  287. return impl_.current_character_set();
  288. }
  289. /**
  290. * \brief Returns format options suitable to format SQL according to the current connection configuration.
  291. * \details
  292. * If the current character set is known (as given by \ref current_character_set), returns
  293. * a value suitable to be passed to SQL formatting functions. Otherwise, returns an error.
  294. *
  295. * \par Errors
  296. * \li \ref client_errc::unknown_character_set if the current character set is unknown.
  297. *
  298. * \par Exception safety
  299. * No-throw guarantee.
  300. */
  301. system::result<format_options> format_opts() const noexcept
  302. {
  303. auto res = current_character_set();
  304. if (res.has_error())
  305. return res.error();
  306. return format_options{res.value(), backslash_escapes()};
  307. }
  308. /**
  309. * \brief Returns the current metadata mode that this connection is using.
  310. * \details
  311. * \par Exception safety
  312. * No-throw guarantee.
  313. *
  314. * \returns The metadata mode that will be used for queries and statement executions.
  315. */
  316. metadata_mode meta_mode() const noexcept { return impl_.meta_mode(); }
  317. /**
  318. * \brief Sets the metadata mode.
  319. * \details
  320. * Will affect any query and statement executions performed after the call.
  321. *
  322. * \par Exception safety
  323. * No-throw guarantee.
  324. *
  325. * \par Preconditions
  326. * No asynchronous operation should be outstanding when this function is called.
  327. *
  328. * \param v The new metadata mode.
  329. */
  330. void set_meta_mode(metadata_mode v) noexcept { impl_.set_meta_mode(v); }
  331. /**
  332. * \brief Retrieves the connection id associated to the current session.
  333. * \details
  334. * If a session has been established, returns its associated connection id.
  335. * If no session has been established (i.e. \ref async_connect hasn't been called yet)
  336. * or the session has been terminated (i.e. \ref async_close has been called), an empty
  337. * optional is returned.
  338. *
  339. * The connection id is a 4 byte value that uniquely identifies a client session
  340. * at a given point in time. It can be used with the
  341. * <a href="https://dev.mysql.com/doc/refman/8.4/en/kill.html">`KILL`</a> SQL statement
  342. * to cancel queries and terminate connections.
  343. *
  344. * The server sends the connection id assigned to the current session as part of the
  345. * handshake process. The value is stored and made available through this function.
  346. * The same id can also be obtained by calling the
  347. * <a
  348. * href="https://dev.mysql.com/doc/refman/8.4/en/information-functions.html#function_connection-id">CONNECTION_ID()</a>
  349. * SQL function. However, this function is faster and more reliable, since it does not entail
  350. * communication with the server.
  351. *
  352. * This function is equivalent to the
  353. * <a href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-thread-id.html">`mysql_thread_id`</a> function
  354. * in the C connector. This function works properly in 64-bit systems, as opposed to what
  355. * the official docs suggest (see
  356. * <a href="https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-5.html">this changelog</a>).
  357. *
  358. * It is safe to call this function while an async operation is outstanding, except for \ref async_connect
  359. * and \ref async_close.
  360. *
  361. * \par Exception safety
  362. * No-throw guarantee.
  363. */
  364. boost::optional<std::uint32_t> connection_id() const noexcept { return impl_.connection_id(); }
  365. /**
  366. * \brief Establishes a connection to a MySQL server.
  367. * \details
  368. * This function performs the following:
  369. * \n
  370. * \li If a connection has already been established (by a previous call to \ref connect
  371. * or \ref async_connect), closes it at the transport layer (by closing any underlying socket)
  372. * and discards any protocol state associated to it. (If you require
  373. * a clean close, call \ref close or \ref async_close before using this function).
  374. * \li If the connection is configured to use TCP (`params.server_address.type() ==
  375. * address_type::host_and_port`), resolves the passed hostname to a set of endpoints. An empty
  376. * hostname is equivalent to `"localhost"`.
  377. * \li Establishes the physical connection (performing the
  378. * TCP or UNIX socket connect).
  379. * \li Performs the MySQL handshake to establish a session. If the
  380. * connection is configured to use TLS, the TLS handshake is performed as part of this step.
  381. * \li If any of the above steps fail, the TCP or UNIX socket connection is closed.
  382. * \n
  383. * You can configure some options using the \ref connect_params struct.
  384. * \n
  385. * The decision to use TLS or not is performed using the following:
  386. * \n
  387. * \li If the transport is not TCP (`params.server_address.type() != address_type::host_and_port`),
  388. * the connection will never use TLS.
  389. * \li If the transport is TCP, and `params.ssl == ssl_mode::disable`, the connection will not use TLS.
  390. * \li If the transport is TCP, and `params.ssl == ssl_mode::enable`, the connection will use TLS
  391. * only if the server supports it.
  392. * \li If the transport is TCP, and `params.ssl == ssl_mode::require`, the connection will always use TLS.
  393. * If the server doesn't support it, the operation will fail with \ref
  394. * client_errc::server_doesnt_support_ssl.
  395. * \n
  396. * If `params.connection_collation` is within a set of well-known collations, this function
  397. * sets the current character set, such that \ref current_character_set returns a non-null value.
  398. * The default collation (`utf8mb4_general_ci`) is the only one guaranteed to be in the set of well-known
  399. * collations.
  400. */
  401. void connect(const connect_params& params, error_code& ec, diagnostics& diag)
  402. {
  403. impl_.connect_v2(params, ec, diag);
  404. }
  405. /// \copydoc connect
  406. void connect(const connect_params& params)
  407. {
  408. error_code err;
  409. diagnostics diag;
  410. connect(params, err, diag);
  411. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  412. }
  413. /**
  414. * \copydoc connect
  415. *
  416. * \par Object lifetimes
  417. * params needs to be kept alive until the operation completes, as no
  418. * copies will be made by the library.
  419. *
  420. * \par Handler signature
  421. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  422. *
  423. * \par Executor
  424. * Intermediate completion handlers, as well as the final handler, are executed using
  425. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  426. * executor.
  427. *
  428. * If the final handler has an associated immediate executor, and the operation
  429. * completes immediately, the final handler is dispatched to it.
  430. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  431. * and is never be called inline from within this function.
  432. */
  433. template <
  434. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  435. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  436. auto async_connect(const connect_params& params, diagnostics& diag, CompletionToken&& token = {})
  437. BOOST_MYSQL_RETURN_TYPE(detail::async_connect_v2_t<CompletionToken&&>)
  438. {
  439. return impl_.async_connect_v2(params, diag, std::forward<CompletionToken>(token));
  440. }
  441. /// \copydoc async_connect
  442. template <
  443. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  444. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  445. auto async_connect(const connect_params& params, CompletionToken&& token = {})
  446. BOOST_MYSQL_RETURN_TYPE(detail::async_connect_v2_t<CompletionToken&&>)
  447. {
  448. return async_connect(params, impl_.shared_diag(), std::forward<CompletionToken>(token));
  449. }
  450. /**
  451. * \brief Executes a text query or prepared statement.
  452. * \details
  453. * Sends `req` to the server for execution and reads the response into `result`.
  454. * `result` may be either a \ref results or \ref static_results object.
  455. * `req` should may be either a type convertible to \ref string_view containing valid SQL
  456. * or a bound prepared statement, obtained by calling \ref statement::bind.
  457. * If a string, it must be encoded using the connection's character set.
  458. * Any string parameters provided to \ref statement::bind should also be encoded
  459. * using the connection's character set.
  460. * \n
  461. * After this operation completes successfully, `result.has_value() == true`.
  462. * \n
  463. * Metadata in `result` will be populated according to `this->meta_mode()`.
  464. */
  465. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  466. void execute(ExecutionRequest&& req, ResultsType& result, error_code& err, diagnostics& diag)
  467. {
  468. impl_.execute(std::forward<ExecutionRequest>(req), result, err, diag);
  469. }
  470. /// \copydoc execute
  471. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  472. void execute(ExecutionRequest&& req, ResultsType& result)
  473. {
  474. error_code err;
  475. diagnostics diag;
  476. execute(std::forward<ExecutionRequest>(req), result, err, diag);
  477. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  478. }
  479. /**
  480. * \copydoc execute
  481. * \par Object lifetimes
  482. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  483. * responsible for managing `req`'s validity following these rules:
  484. * \n
  485. * \li If `req` is `string_view`, the string pointed to by `req`
  486. * must be kept alive by the caller until the operation is initiated.
  487. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  488. * type (like `string_view`), the caller must keep the values pointed by these references alive
  489. * until the operation is initiated.
  490. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  491. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  492. *
  493. * \par Handler signature
  494. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  495. *
  496. * \par Executor
  497. * Intermediate completion handlers, as well as the final handler, are executed using
  498. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  499. * executor.
  500. *
  501. * If the final handler has an associated immediate executor, and the operation
  502. * completes immediately, the final handler is dispatched to it.
  503. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  504. * and is never be called inline from within this function.
  505. */
  506. template <
  507. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  508. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  509. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  510. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  511. auto async_execute(ExecutionRequest&& req, ResultsType& result, CompletionToken&& token = {})
  512. BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  513. {
  514. return async_execute(
  515. std::forward<ExecutionRequest>(req),
  516. result,
  517. impl_.shared_diag(),
  518. std::forward<CompletionToken>(token)
  519. );
  520. }
  521. /// \copydoc async_execute
  522. template <
  523. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  524. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  525. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  526. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  527. auto async_execute(
  528. ExecutionRequest&& req,
  529. ResultsType& result,
  530. diagnostics& diag,
  531. CompletionToken&& token = {}
  532. ) BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  533. {
  534. return impl_.async_execute(
  535. std::forward<ExecutionRequest>(req),
  536. result,
  537. diag,
  538. std::forward<CompletionToken>(token)
  539. );
  540. }
  541. /**
  542. * \brief Starts a SQL execution as a multi-function operation.
  543. * \details
  544. * Writes the execution request and reads the initial server response and the column
  545. * metadata, but not the generated rows or subsequent resultsets, if any.
  546. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  547. * \n
  548. * After this operation completes, `st` will have
  549. * \ref execution_state::meta populated.
  550. * Metadata will be populated according to `this->meta_mode()`.
  551. * \n
  552. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  553. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  554. * Otherwise, the results are undefined.
  555. * \n
  556. * req may be either a type convertible to \ref string_view containing valid SQL
  557. * or a bound prepared statement, obtained by calling \ref statement::bind.
  558. * If a string, it must be encoded using the connection's character set.
  559. * Any string parameters provided to \ref statement::bind should also be encoded
  560. * using the connection's character set.
  561. * \n
  562. * When using the static interface, this function will detect schema mismatches for the first
  563. * resultset. Further errors may be detected by \ref read_resultset_head and \ref read_some_rows.
  564. * \n
  565. */
  566. template <
  567. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  568. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  569. void start_execution(ExecutionRequest&& req, ExecutionStateType& st, error_code& err, diagnostics& diag)
  570. {
  571. impl_.start_execution(std::forward<ExecutionRequest>(req), st, err, diag);
  572. }
  573. /// \copydoc start_execution
  574. template <
  575. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  576. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  577. void start_execution(ExecutionRequest&& req, ExecutionStateType& st)
  578. {
  579. error_code err;
  580. diagnostics diag;
  581. start_execution(std::forward<ExecutionRequest>(req), st, err, diag);
  582. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  583. }
  584. /**
  585. * \copydoc start_execution
  586. * \par Object lifetimes
  587. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  588. * responsible for managing `req`'s validity following these rules:
  589. * \n
  590. * \li If `req` is `string_view`, the string pointed to by `req`
  591. * must be kept alive by the caller until the operation is initiated.
  592. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  593. * type (like `string_view`), the caller must keep the values pointed by these references alive
  594. * until the operation is initiated.
  595. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  596. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  597. *
  598. * \par Handler signature
  599. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  600. *
  601. * \par Executor
  602. * Intermediate completion handlers, as well as the final handler, are executed using
  603. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  604. * executor.
  605. *
  606. * If the final handler has an associated immediate executor, and the operation
  607. * completes immediately, the final handler is dispatched to it.
  608. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  609. * and is never be called inline from within this function.
  610. */
  611. template <
  612. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  613. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  614. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  615. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  616. auto async_start_execution(ExecutionRequest&& req, ExecutionStateType& st, CompletionToken&& token = {})
  617. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  618. ExecutionRequest&&,
  619. ExecutionStateType,
  620. CompletionToken&&>)
  621. {
  622. return async_start_execution(
  623. std::forward<ExecutionRequest>(req),
  624. st,
  625. impl_.shared_diag(),
  626. std::forward<CompletionToken>(token)
  627. );
  628. }
  629. /// \copydoc async_start_execution
  630. template <
  631. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  632. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  633. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  634. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  635. auto async_start_execution(
  636. ExecutionRequest&& req,
  637. ExecutionStateType& st,
  638. diagnostics& diag,
  639. CompletionToken&& token = {}
  640. )
  641. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  642. ExecutionRequest&&,
  643. ExecutionStateType,
  644. CompletionToken&&>)
  645. {
  646. return impl_.async_start_execution(
  647. std::forward<ExecutionRequest>(req),
  648. st,
  649. diag,
  650. std::forward<CompletionToken>(token)
  651. );
  652. }
  653. /**
  654. * \brief Prepares a statement server-side.
  655. * \details
  656. * `stmt` should be encoded using the connection's character set.
  657. * \n
  658. * The returned statement has `valid() == true`.
  659. */
  660. statement prepare_statement(string_view stmt, error_code& err, diagnostics& diag)
  661. {
  662. return impl_.run(detail::prepare_statement_algo_params{stmt}, err, diag);
  663. }
  664. /// \copydoc prepare_statement
  665. statement prepare_statement(string_view stmt)
  666. {
  667. error_code err;
  668. diagnostics diag;
  669. statement res = prepare_statement(stmt, err, diag);
  670. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  671. return res;
  672. }
  673. /**
  674. * \copydoc prepare_statement
  675. * \details
  676. * \par Object lifetimes
  677. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the string
  678. * pointed to by `stmt` must be kept alive by the caller until the operation is
  679. * initiated.
  680. *
  681. * \par Handler signature
  682. * The handler signature for this operation is `void(boost::mysql::error_code, boost::mysql::statement)`.
  683. *
  684. * \par Executor
  685. * Intermediate completion handlers, as well as the final handler, are executed using
  686. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  687. * executor.
  688. *
  689. * If the final handler has an associated immediate executor, and the operation
  690. * completes immediately, the final handler is dispatched to it.
  691. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  692. * and is never be called inline from within this function.
  693. */
  694. template <
  695. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  696. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  697. auto async_prepare_statement(string_view stmt, CompletionToken&& token = {})
  698. BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  699. {
  700. return async_prepare_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  701. }
  702. /// \copydoc async_prepare_statement
  703. template <
  704. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  705. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  706. auto async_prepare_statement(string_view stmt, diagnostics& diag, CompletionToken&& token = {})
  707. BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  708. {
  709. return impl_.async_run(
  710. detail::prepare_statement_algo_params{stmt},
  711. diag,
  712. std::forward<CompletionToken>(token)
  713. );
  714. }
  715. /**
  716. * \brief Closes a statement, deallocating it from the server.
  717. * \details
  718. * After this operation succeeds, `stmt` must not be used again for execution.
  719. * \n
  720. * \par Preconditions
  721. * `stmt.valid() == true`
  722. */
  723. void close_statement(const statement& stmt, error_code& err, diagnostics& diag)
  724. {
  725. impl_.run(impl_.make_params_close_statement(stmt), err, diag);
  726. }
  727. /// \copydoc close_statement
  728. void close_statement(const statement& stmt)
  729. {
  730. error_code err;
  731. diagnostics diag;
  732. close_statement(stmt, err, diag);
  733. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  734. }
  735. /**
  736. * \copydoc close_statement
  737. * \details
  738. * \par Object lifetimes
  739. * It is not required to keep `stmt` alive, as copies are made by the implementation as required.
  740. *
  741. * \par Handler signature
  742. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  743. *
  744. * \par Executor
  745. * Intermediate completion handlers, as well as the final handler, are executed using
  746. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  747. * executor.
  748. *
  749. * If the final handler has an associated immediate executor, and the operation
  750. * completes immediately, the final handler is dispatched to it.
  751. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  752. * and is never be called inline from within this function.
  753. */
  754. template <
  755. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  756. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  757. auto async_close_statement(const statement& stmt, CompletionToken&& token = {})
  758. BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  759. {
  760. return async_close_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  761. }
  762. /// \copydoc async_close_statement
  763. template <
  764. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  765. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  766. auto async_close_statement(const statement& stmt, diagnostics& diag, CompletionToken&& token = {})
  767. BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  768. {
  769. return impl_
  770. .async_run(impl_.make_params_close_statement(stmt), diag, std::forward<CompletionToken>(token));
  771. }
  772. /**
  773. * \brief Reads a batch of rows.
  774. * \details
  775. * The number of rows that will be read is unspecified. If the operation represented by `st`
  776. * has still rows to read, at least one will be read. If there are no more rows, or
  777. * `st.should_read_rows() == false`, returns an empty `rows_view`.
  778. * \n
  779. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  780. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  781. * constructor. The buffer may be
  782. * grown bigger by other read operations, if required.
  783. * \n
  784. * The returned view points into memory owned by `*this`. It will be valid until
  785. * `*this` performs the next network operation or is destroyed.
  786. */
  787. rows_view read_some_rows(execution_state& st, error_code& err, diagnostics& diag)
  788. {
  789. return impl_.run(impl_.make_params_read_some_rows(st), err, diag);
  790. }
  791. /// \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  792. rows_view read_some_rows(execution_state& st)
  793. {
  794. error_code err;
  795. diagnostics diag;
  796. rows_view res = read_some_rows(st, err, diag);
  797. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  798. return res;
  799. }
  800. /**
  801. * \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  802. * \details
  803. * \par Handler signature
  804. * The handler signature for this operation is
  805. * `void(boost::mysql::error_code, boost::mysql::rows_view)`.
  806. *
  807. * \par Executor
  808. * Intermediate completion handlers, as well as the final handler, are executed using
  809. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  810. * executor.
  811. *
  812. * If the final handler has an associated immediate executor, and the operation
  813. * completes immediately, the final handler is dispatched to it.
  814. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  815. * and is never be called inline from within this function.
  816. */
  817. template <
  818. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  819. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  820. auto async_read_some_rows(execution_state& st, CompletionToken&& token = {})
  821. BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  822. {
  823. return async_read_some_rows(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  824. }
  825. /// \copydoc async_read_some_rows(execution_state&,CompletionToken&&)
  826. template <
  827. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  828. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  829. auto async_read_some_rows(execution_state& st, diagnostics& diag, CompletionToken&& token = {})
  830. BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  831. {
  832. return impl_
  833. .async_run(impl_.make_params_read_some_rows(st), diag, std::forward<CompletionToken>(token));
  834. }
  835. #ifdef BOOST_MYSQL_CXX14
  836. /**
  837. * \brief Reads a batch of rows.
  838. * \details
  839. * Reads a batch of rows of unspecified size into the storage given by `output`.
  840. * At most `output.size()` rows will be read. If the operation represented by `st`
  841. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  842. * \n
  843. * Returns the number of read rows.
  844. * \n
  845. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  846. * zero.
  847. * \n
  848. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  849. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  850. * constructor. The buffer may be grown bigger by other read operations, if required.
  851. * \n
  852. * Rows read by this function are owning objects, and don't hold any reference to
  853. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  854. * \n
  855. * The type `SpanElementType` must be the underlying row type for one of the types in the
  856. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  857. * The type must match the resultset that is currently being processed by `st`. For instance,
  858. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  859. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  860. * \n
  861. * This function can report schema mismatches.
  862. */
  863. template <class SpanElementType, class... StaticRow>
  864. std::size_t read_some_rows(
  865. static_execution_state<StaticRow...>& st,
  866. span<SpanElementType> output,
  867. error_code& err,
  868. diagnostics& diag
  869. )
  870. {
  871. return impl_.run(impl_.make_params_read_some_rows_static(st, output), err, diag);
  872. }
  873. /**
  874. * \brief Reads a batch of rows.
  875. * \details
  876. * Reads a batch of rows of unspecified size into the storage given by `output`.
  877. * At most `output.size()` rows will be read. If the operation represented by `st`
  878. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  879. * \n
  880. * Returns the number of read rows.
  881. * \n
  882. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  883. * zero.
  884. * \n
  885. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  886. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  887. * constructor. The buffer may be grown bigger by other read operations, if required.
  888. * \n
  889. * Rows read by this function are owning objects, and don't hold any reference to
  890. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  891. * \n
  892. * The type `SpanElementType` must be the underlying row type for one of the types in the
  893. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  894. * The type must match the resultset that is currently being processed by `st`. For instance,
  895. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  896. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  897. * \n
  898. * This function can report schema mismatches.
  899. */
  900. template <class SpanElementType, class... StaticRow>
  901. std::size_t read_some_rows(static_execution_state<StaticRow...>& st, span<SpanElementType> output)
  902. {
  903. error_code err;
  904. diagnostics diag;
  905. std::size_t res = read_some_rows(st, output, err, diag);
  906. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  907. return res;
  908. }
  909. /**
  910. * \brief Reads a batch of rows.
  911. * \details
  912. * Reads a batch of rows of unspecified size into the storage given by `output`.
  913. * At most `output.size()` rows will be read. If the operation represented by `st`
  914. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  915. * \n
  916. * Returns the number of read rows.
  917. * \n
  918. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  919. * zero.
  920. * \n
  921. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  922. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  923. * constructor. The buffer may be grown bigger by other read operations, if required.
  924. * \n
  925. * Rows read by this function are owning objects, and don't hold any reference to
  926. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  927. * \n
  928. * The type `SpanElementType` must be the underlying row type for one of the types in the
  929. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  930. * The type must match the resultset that is currently being processed by `st`. For instance,
  931. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  932. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  933. * \n
  934. * This function can report schema mismatches.
  935. *
  936. * \par Handler signature
  937. * The handler signature for this operation is
  938. * `void(boost::mysql::error_code, std::size_t)`.
  939. *
  940. * \par Executor
  941. * Intermediate completion handlers, as well as the final handler, are executed using
  942. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  943. * executor.
  944. *
  945. * If the final handler has an associated immediate executor, and the operation
  946. * completes immediately, the final handler is dispatched to it.
  947. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  948. * and is never be called inline from within this function.
  949. *
  950. * \par Object lifetimes
  951. * The storage that `output` references must be kept alive until the operation completes.
  952. */
  953. template <
  954. class SpanElementType,
  955. class... StaticRow,
  956. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  957. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  958. auto async_read_some_rows(
  959. static_execution_state<StaticRow...>& st,
  960. span<SpanElementType> output,
  961. CompletionToken&& token = {}
  962. )
  963. {
  964. return async_read_some_rows(st, output, impl_.shared_diag(), std::forward<CompletionToken>(token));
  965. }
  966. /**
  967. * \brief Reads a batch of rows.
  968. * \details
  969. * Reads a batch of rows of unspecified size into the storage given by `output`.
  970. * At most `output.size()` rows will be read. If the operation represented by `st`
  971. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  972. * \n
  973. * Returns the number of read rows.
  974. * \n
  975. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  976. * zero.
  977. * \n
  978. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  979. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  980. * constructor. The buffer may be grown bigger by other read operations, if required.
  981. * \n
  982. * Rows read by this function are owning objects, and don't hold any reference to
  983. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  984. * \n
  985. * The type `SpanElementType` must be the underlying row type for one of the types in the
  986. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  987. * The type must match the resultset that is currently being processed by `st`. For instance,
  988. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  989. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  990. * \n
  991. * This function can report schema mismatches.
  992. *
  993. * \par Handler signature
  994. * The handler signature for this operation is
  995. * `void(boost::mysql::error_code, std::size_t)`.
  996. *
  997. * \par Executor
  998. * Intermediate completion handlers, as well as the final handler, are executed using
  999. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1000. * executor.
  1001. *
  1002. * If the final handler has an associated immediate executor, and the operation
  1003. * completes immediately, the final handler is dispatched to it.
  1004. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1005. * and is never be called inline from within this function.
  1006. *
  1007. * \par Object lifetimes
  1008. * The storage that `output` references must be kept alive until the operation completes.
  1009. */
  1010. template <
  1011. class SpanElementType,
  1012. class... StaticRow,
  1013. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  1014. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1015. auto async_read_some_rows(
  1016. static_execution_state<StaticRow...>& st,
  1017. span<SpanElementType> output,
  1018. diagnostics& diag,
  1019. CompletionToken&& token = {}
  1020. )
  1021. {
  1022. return impl_.async_run(
  1023. impl_.make_params_read_some_rows_static(st, output),
  1024. diag,
  1025. std::forward<CompletionToken>(token)
  1026. );
  1027. }
  1028. #endif
  1029. /**
  1030. * \brief Reads metadata for subsequent resultsets in a multi-resultset operation.
  1031. * \details
  1032. * If `st.should_read_head() == true`, this function will read the next resultset's
  1033. * initial response message and metadata, if any. If the resultset indicates a failure
  1034. * (e.g. the query associated to this resultset contained an error), this function will fail
  1035. * with that error.
  1036. * \n
  1037. * If `st.should_read_head() == false`, this function is a no-op.
  1038. * \n
  1039. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  1040. * \n
  1041. * This function is only relevant when using multi-function operations with statements
  1042. * that return more than one resultset.
  1043. * \n
  1044. * When using the static interface, this function will detect schema mismatches for the resultset
  1045. * currently being read. Further errors may be detected by subsequent invocations of this function
  1046. * and by \ref read_some_rows.
  1047. * \n
  1048. */
  1049. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  1050. void read_resultset_head(ExecutionStateType& st, error_code& err, diagnostics& diag)
  1051. {
  1052. return impl_.run(impl_.make_params_read_resultset_head(st), err, diag);
  1053. }
  1054. /// \copydoc read_resultset_head
  1055. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  1056. void read_resultset_head(ExecutionStateType& st)
  1057. {
  1058. error_code err;
  1059. diagnostics diag;
  1060. read_resultset_head(st, err, diag);
  1061. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1062. }
  1063. /**
  1064. * \copydoc read_resultset_head
  1065. * \par Handler signature
  1066. * The handler signature for this operation is
  1067. * `void(boost::mysql::error_code)`.
  1068. *
  1069. * \par Executor
  1070. * Intermediate completion handlers, as well as the final handler, are executed using
  1071. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1072. * executor.
  1073. *
  1074. * If the final handler has an associated immediate executor, and the operation
  1075. * completes immediately, the final handler is dispatched to it.
  1076. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1077. * and is never be called inline from within this function.
  1078. */
  1079. template <
  1080. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  1081. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1082. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1083. auto async_read_resultset_head(ExecutionStateType& st, CompletionToken&& token = {})
  1084. BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  1085. {
  1086. return async_read_resultset_head(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  1087. }
  1088. /// \copydoc async_read_resultset_head
  1089. template <
  1090. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  1091. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1092. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1093. auto async_read_resultset_head(ExecutionStateType& st, diagnostics& diag, CompletionToken&& token = {})
  1094. BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  1095. {
  1096. return impl_
  1097. .async_run(impl_.make_params_read_resultset_head(st), diag, std::forward<CompletionToken>(token));
  1098. }
  1099. /**
  1100. * \brief Sets the connection's character set, as per SET NAMES.
  1101. * \details
  1102. * Sets the connection's character set by running a
  1103. * <a href="https://dev.mysql.com/doc/refman/8.0/en/set-names.html">`SET NAMES`</a>
  1104. * SQL statement, using the passed \ref character_set::name as the charset name to set.
  1105. * \n
  1106. * This function will also update the value returned by \ref current_character_set, so
  1107. * prefer using this function over raw SQL statements.
  1108. * \n
  1109. * If the server was unable to set the character set to the requested value (e.g. because
  1110. * the server does not support the requested charset), this function will fail,
  1111. * as opposed to how \ref connect behaves when an unsupported collation is passed.
  1112. * This is a limitation of MySQL servers.
  1113. * \n
  1114. * You need to perform connection establishment for this function to succeed, since it
  1115. * involves communicating with the server.
  1116. *
  1117. * \par Object lifetimes
  1118. * `charset` will be copied as required, and does not need to be kept alive.
  1119. */
  1120. void set_character_set(const character_set& charset, error_code& err, diagnostics& diag)
  1121. {
  1122. impl_.run(detail::set_character_set_algo_params{charset}, err, diag);
  1123. }
  1124. /// \copydoc set_character_set
  1125. void set_character_set(const character_set& charset)
  1126. {
  1127. error_code err;
  1128. diagnostics diag;
  1129. set_character_set(charset, err, diag);
  1130. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1131. }
  1132. /**
  1133. * \copydoc set_character_set
  1134. * \details
  1135. * \n
  1136. * \par Handler signature
  1137. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1138. *
  1139. * \par Executor
  1140. * Intermediate completion handlers, as well as the final handler, are executed using
  1141. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1142. * executor.
  1143. *
  1144. * If the final handler has an associated immediate executor, and the operation
  1145. * completes immediately, the final handler is dispatched to it.
  1146. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1147. * and is never be called inline from within this function.
  1148. */
  1149. template <
  1150. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1151. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1152. auto async_set_character_set(const character_set& charset, CompletionToken&& token = {})
  1153. BOOST_MYSQL_RETURN_TYPE(detail::async_set_character_set_t<CompletionToken&&>)
  1154. {
  1155. return async_set_character_set(charset, impl_.shared_diag(), std::forward<CompletionToken>(token));
  1156. }
  1157. /// \copydoc async_set_character_set
  1158. template <
  1159. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1160. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1161. auto async_set_character_set(
  1162. const character_set& charset,
  1163. diagnostics& diag,
  1164. CompletionToken&& token = {}
  1165. ) BOOST_MYSQL_RETURN_TYPE(detail::async_set_character_set_t<CompletionToken&&>)
  1166. {
  1167. return impl_.async_run(
  1168. detail::set_character_set_algo_params{charset},
  1169. diag,
  1170. std::forward<CompletionToken>(token)
  1171. );
  1172. }
  1173. /**
  1174. * \brief Checks whether the server is alive.
  1175. * \details
  1176. * If the server is alive, this function will complete without error.
  1177. * If it's not, it will fail with the relevant network or protocol error.
  1178. * \n
  1179. * Note that ping requests are treated as any other type of request at the protocol
  1180. * level, and won't be prioritized anyhow by the server. If the server is stuck
  1181. * in a long-running query, the ping request won't be answered until the query is
  1182. * finished.
  1183. */
  1184. void ping(error_code& err, diagnostics& diag) { impl_.run(detail::ping_algo_params{}, err, diag); }
  1185. /// \copydoc ping
  1186. void ping()
  1187. {
  1188. error_code err;
  1189. diagnostics diag;
  1190. ping(err, diag);
  1191. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1192. }
  1193. /**
  1194. * \copydoc ping
  1195. * \details
  1196. * \n
  1197. * \par Handler signature
  1198. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1199. *
  1200. * \par Executor
  1201. * Intermediate completion handlers, as well as the final handler, are executed using
  1202. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1203. * executor.
  1204. *
  1205. * If the final handler has an associated immediate executor, and the operation
  1206. * completes immediately, the final handler is dispatched to it.
  1207. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1208. * and is never be called inline from within this function.
  1209. */
  1210. template <
  1211. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1212. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1213. auto async_ping(CompletionToken&& token = {})
  1214. BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  1215. {
  1216. return async_ping(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1217. }
  1218. /// \copydoc async_ping
  1219. template <
  1220. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1221. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1222. auto async_ping(diagnostics& diag, CompletionToken&& token = {})
  1223. BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  1224. {
  1225. return impl_.async_run(detail::ping_algo_params{}, diag, std::forward<CompletionToken>(token));
  1226. }
  1227. /**
  1228. * \brief Resets server-side session state, like variables and prepared statements.
  1229. * \details
  1230. * Resets all server-side state for the current session:
  1231. * \n
  1232. * \li Rolls back any active transactions and resets autocommit mode.
  1233. * \li Releases all table locks.
  1234. * \li Drops all temporary tables.
  1235. * \li Resets all session system variables to their default values (including the ones set by `SET
  1236. * NAMES`) and clears all user-defined variables.
  1237. * \li Closes all prepared statements.
  1238. * \n
  1239. * A full reference on the affected session state can be found
  1240. * <a href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-reset-connection.html">here</a>.
  1241. * \n
  1242. * \n
  1243. * This function will not reset the current physical connection and won't cause re-authentication.
  1244. * It is faster than closing and re-opening a connection.
  1245. * \n
  1246. * The connection must be connected and authenticated before calling this function.
  1247. * This function involves communication with the server, and thus may fail.
  1248. *
  1249. * \par Warning on character sets
  1250. * This function will restore the connection's character set and collation **to the server's default**,
  1251. * and not to the one specified during connection establishment. Some servers have `latin1` as their
  1252. * default character set, which is not usually what you want. Since there is no way to know this
  1253. * character set, \ref current_character_set will return `nullptr` after the operation succeeds.
  1254. * We recommend always using \ref set_character_set or \ref async_set_character_set after calling this
  1255. * function.
  1256. * \n
  1257. * You can find the character set that your server will use after the reset by running:
  1258. * \code
  1259. * "SELECT @@global.character_set_client, @@global.character_set_results;"
  1260. * \endcode
  1261. */
  1262. void reset_connection(error_code& err, diagnostics& diag)
  1263. {
  1264. impl_.run(detail::reset_connection_algo_params{}, err, diag);
  1265. }
  1266. /// \copydoc reset_connection
  1267. void reset_connection()
  1268. {
  1269. error_code err;
  1270. diagnostics diag;
  1271. reset_connection(err, diag);
  1272. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1273. }
  1274. /**
  1275. * \copydoc reset_connection
  1276. * \details
  1277. * \n
  1278. * \par Handler signature
  1279. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1280. *
  1281. * \par Executor
  1282. * Intermediate completion handlers, as well as the final handler, are executed using
  1283. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1284. * executor.
  1285. *
  1286. * If the final handler has an associated immediate executor, and the operation
  1287. * completes immediately, the final handler is dispatched to it.
  1288. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1289. * and is never be called inline from within this function.
  1290. */
  1291. template <
  1292. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1293. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1294. auto async_reset_connection(CompletionToken&& token = {})
  1295. BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  1296. {
  1297. return async_reset_connection(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1298. }
  1299. /// \copydoc async_reset_connection
  1300. template <
  1301. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1302. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1303. auto async_reset_connection(diagnostics& diag, CompletionToken&& token = {})
  1304. BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  1305. {
  1306. return impl_
  1307. .async_run(detail::reset_connection_algo_params{}, diag, std::forward<CompletionToken>(token));
  1308. }
  1309. /**
  1310. * \brief Cleanly closes the connection to the server.
  1311. * \details
  1312. * This function does the following:
  1313. * \n
  1314. * \li Sends a quit request. This is required by the MySQL protocol, to inform
  1315. * the server that we're closing the connection gracefully.
  1316. * \li If the connection is using TLS (`this->uses_ssl() == true`), performs
  1317. * the TLS shutdown.
  1318. * \li Closes the transport-level connection (the TCP or UNIX socket).
  1319. * \n
  1320. * Since this function involves writing a message to the server, it can fail.
  1321. * Only use this function if you know that the connection is healthy and you want
  1322. * to cleanly close it.
  1323. * \n
  1324. * If you don't call this function, the destructor or successive connects will
  1325. * perform a transport-layer close. This doesn't cause any resource leaks, but may
  1326. * cause warnings to be written to the server logs.
  1327. */
  1328. void close(error_code& err, diagnostics& diag)
  1329. {
  1330. impl_.run(detail::close_connection_algo_params{}, err, diag);
  1331. }
  1332. /// \copydoc close
  1333. void close()
  1334. {
  1335. error_code err;
  1336. diagnostics diag;
  1337. close(err, diag);
  1338. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1339. }
  1340. /**
  1341. * \copydoc close
  1342. * \details
  1343. * \par Handler signature
  1344. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1345. *
  1346. * \par Executor
  1347. * Intermediate completion handlers, as well as the final handler, are executed using
  1348. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1349. * executor.
  1350. *
  1351. * If the final handler has an associated immediate executor, and the operation
  1352. * completes immediately, the final handler is dispatched to it.
  1353. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1354. * and is never be called inline from within this function.
  1355. */
  1356. template <
  1357. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  1358. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1359. auto async_close(CompletionToken&& token = {})
  1360. BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  1361. {
  1362. return async_close(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1363. }
  1364. /// \copydoc async_close
  1365. template <
  1366. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  1367. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1368. auto async_close(diagnostics& diag, CompletionToken&& token = {})
  1369. BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  1370. {
  1371. return this->impl_
  1372. .async_run(detail::close_connection_algo_params{}, diag, std::forward<CompletionToken>(token));
  1373. }
  1374. /**
  1375. * \brief Runs a set of pipelined requests.
  1376. * \details
  1377. * Runs the pipeline described by `req` and stores its response in `res`.
  1378. * After the operation completes, `res` will have as many elements as stages
  1379. * were in `req`, even if the operation fails.
  1380. * \n
  1381. * Request stages are seen by the server as a series of unrelated requests.
  1382. * As a consequence, all stages are always run, even if previous stages fail.
  1383. * \n
  1384. * If all stages succeed, the operation completes successfully. Thus, there is no need to check
  1385. * the per-stage error code in `res` if this operation completed successfully.
  1386. * \n
  1387. * If any stage fails with a non-fatal error (as per \ref is_fatal_error), the result of the operation
  1388. * is the first encountered error. You can check which stages succeeded and which ones didn't by
  1389. * inspecting each stage in `res`.
  1390. * \n
  1391. * If any stage fails with a fatal error, the result of the operation is the fatal error.
  1392. * Successive stages will be marked as failed with the fatal error. The server may or may
  1393. * not have processed such stages.
  1394. */
  1395. void run_pipeline(
  1396. const pipeline_request& req,
  1397. std::vector<stage_response>& res,
  1398. error_code& err,
  1399. diagnostics& diag
  1400. )
  1401. {
  1402. impl_.run(impl_.make_params_pipeline(req, res), err, diag);
  1403. }
  1404. /// \copydoc run_pipeline
  1405. void run_pipeline(const pipeline_request& req, std::vector<stage_response>& res)
  1406. {
  1407. error_code err;
  1408. diagnostics diag;
  1409. run_pipeline(req, res, err, diag);
  1410. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1411. }
  1412. /**
  1413. * \copydoc run_pipeline
  1414. * \details
  1415. * \par Handler signature
  1416. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1417. *
  1418. * \par Executor
  1419. * Intermediate completion handlers, as well as the final handler, are executed using
  1420. * `token`'s associated executor, or `this->get_executor()` if the token doesn't have an associated
  1421. * executor.
  1422. *
  1423. * If the final handler has an associated immediate executor, and the operation
  1424. * completes immediately, the final handler is dispatched to it.
  1425. * Otherwise, the final handler is called as if it was submitted using `asio::post`,
  1426. * and is never be called inline from within this function.
  1427. *
  1428. * \par Object lifetimes
  1429. * The request and response objects must be kept alive and should not be modified
  1430. * until the operation completes.
  1431. */
  1432. template <
  1433. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  1434. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1435. auto async_run_pipeline(
  1436. const pipeline_request& req,
  1437. std::vector<stage_response>& res,
  1438. CompletionToken&& token = {}
  1439. ) BOOST_MYSQL_RETURN_TYPE(detail::async_run_pipeline_t<CompletionToken&&>)
  1440. {
  1441. return async_run_pipeline(req, res, impl_.shared_diag(), std::forward<CompletionToken>(token));
  1442. }
  1443. /// \copydoc async_run_pipeline
  1444. template <
  1445. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code))
  1446. CompletionToken = with_diagnostics_t<asio::deferred_t>>
  1447. auto async_run_pipeline(
  1448. const pipeline_request& req,
  1449. std::vector<stage_response>& res,
  1450. diagnostics& diag,
  1451. CompletionToken&& token = {}
  1452. ) BOOST_MYSQL_RETURN_TYPE(detail::async_run_pipeline_t<CompletionToken&&>)
  1453. {
  1454. return this->impl_
  1455. .async_run(impl_.make_params_pipeline(req, res), diag, std::forward<CompletionToken>(token));
  1456. }
  1457. };
  1458. } // namespace mysql
  1459. } // namespace boost
  1460. #ifdef BOOST_MYSQL_HEADER_ONLY
  1461. #include <boost/mysql/impl/any_connection.ipp>
  1462. #endif
  1463. #endif