any_connection.hpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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_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/detail/access.hpp>
  21. #include <boost/mysql/detail/algo_params.hpp>
  22. #include <boost/mysql/detail/config.hpp>
  23. #include <boost/mysql/detail/connect_params_helpers.hpp>
  24. #include <boost/mysql/detail/connection_impl.hpp>
  25. #include <boost/mysql/detail/engine.hpp>
  26. #include <boost/mysql/detail/execution_concepts.hpp>
  27. #include <boost/mysql/detail/ssl_fwd.hpp>
  28. #include <boost/mysql/detail/throw_on_error_loc.hpp>
  29. #include <boost/asio/any_io_executor.hpp>
  30. #include <boost/assert.hpp>
  31. #include <boost/system/result.hpp>
  32. #include <cstddef>
  33. #include <memory>
  34. #include <type_traits>
  35. #include <utility>
  36. #include <vector>
  37. namespace boost {
  38. namespace mysql {
  39. // Forward declarations
  40. template <class... StaticRow>
  41. class static_execution_state;
  42. class pipeline_request;
  43. class stage_response;
  44. /**
  45. * \brief (EXPERIMENTAL) Configuration parameters that can be passed to \ref any_connection's constructor.
  46. *
  47. * \par Experimental
  48. * This part of the API is experimental, and may change in successive
  49. * releases without previous notice.
  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 (EXPERIMENTAL) A type-erased connection to a MySQL server.
  100. * \details
  101. * Represents a connection to a MySQL server. Compared to \ref connection, this class:
  102. * \n
  103. * \li Is type-erased. The type of the connection doesn't depend on the transport being used.
  104. * Supported transports include plaintext TCP, SSL over TCP and UNIX domain sockets.
  105. * \li Is easier to connect, as \ref connect and \ref async_connect handle hostname resolution.
  106. * \li Can always be re-connected after being used or encountering an error.
  107. * \li Doesn't support default completion tokens.
  108. * \n
  109. * Provides a level of performance similar to \ref connection.
  110. * \n
  111. * This is a move-only type.
  112. * \n
  113. * \par Thread safety
  114. * Distinct objects: safe. \n
  115. * Shared objects: unsafe. \n
  116. * This class is <b>not thread-safe</b>: for a single object, if you
  117. * call its member functions concurrently from separate threads, you will get a race condition.
  118. *
  119. * \par Experimental
  120. * This part of the API is experimental, and may change in successive
  121. * releases without previous notice.
  122. */
  123. class any_connection
  124. {
  125. detail::connection_impl impl_;
  126. #ifndef BOOST_MYSQL_DOXYGEN
  127. friend struct detail::access;
  128. #endif
  129. BOOST_MYSQL_DECL
  130. static std::unique_ptr<detail::engine> create_engine(asio::any_io_executor ex, asio::ssl::context* ctx);
  131. // Used by tests
  132. any_connection(
  133. std::size_t initial_buffer_size,
  134. std::size_t max_buffer_size,
  135. std::unique_ptr<detail::engine> eng
  136. )
  137. : impl_(initial_buffer_size, max_buffer_size, std::move(eng))
  138. {
  139. }
  140. public:
  141. /**
  142. * \brief Constructs a connection object from an executor and an optional set of parameters.
  143. * \details
  144. * The resulting connection has `this->get_executor() == ex`. Any internally required I/O objects
  145. * will be constructed using this executor.
  146. * \n
  147. * You can configure extra parameters, like the SSL context and buffer sizes, by passing
  148. * an \ref any_connection_params object to this constructor.
  149. */
  150. any_connection(boost::asio::any_io_executor ex, any_connection_params params = {})
  151. : any_connection(
  152. params.initial_buffer_size,
  153. params.max_buffer_size,
  154. create_engine(std::move(ex), params.ssl_context)
  155. )
  156. {
  157. }
  158. /**
  159. * \brief Constructs a connection object from an execution context and an optional set of parameters.
  160. * \details
  161. * The resulting connection has `this->get_executor() == ctx.get_executor()`.
  162. * Any internally required I/O objects will be constructed using this executor.
  163. * \n
  164. * You can configure extra parameters, like the SSL context and buffer sizes, by passing
  165. * an \ref any_connection_params object to this constructor.
  166. * \n
  167. * This function participates in overload resolution only if `ExecutionContext`
  168. * satisfies the `ExecutionContext` requirements imposed by Boost.Asio.
  169. */
  170. template <
  171. class ExecutionContext
  172. #ifndef BOOST_MYSQL_DOXYGEN
  173. ,
  174. class = typename std::enable_if<std::is_convertible<
  175. decltype(std::declval<ExecutionContext&>().get_executor()),
  176. asio::any_io_executor>::value>::type
  177. #endif
  178. >
  179. any_connection(ExecutionContext& ctx, any_connection_params params = {})
  180. : any_connection(ctx.get_executor(), params)
  181. {
  182. }
  183. /**
  184. * \brief Move constructor.
  185. */
  186. any_connection(any_connection&& other) = default;
  187. /**
  188. * \brief Move assignment.
  189. */
  190. any_connection& operator=(any_connection&& rhs) = default;
  191. #ifndef BOOST_MYSQL_DOXYGEN
  192. any_connection(const any_connection&) = delete;
  193. any_connection& operator=(const any_connection&) = delete;
  194. #endif
  195. /**
  196. * \brief Destructor.
  197. * \details
  198. * Closes the connection at the transport layer (by closing any underlying socket objects).
  199. * If you require a clean close, call \ref close or \ref async_close before the connection
  200. * is destroyed.
  201. */
  202. ~any_connection() = default;
  203. /// The executor type associated to this object.
  204. using executor_type = asio::any_io_executor;
  205. /**
  206. * \brief Retrieves the executor associated to this object.
  207. * \par Exception safety
  208. * No-throw guarantee.
  209. */
  210. executor_type get_executor() noexcept { return impl_.get_engine().get_executor(); }
  211. /**
  212. * \brief Returns whether the connection negotiated the use of SSL or not.
  213. * \details
  214. * This function can be used to determine whether you are using a SSL
  215. * connection or not when using SSL negotiation.
  216. * \n
  217. * This function always returns `false`
  218. * for connections that haven't been established yet. If the connection establishment fails,
  219. * the return value is undefined.
  220. *
  221. * \par Exception safety
  222. * No-throw guarantee.
  223. */
  224. bool uses_ssl() const noexcept { return impl_.ssl_active(); }
  225. /**
  226. * \brief Returns whether backslashes are being treated as escape sequences.
  227. * \details
  228. * By default, the server treats backslashes in string values as escape characters.
  229. * This behavior can be disabled by activating the <a
  230. * href="https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_no_backslash_escapes">`NO_BACKSLASH_ESCAPES`</a>
  231. * SQL mode.
  232. * \n
  233. * Every time an operation involving server communication completes, the server reports whether
  234. * this mode was activated or not as part of the response. Connections store this information
  235. * and make it available through this function.
  236. * \n
  237. * \li If backslash are treated like escape characters, returns `true`.
  238. * \li If `NO_BACKSLASH_ESCAPES` has been activated, returns `false`.
  239. * \li If connection establishment hasn't happened yet, returns `true`.
  240. * \li Calling this function while an async operation that changes backslash behavior
  241. * is outstanding may return `true` or `false`.
  242. * \n
  243. * This function does not involve server communication.
  244. *
  245. * \par Exception safety
  246. * No-throw guarantee.
  247. */
  248. bool backslash_escapes() const noexcept { return impl_.backslash_escapes(); }
  249. /**
  250. * \brief Returns the character set used by this connection.
  251. * \details
  252. * Connections attempt to keep track of the current character set.
  253. * Deficiencies in the protocol can cause the character set to be unknown, though.
  254. * When the character set is known, this function returns
  255. * the character set currently in use. Otherwise, returns \ref client_errc::unknown_character_set.
  256. * \n
  257. * The following functions can modify the return value of this function: \n
  258. * \li Prior to connection, the character set is always unknown.
  259. * \li \ref connect and \ref async_connect may set the current character set
  260. * to a known value, depending on the requested collation.
  261. * \li \ref set_character_set always and \ref async_set_character_set always
  262. * set the current character set to the passed value.
  263. * \li \ref reset_connection and \ref async_reset_connection always makes the current character
  264. * unknown.
  265. *
  266. * \par Avoid changing the character set directly
  267. * If you change the connection's character set directly using SQL statements
  268. * like `"SET NAMES utf8mb4"`, the client has no way to track this change,
  269. * and this function will return incorrect results.
  270. *
  271. * \par Errors
  272. * \li \ref client_errc::unknown_character_set if the current character set is unknown.
  273. *
  274. * \par Exception safety
  275. * No-throw guarantee.
  276. */
  277. system::result<character_set> current_character_set() const noexcept
  278. {
  279. return impl_.current_character_set();
  280. }
  281. /**
  282. * \brief Returns format options suitable to format SQL according to the current connection configuation.
  283. * \details
  284. * If the current character set is known (as given by \ref current_character_set), returns
  285. * a value suitable to be passed to SQL formatting functions. Otherwise, returns an error.
  286. *
  287. * \par Errors
  288. * \li \ref client_errc::unknown_character_set if the current character set is unknown.
  289. *
  290. * \par Exception safety
  291. * No-throw guarantee.
  292. */
  293. system::result<format_options> format_opts() const noexcept
  294. {
  295. auto res = current_character_set();
  296. if (res.has_error())
  297. return res.error();
  298. return format_options{res.value(), backslash_escapes()};
  299. }
  300. /// \copydoc connection::meta_mode
  301. metadata_mode meta_mode() const noexcept { return impl_.meta_mode(); }
  302. /// \copydoc connection::set_meta_mode
  303. void set_meta_mode(metadata_mode v) noexcept { impl_.set_meta_mode(v); }
  304. /**
  305. * \brief Establishes a connection to a MySQL server.
  306. * \details
  307. * This function performs the following:
  308. * \n
  309. * \li If a connection has already been established (by a previous call to \ref connect
  310. * or \ref async_connect), closes it at the transport layer (by closing any underlying socket)
  311. * and discards any protocol state associated to it. (If you require
  312. * a clean close, call \ref close or \ref async_close before using this function).
  313. * \li If the connection is configured to use TCP (`params.server_address.type() ==
  314. * address_type::host_and_port`), resolves the passed hostname to a set of endpoints. An empty
  315. * hostname is equivalent to `"localhost"`.
  316. * \li Establishes the physical connection (performing the
  317. * TCP or UNIX socket connect).
  318. * \li Performs the MySQL handshake to establish a session. If the
  319. * connection is configured to use TLS, the TLS handshake is performed as part of this step.
  320. * \li If any of the above steps fail, the TCP or UNIX socket connection is closed.
  321. * \n
  322. * You can configure some options using the \ref connect_params struct.
  323. * \n
  324. * The decision to use TLS or not is performed using the following:
  325. * \n
  326. * \li If the transport is not TCP (`params.server_address.type() != address_type::host_and_port`),
  327. * the connection will never use TLS.
  328. * \li If the transport is TCP, and `params.ssl == ssl_mode::disable`, the connection will not use TLS.
  329. * \li If the transport is TCP, and `params.ssl == ssl_mode::enable`, the connection will use TLS
  330. * only if the server supports it.
  331. * \li If the transport is TCP, and `params.ssl == ssl_mode::require`, the connection will always use TLS.
  332. * If the server doesn't support it, this function will fail with \ref
  333. * client_errc::server_doesnt_support_ssl.
  334. * \n
  335. * If `params.connection_collation` is within a set of well-known collations, this function
  336. * sets the current character set, such that \ref current_character_set returns a non-null value.
  337. * The default collation (`utf8mb4_general_ci`) is the only one guaranteed to be in the set of well-known
  338. * collations.
  339. */
  340. void connect(const connect_params& params, error_code& ec, diagnostics& diag)
  341. {
  342. impl_.connect_v2(params, ec, diag);
  343. }
  344. /// \copydoc connect
  345. void connect(const connect_params& params)
  346. {
  347. error_code err;
  348. diagnostics diag;
  349. connect(params, err, diag);
  350. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  351. }
  352. /**
  353. * \copydoc connect
  354. *
  355. * \par Object lifetimes
  356. * params needs to be kept alive until the operation completes, as no
  357. * copies will be made by the library.
  358. *
  359. * \par Handler signature
  360. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  361. */
  362. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  363. auto async_connect(const connect_params& params, diagnostics& diag, CompletionToken&& token)
  364. BOOST_MYSQL_RETURN_TYPE(detail::async_connect_v2_t<CompletionToken&&>)
  365. {
  366. return impl_.async_connect_v2(params, diag, std::forward<CompletionToken>(token));
  367. }
  368. /// \copydoc async_connect
  369. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  370. auto async_connect(const connect_params& params, CompletionToken&& token)
  371. BOOST_MYSQL_RETURN_TYPE(detail::async_connect_v2_t<CompletionToken&&>)
  372. {
  373. return async_connect(params, impl_.shared_diag(), std::forward<CompletionToken>(token));
  374. }
  375. /// \copydoc connection::execute
  376. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  377. void execute(const ExecutionRequest& req, ResultsType& result, error_code& err, diagnostics& diag)
  378. {
  379. impl_.execute(req, result, err, diag);
  380. }
  381. /// \copydoc execute
  382. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  383. void execute(const ExecutionRequest& req, ResultsType& result)
  384. {
  385. error_code err;
  386. diagnostics diag;
  387. execute(req, result, err, diag);
  388. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  389. }
  390. /// \copydoc connection::async_execute
  391. template <
  392. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  393. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  394. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  395. auto async_execute(ExecutionRequest&& req, ResultsType& result, CompletionToken&& token)
  396. BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  397. {
  398. return async_execute(
  399. std::forward<ExecutionRequest>(req),
  400. result,
  401. impl_.shared_diag(),
  402. std::forward<CompletionToken>(token)
  403. );
  404. }
  405. /// \copydoc async_execute
  406. template <
  407. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  408. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  409. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  410. auto async_execute(
  411. ExecutionRequest&& req,
  412. ResultsType& result,
  413. diagnostics& diag,
  414. CompletionToken&& token
  415. ) BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  416. {
  417. return impl_.async_execute(
  418. std::forward<ExecutionRequest>(req),
  419. result,
  420. diag,
  421. std::forward<CompletionToken>(token)
  422. );
  423. }
  424. /// \copydoc connection::start_execution
  425. template <
  426. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  427. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  428. void start_execution(
  429. const ExecutionRequest& req,
  430. ExecutionStateType& st,
  431. error_code& err,
  432. diagnostics& diag
  433. )
  434. {
  435. impl_.start_execution(req, st, err, diag);
  436. }
  437. /// \copydoc start_execution
  438. template <
  439. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  440. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  441. void start_execution(const ExecutionRequest& req, ExecutionStateType& st)
  442. {
  443. error_code err;
  444. diagnostics diag;
  445. start_execution(req, st, err, diag);
  446. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  447. }
  448. /// \copydoc connection::async_start_execution
  449. template <
  450. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  451. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  452. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  453. auto async_start_execution(ExecutionRequest&& req, ExecutionStateType& st, CompletionToken&& token)
  454. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  455. ExecutionRequest&&,
  456. ExecutionStateType,
  457. CompletionToken&&>)
  458. {
  459. return async_start_execution(
  460. std::forward<ExecutionRequest>(req),
  461. st,
  462. impl_.shared_diag(),
  463. std::forward<CompletionToken>(token)
  464. );
  465. }
  466. /// \copydoc async_start_execution
  467. template <
  468. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  469. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  470. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  471. auto async_start_execution(
  472. ExecutionRequest&& req,
  473. ExecutionStateType& st,
  474. diagnostics& diag,
  475. CompletionToken&& token
  476. )
  477. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  478. ExecutionRequest&&,
  479. ExecutionStateType,
  480. CompletionToken&&>)
  481. {
  482. return impl_.async_start_execution(
  483. std::forward<ExecutionRequest>(req),
  484. st,
  485. diag,
  486. std::forward<CompletionToken>(token)
  487. );
  488. }
  489. /// \copydoc connection::prepare_statement
  490. statement prepare_statement(string_view stmt, error_code& err, diagnostics& diag)
  491. {
  492. return impl_.run(detail::prepare_statement_algo_params{&diag, stmt}, err);
  493. }
  494. /// \copydoc prepare_statement
  495. statement prepare_statement(string_view stmt)
  496. {
  497. error_code err;
  498. diagnostics diag;
  499. statement res = prepare_statement(stmt, err, diag);
  500. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  501. return res;
  502. }
  503. /// \copydoc connection::async_prepare_statement
  504. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  505. CompletionToken>
  506. auto async_prepare_statement(string_view stmt, CompletionToken&& token)
  507. BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  508. {
  509. return async_prepare_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  510. }
  511. /// \copydoc async_prepare_statement
  512. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  513. CompletionToken>
  514. auto async_prepare_statement(string_view stmt, diagnostics& diag, CompletionToken&& token)
  515. BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  516. {
  517. return impl_.async_run(
  518. detail::prepare_statement_algo_params{&diag, stmt},
  519. std::forward<CompletionToken>(token)
  520. );
  521. }
  522. /// \copydoc connection::close_statement
  523. void close_statement(const statement& stmt, error_code& err, diagnostics& diag)
  524. {
  525. impl_.run(impl_.make_params_close_statement(stmt, diag), err);
  526. }
  527. /// \copydoc close_statement
  528. void close_statement(const statement& stmt)
  529. {
  530. error_code err;
  531. diagnostics diag;
  532. close_statement(stmt, err, diag);
  533. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  534. }
  535. /// \copydoc connection::async_close_statement
  536. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  537. auto async_close_statement(const statement& stmt, CompletionToken&& token)
  538. BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  539. {
  540. return async_close_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  541. }
  542. /// \copydoc async_close_statement
  543. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  544. auto async_close_statement(const statement& stmt, diagnostics& diag, CompletionToken&& token)
  545. BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  546. {
  547. return impl_.async_run(
  548. impl_.make_params_close_statement(stmt, diag),
  549. std::forward<CompletionToken>(token)
  550. );
  551. }
  552. /// \copydoc connection::read_some_rows
  553. rows_view read_some_rows(execution_state& st, error_code& err, diagnostics& diag)
  554. {
  555. return impl_.run(impl_.make_params_read_some_rows(st, diag), err);
  556. }
  557. /// \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  558. rows_view read_some_rows(execution_state& st)
  559. {
  560. error_code err;
  561. diagnostics diag;
  562. rows_view res = read_some_rows(st, err, diag);
  563. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  564. return res;
  565. }
  566. /// \copydoc connection::async_read_some_rows(execution_state&,CompletionToken&&)
  567. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  568. CompletionToken>
  569. auto async_read_some_rows(execution_state& st, CompletionToken&& token)
  570. BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  571. {
  572. return async_read_some_rows(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  573. }
  574. /// \copydoc async_read_some_rows(execution_state&,CompletionToken&&)
  575. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  576. CompletionToken>
  577. auto async_read_some_rows(execution_state& st, diagnostics& diag, CompletionToken&& token)
  578. BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  579. {
  580. return impl_.async_run(
  581. impl_.make_params_read_some_rows(st, diag),
  582. std::forward<CompletionToken>(token)
  583. );
  584. }
  585. #ifdef BOOST_MYSQL_CXX14
  586. /**
  587. * \brief Reads a batch of rows.
  588. * \details
  589. * Reads a batch of rows of unspecified size into the storage given by `output`.
  590. * At most `output.size()` rows will be read. If the operation represented by `st`
  591. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  592. * \n
  593. * Returns the number of read rows.
  594. * \n
  595. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  596. * zero.
  597. * \n
  598. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  599. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  600. * constructor. The buffer may be grown bigger by other read operations, if required.
  601. * \n
  602. * Rows read by this function are owning objects, and don't hold any reference to
  603. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  604. * \n
  605. * The type `SpanElementType` must be the underlying row type for one of the types in the
  606. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  607. * The type must match the resultset that is currently being processed by `st`. For instance,
  608. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  609. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  610. * \n
  611. * This function can report schema mismatches.
  612. */
  613. template <class SpanElementType, class... StaticRow>
  614. std::size_t read_some_rows(
  615. static_execution_state<StaticRow...>& st,
  616. span<SpanElementType> output,
  617. error_code& err,
  618. diagnostics& diag
  619. )
  620. {
  621. return impl_.run(impl_.make_params_read_some_rows_static(st, output, diag), err);
  622. }
  623. /**
  624. * \brief Reads a batch of rows.
  625. * \details
  626. * Reads a batch of rows of unspecified size into the storage given by `output`.
  627. * At most `output.size()` rows will be read. If the operation represented by `st`
  628. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  629. * \n
  630. * Returns the number of read rows.
  631. * \n
  632. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  633. * zero.
  634. * \n
  635. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  636. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  637. * constructor. The buffer may be grown bigger by other read operations, if required.
  638. * \n
  639. * Rows read by this function are owning objects, and don't hold any reference to
  640. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  641. * \n
  642. * The type `SpanElementType` must be the underlying row type for one of the types in the
  643. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  644. * The type must match the resultset that is currently being processed by `st`. For instance,
  645. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  646. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  647. * \n
  648. * This function can report schema mismatches.
  649. */
  650. template <class SpanElementType, class... StaticRow>
  651. std::size_t read_some_rows(static_execution_state<StaticRow...>& st, span<SpanElementType> output)
  652. {
  653. error_code err;
  654. diagnostics diag;
  655. std::size_t res = read_some_rows(st, output, err, diag);
  656. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  657. return res;
  658. }
  659. /**
  660. * \brief Reads a batch of rows.
  661. * \details
  662. * Reads a batch of rows of unspecified size into the storage given by `output`.
  663. * At most `output.size()` rows will be read. If the operation represented by `st`
  664. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  665. * \n
  666. * Returns the number of read rows.
  667. * \n
  668. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  669. * zero.
  670. * \n
  671. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  672. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  673. * constructor. The buffer may be grown bigger by other read operations, if required.
  674. * \n
  675. * Rows read by this function are owning objects, and don't hold any reference to
  676. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  677. * \n
  678. * The type `SpanElementType` must be the underlying row type for one of the types in the
  679. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  680. * The type must match the resultset that is currently being processed by `st`. For instance,
  681. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  682. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  683. * \n
  684. * This function can report schema mismatches.
  685. *
  686. * \par Handler signature
  687. * The handler signature for this operation is
  688. * `void(boost::mysql::error_code, std::size_t)`.
  689. *
  690. * \par Object lifetimes
  691. * The storage that `output` references must be kept alive until the operation completes.
  692. */
  693. template <
  694. class SpanElementType,
  695. class... StaticRow,
  696. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  697. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  698. auto async_read_some_rows(
  699. static_execution_state<StaticRow...>& st,
  700. span<SpanElementType> output,
  701. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  702. )
  703. {
  704. return async_read_some_rows(st, output, impl_.shared_diag(), std::forward<CompletionToken>(token));
  705. }
  706. /**
  707. * \brief Reads a batch of rows.
  708. * \details
  709. * Reads a batch of rows of unspecified size into the storage given by `output`.
  710. * At most `output.size()` rows will be read. If the operation represented by `st`
  711. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  712. * \n
  713. * Returns the number of read rows.
  714. * \n
  715. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  716. * zero.
  717. * \n
  718. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  719. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  720. * constructor. The buffer may be grown bigger by other read operations, if required.
  721. * \n
  722. * Rows read by this function are owning objects, and don't hold any reference to
  723. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  724. * \n
  725. * The type `SpanElementType` must be the underlying row type for one of the types in the
  726. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  727. * The type must match the resultset that is currently being processed by `st`. For instance,
  728. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  729. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  730. * \n
  731. * This function can report schema mismatches.
  732. *
  733. * \par Handler signature
  734. * The handler signature for this operation is
  735. * `void(boost::mysql::error_code, std::size_t)`.
  736. *
  737. * \par Object lifetimes
  738. * The storage that `output` references must be kept alive until the operation completes.
  739. */
  740. template <
  741. class SpanElementType,
  742. class... StaticRow,
  743. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  744. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  745. auto async_read_some_rows(
  746. static_execution_state<StaticRow...>& st,
  747. span<SpanElementType> output,
  748. diagnostics& diag,
  749. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  750. )
  751. {
  752. return impl_.async_run(
  753. impl_.make_params_read_some_rows_static(st, output, diag),
  754. std::forward<CompletionToken>(token)
  755. );
  756. }
  757. #endif
  758. /// \copydoc connection::read_resultset_head
  759. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  760. void read_resultset_head(ExecutionStateType& st, error_code& err, diagnostics& diag)
  761. {
  762. return impl_.run(impl_.make_params_read_resultset_head(st, diag), err);
  763. }
  764. /// \copydoc read_resultset_head
  765. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  766. void read_resultset_head(ExecutionStateType& st)
  767. {
  768. error_code err;
  769. diagnostics diag;
  770. read_resultset_head(st, err, diag);
  771. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  772. }
  773. /// \copydoc connection::async_read_resultset_head
  774. template <
  775. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  776. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  777. auto async_read_resultset_head(ExecutionStateType& st, CompletionToken&& token)
  778. BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  779. {
  780. return async_read_resultset_head(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  781. }
  782. /// \copydoc async_read_resultset_head
  783. template <
  784. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  785. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  786. auto async_read_resultset_head(ExecutionStateType& st, diagnostics& diag, CompletionToken&& token)
  787. BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  788. {
  789. return impl_.async_run(
  790. impl_.make_params_read_resultset_head(st, diag),
  791. std::forward<CompletionToken>(token)
  792. );
  793. }
  794. /**
  795. * \brief Sets the connection's character set, as per SET NAMES.
  796. * \details
  797. * Sets the connection's character set by running a
  798. * <a href="https://dev.mysql.com/doc/refman/8.0/en/set-names.html">`SET NAMES`</a>
  799. * SQL statement, using the passed \ref character_set::name as the charset name to set.
  800. * \n
  801. * This function will also update the value returned by \ref current_character_set, so
  802. * prefer using this function over raw SQL statements.
  803. * \n
  804. * If the server was unable to set the character set to the requested value (e.g. because
  805. * the server does not support the requested charset), this function will fail,
  806. * as opposed to how \ref connect behaves when an unsupported collation is passed.
  807. * This is a limitation of MySQL servers.
  808. * \n
  809. * You need to perform connection establishment for this function to succeed, since it
  810. * involves communicating with the server.
  811. *
  812. * \par Object lifetimes
  813. * `charset` will be copied as required, and does not need to be kept alive.
  814. */
  815. void set_character_set(const character_set& charset, error_code& err, diagnostics& diag)
  816. {
  817. impl_.run(impl_.make_params_set_character_set(charset, diag), err);
  818. }
  819. /// \copydoc set_character_set
  820. void set_character_set(const character_set& charset)
  821. {
  822. error_code err;
  823. diagnostics diag;
  824. set_character_set(charset, err, diag);
  825. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  826. }
  827. /**
  828. * \copydoc set_character_set
  829. * \details
  830. * \n
  831. * \par Handler signature
  832. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  833. */
  834. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  835. auto async_set_character_set(const character_set& charset, CompletionToken&& token)
  836. BOOST_MYSQL_RETURN_TYPE(detail::async_set_character_set_t<CompletionToken&&>)
  837. {
  838. return async_set_character_set(charset, impl_.shared_diag(), std::forward<CompletionToken>(token));
  839. }
  840. /// \copydoc async_set_character_set
  841. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  842. auto async_set_character_set(const character_set& charset, diagnostics& diag, CompletionToken&& token)
  843. BOOST_MYSQL_RETURN_TYPE(detail::async_set_character_set_t<CompletionToken&&>)
  844. {
  845. return impl_.async_run(
  846. impl_.make_params_set_character_set(charset, diag),
  847. std::forward<CompletionToken>(token)
  848. );
  849. }
  850. /// \copydoc connection::ping
  851. void ping(error_code& err, diagnostics& diag) { impl_.run(impl_.make_params_ping(diag), err); }
  852. /// \copydoc ping
  853. void ping()
  854. {
  855. error_code err;
  856. diagnostics diag;
  857. ping(err, diag);
  858. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  859. }
  860. /// \copydoc connection::async_ping
  861. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  862. auto async_ping(CompletionToken&& token) BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  863. {
  864. return async_ping(impl_.shared_diag(), std::forward<CompletionToken>(token));
  865. }
  866. /// \copydoc async_ping
  867. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  868. auto async_ping(diagnostics& diag, CompletionToken&& token)
  869. BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  870. {
  871. return impl_.async_run(impl_.make_params_ping(diag), std::forward<CompletionToken>(token));
  872. }
  873. /**
  874. * \brief Resets server-side session state, like variables and prepared statements.
  875. * \details
  876. * Resets all server-side state for the current session:
  877. * \n
  878. * \li Rolls back any active transactions and resets autocommit mode.
  879. * \li Releases all table locks.
  880. * \li Drops all temporary tables.
  881. * \li Resets all session system variables to their default values (including the ones set by `SET
  882. * NAMES`) and clears all user-defined variables.
  883. * \li Closes all prepared statements.
  884. * \n
  885. * A full reference on the affected session state can be found
  886. * <a href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-reset-connection.html">here</a>.
  887. * \n
  888. * \n
  889. * This function will not reset the current physical connection and won't cause re-authentication.
  890. * It is faster than closing and re-opening a connection.
  891. * \n
  892. * The connection must be connected and authenticated before calling this function.
  893. * This function involves communication with the server, and thus may fail.
  894. *
  895. * \par Warning on character sets
  896. * This function will restore the connection's character set and collation **to the server's default**,
  897. * and not to the one specified during connection establishment. Some servers have `latin1` as their
  898. * default character set, which is not usually what you want. Since there is no way to know this
  899. * character set, \ref current_character_set will return `nullptr` after the operation succeeds.
  900. * We recommend always using \ref set_character_set or \ref async_set_character_set after calling this
  901. * function.
  902. * \n
  903. * You can find the character set that your server will use after the reset by running:
  904. * \code
  905. * "SELECT @@global.character_set_client, @@global.character_set_results;"
  906. * \endcode
  907. */
  908. void reset_connection(error_code& err, diagnostics& diag)
  909. {
  910. impl_.run(impl_.make_params_reset_connection(diag), err);
  911. }
  912. /// \copydoc reset_connection
  913. void reset_connection()
  914. {
  915. error_code err;
  916. diagnostics diag;
  917. reset_connection(err, diag);
  918. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  919. }
  920. /**
  921. * \copydoc reset_connection
  922. * \details
  923. * \n
  924. * \par Handler signature
  925. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  926. */
  927. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  928. auto async_reset_connection(CompletionToken&& token)
  929. BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  930. {
  931. return async_reset_connection(impl_.shared_diag(), std::forward<CompletionToken>(token));
  932. }
  933. /// \copydoc async_reset_connection
  934. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code)) CompletionToken>
  935. auto async_reset_connection(diagnostics& diag, CompletionToken&& token)
  936. BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  937. {
  938. return impl_.async_run(
  939. impl_.make_params_reset_connection(diag),
  940. std::forward<CompletionToken>(token)
  941. );
  942. }
  943. /**
  944. * \brief Cleanly closes the connection to the server.
  945. * \details
  946. * This function does the following:
  947. * \n
  948. * \li Sends a quit request. This is required by the MySQL protocol, to inform
  949. * the server that we're closing the connection gracefully.
  950. * \li If the connection is using TLS (`this->uses_ssl() == true`), performs
  951. * the TLS shutdown.
  952. * \li Closes the transport-level connection (the TCP or UNIX socket).
  953. * \n
  954. * Since this function involves writing a message to the server, it can fail.
  955. * Only use this function if you know that the connection is healthy and you want
  956. * to cleanly close it.
  957. * \n
  958. * If you don't call this function, the destructor or successive connects will
  959. * perform a transport-layer close. This doesn't cause any resource leaks, but may
  960. * cause warnings to be written to the server logs.
  961. */
  962. void close(error_code& err, diagnostics& diag)
  963. {
  964. this->impl_.run(this->impl_.make_params_close(diag), err);
  965. }
  966. /// \copydoc close
  967. void close()
  968. {
  969. error_code err;
  970. diagnostics diag;
  971. close(err, diag);
  972. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  973. }
  974. /**
  975. * \copydoc close
  976. * \details
  977. * \par Handler signature
  978. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  979. */
  980. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  981. auto async_close(CompletionToken&& token)
  982. BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  983. {
  984. return async_close(impl_.shared_diag(), std::forward<CompletionToken>(token));
  985. }
  986. /// \copydoc async_close
  987. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  988. auto async_close(diagnostics& diag, CompletionToken&& token)
  989. BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  990. {
  991. return this->impl_.async_run(
  992. this->impl_.make_params_close(diag),
  993. std::forward<CompletionToken>(token)
  994. );
  995. }
  996. /**
  997. * \brief Runs a set of pipelined requests.
  998. * \details
  999. * Runs the pipeline described by `req` and stores its response in `res`.
  1000. * After the operation completes, `res` will have as many elements as stages
  1001. * were in `req`, even if the operation fails.
  1002. * \n
  1003. * Request stages are seen by the server as a series of unrelated requests.
  1004. * As a consequence, all stages are always run, even if previous stages fail.
  1005. * \n
  1006. * If all stages succeed, the operation completes successfully. Thus, there is no need to check
  1007. * the per-stage error code in `res` if this operation completed successfully.
  1008. * \n
  1009. * If any stage fails with a non-fatal error (as per \ref is_fatal_error), the result of the operation
  1010. * is the first encountered error. You can check which stages succeeded and which ones didn't by
  1011. * inspecting each stage in `res`.
  1012. * \n
  1013. * If any stage fails with a fatal error, the result of the operation is the fatal error.
  1014. * Successive stages will be marked as failed with the fatal error. The server may or may
  1015. * not have processed such stages.
  1016. */
  1017. void run_pipeline(
  1018. const pipeline_request& req,
  1019. std::vector<stage_response>& res,
  1020. error_code& err,
  1021. diagnostics& diag
  1022. )
  1023. {
  1024. impl_.run(impl_.make_params_pipeline(req, res, diag), err);
  1025. }
  1026. /// \copydoc run_pipeline
  1027. void run_pipeline(const pipeline_request& req, std::vector<stage_response>& res)
  1028. {
  1029. error_code err;
  1030. diagnostics diag;
  1031. run_pipeline(req, res, err, diag);
  1032. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1033. }
  1034. /**
  1035. * \copydoc run_pipeline
  1036. * \details
  1037. * \par Handler signature
  1038. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1039. *
  1040. * \par Object lifetimes
  1041. * The request and response objects must be kept alive and should not be modified
  1042. * until the operation completes.
  1043. */
  1044. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  1045. auto async_run_pipeline(
  1046. const pipeline_request& req,
  1047. std::vector<stage_response>& res,
  1048. CompletionToken&& token
  1049. ) BOOST_MYSQL_RETURN_TYPE(detail::async_run_pipeline_t<CompletionToken&&>)
  1050. {
  1051. return async_run_pipeline(req, res, impl_.shared_diag(), std::forward<CompletionToken>(token));
  1052. }
  1053. /// \copydoc async_run_pipeline
  1054. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code)) CompletionToken>
  1055. auto async_run_pipeline(
  1056. const pipeline_request& req,
  1057. std::vector<stage_response>& res,
  1058. diagnostics& diag,
  1059. CompletionToken&& token
  1060. ) BOOST_MYSQL_RETURN_TYPE(detail::async_run_pipeline_t<CompletionToken&&>)
  1061. {
  1062. return this->impl_.async_run(
  1063. impl_.make_params_pipeline(req, res, diag),
  1064. std::forward<CompletionToken>(token)
  1065. );
  1066. }
  1067. };
  1068. } // namespace mysql
  1069. } // namespace boost
  1070. #ifdef BOOST_MYSQL_HEADER_ONLY
  1071. #include <boost/mysql/impl/any_connection.ipp>
  1072. #endif
  1073. #endif