connection.hpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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_CONNECTION_HPP
  8. #define BOOST_MYSQL_CONNECTION_HPP
  9. #include <boost/mysql/buffer_params.hpp>
  10. #include <boost/mysql/defaults.hpp>
  11. #include <boost/mysql/diagnostics.hpp>
  12. #include <boost/mysql/error_code.hpp>
  13. #include <boost/mysql/execution_state.hpp>
  14. #include <boost/mysql/handshake_params.hpp>
  15. #include <boost/mysql/metadata_mode.hpp>
  16. #include <boost/mysql/results.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/connection_impl.hpp>
  23. #include <boost/mysql/detail/engine_stream_adaptor.hpp>
  24. #include <boost/mysql/detail/execution_concepts.hpp>
  25. #include <boost/mysql/detail/rebind_executor.hpp>
  26. #include <boost/mysql/detail/socket_stream.hpp>
  27. #include <boost/mysql/detail/throw_on_error_loc.hpp>
  28. #include <boost/mysql/detail/writable_field_traits.hpp>
  29. #include <boost/assert.hpp>
  30. #include <cstddef>
  31. #include <type_traits>
  32. #include <utility>
  33. /// The Boost libraries namespace.
  34. namespace boost {
  35. /// Boost.MySQL library namespace.
  36. namespace mysql {
  37. // Forward declarations
  38. template <class... StaticRow>
  39. class static_execution_state;
  40. /**
  41. * \brief A connection to a MySQL server.
  42. * \details
  43. * Represents a connection to a MySQL server.
  44. *\n
  45. * `connection` is the main I/O object that this library implements. It owns a `Stream` object that
  46. * is accessed by functions involving network operations, as well as session state. You can access
  47. * the stream using \ref connection::stream, and its executor via \ref connection::get_executor. The
  48. * executor used by this object is always the same as the underlying stream.
  49. *\n
  50. * \par Thread safety
  51. * Distinct objects: safe. \n
  52. * Shared objects: unsafe. \n
  53. * This class is <b>not thread-safe</b>: for a single object, if you
  54. * call its member functions concurrently from separate threads, you will get a race condition.
  55. */
  56. template <class Stream>
  57. class connection
  58. {
  59. detail::connection_impl impl_;
  60. public:
  61. /**
  62. * \brief Initializing constructor.
  63. * \details
  64. * As part of the initialization, an internal `Stream` object is created.
  65. *
  66. * \par Exception safety
  67. * Basic guarantee. Throws if the `Stream` constructor throws
  68. * or if memory allocation for internal state fails.
  69. *
  70. * \param args Arguments to be forwarded to the `Stream` constructor.
  71. */
  72. template <
  73. class... Args,
  74. class EnableIf = typename std::enable_if<std::is_constructible<Stream, Args...>::value>::type>
  75. connection(Args&&... args) : connection(buffer_params(), std::forward<Args>(args)...)
  76. {
  77. }
  78. /**
  79. * \brief Initializing constructor with buffer params.
  80. * \details
  81. * As part of the initialization, an internal `Stream` object is created.
  82. *
  83. * \par Exception safety
  84. * Basic guarantee. Throws if the `Stream` constructor throws
  85. * or if memory allocation for internal state fails.
  86. *
  87. * \param buff_params Specifies initial sizes for internal buffers.
  88. * \param args Arguments to be forwarded to the `Stream` constructor.
  89. */
  90. template <
  91. class... Args,
  92. class EnableIf = typename std::enable_if<std::is_constructible<Stream, Args...>::value>::type>
  93. connection(const buffer_params& buff_params, Args&&... args)
  94. : impl_(
  95. buff_params.initial_read_size(),
  96. static_cast<std::size_t>(-1),
  97. detail::make_engine<Stream>(std::forward<Args>(args)...)
  98. )
  99. {
  100. }
  101. /**
  102. * \brief Move constructor.
  103. */
  104. connection(connection&& other) = default;
  105. /**
  106. * \brief Move assignment.
  107. */
  108. connection& operator=(connection&& rhs) = default;
  109. #ifndef BOOST_MYSQL_DOXYGEN
  110. connection(const connection&) = delete;
  111. connection& operator=(const connection&) = delete;
  112. #endif
  113. /// The executor type associated to this object.
  114. using executor_type = typename Stream::executor_type;
  115. /// Retrieves the executor associated to this object.
  116. executor_type get_executor() { return stream().get_executor(); }
  117. /// The `Stream` type this connection is using.
  118. using stream_type = Stream;
  119. /**
  120. * \brief Retrieves the underlying Stream object.
  121. * \details
  122. *
  123. * \par Exception safety
  124. * No-throw guarantee.
  125. */
  126. Stream& stream() noexcept { return detail::stream_from_engine<Stream>(impl_.get_engine()); }
  127. /**
  128. * \brief Retrieves the underlying Stream object.
  129. * \details
  130. *
  131. * \par Exception safety
  132. * No-throw guarantee.
  133. */
  134. const Stream& stream() const noexcept { return detail::stream_from_engine<Stream>(impl_.get_engine()); }
  135. /**
  136. * \brief Returns whether the connection negotiated the use of SSL or not.
  137. * \details
  138. * This function can be used to determine whether you are using a SSL
  139. * connection or not when using SSL negotiation.
  140. * \n
  141. * This function always returns `false` if the underlying
  142. * stream does not support SSL. This function always returns `false`
  143. * for connections that haven't been
  144. * established yet (handshake not run yet). If the handshake fails,
  145. * the return value is undefined.
  146. *
  147. * \par Exception safety
  148. * No-throw guarantee.
  149. *
  150. * \returns Whether the connection is using SSL.
  151. */
  152. bool uses_ssl() const noexcept { return impl_.ssl_active(); }
  153. /**
  154. * \brief Returns the current metadata mode that this connection is using.
  155. * \details
  156. * \par Exception safety
  157. * No-throw guarantee.
  158. *
  159. * \returns The matadata mode that will be used for queries and statement executions.
  160. */
  161. metadata_mode meta_mode() const noexcept { return impl_.meta_mode(); }
  162. /**
  163. * \brief Sets the metadata mode.
  164. * \details
  165. * Will affect any query and statement executions performed after the call.
  166. *
  167. * \par Exception safety
  168. * No-throw guarantee.
  169. *
  170. * \par Preconditions
  171. * No asynchronous operation should be outstanding when this function is called.
  172. *
  173. * \param v The new metadata mode.
  174. */
  175. void set_meta_mode(metadata_mode v) noexcept { impl_.set_meta_mode(v); }
  176. /**
  177. * \brief Establishes a connection to a MySQL server.
  178. * \details
  179. * This function is only available if `Stream` satisfies the
  180. * `SocketStream` concept.
  181. * \n
  182. * Connects the underlying stream and performs the handshake
  183. * with the server. The underlying stream is closed in case of error. Prefer
  184. * this function to \ref connection::handshake.
  185. * \n
  186. * If using a SSL-capable stream, the SSL handshake will be performed by this function.
  187. * \n
  188. * `endpoint` should be convertible to `Stream::lowest_layer_type::endpoint_type`.
  189. */
  190. template <typename EndpointType>
  191. void connect(
  192. const EndpointType& endpoint,
  193. const handshake_params& params,
  194. error_code& ec,
  195. diagnostics& diag
  196. )
  197. {
  198. static_assert(
  199. detail::is_socket_stream<Stream>::value,
  200. "connect can only be used if Stream satisfies the SocketStream concept"
  201. );
  202. impl_.connect<typename Stream::lowest_layer_type::endpoint_type>(endpoint, params, ec, diag);
  203. }
  204. /// \copydoc connect
  205. template <typename EndpointType>
  206. void connect(const EndpointType& endpoint, const handshake_params& params)
  207. {
  208. static_assert(
  209. detail::is_socket_stream<Stream>::value,
  210. "connect can only be used if Stream satisfies the SocketStream concept"
  211. );
  212. error_code err;
  213. diagnostics diag;
  214. connect(endpoint, params, err, diag);
  215. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  216. }
  217. /**
  218. * \copydoc connect
  219. * \par Object lifetimes
  220. * The strings pointed to by `params` should be kept alive by the caller
  221. * until the operation completes, as no copy is made by the library.
  222. * `endpoint` is copied as required and doesn't need to be kept alive.
  223. *
  224. * \par Handler signature
  225. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  226. */
  227. template <
  228. typename EndpointType,
  229. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  230. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  231. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  232. async_connect(
  233. const EndpointType& endpoint,
  234. const handshake_params& params,
  235. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  236. )
  237. {
  238. static_assert(
  239. detail::is_socket_stream<Stream>::value,
  240. "async_connect can only be used if Stream satisfies the SocketStream concept"
  241. );
  242. return async_connect(endpoint, params, impl_.shared_diag(), std::forward<CompletionToken>(token));
  243. }
  244. /// \copydoc async_connect
  245. template <
  246. typename EndpointType,
  247. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  248. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  249. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  250. async_connect(
  251. const EndpointType& endpoint,
  252. const handshake_params& params,
  253. diagnostics& diag,
  254. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  255. )
  256. {
  257. static_assert(
  258. detail::is_socket_stream<Stream>::value,
  259. "async_connect can only be used if Stream satisfies the SocketStream concept"
  260. );
  261. return impl_.async_connect<typename Stream::lowest_layer_type::endpoint_type>(
  262. endpoint,
  263. params,
  264. diag,
  265. std::forward<CompletionToken>(token)
  266. );
  267. }
  268. /**
  269. * \brief Performs the MySQL-level handshake.
  270. * \details
  271. * Does not connect the underlying stream.
  272. * If the `Stream` template parameter fulfills the `SocketConnection`
  273. * requirements, use \ref connection::connect instead of this function.
  274. * \n
  275. * If using a SSL-capable stream, the SSL handshake will be performed by this function.
  276. */
  277. void handshake(const handshake_params& params, error_code& ec, diagnostics& diag)
  278. {
  279. impl_.run(impl_.make_params_handshake(params, diag), ec);
  280. }
  281. /// \copydoc handshake
  282. void handshake(const handshake_params& params)
  283. {
  284. error_code err;
  285. diagnostics diag;
  286. handshake(params, err, diag);
  287. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  288. }
  289. /**
  290. * \copydoc handshake
  291. * \par Object lifetimes
  292. * The strings pointed to by `params` should be kept alive by the caller
  293. * until the operation completes, as no copy is made by the library.
  294. *
  295. * \par Handler signature
  296. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  297. */
  298. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  299. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  300. auto async_handshake(
  301. const handshake_params& params,
  302. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  303. ) BOOST_MYSQL_RETURN_TYPE(detail::async_handshake_t<CompletionToken&&>)
  304. {
  305. return async_handshake(params, impl_.shared_diag(), std::forward<CompletionToken>(token));
  306. }
  307. /// \copydoc async_handshake
  308. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  309. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  310. auto async_handshake(
  311. const handshake_params& params,
  312. diagnostics& diag,
  313. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  314. ) BOOST_MYSQL_RETURN_TYPE(detail::async_handshake_t<CompletionToken&&>)
  315. {
  316. return impl_.async_run(
  317. impl_.make_params_handshake(params, diag),
  318. std::forward<CompletionToken>(token)
  319. );
  320. }
  321. /**
  322. * \brief Executes a text query or prepared statement.
  323. * \details
  324. * Sends `req` to the server for execution and reads the response into `result`.
  325. * `result` may be either a \ref results or \ref static_results object.
  326. * `req` should may be either a type convertible to \ref string_view containing valid SQL
  327. * or a bound prepared statement, obtained by calling \ref statement::bind.
  328. * If a string, it must be encoded using the connection's character set.
  329. * Any string parameters provided to \ref statement::bind should also be encoded
  330. * using the connection's character set.
  331. * \n
  332. * After this operation completes successfully, `result.has_value() == true`.
  333. * \n
  334. * Metadata in `result` will be populated according to `this->meta_mode()`.
  335. */
  336. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  337. void execute(const ExecutionRequest& req, ResultsType& result, error_code& err, diagnostics& diag)
  338. {
  339. impl_.execute(req, result, err, diag);
  340. }
  341. /// \copydoc execute
  342. template <BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest, BOOST_MYSQL_RESULTS_TYPE ResultsType>
  343. void execute(const ExecutionRequest& req, ResultsType& result)
  344. {
  345. error_code err;
  346. diagnostics diag;
  347. execute(req, result, err, diag);
  348. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  349. }
  350. /**
  351. * \copydoc execute
  352. * \par Object lifetimes
  353. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  354. * responsible for managing `req`'s validity following these rules:
  355. * \n
  356. * \li If `req` is `string_view`, the string pointed to by `req`
  357. * must be kept alive by the caller until the operation is initiated.
  358. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  359. * type (like `string_view`), the caller must keep the values pointed by these references alive
  360. * until the operation is initiated.
  361. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  362. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  363. *
  364. * \par Handler signature
  365. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  366. */
  367. template <
  368. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  369. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  370. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  371. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  372. auto async_execute(
  373. ExecutionRequest&& req,
  374. ResultsType& result,
  375. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  376. ) BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  377. {
  378. return async_execute(
  379. std::forward<ExecutionRequest>(req),
  380. result,
  381. impl_.shared_diag(),
  382. std::forward<CompletionToken>(token)
  383. );
  384. }
  385. /// \copydoc async_execute
  386. template <
  387. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  388. BOOST_MYSQL_RESULTS_TYPE ResultsType,
  389. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  390. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  391. auto async_execute(
  392. ExecutionRequest&& req,
  393. ResultsType& result,
  394. diagnostics& diag,
  395. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  396. ) BOOST_MYSQL_RETURN_TYPE(detail::async_execute_t<ExecutionRequest&&, ResultsType, CompletionToken&&>)
  397. {
  398. return impl_.async_execute(
  399. std::forward<ExecutionRequest>(req),
  400. result,
  401. diag,
  402. std::forward<CompletionToken>(token)
  403. );
  404. }
  405. /**
  406. * \brief Starts a SQL execution as a multi-function operation.
  407. * \details
  408. * Writes the execution request and reads the initial server response and the column
  409. * metadata, but not the generated rows or subsequent resultsets, if any.
  410. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  411. * \n
  412. * After this operation completes, `st` will have
  413. * \ref execution_state::meta populated.
  414. * Metadata will be populated according to `this->meta_mode()`.
  415. * \n
  416. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  417. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  418. * Otherwise, the results are undefined.
  419. * \n
  420. * req may be either a type convertible to \ref string_view containing valid SQL
  421. * or a bound prepared statement, obtained by calling \ref statement::bind.
  422. * If a string, it must be encoded using the connection's character set.
  423. * Any string parameters provided to \ref statement::bind should also be encoded
  424. * using the connection's character set.
  425. * \n
  426. * When using the static interface, this function will detect schema mismatches for the first
  427. * resultset. Further errors may be detected by \ref read_resultset_head and \ref read_some_rows.
  428. */
  429. template <
  430. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  431. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  432. void start_execution(
  433. const ExecutionRequest& req,
  434. ExecutionStateType& st,
  435. error_code& err,
  436. diagnostics& diag
  437. )
  438. {
  439. impl_.start_execution(req, st, err, diag);
  440. }
  441. /// \copydoc start_execution
  442. template <
  443. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  444. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  445. void start_execution(const ExecutionRequest& req, ExecutionStateType& st)
  446. {
  447. error_code err;
  448. diagnostics diag;
  449. start_execution(req, st, err, diag);
  450. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  451. }
  452. /**
  453. * \copydoc start_execution
  454. * \par Object lifetimes
  455. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  456. * responsible for managing `req`'s validity following these rules:
  457. * \n
  458. * \li If `req` is `string_view`, the string pointed to by `req`
  459. * must be kept alive by the caller until the operation is initiated.
  460. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  461. * type (like `string_view`), the caller must keep the values pointed by these references alive
  462. * until the operation is initiated.
  463. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  464. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  465. *
  466. * \par Handler signature
  467. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  468. */
  469. template <
  470. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  471. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  472. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  473. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  474. auto async_start_execution(
  475. ExecutionRequest&& req,
  476. ExecutionStateType& st,
  477. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  478. )
  479. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  480. ExecutionRequest&&,
  481. ExecutionStateType,
  482. CompletionToken&&>)
  483. {
  484. return async_start_execution(
  485. std::forward<ExecutionRequest>(req),
  486. st,
  487. impl_.shared_diag(),
  488. std::forward<CompletionToken>(token)
  489. );
  490. }
  491. /// \copydoc async_start_execution
  492. template <
  493. BOOST_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  494. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  495. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  496. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  497. auto async_start_execution(
  498. ExecutionRequest&& req,
  499. ExecutionStateType& st,
  500. diagnostics& diag,
  501. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  502. )
  503. BOOST_MYSQL_RETURN_TYPE(detail::async_start_execution_t<
  504. ExecutionRequest&&,
  505. ExecutionStateType,
  506. CompletionToken&&>)
  507. {
  508. return impl_.async_start_execution(
  509. std::forward<ExecutionRequest>(req),
  510. st,
  511. diag,
  512. std::forward<CompletionToken>(token)
  513. );
  514. }
  515. /**
  516. * \brief Prepares a statement server-side.
  517. * \details
  518. * `stmt` should be encoded using the connection's character set.
  519. * \n
  520. * The returned statement has `valid() == true`.
  521. */
  522. statement prepare_statement(string_view stmt, error_code& err, diagnostics& diag)
  523. {
  524. return impl_.run(detail::prepare_statement_algo_params{&diag, stmt}, err);
  525. }
  526. /// \copydoc prepare_statement
  527. statement prepare_statement(string_view stmt)
  528. {
  529. error_code err;
  530. diagnostics diag;
  531. statement res = prepare_statement(stmt, err, diag);
  532. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  533. return res;
  534. }
  535. /**
  536. * \copydoc prepare_statement
  537. * \details
  538. * \par Object lifetimes
  539. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the string
  540. * pointed to by `stmt` must be kept alive by the caller until the operation is
  541. * initiated.
  542. *
  543. * \par Handler signature
  544. * The handler signature for this operation is `void(boost::mysql::error_code, boost::mysql::statement)`.
  545. */
  546. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  547. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  548. auto async_prepare_statement(
  549. string_view stmt,
  550. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  551. ) BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  552. {
  553. return async_prepare_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  554. }
  555. /// \copydoc async_prepare_statement
  556. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::statement))
  557. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  558. auto async_prepare_statement(
  559. string_view stmt,
  560. diagnostics& diag,
  561. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  562. ) BOOST_MYSQL_RETURN_TYPE(detail::async_prepare_statement_t<CompletionToken&&>)
  563. {
  564. return impl_.async_run(
  565. detail::prepare_statement_algo_params{&diag, stmt},
  566. std::forward<CompletionToken>(token)
  567. );
  568. }
  569. /**
  570. * \brief Closes a statement, deallocating it from the server.
  571. * \details
  572. * After this operation succeeds, `stmt` must not be used again for execution.
  573. * \par Performance warning
  574. * This function is currently affected by a peformance issue described
  575. * in https://github.com/boostorg/mysql/issues/181. Consider using
  576. * \ref reset_connection or \ref async_reset_connection instead.
  577. * \n
  578. * \par Preconditions
  579. * `stmt.valid() == true`
  580. */
  581. void close_statement(const statement& stmt, error_code& err, diagnostics& diag)
  582. {
  583. impl_.run(impl_.make_params_close_statement(stmt, diag), err);
  584. }
  585. /// \copydoc close_statement
  586. void close_statement(const statement& stmt)
  587. {
  588. error_code err;
  589. diagnostics diag;
  590. close_statement(stmt, err, diag);
  591. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  592. }
  593. /**
  594. * \copydoc close_statement
  595. * \details
  596. * \par Object lifetimes
  597. * It is not required to keep `stmt` alive, as copies are made by the implementation as required.
  598. *
  599. * \par Handler signature
  600. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  601. */
  602. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  603. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  604. auto async_close_statement(
  605. const statement& stmt,
  606. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  607. ) BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  608. {
  609. return async_close_statement(stmt, impl_.shared_diag(), std::forward<CompletionToken>(token));
  610. }
  611. /// \copydoc async_close_statement
  612. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  613. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  614. auto async_close_statement(
  615. const statement& stmt,
  616. diagnostics& diag,
  617. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  618. ) BOOST_MYSQL_RETURN_TYPE(detail::async_close_statement_t<CompletionToken&&>)
  619. {
  620. return impl_.async_run(
  621. impl_.make_params_close_statement(stmt, diag),
  622. std::forward<CompletionToken>(token)
  623. );
  624. }
  625. /**
  626. * \brief Reads a batch of rows.
  627. * \details
  628. * The number of rows that will be read is unspecified. If the operation represented by `st`
  629. * has still rows to read, at least one will be read. If there are no more rows, or
  630. * `st.should_read_rows() == false`, returns an empty `rows_view`.
  631. * \n
  632. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  633. * the greater the batch size (up to a maximum). You can set the initial buffer size in the
  634. * constructor. The buffer may be
  635. * grown bigger by other read operations, if required.
  636. * \n
  637. * The returned view points into memory owned by `*this`. It will be valid until
  638. * `*this` performs the next network operation or is destroyed.
  639. */
  640. rows_view read_some_rows(execution_state& st, error_code& err, diagnostics& diag)
  641. {
  642. return impl_.run(impl_.make_params_read_some_rows(st, diag), err);
  643. }
  644. /// \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  645. rows_view read_some_rows(execution_state& st)
  646. {
  647. error_code err;
  648. diagnostics diag;
  649. rows_view res = read_some_rows(st, err, diag);
  650. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  651. return res;
  652. }
  653. /**
  654. * \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  655. * \details
  656. * \par Handler signature
  657. * The handler signature for this operation is
  658. * `void(boost::mysql::error_code, boost::mysql::rows_view)`.
  659. */
  660. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  661. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  662. auto async_read_some_rows(
  663. execution_state& st,
  664. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  665. ) BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  666. {
  667. return async_read_some_rows(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  668. }
  669. /// \copydoc async_read_some_rows(execution_state&,CompletionToken&&)
  670. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, ::boost::mysql::rows_view))
  671. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  672. auto async_read_some_rows(
  673. execution_state& st,
  674. diagnostics& diag,
  675. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  676. ) BOOST_MYSQL_RETURN_TYPE(detail::async_read_some_rows_dynamic_t<CompletionToken&&>)
  677. {
  678. return impl_.async_run(
  679. impl_.make_params_read_some_rows(st, diag),
  680. std::forward<CompletionToken>(token)
  681. );
  682. }
  683. #ifdef BOOST_MYSQL_CXX14
  684. /**
  685. * \brief Reads a batch of rows.
  686. * \details
  687. * Reads a batch of rows of unspecified size into the storage given by `output`.
  688. * At most `output.size()` rows will be read. If the operation represented by `st`
  689. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  690. * \n
  691. * Returns the number of read rows.
  692. * \n
  693. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  694. * zero.
  695. * \n
  696. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  697. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  698. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  699. * grown bigger by other read operations, if required.
  700. * \n
  701. * Rows read by this function are owning objects, and don't hold any reference to
  702. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  703. * \n
  704. * The type `SpanElementType` must be the underlying row type for one of the types in the
  705. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  706. * The type must match the resultset that is currently being processed by `st`. For instance,
  707. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  708. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  709. * \n
  710. * This function can report schema mismatches.
  711. */
  712. template <class SpanElementType, class... StaticRow>
  713. std::size_t read_some_rows(
  714. static_execution_state<StaticRow...>& st,
  715. span<SpanElementType> output,
  716. error_code& err,
  717. diagnostics& diag
  718. )
  719. {
  720. return impl_.run(impl_.make_params_read_some_rows_static(st, output, diag), err);
  721. }
  722. /**
  723. * \brief Reads a batch of rows.
  724. * \details
  725. * Reads a batch of rows of unspecified size into the storage given by `output`.
  726. * At most `output.size()` rows will be read. If the operation represented by `st`
  727. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  728. * \n
  729. * Returns the number of read rows.
  730. * \n
  731. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  732. * zero.
  733. * \n
  734. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  735. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  736. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  737. * grown bigger by other read operations, if required.
  738. * \n
  739. * Rows read by this function are owning objects, and don't hold any reference to
  740. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  741. * \n
  742. * The type `SpanElementType` must be the underlying row type for one of the types in the
  743. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  744. * The type must match the resultset that is currently being processed by `st`. For instance,
  745. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  746. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  747. * \n
  748. * This function can report schema mismatches.
  749. */
  750. template <class SpanElementType, class... StaticRow>
  751. std::size_t read_some_rows(static_execution_state<StaticRow...>& st, span<SpanElementType> output)
  752. {
  753. error_code err;
  754. diagnostics diag;
  755. std::size_t res = read_some_rows(st, output, err, diag);
  756. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  757. return res;
  758. }
  759. /**
  760. * \brief Reads a batch of rows.
  761. * \details
  762. * Reads a batch of rows of unspecified size into the storage given by `output`.
  763. * At most `output.size()` rows will be read. If the operation represented by `st`
  764. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  765. * \n
  766. * Returns the number of read rows.
  767. * \n
  768. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  769. * zero.
  770. * \n
  771. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  772. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  773. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  774. * grown bigger by other read operations, if required.
  775. * \n
  776. * Rows read by this function are owning objects, and don't hold any reference to
  777. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  778. * \n
  779. * The type `SpanElementType` must be the underlying row type for one of the types in the
  780. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  781. * The type must match the resultset that is currently being processed by `st`. For instance,
  782. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  783. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  784. * \n
  785. * This function can report schema mismatches.
  786. *
  787. * \par Handler signature
  788. * The handler signature for this operation is
  789. * `void(boost::mysql::error_code, std::size_t)`.
  790. *
  791. * \par Object lifetimes
  792. * The storage that `output` references must be kept alive until the operation completes.
  793. */
  794. template <
  795. class SpanElementType,
  796. class... StaticRow,
  797. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  798. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  799. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, std::size_t))
  800. async_read_some_rows(
  801. static_execution_state<StaticRow...>& st,
  802. span<SpanElementType> output,
  803. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  804. )
  805. {
  806. return async_read_some_rows(st, output, impl_.shared_diag(), std::forward<CompletionToken>(token));
  807. }
  808. /**
  809. * \brief Reads a batch of rows.
  810. * \details
  811. * Reads a batch of rows of unspecified size into the storage given by `output`.
  812. * At most `output.size()` rows will be read. If the operation represented by `st`
  813. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  814. * \n
  815. * Returns the number of read rows.
  816. * \n
  817. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  818. * zero.
  819. * \n
  820. * The number of rows that will be read depends on the connection's buffer size. The bigger the buffer,
  821. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  822. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  823. * grown bigger by other read operations, if required.
  824. * \n
  825. * Rows read by this function are owning objects, and don't hold any reference to
  826. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  827. * \n
  828. * The type `SpanElementType` must be the underlying row type for one of the types in the
  829. * `StaticRow` parameter pack (i.e., one of the types in `underlying_row_t<StaticRow>...`).
  830. * The type must match the resultset that is currently being processed by `st`. For instance,
  831. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanElementType`
  832. * must exactly be `underlying_row_t<T2>`. If this is not the case, a runtime error will be issued.
  833. * \n
  834. * This function can report schema mismatches.
  835. *
  836. * \par Handler signature
  837. * The handler signature for this operation is
  838. * `void(boost::mysql::error_code, std::size_t)`.
  839. *
  840. * \par Object lifetimes
  841. * The storage that `output` references must be kept alive until the operation completes.
  842. */
  843. template <
  844. class SpanElementType,
  845. class... StaticRow,
  846. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code, std::size_t))
  847. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  848. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, std::size_t))
  849. async_read_some_rows(
  850. static_execution_state<StaticRow...>& st,
  851. span<SpanElementType> output,
  852. diagnostics& diag,
  853. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  854. )
  855. {
  856. return impl_.async_run(
  857. impl_.make_params_read_some_rows_static(st, output, diag),
  858. std::forward<CompletionToken>(token)
  859. );
  860. }
  861. #endif
  862. /**
  863. * \brief Reads metadata for subsequent resultsets in a multi-resultset operation.
  864. * \details
  865. * If `st.should_read_head() == true`, this function will read the next resultset's
  866. * initial response message and metadata, if any. If the resultset indicates a failure
  867. * (e.g. the query associated to this resultset contained an error), this function will fail
  868. * with that error.
  869. * \n
  870. * If `st.should_read_head() == false`, this function is a no-op.
  871. * \n
  872. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  873. * \n
  874. * This function is only relevant when using multi-function operations with statements
  875. * that return more than one resultset.
  876. * \n
  877. * When using the static interface, this function will detect schema mismatches for the resultset
  878. * currently being read. Further errors may be detected by subsequent invocations of this function
  879. * and by \ref read_some_rows.
  880. */
  881. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  882. void read_resultset_head(ExecutionStateType& st, error_code& err, diagnostics& diag)
  883. {
  884. return impl_.run(impl_.make_params_read_resultset_head(st, diag), err);
  885. }
  886. /// \copydoc read_resultset_head
  887. template <BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  888. void read_resultset_head(ExecutionStateType& st)
  889. {
  890. error_code err;
  891. diagnostics diag;
  892. read_resultset_head(st, err, diag);
  893. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  894. }
  895. /**
  896. * \copydoc read_resultset_head
  897. * \par Handler signature
  898. * The handler signature for this operation is
  899. * `void(boost::mysql::error_code)`.
  900. */
  901. template <
  902. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  903. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  904. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  905. auto async_read_resultset_head(
  906. ExecutionStateType& st,
  907. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  908. ) BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  909. {
  910. return async_read_resultset_head(st, impl_.shared_diag(), std::forward<CompletionToken>(token));
  911. }
  912. /// \copydoc async_read_resultset_head
  913. template <
  914. BOOST_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  915. BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  916. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  917. auto async_read_resultset_head(
  918. ExecutionStateType& st,
  919. diagnostics& diag,
  920. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  921. ) BOOST_MYSQL_RETURN_TYPE(detail::async_read_resultset_head_t<CompletionToken&&>)
  922. {
  923. return impl_.async_run(
  924. impl_.make_params_read_resultset_head(st, diag),
  925. std::forward<CompletionToken>(token)
  926. );
  927. }
  928. /**
  929. * \brief Checks whether the server is alive.
  930. * \details
  931. * If the server is alive, this function will complete without error.
  932. * If it's not, it will fail with the relevant network or protocol error.
  933. * \n
  934. * Note that ping requests are treated as any other type of request at the protocol
  935. * level, and won't be prioritized anyhow by the server. If the server is stuck
  936. * in a long-running query, the ping request won't be answered until the query is
  937. * finished.
  938. */
  939. void ping(error_code& err, diagnostics& diag) { impl_.run(impl_.make_params_ping(diag), err); }
  940. /// \copydoc ping
  941. void ping()
  942. {
  943. error_code err;
  944. diagnostics diag;
  945. ping(err, diag);
  946. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  947. }
  948. /**
  949. * \copydoc ping
  950. * \details
  951. * \n
  952. * \par Handler signature
  953. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  954. */
  955. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  956. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  957. auto async_ping(CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  958. BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  959. {
  960. return async_ping(impl_.shared_diag(), std::forward<CompletionToken>(token));
  961. }
  962. /// \copydoc async_ping
  963. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  964. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  965. auto async_ping(
  966. diagnostics& diag,
  967. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  968. ) BOOST_MYSQL_RETURN_TYPE(detail::async_ping_t<CompletionToken&&>)
  969. {
  970. return impl_.async_run(impl_.make_params_ping(diag), std::forward<CompletionToken>(token));
  971. }
  972. /**
  973. * \brief Resets server-side session state, like variables and prepared statements.
  974. * \details
  975. * Resets all server-side state for the current session:
  976. * \n
  977. * \li Rolls back any active transactions and resets autocommit mode.
  978. * \li Releases all table locks.
  979. * \li Drops all temporary tables.
  980. * \li Resets all session system variables to their default values (including the ones set by `SET
  981. * NAMES`) and clears all user-defined variables.
  982. * \li Closes all prepared statements.
  983. * \n
  984. * A full reference on the affected session state can be found
  985. * <a href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-reset-connection.html">here</a>.
  986. * \n
  987. * This function will not reset the current physical connection and won't cause re-authentication.
  988. * It is faster than closing and re-opening a connection.
  989. * \n
  990. * The connection must be connected and authenticated before calling this function.
  991. * This function involves communication with the server, and thus may fail.
  992. *
  993. * \par Warning on character sets
  994. * This function will restore the connection's character set and collation **to the server's default**,
  995. * and not to the one specified during connection establishment. Some servers have `latin1` as their
  996. * default character set, which is not usually what you want. Use a `SET NAMES` statement after using
  997. * this function to be sure.
  998. * \n
  999. * You can find the character set that your server will use after reset by running:
  1000. * \code
  1001. * "SELECT @@global.character_set_client, @@global.character_set_results;"
  1002. * \endcode
  1003. */
  1004. void reset_connection(error_code& err, diagnostics& diag)
  1005. {
  1006. impl_.run(impl_.make_params_reset_connection(diag), err);
  1007. }
  1008. /// \copydoc reset_connection
  1009. void reset_connection()
  1010. {
  1011. error_code err;
  1012. diagnostics diag;
  1013. reset_connection(err, diag);
  1014. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1015. }
  1016. /**
  1017. * \copydoc reset_connection
  1018. * \details
  1019. * \n
  1020. * \par Handler signature
  1021. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1022. */
  1023. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1024. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1025. auto async_reset_connection(CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1026. BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  1027. {
  1028. return async_reset_connection(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1029. }
  1030. /// \copydoc async_reset_connection
  1031. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1032. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1033. auto async_reset_connection(
  1034. diagnostics& diag,
  1035. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1036. ) BOOST_MYSQL_RETURN_TYPE(detail::async_reset_connection_t<CompletionToken&&>)
  1037. {
  1038. return impl_.async_run(
  1039. impl_.make_params_reset_connection(diag),
  1040. std::forward<CompletionToken>(token)
  1041. );
  1042. }
  1043. /**
  1044. * \brief Closes the connection to the server.
  1045. * \details
  1046. * This function is only available if `Stream` satisfies the `SocketStream` concept.
  1047. * \n
  1048. * Sends a quit request, performs the TLS shutdown (if required)
  1049. * and closes the underlying stream. Prefer this function to \ref connection::quit.
  1050. */
  1051. void close(error_code& err, diagnostics& diag)
  1052. {
  1053. static_assert(
  1054. detail::is_socket_stream<Stream>::value,
  1055. "close can only be used if Stream satisfies the SocketStream concept"
  1056. );
  1057. impl_.run(impl_.make_params_close(diag), err);
  1058. }
  1059. /// \copydoc close
  1060. void close()
  1061. {
  1062. static_assert(
  1063. detail::is_socket_stream<Stream>::value,
  1064. "close can only be used if Stream satisfies the SocketStream concept"
  1065. );
  1066. error_code err;
  1067. diagnostics diag;
  1068. close(err, diag);
  1069. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1070. }
  1071. /**
  1072. * \copydoc close
  1073. * \details
  1074. * \par Handler signature
  1075. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1076. */
  1077. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1078. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1079. auto async_close(CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1080. BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  1081. {
  1082. static_assert(
  1083. detail::is_socket_stream<Stream>::value,
  1084. "async_close can only be used if Stream satisfies the SocketStream concept"
  1085. );
  1086. return async_close(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1087. }
  1088. /// \copydoc async_close
  1089. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1090. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1091. auto async_close(
  1092. diagnostics& diag,
  1093. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1094. ) BOOST_MYSQL_RETURN_TYPE(detail::async_close_connection_t<CompletionToken&&>)
  1095. {
  1096. static_assert(
  1097. detail::is_socket_stream<Stream>::value,
  1098. "async_close can only be used if Stream satisfies the SocketStream concept"
  1099. );
  1100. return impl_.async_run(impl_.make_params_close(diag), std::forward<CompletionToken>(token));
  1101. }
  1102. /**
  1103. * \brief Notifies the MySQL server that the client wants to end the session and shutdowns SSL.
  1104. * \details Sends a quit request to the MySQL server. If the connection is using SSL,
  1105. * this function will also perform the SSL shutdown. You should
  1106. * close the underlying physical connection after calling this function.
  1107. * \n
  1108. * If the `Stream` template parameter fulfills the `SocketConnection`
  1109. * requirements, use \ref connection::close instead of this function,
  1110. * as it also takes care of closing the underlying stream.
  1111. */
  1112. void quit(error_code& err, diagnostics& diag) { impl_.run(impl_.make_params_quit(diag), err); }
  1113. /// \copydoc quit
  1114. void quit()
  1115. {
  1116. error_code err;
  1117. diagnostics diag;
  1118. quit(err, diag);
  1119. detail::throw_on_error_loc(err, diag, BOOST_CURRENT_LOCATION);
  1120. }
  1121. /**
  1122. * \copydoc quit
  1123. * \details
  1124. * \par Handler signature
  1125. * The handler signature for this operation is `void(boost::mysql::error_code)`.
  1126. */
  1127. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1128. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1129. auto async_quit(CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1130. BOOST_MYSQL_RETURN_TYPE(detail::async_quit_connection_t<CompletionToken&&>)
  1131. {
  1132. return async_quit(impl_.shared_diag(), std::forward<CompletionToken>(token));
  1133. }
  1134. /// \copydoc async_quit
  1135. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(::boost::mysql::error_code))
  1136. CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1137. auto async_quit(
  1138. diagnostics& diag,
  1139. CompletionToken&& token BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1140. ) BOOST_MYSQL_RETURN_TYPE(detail::async_quit_connection_t<CompletionToken&&>)
  1141. {
  1142. return impl_.async_run(impl_.make_params_quit(diag), std::forward<CompletionToken>(token));
  1143. }
  1144. /**
  1145. * \brief Rebinds the connection type to another executor.
  1146. * \details
  1147. * The `Stream` type must either provide a `rebind_executor`
  1148. * member with the same semantics, or be an instantiation of `boost::asio::ssl::stream` with
  1149. * a `Stream` type providing a `rebind_executor` member.
  1150. */
  1151. template <typename Executor1>
  1152. struct rebind_executor
  1153. {
  1154. /// The connection type when rebound to the specified executor.
  1155. using other = connection<typename detail::rebind_executor<Stream, Executor1>::type>;
  1156. };
  1157. };
  1158. } // namespace mysql
  1159. } // namespace boost
  1160. #endif