Skip to content

Commit

Permalink
format_context::get is now move only
Browse files Browse the repository at this point in the history
  • Loading branch information
anarthal committed Feb 7, 2024
1 parent e8e64e7 commit 830d647
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion example/batch_inserts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static std::string compose_batch_insert(
// Formatting can fail (e.g. if you supply strings with invalid UTF-8),
// so get() returns a boost::system::result<std::string>.
// Calling value() will retrieve the string or throw an exception on failure.
return ctx.get().value();
return std::move(ctx).get().value();
}

void main_impl(int argc, char** argv)
Expand Down
2 changes: 1 addition & 1 deletion example/dynamic_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ std::string compose_get_employees_query(const boost::mysql::any_connection& conn
ctx.append_value(boost::mysql::identifier(*filts.order_by));
}

return ctx.get().value();
return std::move(ctx).get().value();
}

void main_impl(int argc, char** argv)
Expand Down
4 changes: 2 additions & 2 deletions example/snippets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ std::string compose_update_query(
ctx.append_value(employee_id);

// Retrieve the generated query string
return ctx.get().value();
return std::move(ctx).get().value();
}
//]
#endif
Expand Down Expand Up @@ -1842,7 +1842,7 @@ void section_sql_formatting(string_view server_hostname, string_view username, s
boost::mysql::basic_format_context<std::pmr::string> ctx(conn.format_opts().value());

// Compose your query as usual
std::pmr::string query = ctx.get().value();
std::pmr::string query = std::move(ctx).get().value();
//]
}
#endif
Expand Down
15 changes: 11 additions & 4 deletions include/boost/mysql/format_sql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,15 @@ class format_context_base
error_code error_state() const noexcept { return impl_.ec; }
};

/// (EXPERIMENTAL) Implements stream-like SQL formatting (TODO).
/**
* \brief (EXPERIMENTAL) Implements stream-like SQL formatting.
* - Concrete classes for SQL stream formatting
* - Owns an OutputString, to which characters will be appended when formatting.
* Should satisfy the OutputString concept.
* - See format_context for the most common type alias.
* - Move only
* - Create, append, then call get.
*/
template <BOOST_MYSQL_OUTPUT_STRING OutputString>
class basic_format_context : public format_context_base
{
Expand Down Expand Up @@ -288,8 +296,7 @@ class basic_format_context : public format_context_base
assign(rhs);
}

// TODO: do we make this move-only?
system::result<OutputString> get()
system::result<OutputString> get() &&
{
auto ec = error_state();
if (ec)
Expand Down Expand Up @@ -334,7 +341,7 @@ std::string format_sql(constant_string_view format_str, const format_options& op
{
format_context ctx(opts);
format_sql_to(format_str, ctx, args...);
return detail::check_format_result(ctx.get());
return detail::check_format_result(std::move(ctx).get());
}

} // namespace mysql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class set_character_set_algo : public sansio_algorithm, asio::coroutine
// For security, if the character set has non-ascii characters in it name, reject it.
format_context ctx(format_options{ascii_charset, true});
ctx.append_raw("SET NAMES ").append_value(charset.name);
return ctx.get();
return std::move(ctx).get();
}

public:
Expand Down
10 changes: 5 additions & 5 deletions test/unit/test/format_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ error_code format_single_error(const Arg& arg)
{
format_context ctx(opts);
ctx.append_value(arg);
return ctx.get().error();
return std::move(ctx).get().error();
}

BOOST_AUTO_TEST_CASE(individual_error)
Expand Down Expand Up @@ -752,7 +752,7 @@ BOOST_AUTO_TEST_CASE(format_strings_invalid_arguments)
BOOST_AUTO_TEST_CASE(format_context_success)
{
// Helper
auto get = [](format_context_base& ctx) { return static_cast<format_context&>(ctx).get().value(); };
auto get = [](format_context_base& ctx) { return static_cast<format_context&&>(ctx).get().value(); };

// Empty
BOOST_TEST(format_context(opts).get().value() == "");
Expand Down Expand Up @@ -804,7 +804,7 @@ BOOST_AUTO_TEST_CASE(format_context_charset)
.append_value("abd\xff{}")
.append_raw(" + ")
.append_value(identifier("i`d`ent\xff`ifier"));
BOOST_TEST(ctx.get().value() == "SELECT '\xff{abc' + 'abd\xff{}' + `i``d``ent\xff`ifier`");
BOOST_TEST(std::move(ctx).get().value() == "SELECT '\xff{abc' + 'abd\xff{}' + `i``d``ent\xff`ifier`");
}

BOOST_AUTO_TEST_CASE(format_context_backslashes)
Expand All @@ -816,13 +816,13 @@ BOOST_AUTO_TEST_CASE(format_context_backslashes)
.append_value("ab'cd\"ef")
.append_raw(" + ")
.append_value(identifier("identif`ier"));
BOOST_TEST(ctx.get().value() == "SELECT 'ab''cd\"ef' + `identif``ier`");
BOOST_TEST(std::move(ctx).get().value() == "SELECT 'ab''cd\"ef' + `identif``ier`");
}

BOOST_AUTO_TEST_CASE(format_context_error)
{
// Helper
auto get = [](format_context_base& ctx) { return static_cast<format_context&>(ctx).get().error(); };
auto get = [](format_context_base& ctx) { return static_cast<format_context&&>(ctx).get().error(); };

// Just an error
BOOST_TEST(get(format_context(opts).append_value("bad\xff")) == client_errc::invalid_encoding);
Expand Down

0 comments on commit 830d647

Please sign in to comment.