format_sql.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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_FORMAT_SQL_HPP
  8. #define BOOST_MYSQL_FORMAT_SQL_HPP
  9. #include <boost/mysql/character_set.hpp>
  10. #include <boost/mysql/constant_string_view.hpp>
  11. #include <boost/mysql/error_code.hpp>
  12. #include <boost/mysql/string_view.hpp>
  13. #include <boost/mysql/detail/access.hpp>
  14. #include <boost/mysql/detail/config.hpp>
  15. #include <boost/mysql/detail/format_sql.hpp>
  16. #include <boost/mysql/detail/output_string.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/core/span.hpp>
  19. #include <boost/system/result.hpp>
  20. #include <initializer_list>
  21. #include <iterator>
  22. #include <string>
  23. #include <type_traits>
  24. #include <utility>
  25. #ifdef BOOST_MYSQL_HAS_CONCEPTS
  26. #include <concepts>
  27. #endif
  28. namespace boost {
  29. namespace mysql {
  30. /**
  31. * \brief (EXPERIMENTAL) An extension point to customize SQL formatting.
  32. * \details
  33. * This type can be specialized for custom types to make them formattable.
  34. * This makes them satisfy the `Formattable` concept, and thus usable in
  35. * \ref format_sql and similar functions.
  36. * \n
  37. * A `formatter` specialization for a type `T` should have the following form:
  38. * ```
  39. * template <>
  40. * struct formatter<T>
  41. * {
  42. * const char* parse(const char* begin, const char* end); // parse format specs
  43. * void format(const T& value, format_context_base& ctx) const; // perform the actual formatting
  44. * };
  45. * ```
  46. * \n
  47. * When a value with a custom formatter is formatted (using \ref format_sql or a similar
  48. * function), the library performs the following actions: \n
  49. * - An instance of `formatter<T>` is default-constructed, where `T` is the type of the
  50. * value being formatted after removing const and references.
  51. * - The `parse` function is invoked on the constructed instance,
  52. * with `[begin, end)` pointing to the format specifier
  53. * that the current replacement field has. If `parse` finds specifiers it understands, it should
  54. * remember them, usually setting some flag in the `formatter` instance.
  55. * `parse` must return an iterator to the first
  56. * unparsed character in the range (or the `end` iterator, if everything was parsed).
  57. * Some examples of what would get passed to `parse`:
  58. * - In `"SELECT {}"`, the range would be empty.
  59. * - In `"SELECT {:abc}"`, the range would be `"abc"`.
  60. * - In `"SELECT {0:i}"`, the range would be `"i"`.
  61. * - If `parse` didn't manage to parse all the passed specifiers (i.e. if it returned an iterator
  62. * different to the passed's end), a \ref client_errc::format_string_invalid_specifier
  63. * is emitted and the format operation finishes.
  64. * - Otherwise, `format` is invoked on the formatter instance, passing the value to be formatted
  65. * and the \ref format_context_base where format operation is running.
  66. * This function should perform the actual formatting, usually calling
  67. * \ref format_sql_to on the passed context.
  68. *
  69. * \n
  70. * Don't specialize `formatter` for built-in types, like `int`, `std::string` or
  71. * optionals (formally, any type satisfying `WritableField`), as the specializations will be ignored.
  72. */
  73. template <class T>
  74. struct formatter
  75. #ifndef BOOST_MYSQL_DOXYGEN
  76. : detail::formatter_is_unspecialized
  77. {
  78. }
  79. #endif
  80. ;
  81. /**
  82. * \brief (EXPERIMENTAL) A type-erased reference to a `Formattable` value.
  83. * \details
  84. * This type can hold references to any value that satisfies the `Formattable`
  85. * concept. The `formattable_ref` type itself satisfies `Formattable`,
  86. * and can thus be used as an argument to format functions.
  87. *
  88. * \par Object lifetimes
  89. * This is a non-owning type. It should be only used as a function argument,
  90. * to avoid lifetime issues.
  91. */
  92. class formattable_ref
  93. {
  94. detail::formattable_ref_impl impl_;
  95. #ifndef BOOST_MYSQL_DOXYGEN
  96. friend struct detail::access;
  97. #endif
  98. public:
  99. /**
  100. * \brief Constructor.
  101. * \details
  102. * Constructs a type-erased formattable reference from a concrete
  103. * `Formattable` type.
  104. * \n
  105. * This constructor participates in overload resolution only if
  106. * the passed value meets the `Formattable` concept and
  107. * is not a `formattable_ref` or a reference to one.
  108. *
  109. * \par Exception safety
  110. * No-throw guarantee.
  111. *
  112. * \par Object lifetimes
  113. * value is potentially stored as a view, although some cheap-to-copy
  114. * types may be stored as values.
  115. */
  116. template <
  117. BOOST_MYSQL_FORMATTABLE Formattable
  118. #ifndef BOOST_MYSQL_DOXYGEN
  119. ,
  120. class = typename std::enable_if<
  121. detail::is_formattable_type<Formattable>() &&
  122. !detail::is_formattable_ref<Formattable>::value>::type
  123. #endif
  124. >
  125. formattable_ref(Formattable&& value) noexcept
  126. : impl_(detail::make_formattable_ref(std::forward<Formattable>(value)))
  127. {
  128. }
  129. };
  130. /**
  131. * \brief (EXPERIMENTAL) A named format argument, to be used in initializer lists.
  132. * \details
  133. * Represents a name, value pair to be passed to a formatting function.
  134. * This type should only be used in initializer lists, as a function argument.
  135. *
  136. * \par Object lifetimes
  137. * This is a non-owning type. Both the argument name and value are stored
  138. * as views.
  139. */
  140. class format_arg
  141. {
  142. #ifndef BOOST_MYSQL_DOXYGEN
  143. struct
  144. {
  145. string_view name;
  146. detail::formattable_ref_impl value;
  147. } impl_;
  148. friend struct detail::access;
  149. #endif
  150. public:
  151. /**
  152. * \brief Constructor.
  153. * \details
  154. * Constructs an argument from a name and a value.
  155. *
  156. * \par Exception safety
  157. * No-throw guarantee.
  158. *
  159. * \par Object lifetimes
  160. * Both `name` and `value` are stored as views.
  161. */
  162. format_arg(string_view name, formattable_ref value) noexcept
  163. : impl_{name, detail::access::get_impl(value)}
  164. {
  165. }
  166. };
  167. /**
  168. * \brief (EXPERIMENTAL) Base class for concrete format contexts.
  169. * \details
  170. * Conceptually, a format context contains: \n
  171. * \li The result string. Output operations append characters to this output string.
  172. * `format_context_base` is agnostic to the output string type.
  173. * \li \ref format_options required to format values.
  174. * \li An error state (\ref error_state) that is set by output operations when they fail.
  175. * The error state is propagated to \ref basic_format_context::get.
  176. * \n
  177. * References to this class are useful when you need to manipulate
  178. * a format context without knowing the type of the actual context that will be used,
  179. * like when specializing \ref formatter.
  180. * \n
  181. * This class can't be
  182. * instantiated directly - use \ref basic_format_context, instead.
  183. * Do not subclass it, either.
  184. */
  185. class format_context_base
  186. {
  187. #ifndef BOOST_MYSQL_DOXYGEN
  188. struct
  189. {
  190. detail::output_string_ref output;
  191. format_options opts;
  192. error_code ec;
  193. } impl_;
  194. friend struct detail::access;
  195. friend class detail::format_state;
  196. #endif
  197. BOOST_MYSQL_DECL void format_arg(detail::formattable_ref_impl arg, string_view format_spec);
  198. protected:
  199. format_context_base(detail::output_string_ref out, format_options opts, error_code ec = {}) noexcept
  200. : impl_{out, opts, ec}
  201. {
  202. }
  203. format_context_base(detail::output_string_ref out, const format_context_base& rhs) noexcept
  204. : impl_{out, rhs.impl_.opts, rhs.impl_.ec}
  205. {
  206. }
  207. void assign(const format_context_base& rhs) noexcept
  208. {
  209. // output never changes, it always points to the derived object's storage
  210. impl_.opts = rhs.impl_.opts;
  211. impl_.ec = rhs.impl_.ec;
  212. }
  213. public:
  214. /**
  215. * \brief Adds raw SQL to the output string (low level).
  216. * \details
  217. * Adds raw, unescaped SQL to the output string. Doesn't alter the error state.
  218. * \n
  219. * By default, the passed SQL should be available at compile-time.
  220. * Use \ref runtime if you need to use runtime values.
  221. * \n
  222. * This is a low level function. In general, prefer \ref format_sql_to, instead.
  223. *
  224. * \par Exception safety
  225. * Basic guarantee. Memory allocations may throw.
  226. *
  227. * \par Object lifetimes
  228. * The passed string is copied as required and doesn't need to be kept alive.
  229. */
  230. format_context_base& append_raw(constant_string_view sql)
  231. {
  232. impl_.output.append(sql.get());
  233. return *this;
  234. }
  235. /**
  236. * \brief Formats a value and adds it to the output string (low level).
  237. * \details
  238. * value is formatted according to its type, applying the passed format specifiers.
  239. * If formatting generates an error (for instance, a string with invalid encoding is passed),
  240. * the error state may be set.
  241. * \n
  242. * This is a low level function. In general, prefer \ref format_sql_to, instead.
  243. *
  244. * \par Exception safety
  245. * Basic guarantee. Memory allocations may throw.
  246. *
  247. * \par Errors
  248. * The error state may be updated with the following errors: \n
  249. * \li \ref client_errc::invalid_encoding if a string with byte sequences that can't be decoded
  250. * with the current character set is passed.
  251. * \li \ref client_errc::unformattable_value if a NaN or infinity `float` or `double` is passed.
  252. * \li \ref client_errc::format_string_invalid_specifier if `format_specifiers` includes
  253. * specifiers not supported by the type being formatted.
  254. * \li Any other error code that user-supplied formatter specializations may add using \ref add_error.
  255. */
  256. format_context_base& append_value(
  257. formattable_ref value,
  258. constant_string_view format_specifiers = string_view()
  259. )
  260. {
  261. format_arg(detail::access::get_impl(value), format_specifiers.get());
  262. return *this;
  263. }
  264. /**
  265. * \brief Adds an error to the current error state.
  266. * \details
  267. * This function can be used by custom formatters to report that they
  268. * received a value that can't be formatted. For instance, it's used by
  269. * the built-in string formatter when a string with an invalid encoding is supplied.
  270. * \n
  271. * If the error state is not set before calling this function, the error
  272. * state is updated to `ec`. Otherwise, the error is ignored.
  273. * This implies that once the error state is set, it can't be reset.
  274. * \n
  275. * \par Exception safety
  276. * No-throw guarantee.
  277. */
  278. void add_error(error_code ec) noexcept
  279. {
  280. if (!impl_.ec)
  281. impl_.ec = ec;
  282. }
  283. /**
  284. * \brief Retrieves the current error state.
  285. *
  286. * \par Exception safety
  287. * No-throw guarantee.
  288. */
  289. error_code error_state() const noexcept { return impl_.ec; }
  290. /**
  291. * \brief Retrieves the format options.
  292. *
  293. * \par Exception safety
  294. * No-throw guarantee.
  295. */
  296. format_options format_opts() const noexcept { return impl_.opts; }
  297. };
  298. /**
  299. * \brief (EXPERIMENTAL) Format context for incremental SQL formatting.
  300. * \details
  301. * The primary interface for incremental SQL formatting. Contrary to \ref format_context_base,
  302. * this type is aware of the output string's actual type. `basic_format_context` owns
  303. * an instance of `OutputString`. Format operations will append characters to such string.
  304. * \n
  305. * Objects of this type are single-use: once the result has been retrieved using \ref get,
  306. * they cannot be re-used. This is a move-only type.
  307. */
  308. template <BOOST_MYSQL_OUTPUT_STRING OutputString>
  309. class basic_format_context : public format_context_base
  310. {
  311. OutputString output_{};
  312. detail::output_string_ref ref() noexcept { return detail::output_string_ref::create(output_); }
  313. public:
  314. /**
  315. * \brief Constructor.
  316. * \details
  317. * Uses a default-constructed `OutputString` as output string, and an empty
  318. * error code as error state. This constructor can only be called if `OutputString`
  319. * is default-constructible.
  320. *
  321. * \par Exception safety
  322. * Strong guarantee: exceptions thrown by default-constructing `OutputString` are propagated.
  323. */
  324. explicit basic_format_context(format_options opts
  325. ) noexcept(std::is_nothrow_default_constructible<OutputString>::value)
  326. : format_context_base(ref(), opts)
  327. {
  328. }
  329. /**
  330. * \brief Constructor.
  331. * \details
  332. * Move constructs an `OutputString` using `storage`. After construction,
  333. * the output string is cleared. Uses an empty
  334. * error code as error state. This constructor allows re-using existing
  335. * memory for the output string.
  336. * \n
  337. *
  338. * \par Exception safety
  339. * Basic guarantee: exceptions thrown by move-constructing `OutputString` are propagated.
  340. */
  341. basic_format_context(format_options opts, OutputString&& storage) noexcept(
  342. std::is_nothrow_move_constructible<OutputString>::value
  343. )
  344. : format_context_base(ref(), opts), output_(std::move(storage))
  345. {
  346. output_.clear();
  347. }
  348. #ifndef BOOST_MYSQL_DOXYGEN
  349. basic_format_context(const basic_format_context&) = delete;
  350. basic_format_context& operator=(const basic_format_context&) = delete;
  351. #endif
  352. /**
  353. * \brief Move constructor.
  354. * \details
  355. * Move constructs an `OutputString` using `rhs`'s output string.
  356. * `*this` will have the same format options and error state than `rhs`.
  357. * `rhs` is left in a valid but unspecified state.
  358. *
  359. * \par Exception safety
  360. * Basic guarantee: exceptions thrown by move-constructing `OutputString` are propagated.
  361. */
  362. basic_format_context(basic_format_context&& rhs
  363. ) noexcept(std::is_nothrow_move_constructible<OutputString>::value)
  364. : format_context_base(ref(), rhs), output_(std::move(rhs.output_))
  365. {
  366. }
  367. /**
  368. * \brief Move assignment.
  369. * \details
  370. * Move assigns `rhs`'s output string to `*this` output string.
  371. * `*this` will have the same format options and error state than `rhs`.
  372. * `rhs` is left in a valid but unspecified state.
  373. *
  374. * \par Exception safety
  375. * Basic guarantee: exceptions thrown by move-constructing `OutputString` are propagated.
  376. */
  377. basic_format_context& operator=(basic_format_context&& rhs
  378. ) noexcept(std::is_nothrow_move_assignable<OutputString>::value)
  379. {
  380. output_ = std::move(rhs.output_);
  381. assign(rhs);
  382. return *this;
  383. }
  384. /**
  385. * \brief Retrieves the result of the formatting operation.
  386. * \details
  387. * After running the relevant formatting operations (using \ref append_raw,
  388. * \ref append_value or \ref format_sql_to), call this function to retrieve the
  389. * overall result of the operation.
  390. * \n
  391. * If \ref error_state is a non-empty error code, returns it as an error.
  392. * Otherwise, returns the output string, move-constructing it into the `system::result` object.
  393. * \n
  394. * This function is move-only: once called, `*this` is left in a valid but unspecified state.
  395. *
  396. * \par Exception safety
  397. * Basic guarantee: exceptions thrown by move-constructing `OutputString` are propagated.
  398. */
  399. system::result<OutputString> get() && noexcept(std::is_nothrow_move_constructible<OutputString>::value)
  400. {
  401. auto ec = error_state();
  402. if (ec)
  403. return ec;
  404. return std::move(output_);
  405. }
  406. };
  407. /**
  408. * \brief (EXPERIMENTAL) Format context for incremental SQL formatting.
  409. * \details
  410. * Convenience type alias for `basic_format_context`'s most common case.
  411. */
  412. using format_context = basic_format_context<std::string>;
  413. /**
  414. * \brief (EXPERIMENTAL) The return type of \ref sequence.
  415. * \details
  416. * Contains a range view (as an interator/sentinel pair), a formatter function, and a glue string.
  417. * This type satisfies the `Formattable` concept. See \ref sequence for a detailed
  418. * description of what formatting this class does.
  419. * \n
  420. * Don't instantiate this class directly - use \ref sequence, instead.
  421. * The exact definition may vary between releases.
  422. */
  423. template <class It, class Sentinel, class FormatFn>
  424. struct format_sequence_view
  425. #ifndef BOOST_MYSQL_DOXYGEN
  426. {
  427. It it;
  428. Sentinel sentinel;
  429. FormatFn fn;
  430. constant_string_view glue;
  431. }
  432. #endif
  433. ;
  434. /**
  435. * \brief Makes a range formattable by supplying a per-element formatter function.
  436. * \details
  437. * Objects returned by this function satisfy `Formattable`.
  438. * When formatted, the formatter function `fn` is invoked for each element
  439. * in the range. The glue string `glue` is output raw (as per \ref format_context_base::append_raw)
  440. * between consecutive invocations of the formatter function, generating an effect
  441. * similar to `std::ranges::views::join`.
  442. * \n
  443. * \par Type requirements
  444. * - FormatFn should be move constructible.
  445. * - Expressions `std::begin(range)` and `std::end(range)` should return an input iterator/sentinel
  446. * pair that can be compared for (in)equality.
  447. * - The expression `static_cast<const FormatFn&>(fn)(*std::begin(range), ctx)`
  448. * should be well formed, with `ctx` begin a `format_context_base&`.
  449. *
  450. * \par Object lifetimes
  451. * The input range is stored in \ref format_sequence_view as a view, using an iterator/sentinel pair,
  452. * and is never copied. The caller must make sure that the elements pointed by the obtained
  453. * iterator/sentinel are kept alive until the view is formatted.
  454. *
  455. * \par Exception safety
  456. * Strong-throw guarantee. Throws any exception that `std::begin`, `std::end`
  457. * or move-constructing `FormatFn` may throw.
  458. */
  459. template <class Range, class FormatFn>
  460. #if defined(BOOST_MYSQL_HAS_CONCEPTS)
  461. requires std::move_constructible<FormatFn> && detail::format_fn_for_range<FormatFn, Range>
  462. #endif
  463. auto sequence(Range&& range, FormatFn fn, constant_string_view glue = ", ")
  464. -> format_sequence_view<decltype(std::begin(range)), decltype(std::end(range)), FormatFn>
  465. {
  466. return {std::begin(range), std::end(range), std::move(fn), glue};
  467. }
  468. template <class It, class Sentinel, class FormatFn>
  469. struct formatter<format_sequence_view<It, Sentinel, FormatFn>>
  470. {
  471. const char* parse(const char* begin, const char*) { return begin; }
  472. void format(const format_sequence_view<It, Sentinel, FormatFn>& value, format_context_base& ctx) const
  473. {
  474. bool is_first = true;
  475. for (auto it = value.it; it != value.sentinel; ++it)
  476. {
  477. if (!is_first)
  478. ctx.append_raw(value.glue);
  479. is_first = false;
  480. value.fn(*it, ctx);
  481. }
  482. }
  483. };
  484. /**
  485. * \brief (EXPERIMENTAL) Composes a SQL query client-side appending it to a format context.
  486. * \details
  487. * Parses `format_str` as a format string, substituting replacement fields (like `{}`, `{1}` or `{name}`)
  488. * by formatted arguments, extracted from `args`.
  489. * \n
  490. * Formatting is performed as if \ref format_context_base::append_raw and
  491. * \ref format_context_base::append_value were called on `ctx`, effectively appending
  492. * characters to its output string.
  493. * \n
  494. * Compared to \ref format_sql, this function is more flexible, allowing the following use cases: \n
  495. * \li Appending characters to an existing context. Can be used to concatenate the output of successive
  496. * format operations efficiently.
  497. * \li Using string types different to `std::string` (works with any \ref basic_format_context).
  498. * \li Avoiding exceptions (see \ref basic_format_context::get).
  499. *
  500. *
  501. * \par Exception safety
  502. * Basic guarantee. Memory allocations may throw.
  503. *
  504. * \par Errors
  505. * \li \ref client_errc::invalid_encoding if `args` contains a string with byte sequences
  506. * that can't be decoded with the current character set.
  507. * \li \ref client_errc::unformattable_value if `args` contains a floating-point value
  508. * that is NaN or infinity.
  509. * \li \ref client_errc::format_string_invalid_specifier if a replacement field includes
  510. * a specifier not supported by the type being formatted.
  511. * \li Any other error generated by user-defined \ref formatter specializations.
  512. * \li \ref client_errc::format_string_invalid_syntax if `format_str` can't be parsed as
  513. * a format string.
  514. * \li \ref client_errc::format_string_invalid_encoding if `format_str` contains byte byte sequences
  515. * that can't be decoded with the current character set.
  516. * \li \ref client_errc::format_string_manual_auto_mix if `format_str` contains a mix of automatic
  517. * (`{}`) and manual indexed (`{1}`) replacement fields.
  518. * \li \ref client_errc::format_arg_not_found if an argument referenced by `format_str` isn't present
  519. * in `args` (there aren't enough arguments or a named argument is not found).
  520. */
  521. template <BOOST_MYSQL_FORMATTABLE... Formattable>
  522. void format_sql_to(format_context_base& ctx, constant_string_view format_str, Formattable&&... args);
  523. /**
  524. * \copydoc format_sql_to
  525. * \details
  526. * \n
  527. * This overload allows using named arguments.
  528. */
  529. BOOST_MYSQL_DECL
  530. void format_sql_to(
  531. format_context_base& ctx,
  532. constant_string_view format_str,
  533. std::initializer_list<format_arg> args
  534. );
  535. /**
  536. * \brief (EXPERIMENTAL) Composes a SQL query client-side.
  537. * \details
  538. * Parses `format_str` as a format string, substituting replacement fields (like `{}`, `{1}` or `{name}`)
  539. * by formatted arguments, extracted from `args`. `opts` is using to parse the string and format string
  540. * arguments.
  541. * \n
  542. * Formatting is performed as if \ref format_context::append_raw and \ref format_context::append_value
  543. * were called on a context created by this function.
  544. * \n
  545. *
  546. * \par Exception safety
  547. * Strong guarantee. Memory allocations may throw. `boost::system::system_error` is thrown if an error
  548. * is found while formatting. See below for more info.
  549. *
  550. * \par Errors
  551. * \li \ref client_errc::invalid_encoding if `args` contains a string with byte sequences
  552. * that can't be decoded with the current character set.
  553. * \li \ref client_errc::unformattable_value if `args` contains a floating-point value
  554. * that is NaN or infinity.
  555. * \li \ref client_errc::format_string_invalid_specifier if a replacement field includes
  556. * a specifier not supported by the type being formatted.
  557. * \li Any other error generated by user-defined \ref formatter specializations.
  558. * \li \ref client_errc::format_string_invalid_syntax if `format_str` can't be parsed as
  559. * a format string.
  560. * \li \ref client_errc::format_string_invalid_encoding if `format_str` contains byte byte sequences
  561. * that can't be decoded with the current character set.
  562. * \li \ref client_errc::format_string_manual_auto_mix if `format_str` contains a mix of automatic
  563. * (`{}`) and manual indexed (`{1}`) replacement fields.
  564. * \li \ref client_errc::format_arg_not_found if an argument referenced by `format_str` isn't present
  565. * in `args` (there aren't enough arguments or a named argument is not found).
  566. */
  567. template <BOOST_MYSQL_FORMATTABLE... Formattable>
  568. std::string format_sql(format_options opts, constant_string_view format_str, Formattable&&... args);
  569. /**
  570. * \copydoc format_sql
  571. * \details
  572. * \n
  573. * This overload allows using named arguments.
  574. */
  575. BOOST_MYSQL_DECL
  576. std::string format_sql(
  577. format_options opts,
  578. constant_string_view format_str,
  579. std::initializer_list<format_arg> args
  580. );
  581. } // namespace mysql
  582. } // namespace boost
  583. #include <boost/mysql/impl/format_sql.hpp>
  584. #ifdef BOOST_MYSQL_HEADER_ONLY
  585. #include <boost/mysql/impl/format_sql.ipp>
  586. #endif
  587. #endif