123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- #ifndef BOOST_MYSQL_FORMAT_SQL_HPP
- #define BOOST_MYSQL_FORMAT_SQL_HPP
- #include <boost/mysql/character_set.hpp>
- #include <boost/mysql/constant_string_view.hpp>
- #include <boost/mysql/error_code.hpp>
- #include <boost/mysql/string_view.hpp>
- #include <boost/mysql/detail/access.hpp>
- #include <boost/mysql/detail/config.hpp>
- #include <boost/mysql/detail/format_sql.hpp>
- #include <boost/mysql/detail/output_string.hpp>
- #include <boost/config.hpp>
- #include <boost/core/span.hpp>
- #include <boost/system/result.hpp>
- #include <initializer_list>
- #include <iterator>
- #include <string>
- #include <type_traits>
- #include <utility>
- #ifdef BOOST_MYSQL_HAS_CONCEPTS
- #include <concepts>
- #endif
- namespace boost {
- namespace mysql {
- template <class T>
- struct formatter
- #ifndef BOOST_MYSQL_DOXYGEN
- : detail::formatter_is_unspecialized
- {
- }
- #endif
- ;
- class formattable_ref
- {
- detail::formattable_ref_impl impl_;
- #ifndef BOOST_MYSQL_DOXYGEN
- friend struct detail::access;
- #endif
- public:
-
- template <
- BOOST_MYSQL_FORMATTABLE Formattable
- #ifndef BOOST_MYSQL_DOXYGEN
- ,
- class = typename std::enable_if<
- detail::is_formattable_type<Formattable>() &&
- !detail::is_formattable_ref<Formattable>::value>::type
- #endif
- >
- formattable_ref(Formattable&& value) noexcept
- : impl_(detail::make_formattable_ref(std::forward<Formattable>(value)))
- {
- }
- };
- class format_arg
- {
- #ifndef BOOST_MYSQL_DOXYGEN
- struct
- {
- string_view name;
- detail::formattable_ref_impl value;
- } impl_;
- friend struct detail::access;
- #endif
- public:
-
- format_arg(string_view name, formattable_ref value) noexcept
- : impl_{name, detail::access::get_impl(value)}
- {
- }
- };
- class format_context_base
- {
- #ifndef BOOST_MYSQL_DOXYGEN
- struct
- {
- detail::output_string_ref output;
- format_options opts;
- error_code ec;
- } impl_;
- friend struct detail::access;
- friend class detail::format_state;
- #endif
- BOOST_MYSQL_DECL void format_arg(detail::formattable_ref_impl arg, string_view format_spec);
- protected:
- format_context_base(detail::output_string_ref out, format_options opts, error_code ec = {}) noexcept
- : impl_{out, opts, ec}
- {
- }
- format_context_base(detail::output_string_ref out, const format_context_base& rhs) noexcept
- : impl_{out, rhs.impl_.opts, rhs.impl_.ec}
- {
- }
- void assign(const format_context_base& rhs) noexcept
- {
-
- impl_.opts = rhs.impl_.opts;
- impl_.ec = rhs.impl_.ec;
- }
- public:
-
- format_context_base& append_raw(constant_string_view sql)
- {
- impl_.output.append(sql.get());
- return *this;
- }
-
- format_context_base& append_value(
- formattable_ref value,
- constant_string_view format_specifiers = string_view()
- )
- {
- format_arg(detail::access::get_impl(value), format_specifiers.get());
- return *this;
- }
-
- void add_error(error_code ec) noexcept
- {
- if (!impl_.ec)
- impl_.ec = ec;
- }
-
- error_code error_state() const noexcept { return impl_.ec; }
-
- format_options format_opts() const noexcept { return impl_.opts; }
- };
- template <BOOST_MYSQL_OUTPUT_STRING OutputString>
- class basic_format_context : public format_context_base
- {
- OutputString output_{};
- detail::output_string_ref ref() noexcept { return detail::output_string_ref::create(output_); }
- public:
-
- explicit basic_format_context(format_options opts
- ) noexcept(std::is_nothrow_default_constructible<OutputString>::value)
- : format_context_base(ref(), opts)
- {
- }
-
- basic_format_context(format_options opts, OutputString&& storage) noexcept(
- std::is_nothrow_move_constructible<OutputString>::value
- )
- : format_context_base(ref(), opts), output_(std::move(storage))
- {
- output_.clear();
- }
- #ifndef BOOST_MYSQL_DOXYGEN
- basic_format_context(const basic_format_context&) = delete;
- basic_format_context& operator=(const basic_format_context&) = delete;
- #endif
-
- basic_format_context(basic_format_context&& rhs
- ) noexcept(std::is_nothrow_move_constructible<OutputString>::value)
- : format_context_base(ref(), rhs), output_(std::move(rhs.output_))
- {
- }
-
- basic_format_context& operator=(basic_format_context&& rhs
- ) noexcept(std::is_nothrow_move_assignable<OutputString>::value)
- {
- output_ = std::move(rhs.output_);
- assign(rhs);
- return *this;
- }
-
- system::result<OutputString> get() && noexcept(std::is_nothrow_move_constructible<OutputString>::value)
- {
- auto ec = error_state();
- if (ec)
- return ec;
- return std::move(output_);
- }
- };
- using format_context = basic_format_context<std::string>;
- template <class It, class Sentinel, class FormatFn>
- struct format_sequence_view
- #ifndef BOOST_MYSQL_DOXYGEN
- {
- It it;
- Sentinel sentinel;
- FormatFn fn;
- constant_string_view glue;
- }
- #endif
- ;
- template <class Range, class FormatFn>
- #if defined(BOOST_MYSQL_HAS_CONCEPTS)
- requires std::move_constructible<FormatFn> && detail::format_fn_for_range<FormatFn, Range>
- #endif
- auto sequence(Range&& range, FormatFn fn, constant_string_view glue = ", ")
- -> format_sequence_view<decltype(std::begin(range)), decltype(std::end(range)), FormatFn>
- {
- return {std::begin(range), std::end(range), std::move(fn), glue};
- }
- template <class It, class Sentinel, class FormatFn>
- struct formatter<format_sequence_view<It, Sentinel, FormatFn>>
- {
- const char* parse(const char* begin, const char*) { return begin; }
- void format(const format_sequence_view<It, Sentinel, FormatFn>& value, format_context_base& ctx) const
- {
- bool is_first = true;
- for (auto it = value.it; it != value.sentinel; ++it)
- {
- if (!is_first)
- ctx.append_raw(value.glue);
- is_first = false;
- value.fn(*it, ctx);
- }
- }
- };
- template <BOOST_MYSQL_FORMATTABLE... Formattable>
- void format_sql_to(format_context_base& ctx, constant_string_view format_str, Formattable&&... args);
- BOOST_MYSQL_DECL
- void format_sql_to(
- format_context_base& ctx,
- constant_string_view format_str,
- std::initializer_list<format_arg> args
- );
- template <BOOST_MYSQL_FORMATTABLE... Formattable>
- std::string format_sql(format_options opts, constant_string_view format_str, Formattable&&... args);
- BOOST_MYSQL_DECL
- std::string format_sql(
- format_options opts,
- constant_string_view format_str,
- std::initializer_list<format_arg> args
- );
- }
- }
- #include <boost/mysql/impl/format_sql.hpp>
- #ifdef BOOST_MYSQL_HEADER_ONLY
- #include <boost/mysql/impl/format_sql.ipp>
- #endif
- #endif
|