connection.hpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BHO_MYSQL_CONNECTION_HPP
  8. #define BHO_MYSQL_CONNECTION_HPP
  9. #include <asio2/bho/mysql/buffer_params.hpp>
  10. #include <asio2/bho/mysql/diagnostics.hpp>
  11. #include <asio2/bho/mysql/error_code.hpp>
  12. #include <asio2/bho/mysql/execution_state.hpp>
  13. #include <asio2/bho/mysql/handshake_params.hpp>
  14. #include <asio2/bho/mysql/metadata_mode.hpp>
  15. #include <asio2/bho/mysql/results.hpp>
  16. #include <asio2/bho/mysql/rows_view.hpp>
  17. #include <asio2/bho/mysql/statement.hpp>
  18. #include <asio2/bho/mysql/string_view.hpp>
  19. #include <asio2/bho/mysql/detail/access.hpp>
  20. #include <asio2/bho/mysql/detail/any_stream_impl.hpp>
  21. #include <asio2/bho/mysql/detail/channel_ptr.hpp>
  22. #include <asio2/bho/mysql/detail/execution_concepts.hpp>
  23. #include <asio2/bho/mysql/detail/network_algorithms.hpp>
  24. #include <asio2/bho/mysql/detail/rebind_executor.hpp>
  25. #include <asio2/bho/mysql/detail/socket_stream.hpp>
  26. #include <asio2/bho/mysql/detail/throw_on_error_loc.hpp>
  27. #include <asio2/bho/mysql/detail/writable_field_traits.hpp>
  28. #include <asio2/bho/assert.hpp>
  29. #include <type_traits>
  30. #include <utility>
  31. /// The Boost libraries namespace.
  32. namespace bho {
  33. /// BHO.MySQL library namespace.
  34. namespace mysql {
  35. // Forward declarations
  36. template <class... StaticRow>
  37. class static_execution_state;
  38. /**
  39. * \brief A connection to a MySQL server.
  40. * \details
  41. * Represents a connection to a MySQL server.
  42. *\n
  43. * `connection` is the main I/O object that this library implements. It owns a `Stream` object that
  44. * is accessed by functions involving network operations, as well as session state. You can access
  45. * the stream using \ref connection::stream, and its executor via \ref connection::get_executor. The
  46. * executor used by this object is always the same as the underlying stream.
  47. *\n
  48. * \par Thread safety
  49. * Distinct objects: safe. \n
  50. * Shared objects: unsafe. \n
  51. * This class is <b>not thread-safe</b>: for a single object, if you
  52. * call its member functions concurrently from separate threads, you will get a race condition.
  53. */
  54. template <class Stream>
  55. class connection
  56. {
  57. detail::channel_ptr channel_;
  58. diagnostics& shared_diag() noexcept { return channel_.shared_diag(); }
  59. #ifndef BHO_MYSQL_DOXYGEN
  60. friend struct detail::access;
  61. #endif
  62. public:
  63. /**
  64. * \brief Initializing constructor.
  65. * \details
  66. * As part of the initialization, an internal `Stream` object is created.
  67. *
  68. * \par Exception safety
  69. * Basic guarantee. Throws if the `Stream` constructor throws
  70. * or if memory allocation for internal state fails.
  71. *
  72. * \param args Arguments to be forwarded to the `Stream` constructor.
  73. */
  74. template <
  75. class... Args,
  76. class EnableIf = typename std::enable_if<std::is_constructible<Stream, Args...>::value>::type>
  77. connection(Args&&... args) : connection(buffer_params(), std::forward<Args>(args)...)
  78. {
  79. }
  80. /**
  81. * \brief Initializing constructor with buffer params.
  82. * \details
  83. * As part of the initialization, an internal `Stream` object is created.
  84. *
  85. * \par Exception safety
  86. * Basic guarantee. Throws if the `Stream` constructor throws
  87. * or if memory allocation for internal state fails.
  88. *
  89. * \param buff_params Specifies initial sizes for internal buffers.
  90. * \param args Arguments to be forwarded to the `Stream` constructor.
  91. */
  92. template <
  93. class... Args,
  94. class EnableIf = typename std::enable_if<std::is_constructible<Stream, Args...>::value>::type>
  95. connection(const buffer_params& buff_params, Args&&... args)
  96. : channel_(
  97. buff_params.initial_read_size(),
  98. std::unique_ptr<detail::any_stream>(new detail::any_stream_impl<Stream>(std::forward<Args>(args
  99. )...))
  100. )
  101. {
  102. }
  103. /**
  104. * \brief Move constructor.
  105. */
  106. connection(connection&& other) = default;
  107. /**
  108. * \brief Move assignment.
  109. */
  110. connection& operator=(connection&& rhs) = default;
  111. #ifndef BHO_MYSQL_DOXYGEN
  112. connection(const connection&) = delete;
  113. connection& operator=(const connection&) = delete;
  114. #endif
  115. /// The executor type associated to this object.
  116. using executor_type = typename Stream::executor_type;
  117. /// Retrieves the executor associated to this object.
  118. executor_type get_executor() { return stream().get_executor(); }
  119. /// The `Stream` type this connection is using.
  120. using stream_type = Stream;
  121. /**
  122. * \brief Retrieves the underlying Stream object.
  123. * \details
  124. *
  125. * \par Exception safety
  126. * No-throw guarantee.
  127. */
  128. Stream& stream() noexcept { return detail::cast<Stream>(channel_.stream()); }
  129. /**
  130. * \brief Retrieves the underlying Stream object.
  131. * \details
  132. *
  133. * \par Exception safety
  134. * No-throw guarantee.
  135. */
  136. const Stream& stream() const noexcept { return detail::cast<Stream>(channel_.stream()); }
  137. /**
  138. * \brief Returns whether the connection negotiated the use of SSL or not.
  139. * \details
  140. * This function can be used to determine whether you are using a SSL
  141. * connection or not when using SSL negotiation.
  142. * \n
  143. * This function always returns `false` if the underlying
  144. * stream does not support SSL. This function always returns `false`
  145. * for connections that haven't been
  146. * established yet (handshake not run yet). If the handshake fails,
  147. * the return value is undefined.
  148. *
  149. * \par Exception safety
  150. * No-throw guarantee.
  151. *
  152. * \returns Whether the connection is using SSL.
  153. */
  154. bool uses_ssl() const noexcept { return channel_.stream().ssl_active(); }
  155. /**
  156. * \brief Returns the current metadata mode that this connection is using.
  157. * \details
  158. * \par Exception safety
  159. * No-throw guarantee.
  160. *
  161. * \returns The matadata mode that will be used for queries and statement executions.
  162. */
  163. metadata_mode meta_mode() const noexcept { return channel_.meta_mode(); }
  164. /**
  165. * \brief Sets the metadata mode.
  166. * \details
  167. * Will affect any query and statement executions performed after the call.
  168. *
  169. * \par Exception safety
  170. * No-throw guarantee.
  171. *
  172. * \par Preconditions
  173. * No asynchronous operation should be outstanding when this function is called.
  174. *
  175. * \param v The new metadata mode.
  176. */
  177. void set_meta_mode(metadata_mode v) noexcept { channel_.set_meta_mode(v); }
  178. /**
  179. * \brief Establishes a connection to a MySQL server.
  180. * \details
  181. * This function is only available if `Stream` satisfies the
  182. * `SocketStream` concept.
  183. * \n
  184. * Connects the underlying stream and performs the handshake
  185. * with the server. The underlying stream is closed in case of error. Prefer
  186. * this function to \ref connection::handshake.
  187. * \n
  188. * If using a SSL-capable stream, the SSL handshake will be performed by this function.
  189. * \n
  190. * `endpoint` should be convertible to `Stream::lowest_layer_type::endpoint_type`.
  191. */
  192. template <typename EndpointType>
  193. void connect(
  194. const EndpointType& endpoint,
  195. const handshake_params& params,
  196. error_code& ec,
  197. diagnostics& diag
  198. )
  199. {
  200. static_assert(
  201. detail::is_socket_stream<Stream>::value,
  202. "connect can only be used if Stream satisfies the SocketStream concept"
  203. );
  204. detail::connect_interface<Stream>(channel_.get(), endpoint, params, ec, diag);
  205. }
  206. /// \copydoc connect
  207. template <typename EndpointType>
  208. void connect(const EndpointType& endpoint, const handshake_params& params)
  209. {
  210. static_assert(
  211. detail::is_socket_stream<Stream>::value,
  212. "connect can only be used if Stream satisfies the SocketStream concept"
  213. );
  214. error_code err;
  215. diagnostics diag;
  216. connect(endpoint, params, err, diag);
  217. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  218. }
  219. /**
  220. * \copydoc connect
  221. * \par Object lifetimes
  222. * The strings pointed to by `params` should be kept alive by the caller
  223. * until the operation completes, as no copy is made by the library.
  224. * `endpoint` is copied as required and doesn't need to be kept alive.
  225. *
  226. * \par Handler signature
  227. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  228. */
  229. template <
  230. typename EndpointType,
  231. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  232. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  233. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  234. async_connect(
  235. const EndpointType& endpoint,
  236. const handshake_params& params,
  237. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  238. )
  239. {
  240. static_assert(
  241. detail::is_socket_stream<Stream>::value,
  242. "async_connect can only be used if Stream satisfies the SocketStream concept"
  243. );
  244. return async_connect(endpoint, params, this->shared_diag(), std::forward<CompletionToken>(token));
  245. }
  246. /// \copydoc async_connect
  247. template <
  248. typename EndpointType,
  249. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  250. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  251. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  252. async_connect(
  253. const EndpointType& endpoint,
  254. const handshake_params& params,
  255. diagnostics& diag,
  256. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  257. )
  258. {
  259. static_assert(
  260. detail::is_socket_stream<Stream>::value,
  261. "async_connect can only be used if Stream satisfies the SocketStream concept"
  262. );
  263. return detail::async_connect_interface<Stream>(
  264. channel_.get(),
  265. endpoint,
  266. params,
  267. diag,
  268. std::forward<CompletionToken>(token)
  269. );
  270. }
  271. /**
  272. * \brief Performs the MySQL-level handshake.
  273. * \details
  274. * Does not connect the underlying stream.
  275. * If the `Stream` template parameter fulfills the `SocketConnection`
  276. * requirements, use \ref connection::connect instead of this function.
  277. * \n
  278. * If using a SSL-capable stream, the SSL handshake will be performed by this function.
  279. */
  280. void handshake(const handshake_params& params, error_code& ec, diagnostics& diag)
  281. {
  282. detail::handshake_interface(channel_.get(), params, ec, diag);
  283. }
  284. /// \copydoc handshake
  285. void handshake(const handshake_params& params)
  286. {
  287. error_code err;
  288. diagnostics diag;
  289. handshake(params, err, diag);
  290. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  291. }
  292. /**
  293. * \copydoc handshake
  294. * \par Object lifetimes
  295. * The strings pointed to by `params` should be kept alive by the caller
  296. * until the operation completes, as no copy is made by the library.
  297. *
  298. * \par Handler signature
  299. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  300. */
  301. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  302. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  303. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  304. async_handshake(
  305. const handshake_params& params,
  306. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  307. )
  308. {
  309. return async_handshake(params, shared_diag(), std::forward<CompletionToken>(token));
  310. }
  311. /// \copydoc async_handshake
  312. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  313. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  314. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  315. async_handshake(
  316. const handshake_params& params,
  317. diagnostics& diag,
  318. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  319. )
  320. {
  321. return detail::async_handshake_interface(
  322. channel_.get(),
  323. params,
  324. diag,
  325. std::forward<CompletionToken>(token)
  326. );
  327. }
  328. /**
  329. * \brief Executes a text query or prepared statement.
  330. * \details
  331. * Sends `req` to the server for execution and reads the response into `result`.
  332. * `result` may be either a \ref results or \ref static_results object.
  333. * `req` should may be either a type convertible to \ref string_view containing valid SQL
  334. * or a bound prepared statement, obtained by calling \ref statement::bind.
  335. * If a string, it must be encoded using the connection's character set.
  336. * Any string parameters provided to \ref statement::bind should also be encoded
  337. * using the connection's character set.
  338. * \n
  339. * After this operation completes successfully, `result.has_value() == true`.
  340. * \n
  341. * Metadata in `result` will be populated according to `this->meta_mode()`.
  342. */
  343. template <BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest, BHO_MYSQL_RESULTS_TYPE ResultsType>
  344. void execute(const ExecutionRequest& req, ResultsType& result, error_code& err, diagnostics& diag)
  345. {
  346. detail::execute_interface(channel_.get(), req, result, err, diag);
  347. }
  348. /// \copydoc execute
  349. template <BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest, BHO_MYSQL_RESULTS_TYPE ResultsType>
  350. void execute(const ExecutionRequest& req, ResultsType& result)
  351. {
  352. error_code err;
  353. diagnostics diag;
  354. execute(req, result, err, diag);
  355. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  356. }
  357. /**
  358. * \copydoc execute
  359. * \par Object lifetimes
  360. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  361. * responsible for managing `req`'s validity following these rules:
  362. * \n
  363. * \li If `req` is `string_view`, the string pointed to by `req`
  364. * must be kept alive by the caller until the operation is initiated.
  365. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  366. * type (like `string_view`), the caller must keep the values pointed by these references alive
  367. * until the operation is initiated.
  368. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  369. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  370. *
  371. * \par Handler signature
  372. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  373. */
  374. template <
  375. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  376. BHO_MYSQL_RESULTS_TYPE ResultsType,
  377. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  378. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  379. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  380. async_execute(
  381. ExecutionRequest&& req,
  382. ResultsType& result,
  383. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  384. )
  385. {
  386. return async_execute(
  387. std::forward<ExecutionRequest>(req),
  388. result,
  389. shared_diag(),
  390. std::forward<CompletionToken>(token)
  391. );
  392. }
  393. /// \copydoc async_execute
  394. template <
  395. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  396. BHO_MYSQL_RESULTS_TYPE ResultsType,
  397. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  398. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  399. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  400. async_execute(
  401. ExecutionRequest&& req,
  402. ResultsType& result,
  403. diagnostics& diag,
  404. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  405. )
  406. {
  407. return detail::async_execute_interface(
  408. channel_.get(),
  409. std::forward<ExecutionRequest>(req),
  410. result,
  411. diag,
  412. std::forward<CompletionToken>(token)
  413. );
  414. }
  415. /**
  416. * \brief Starts a SQL execution as a multi-function operation.
  417. * \details
  418. * Writes the execution request and reads the initial server response and the column
  419. * metadata, but not the generated rows or subsequent resultsets, if any.
  420. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  421. * \n
  422. * After this operation completes, `st` will have
  423. * \ref execution_state::meta populated.
  424. * Metadata will be populated according to `this->meta_mode()`.
  425. * \n
  426. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  427. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  428. * Otherwise, the results are undefined.
  429. * \n
  430. * req may be either a type convertible to \ref string_view containing valid SQL
  431. * or a bound prepared statement, obtained by calling \ref statement::bind.
  432. * If a string, it must be encoded using the connection's character set.
  433. * Any string parameters provided to \ref statement::bind should also be encoded
  434. * using the connection's character set.
  435. * \n
  436. * When using the static interface, this function will detect schema mismatches for the first
  437. * resultset. Further errors may be detected by \ref read_resultset_head and \ref read_some_rows.
  438. */
  439. template <
  440. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  441. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  442. void start_execution(
  443. const ExecutionRequest& req,
  444. ExecutionStateType& st,
  445. error_code& err,
  446. diagnostics& diag
  447. )
  448. {
  449. detail::start_execution_interface(channel_.get(), req, st, err, diag);
  450. }
  451. /// \copydoc start_execution
  452. template <
  453. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  454. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  455. void start_execution(const ExecutionRequest& req, ExecutionStateType& st)
  456. {
  457. error_code err;
  458. diagnostics diag;
  459. start_execution(req, st, err, diag);
  460. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  461. }
  462. /**
  463. * \copydoc start_execution
  464. * \par Object lifetimes
  465. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the caller is
  466. * responsible for managing `req`'s validity following these rules:
  467. * \n
  468. * \li If `req` is `string_view`, the string pointed to by `req`
  469. * must be kept alive by the caller until the operation is initiated.
  470. * \li If `req` is a \ref bound_statement_tuple, and any of the parameters is a reference
  471. * type (like `string_view`), the caller must keep the values pointed by these references alive
  472. * until the operation is initiated.
  473. * \li If `req` is a \ref bound_statement_iterator_range, the caller must keep objects in
  474. * the iterator range passed to \ref statement::bind alive until the operation is initiated.
  475. *
  476. * \par Handler signature
  477. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  478. */
  479. template <
  480. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  481. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  482. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  483. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  484. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  485. async_start_execution(
  486. ExecutionRequest&& req,
  487. ExecutionStateType& st,
  488. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  489. )
  490. {
  491. return async_start_execution(
  492. std::forward<ExecutionRequest>(req),
  493. st,
  494. shared_diag(),
  495. std::forward<CompletionToken>(token)
  496. );
  497. }
  498. /// \copydoc async_start_execution
  499. template <
  500. BHO_MYSQL_EXECUTION_REQUEST ExecutionRequest,
  501. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  502. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  503. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  504. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  505. async_start_execution(
  506. ExecutionRequest&& req,
  507. ExecutionStateType& st,
  508. diagnostics& diag,
  509. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  510. )
  511. {
  512. return detail::async_start_execution_interface(
  513. channel_.get(),
  514. std::forward<ExecutionRequest>(req),
  515. st,
  516. diag,
  517. std::forward<CompletionToken>(token)
  518. );
  519. }
  520. /**
  521. * \brief (Deprecated) Executes a SQL text query.
  522. * \details
  523. * Sends `query_string` to the server for execution and reads the response into `result`.
  524. * query_string should be encoded using the connection's character set.
  525. * \n
  526. * After this operation completes successfully, `result.has_value() == true`.
  527. * \n
  528. * Metadata in `result` will be populated according to `this->meta_mode()`.
  529. * \n
  530. * \par Security
  531. * If you compose `query_string` by concatenating strings manually, <b>your code is
  532. * vulnerable to SQL injection attacks</b>. If your query contains patameters unknown at
  533. * compile time, use prepared statements instead of this function.
  534. *
  535. * \par Deprecation notice
  536. * This function is only provided for backwards-compatibility. For new code, please
  537. * use \ref execute or \ref async_execute instead.
  538. */
  539. void query(string_view query_string, results& result, error_code& err, diagnostics& diag)
  540. {
  541. execute(query_string, result, err, diag);
  542. }
  543. /// \copydoc query
  544. void query(string_view query_string, results& result) { execute(query_string, result); }
  545. /**
  546. * \copydoc query
  547. * \details
  548. * \par Object lifetimes
  549. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the string
  550. * pointed to by `query_string` must be kept alive by the caller until the operation is
  551. * initiated.
  552. *
  553. * \par Handler signature
  554. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  555. */
  556. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  557. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  558. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  559. async_query(
  560. string_view query_string,
  561. results& result,
  562. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  563. )
  564. {
  565. return async_query(query_string, result, shared_diag(), std::forward<CompletionToken>(token));
  566. }
  567. /// \copydoc async_query
  568. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  569. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  570. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  571. async_query(
  572. string_view query_string,
  573. results& result,
  574. diagnostics& diag,
  575. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  576. )
  577. {
  578. return async_execute(query_string, result, diag, std::forward<CompletionToken>(token));
  579. }
  580. /**
  581. * \brief (Deprecated) Starts a text query as a multi-function operation.
  582. * \details
  583. * Writes the query request and reads the initial server response and the column
  584. * metadata, but not the generated rows or subsequent resultsets, if any.
  585. * After this operation completes, `st` will have
  586. * \ref execution_state::meta populated.
  587. * Metadata will be populated according to `this->meta_mode()`.
  588. * \n
  589. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  590. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  591. * Otherwise, the results are undefined.
  592. * \n
  593. * `query_string` should be encoded using the connection's character set.
  594. *
  595. * \par Deprecation notice
  596. * This function is only provided for backwards-compatibility. For new code, please
  597. * use \ref start_execution or \ref async_start_execution instead.
  598. */
  599. void start_query(string_view query_string, execution_state& st, error_code& err, diagnostics& diag)
  600. {
  601. start_execution(query_string, st, err, diag);
  602. }
  603. /// \copydoc start_query
  604. void start_query(string_view query_string, execution_state& st) { start_execution(query_string, st); }
  605. /**
  606. * \copydoc start_query
  607. * \details
  608. * \par Object lifetimes
  609. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the string
  610. * pointed to by `query_string` must be kept alive by the caller until the operation is
  611. * initiated.
  612. *
  613. * \par Handler signature
  614. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  615. */
  616. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  617. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  618. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  619. async_start_query(
  620. string_view query_string,
  621. execution_state& st,
  622. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  623. )
  624. {
  625. return async_start_query(query_string, st, shared_diag(), std::forward<CompletionToken>(token));
  626. }
  627. /// \copydoc async_start_query
  628. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  629. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  630. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  631. async_start_query(
  632. string_view query_string,
  633. execution_state& st,
  634. diagnostics& diag,
  635. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  636. )
  637. {
  638. return async_start_execution(query_string, st, diag, std::forward<CompletionToken>(token));
  639. }
  640. /**
  641. * \brief Prepares a statement server-side.
  642. * \details
  643. * `stmt` should be encoded using the connection's character set.
  644. * \n
  645. * The returned statement has `valid() == true`.
  646. */
  647. statement prepare_statement(string_view stmt, error_code& err, diagnostics& diag)
  648. {
  649. return detail::prepare_statement_interface(channel_.get(), stmt, err, diag);
  650. }
  651. /// \copydoc prepare_statement
  652. statement prepare_statement(string_view stmt)
  653. {
  654. error_code err;
  655. diagnostics diag;
  656. statement res = prepare_statement(stmt, err, diag);
  657. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  658. return res;
  659. }
  660. /**
  661. * \copydoc prepare_statement
  662. * \details
  663. * \par Object lifetimes
  664. * If `CompletionToken` is a deferred completion token (e.g. `use_awaitable`), the string
  665. * pointed to by `stmt` must be kept alive by the caller until the operation is
  666. * initiated.
  667. *
  668. * \par Handler signature
  669. * The handler signature for this operation is `void(bho::mysql::error_code, bho::mysql::statement)`.
  670. */
  671. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, ::bho::mysql::statement))
  672. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  673. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, statement))
  674. async_prepare_statement(
  675. string_view stmt,
  676. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  677. )
  678. {
  679. return async_prepare_statement(stmt, shared_diag(), std::forward<CompletionToken>(token));
  680. }
  681. /// \copydoc async_prepare_statement
  682. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, ::bho::mysql::statement))
  683. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  684. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, statement))
  685. async_prepare_statement(
  686. string_view stmt,
  687. diagnostics& diag,
  688. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  689. )
  690. {
  691. return detail::async_prepare_statement_interface(
  692. channel_.get(),
  693. stmt,
  694. diag,
  695. std::forward<CompletionToken>(token)
  696. );
  697. }
  698. /**
  699. * \brief (Deprecated) Executes a prepared statement.
  700. * \details
  701. * Executes a statement with the given parameters and reads the response into `result`.
  702. * \n
  703. * After this operation completes successfully, `result.has_value() == true`.
  704. * \n
  705. * The statement actual parameters (`params`) are passed as a `std::tuple` of elements.
  706. * See the `WritableFieldTuple` concept defition for more info. You should pass exactly as many
  707. * parameters as `this->num_params()`, or the operation will fail with an error.
  708. * String parameters should be encoded using the connection's character set.
  709. * \n
  710. * Metadata in `result` will be populated according to `conn.meta_mode()`, where `conn`
  711. * is the connection that prepared this statement.
  712. *
  713. * \par Deprecation notice
  714. * This function is only provided for backwards-compatibility. For new code, please
  715. * use \ref execute or \ref async_execute instead.
  716. *
  717. * \par Preconditions
  718. * `stmt.valid() == true`
  719. */
  720. template <
  721. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  722. class EnableIf =
  723. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  724. void execute_statement(
  725. const statement& stmt,
  726. const WritableFieldTuple& params,
  727. results& result,
  728. error_code& err,
  729. diagnostics& diag
  730. )
  731. {
  732. execute(stmt.bind(params), result, err, diag);
  733. }
  734. /// \copydoc execute_statement
  735. template <
  736. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  737. class EnableIf =
  738. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  739. void execute_statement(const statement& stmt, const WritableFieldTuple& params, results& result)
  740. {
  741. execute(stmt.bind(params), result);
  742. }
  743. /**
  744. * \copydoc execute_statement
  745. * \par Object lifetimes
  746. * If `CompletionToken` is deferred (like `use_awaitable`), and `params` contains any reference
  747. * type (like `string_view`), the caller must keep the values pointed by these references alive
  748. * until the operation is initiated. Value types will be copied/moved as required, so don't need
  749. * to be kept alive. It's not required to keep `stmt` alive, either.
  750. *
  751. * \par Handler signature
  752. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  753. */
  754. template <
  755. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  756. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  757. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type),
  758. class EnableIf =
  759. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  760. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  761. async_execute_statement(
  762. const statement& stmt,
  763. WritableFieldTuple&& params,
  764. results& result,
  765. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  766. )
  767. {
  768. return async_execute_statement(
  769. stmt,
  770. std::forward<WritableFieldTuple>(params),
  771. result,
  772. shared_diag(),
  773. std::forward<CompletionToken>(token)
  774. );
  775. }
  776. /// \copydoc async_execute_statement
  777. template <
  778. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  779. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  780. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type),
  781. class EnableIf =
  782. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  783. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  784. async_execute_statement(
  785. const statement& stmt,
  786. WritableFieldTuple&& params,
  787. results& result,
  788. diagnostics& diag,
  789. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  790. )
  791. {
  792. return async_execute(
  793. stmt.bind(std::forward<WritableFieldTuple>(params)),
  794. result,
  795. diag,
  796. std::forward<CompletionToken>(token)
  797. );
  798. }
  799. /**
  800. * \brief (Deprecated) Starts a statement execution as a multi-function operation.
  801. * \details
  802. * Writes the execute request and reads the initial server response and the column
  803. * metadata, but not the generated rows or subsequent resultsets, if any. After this operation completes,
  804. * `st` will have \ref execution_state::meta populated. Metadata will be populated according to
  805. * `this->meta_mode()`.
  806. * \n
  807. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  808. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  809. * Otherwise, the results are undefined.
  810. * \n
  811. * The statement actual parameters (`params`) are passed as a `std::tuple` of elements.
  812. * String parameters should be encoded using the connection's character set.
  813. *
  814. * \par Deprecation notice
  815. * This function is only provided for backwards-compatibility. For new code, please
  816. * use \ref start_execution or \ref async_start_execution instead.
  817. *
  818. * \par Preconditions
  819. * `stmt.valid() == true`
  820. */
  821. template <
  822. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  823. class EnableIf =
  824. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  825. void start_statement_execution(
  826. const statement& stmt,
  827. const WritableFieldTuple& params,
  828. execution_state& st,
  829. error_code& err,
  830. diagnostics& diag
  831. )
  832. {
  833. start_execution(stmt.bind(params), st, err, diag);
  834. }
  835. /// \copydoc start_statement_execution(const statement&,const WritableFieldTuple&,execution_state&,error_code&,diagnostics&)
  836. template <
  837. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  838. class EnableIf =
  839. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  840. void start_statement_execution(
  841. const statement& stmt,
  842. const WritableFieldTuple& params,
  843. execution_state& st
  844. )
  845. {
  846. start_execution(stmt.bind(params), st);
  847. }
  848. /**
  849. * \copydoc start_statement_execution(const statement&,const WritableFieldTuple&,execution_state&,error_code&,diagnostics&)
  850. * \details
  851. * \par Object lifetimes
  852. * If `CompletionToken` is deferred (like `use_awaitable`), and `params` contains any reference
  853. * type (like `string_view`), the caller must keep the values pointed by these references alive
  854. * until the operation is initiated. Value types will be copied/moved as required, so don't need
  855. * to be kept alive. It's not required to keep `stmt` alive, either.
  856. *
  857. * \par Handler signature
  858. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  859. */
  860. template <
  861. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  862. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  863. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type),
  864. class EnableIf =
  865. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  866. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  867. async_start_statement_execution(
  868. const statement& stmt,
  869. WritableFieldTuple&& params,
  870. execution_state& st,
  871. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  872. )
  873. {
  874. return async_start_statement_execution(
  875. stmt,
  876. std::forward<WritableFieldTuple>(params),
  877. st,
  878. shared_diag(),
  879. std::forward<CompletionToken>(token)
  880. );
  881. }
  882. /// \copydoc async_start_statement_execution(const statement&,WritableFieldTuple&&,execution_state&,CompletionToken&&)
  883. template <
  884. BHO_MYSQL_WRITABLE_FIELD_TUPLE WritableFieldTuple,
  885. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  886. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type),
  887. class EnableIf =
  888. typename std::enable_if<detail::is_writable_field_tuple<WritableFieldTuple>::value>::type>
  889. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  890. async_start_statement_execution(
  891. const statement& stmt,
  892. WritableFieldTuple&& params,
  893. execution_state& st,
  894. diagnostics& diag,
  895. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  896. )
  897. {
  898. return async_start_execution(
  899. stmt.bind(std::forward<WritableFieldTuple>(params)),
  900. st,
  901. diag,
  902. std::forward<CompletionToken>(token)
  903. );
  904. }
  905. /**
  906. * \brief (Deprecated) Starts a statement execution as a multi-function operation.
  907. * \details
  908. * Writes the execute request and reads the initial server response and the column
  909. * metadata, but not the generated rows or any subsequent resultsets, if any. After this operation
  910. * completes, `st` will have \ref execution_state::meta populated.
  911. * \n
  912. * If the operation generated any rows or more than one resultset, these <b>must</b> be read (by using
  913. * \ref read_some_rows and \ref read_resultset_head) before engaging in any further network operation.
  914. * Otherwise, the results are undefined.
  915. * \n
  916. * The statement actual parameters are passed as an iterator range.
  917. * String parameters should be encoded using the connection's character set.
  918. *
  919. * \par Deprecation notice
  920. * This function is only provided for backwards-compatibility. For new code, please
  921. * use \ref start_execution or \ref async_start_execution instead.
  922. *
  923. * \par Preconditions
  924. * `stmt.valid() == true`
  925. */
  926. template <BHO_MYSQL_FIELD_VIEW_FORWARD_ITERATOR FieldViewFwdIterator>
  927. void start_statement_execution(
  928. const statement& stmt,
  929. FieldViewFwdIterator params_first,
  930. FieldViewFwdIterator params_last,
  931. execution_state& st,
  932. error_code& ec,
  933. diagnostics& diag
  934. )
  935. {
  936. start_execution(stmt.bind(params_first, params_last), st, ec, diag);
  937. }
  938. /// \copydoc start_statement_execution(const statement&,FieldViewFwdIterator,FieldViewFwdIterator,execution_state&,error_code&,diagnostics&)
  939. template <BHO_MYSQL_FIELD_VIEW_FORWARD_ITERATOR FieldViewFwdIterator>
  940. void start_statement_execution(
  941. const statement& stmt,
  942. FieldViewFwdIterator params_first,
  943. FieldViewFwdIterator params_last,
  944. execution_state& st
  945. )
  946. {
  947. start_execution(stmt.bind(params_first, params_last), st);
  948. }
  949. /**
  950. * \copydoc start_statement_execution(const statement&,FieldViewFwdIterator,FieldViewFwdIterator,execution_state&,error_code&,diagnostics&)
  951. * \details
  952. * \par Object lifetimes
  953. * If `CompletionToken` is deferred (like `use_awaitable`), the caller must keep objects in
  954. * the iterator range alive until the operation is initiated.
  955. *
  956. * \par Handler signature
  957. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  958. */
  959. template <
  960. BHO_MYSQL_FIELD_VIEW_FORWARD_ITERATOR FieldViewFwdIterator,
  961. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  962. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  963. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  964. async_start_statement_execution(
  965. const statement& stmt,
  966. FieldViewFwdIterator params_first,
  967. FieldViewFwdIterator params_last,
  968. execution_state& st,
  969. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  970. )
  971. {
  972. return async_start_statement_execution(
  973. stmt,
  974. params_first,
  975. params_last,
  976. st,
  977. shared_diag(),
  978. std::forward<CompletionToken>(token)
  979. );
  980. }
  981. /// \copydoc async_start_statement_execution(const statement&,FieldViewFwdIterator,FieldViewFwdIterator,execution_state&,CompletionToken&&)
  982. template <
  983. BHO_MYSQL_FIELD_VIEW_FORWARD_ITERATOR FieldViewFwdIterator,
  984. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  985. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  986. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  987. async_start_statement_execution(
  988. const statement& stmt,
  989. FieldViewFwdIterator params_first,
  990. FieldViewFwdIterator params_last,
  991. execution_state& st,
  992. diagnostics& diag,
  993. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  994. )
  995. {
  996. return async_start_execution(
  997. stmt.bind(params_first, params_last),
  998. st,
  999. diag,
  1000. std::forward<CompletionToken>(token)
  1001. );
  1002. }
  1003. /**
  1004. * \brief Closes a statement, deallocating it from the server.
  1005. * \details
  1006. * After this operation succeeds, `stmt` must not be used again for execution.
  1007. * \n
  1008. * \par Preconditions
  1009. * `stmt.valid() == true`
  1010. */
  1011. void close_statement(const statement& stmt, error_code& err, diagnostics& diag)
  1012. {
  1013. detail::close_statement_interface(channel_.get(), stmt, err, diag);
  1014. }
  1015. /// \copydoc close_statement
  1016. void close_statement(const statement& stmt)
  1017. {
  1018. error_code err;
  1019. diagnostics diag;
  1020. close_statement(stmt, err, diag);
  1021. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1022. }
  1023. /**
  1024. * \copydoc close_statement
  1025. * \details
  1026. * \par Object lifetimes
  1027. * It is not required to keep `stmt` alive, as copies are made by the implementation as required.
  1028. *
  1029. * \par Handler signature
  1030. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  1031. */
  1032. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1033. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1034. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1035. async_close_statement(
  1036. const statement& stmt,
  1037. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1038. )
  1039. {
  1040. return async_close_statement(stmt, shared_diag(), std::forward<CompletionToken>(token));
  1041. }
  1042. /// \copydoc async_close_statement
  1043. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1044. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1045. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1046. async_close_statement(
  1047. const statement& stmt,
  1048. diagnostics& diag,
  1049. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1050. )
  1051. {
  1052. return detail::async_close_statement_interface(
  1053. channel_.get(),
  1054. stmt,
  1055. diag,
  1056. std::forward<CompletionToken>(token)
  1057. );
  1058. }
  1059. /**
  1060. * \brief Reads a batch of rows.
  1061. * \details
  1062. * The number of rows that will be read is unspecified. If the operation represented by `st`
  1063. * has still rows to read, at least one will be read. If there are no more rows, or
  1064. * `st.should_read_rows() == false`, returns an empty `rows_view`.
  1065. * \n
  1066. * The number of rows that will be read depends on the input buffer size. The bigger the buffer,
  1067. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  1068. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  1069. * grown bigger by other read operations, if required.
  1070. * \n
  1071. * The returned view points into memory owned by `*this`. It will be valid until
  1072. * `*this` performs the next network operation or is destroyed.
  1073. */
  1074. rows_view read_some_rows(execution_state& st, error_code& err, diagnostics& diag)
  1075. {
  1076. return detail::read_some_rows_dynamic_interface(channel_.get(), st, err, diag);
  1077. }
  1078. /// \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  1079. rows_view read_some_rows(execution_state& st)
  1080. {
  1081. error_code err;
  1082. diagnostics diag;
  1083. rows_view res = read_some_rows(st, err, diag);
  1084. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1085. return res;
  1086. }
  1087. /**
  1088. * \copydoc read_some_rows(execution_state&,error_code&,diagnostics&)
  1089. * \details
  1090. * \par Handler signature
  1091. * The handler signature for this operation is
  1092. * `void(bho::mysql::error_code, bho::mysql::rows_view)`.
  1093. */
  1094. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, ::bho::mysql::rows_view))
  1095. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1096. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, rows_view))
  1097. async_read_some_rows(
  1098. execution_state& st,
  1099. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1100. )
  1101. {
  1102. return async_read_some_rows(st, shared_diag(), std::forward<CompletionToken>(token));
  1103. }
  1104. /// \copydoc async_read_some_rows(execution_state&,CompletionToken&&)
  1105. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, ::bho::mysql::rows_view))
  1106. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1107. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, rows_view))
  1108. async_read_some_rows(
  1109. execution_state& st,
  1110. diagnostics& diag,
  1111. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1112. )
  1113. {
  1114. return detail::async_read_some_rows_dynamic_interface(
  1115. channel_.get(),
  1116. st,
  1117. diag,
  1118. std::forward<CompletionToken>(token)
  1119. );
  1120. }
  1121. #ifdef BHO_MYSQL_CXX14
  1122. /**
  1123. * \brief Reads a batch of rows.
  1124. * \details
  1125. * Reads a batch of rows of unspecified size into the storage given by `output`.
  1126. * At most `output.size()` rows will be read. If the operation represented by `st`
  1127. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  1128. * \n
  1129. * Returns the number of read rows.
  1130. * \n
  1131. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  1132. * zero.
  1133. * \n
  1134. * The number of rows that will be read depends on the input buffer size. The bigger the buffer,
  1135. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  1136. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  1137. * grown bigger by other read operations, if required.
  1138. * \n
  1139. * Rows read by this function are owning objects, and don't hold any reference to
  1140. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  1141. * \n
  1142. * `SpanStaticRow` must exactly be one of the types in the `StaticRow` parameter pack.
  1143. * The type must match the resultset that is currently being processed by `st`. For instance,
  1144. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanStaticRow`
  1145. * must exactly be `T2`. If this is not the case, a runtime error will be issued.
  1146. * \n
  1147. * This function can report schema mismatches.
  1148. */
  1149. template <class SpanStaticRow, class... StaticRow>
  1150. std::size_t read_some_rows(
  1151. static_execution_state<StaticRow...>& st,
  1152. span<SpanStaticRow> output,
  1153. error_code& err,
  1154. diagnostics& diag
  1155. )
  1156. {
  1157. return detail::read_some_rows_static_interface(channel_.get(), st, output, err, diag);
  1158. }
  1159. /**
  1160. * \brief Reads a batch of rows.
  1161. * \details
  1162. * Reads a batch of rows of unspecified size into the storage given by `output`.
  1163. * At most `output.size()` rows will be read. If the operation represented by `st`
  1164. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  1165. * \n
  1166. * Returns the number of read rows.
  1167. * \n
  1168. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  1169. * zero.
  1170. * \n
  1171. * The number of rows that will be read depends on the input buffer size. The bigger the buffer,
  1172. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  1173. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  1174. * grown bigger by other read operations, if required.
  1175. * \n
  1176. * Rows read by this function are owning objects, and don't hold any reference to
  1177. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  1178. * \n
  1179. * `SpanStaticRow` must exactly be one of the types in the `StaticRow` parameter pack.
  1180. * The type must match the resultset that is currently being processed by `st`. For instance,
  1181. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanStaticRow`
  1182. * must exactly be `T2`. If this is not the case, a runtime error will be issued.
  1183. * \n
  1184. * This function can report schema mismatches.
  1185. */
  1186. template <class SpanStaticRow, class... StaticRow>
  1187. std::size_t read_some_rows(static_execution_state<StaticRow...>& st, span<SpanStaticRow> output)
  1188. {
  1189. error_code err;
  1190. diagnostics diag;
  1191. std::size_t res = read_some_rows(st, output, err, diag);
  1192. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1193. return res;
  1194. }
  1195. /**
  1196. * \brief Reads a batch of rows.
  1197. * \details
  1198. * Reads a batch of rows of unspecified size into the storage given by `output`.
  1199. * At most `output.size()` rows will be read. If the operation represented by `st`
  1200. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  1201. * \n
  1202. * Returns the number of read rows.
  1203. * \n
  1204. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  1205. * zero.
  1206. * \n
  1207. * The number of rows that will be read depends on the input buffer size. The bigger the buffer,
  1208. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  1209. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  1210. * grown bigger by other read operations, if required.
  1211. * \n
  1212. * Rows read by this function are owning objects, and don't hold any reference to
  1213. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  1214. * \n
  1215. * `SpanStaticRow` must exactly be one of the types in the `StaticRow` parameter pack.
  1216. * The type must match the resultset that is currently being processed by `st`. For instance,
  1217. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanStaticRow`
  1218. * must exactly be `T2`. If this is not the case, a runtime error will be issued.
  1219. * \n
  1220. * This function can report schema mismatches.
  1221. *
  1222. * \par Handler signature
  1223. * The handler signature for this operation is
  1224. * `void(bho::mysql::error_code, std::size_t)`.
  1225. *
  1226. * \par Object lifetimes
  1227. * The storage that `output` references must be kept alive until the operation completes.
  1228. */
  1229. template <
  1230. class SpanStaticRow,
  1231. class... StaticRow,
  1232. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, std::size_t))
  1233. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1234. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, std::size_t))
  1235. async_read_some_rows(
  1236. static_execution_state<StaticRow...>& st,
  1237. span<SpanStaticRow> output,
  1238. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1239. )
  1240. {
  1241. return async_read_some_rows(st, output, shared_diag(), std::forward<CompletionToken>(token));
  1242. }
  1243. /**
  1244. * \brief Reads a batch of rows.
  1245. * \details
  1246. * Reads a batch of rows of unspecified size into the storage given by `output`.
  1247. * At most `output.size()` rows will be read. If the operation represented by `st`
  1248. * has still rows to read, and `output.size() > 0`, at least one row will be read.
  1249. * \n
  1250. * Returns the number of read rows.
  1251. * \n
  1252. * If there are no more rows, or `st.should_read_rows() == false`, this function is a no-op and returns
  1253. * zero.
  1254. * \n
  1255. * The number of rows that will be read depends on the input buffer size. The bigger the buffer,
  1256. * the greater the batch size (up to a maximum). You can set the initial buffer size in `connection`'s
  1257. * constructor, using \ref buffer_params::initial_read_size. The buffer may be
  1258. * grown bigger by other read operations, if required.
  1259. * \n
  1260. * Rows read by this function are owning objects, and don't hold any reference to
  1261. * the connection's internal buffers (contrary what happens with the dynamic interface's counterpart).
  1262. * \n
  1263. * `SpanStaticRow` must exactly be one of the types in the `StaticRow` parameter pack.
  1264. * The type must match the resultset that is currently being processed by `st`. For instance,
  1265. * given `static_execution_state<T1, T2>`, when reading rows for the second resultset, `SpanStaticRow`
  1266. * must exactly be `T2`. If this is not the case, a runtime error will be issued.
  1267. * \n
  1268. * This function can report schema mismatches.
  1269. *
  1270. * \par Handler signature
  1271. * The handler signature for this operation is
  1272. * `void(bho::mysql::error_code, std::size_t)`.
  1273. *
  1274. * \par Object lifetimes
  1275. * The storage that `output` references must be kept alive until the operation completes.
  1276. */
  1277. template <
  1278. class SpanStaticRow,
  1279. class... StaticRow,
  1280. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code, std::size_t))
  1281. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1282. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code, std::size_t))
  1283. async_read_some_rows(
  1284. static_execution_state<StaticRow...>& st,
  1285. span<SpanStaticRow> output,
  1286. diagnostics& diag,
  1287. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1288. )
  1289. {
  1290. return detail::async_read_some_rows_static_interface(
  1291. channel_.get(),
  1292. st,
  1293. output,
  1294. diag,
  1295. std::forward<CompletionToken>(token)
  1296. );
  1297. }
  1298. #endif
  1299. /**
  1300. * \brief Reads metadata for subsequent resultsets in a multi-resultset operation.
  1301. * \details
  1302. * If `st.should_read_head() == true`, this function will read the next resultset's
  1303. * initial response message and metadata, if any. If the resultset indicates a failure
  1304. * (e.g. the query associated to this resultset contained an error), this function will fail
  1305. * with that error.
  1306. * \n
  1307. * If `st.should_read_head() == false`, this function is a no-op.
  1308. * \n
  1309. * `st` may be either an \ref execution_state or \ref static_execution_state object.
  1310. * \n
  1311. * This function is only relevant when using multi-function operations with statements
  1312. * that return more than one resultset.
  1313. * \n
  1314. * When using the static interface, this function will detect schema mismatches for the resultset
  1315. * currently being read. Further errors may be detected by subsequent invocations of this function
  1316. * and by \ref read_some_rows.
  1317. */
  1318. template <BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  1319. void read_resultset_head(ExecutionStateType& st, error_code& err, diagnostics& diag)
  1320. {
  1321. return detail::read_resultset_head_interface(channel_.get(), st, err, diag);
  1322. }
  1323. /// \copydoc read_resultset_head
  1324. template <BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType>
  1325. void read_resultset_head(ExecutionStateType& st)
  1326. {
  1327. error_code err;
  1328. diagnostics diag;
  1329. read_resultset_head(st, err, diag);
  1330. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1331. }
  1332. /**
  1333. * \copydoc read_resultset_head
  1334. * \par Handler signature
  1335. * The handler signature for this operation is
  1336. * `void(bho::mysql::error_code)`.
  1337. */
  1338. template <
  1339. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  1340. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1341. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1342. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1343. async_read_resultset_head(
  1344. ExecutionStateType& st,
  1345. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1346. )
  1347. {
  1348. return async_read_resultset_head(st, shared_diag(), std::forward<CompletionToken>(token));
  1349. }
  1350. /// \copydoc async_read_resultset_head
  1351. template <
  1352. BHO_MYSQL_EXECUTION_STATE_TYPE ExecutionStateType,
  1353. ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1354. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1355. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1356. async_read_resultset_head(
  1357. ExecutionStateType& st,
  1358. diagnostics& diag,
  1359. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1360. )
  1361. {
  1362. return detail::async_read_resultset_head_interface(
  1363. channel_.get(),
  1364. st,
  1365. diag,
  1366. std::forward<CompletionToken>(token)
  1367. );
  1368. }
  1369. /**
  1370. * \brief Checks whether the server is alive.
  1371. * \details
  1372. * If the server is alive, this function will complete without error.
  1373. * If it's not, it will fail with the relevant network or protocol error.
  1374. * \n
  1375. * Note that ping requests are treated as any other type of request at the protocol
  1376. * level, and won't be prioritized anyhow by the server. If the server is stuck
  1377. * in a long-running query, the ping request won't be answered until the query is
  1378. * finished.
  1379. */
  1380. void ping(error_code& err, diagnostics& diag) { detail::ping_interface(channel_.get(), err, diag); }
  1381. /// \copydoc ping
  1382. void ping()
  1383. {
  1384. error_code err;
  1385. diagnostics diag;
  1386. ping(err, diag);
  1387. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1388. }
  1389. /**
  1390. * \copydoc ping
  1391. * \details
  1392. * \n
  1393. * \par Handler signature
  1394. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  1395. */
  1396. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1397. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1398. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1399. async_ping(CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1400. {
  1401. return async_ping(shared_diag(), std::forward<CompletionToken>(token));
  1402. }
  1403. /// \copydoc async_ping
  1404. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1405. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1406. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1407. async_ping(diagnostics& diag, CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1408. {
  1409. return detail::async_ping_interface(channel_.get(), diag, std::forward<CompletionToken>(token));
  1410. }
  1411. /**
  1412. * \brief Resets server-side session state, like variables and prepared statements.
  1413. * \details
  1414. * Resets all server-side state for the current session:
  1415. * \n
  1416. * \li Rolls back any active transactions and resets autocommit mode.
  1417. * \li Releases all table locks.
  1418. * \li Drops all temporary tables.
  1419. * \li Resets all session system variables to their default values (including the ones set by `SET
  1420. * NAMES`) and clears all user-defined variables. \li Closes all prepared statements.
  1421. * \n
  1422. * A full reference on the affected session state can be found
  1423. * <a href="https://dev.mysql.com/doc/c-api/8.0/en/mysql-reset-connection.html">here</a>.
  1424. * \n
  1425. * This function will not reset the current physical connection and won't cause re-authentication.
  1426. * It is faster than closing and re-opening a connection.
  1427. * \n
  1428. * The connection must be connected and authenticated before calling this function.
  1429. * This function involves communication with the server, and thus may fail.
  1430. */
  1431. void reset_connection(error_code& err, diagnostics& diag)
  1432. {
  1433. detail::reset_connection_interface(channel_.get(), err, diag);
  1434. }
  1435. /// \copydoc reset_connection
  1436. void reset_connection()
  1437. {
  1438. error_code err;
  1439. diagnostics diag;
  1440. reset_connection(err, diag);
  1441. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1442. }
  1443. /**
  1444. * \copydoc reset_connection
  1445. * \details
  1446. * \n
  1447. * \par Handler signature
  1448. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  1449. */
  1450. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1451. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1452. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1453. async_reset_connection(CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1454. {
  1455. return async_reset_connection(shared_diag(), std::forward<CompletionToken>(token));
  1456. }
  1457. /// \copydoc async_reset_connection
  1458. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1459. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1460. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1461. async_reset_connection(
  1462. diagnostics& diag,
  1463. CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
  1464. )
  1465. {
  1466. return detail::async_reset_connection_interface(
  1467. channel_.get(),
  1468. diag,
  1469. std::forward<CompletionToken>(token)
  1470. );
  1471. }
  1472. /**
  1473. * \brief Closes the connection to the server.
  1474. * \details
  1475. * This function is only available if `Stream` satisfies the `SocketStream` concept.
  1476. * \n
  1477. * Sends a quit request, performs the TLS shutdown (if required)
  1478. * and closes the underlying stream. Prefer this function to \ref connection::quit.
  1479. */
  1480. void close(error_code& err, diagnostics& diag)
  1481. {
  1482. static_assert(
  1483. detail::is_socket_stream<Stream>::value,
  1484. "close can only be used if Stream satisfies the SocketStream concept"
  1485. );
  1486. detail::close_connection_interface(channel_.get(), err, diag);
  1487. }
  1488. /// \copydoc close
  1489. void close()
  1490. {
  1491. static_assert(
  1492. detail::is_socket_stream<Stream>::value,
  1493. "close can only be used if Stream satisfies the SocketStream concept"
  1494. );
  1495. error_code err;
  1496. diagnostics diag;
  1497. close(err, diag);
  1498. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1499. }
  1500. /**
  1501. * \copydoc close
  1502. * \details
  1503. * \par Handler signature
  1504. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  1505. */
  1506. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1507. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1508. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1509. async_close(CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1510. {
  1511. static_assert(
  1512. detail::is_socket_stream<Stream>::value,
  1513. "async_close can only be used if Stream satisfies the SocketStream concept"
  1514. );
  1515. return async_close(shared_diag(), std::forward<CompletionToken>(token));
  1516. }
  1517. /// \copydoc async_close
  1518. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1519. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1520. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1521. async_close(diagnostics& diag, CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1522. {
  1523. static_assert(
  1524. detail::is_socket_stream<Stream>::value,
  1525. "async_close can only be used if Stream satisfies the SocketStream concept"
  1526. );
  1527. return detail::async_close_connection_interface(
  1528. channel_.get(),
  1529. diag,
  1530. std::forward<CompletionToken>(token)
  1531. );
  1532. }
  1533. /**
  1534. * \brief Notifies the MySQL server that the client wants to end the session and shutdowns SSL.
  1535. * \details Sends a quit request to the MySQL server. If the connection is using SSL,
  1536. * this function will also perform the SSL shutdown. You should
  1537. * close the underlying physical connection after calling this function.
  1538. * \n
  1539. * If the `Stream` template parameter fulfills the `SocketConnection`
  1540. * requirements, use \ref connection::close instead of this function,
  1541. * as it also takes care of closing the underlying stream.
  1542. */
  1543. void quit(error_code& err, diagnostics& diag)
  1544. {
  1545. detail::quit_connection_interface(channel_.get(), err, diag);
  1546. }
  1547. /// \copydoc quit
  1548. void quit()
  1549. {
  1550. error_code err;
  1551. diagnostics diag;
  1552. quit(err, diag);
  1553. detail::throw_on_error_loc(err, diag, BHO_CURRENT_LOCATION);
  1554. }
  1555. /**
  1556. * \copydoc quit
  1557. * \details
  1558. * \par Handler signature
  1559. * The handler signature for this operation is `void(bho::mysql::error_code)`.
  1560. */
  1561. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1562. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1563. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1564. async_quit(CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1565. {
  1566. return async_quit(shared_diag(), std::forward<CompletionToken>(token));
  1567. }
  1568. /// \copydoc async_quit
  1569. template <ASIO_COMPLETION_TOKEN_FOR(void(::bho::mysql::error_code))
  1570. CompletionToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1571. ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(error_code))
  1572. async_quit(diagnostics& diag, CompletionToken&& token ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1573. {
  1574. return detail::async_quit_connection_interface(
  1575. channel_.get(),
  1576. diag,
  1577. std::forward<CompletionToken>(token)
  1578. );
  1579. }
  1580. /**
  1581. * \brief Rebinds the connection type to another executor.
  1582. * \details
  1583. * The `Stream` type must either provide a `rebind_executor`
  1584. * member with the same semantics, or be an instantiation of `asio::ssl::stream` with
  1585. * a `Stream` type providing a `rebind_executor` member.
  1586. */
  1587. template <typename Executor1>
  1588. struct rebind_executor
  1589. {
  1590. /// The connection type when rebound to the specified executor.
  1591. using other = connection<typename detail::rebind_executor<Stream, Executor1>::type>;
  1592. };
  1593. };
  1594. /// The default TCP port for the MySQL protocol.
  1595. constexpr unsigned short default_port = 3306;
  1596. /// The default TCP port for the MySQL protocol, as a string. Useful for hostname resolution.
  1597. constexpr const char* default_port_string = "3306";
  1598. } // namespace mysql
  1599. } // namespace bho
  1600. #endif