mysql/example/async_coroutines.cpp
Anarthal (Rubén Pérez) 95a9aa1068
Added with_diagnostics completion token
Made with_diagnostics(deferred) the default token for any_connection and
connection_pool.
throw_on_error is now marked as legacy.

close #329 
close #296
2024-08-13 10:48:49 +02:00

141 lines
5.1 KiB
C++

//
// Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//[example_async_coroutines
// To use coroutines created by boost::asio::spawn, you need to link
// against Boost.Context.
#include <boost/mysql/error_with_diagnostics.hpp>
#include <boost/mysql/handshake_params.hpp>
#include <boost/mysql/row_view.hpp>
#include <boost/mysql/tcp_ssl.hpp>
#include <boost/mysql/with_diagnostics.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ssl/context.hpp>
#include <iostream>
using boost::mysql::with_diagnostics;
void print_employee(boost::mysql::row_view employee)
{
std::cout << "Employee '" << employee.at(0) << " " // first_name (string)
<< employee.at(1) << "' earns " // last_name (string)
<< employee.at(2) << " dollars yearly\n"; // salary (double)
}
void main_impl(int argc, char** argv)
{
if (argc != 4 && argc != 5)
{
std::cerr << "Usage: " << argv[0] << " <username> <password> <server-hostname> [company-id]\n";
exit(1);
}
const char* hostname = argv[3];
// The company_id whose employees we will be listing. This
// is user-supplied input, and should be treated as untrusted.
const char* company_id = argc == 5 ? argv[4] : "HGS";
// I/O context and connection. We use SSL because MySQL 8+ default settings require it.
boost::asio::io_context ctx;
boost::asio::ssl::context ssl_ctx(boost::asio::ssl::context::tls_client);
boost::mysql::tcp_ssl_connection conn(ctx, ssl_ctx);
// Connection params
boost::mysql::handshake_params params(
argv[1], // username
argv[2], // password
"boost_mysql_examples" // database to use; leave empty or omit for no database
);
// Resolver for hostname resolution
boost::asio::ip::tcp::resolver resolver(ctx.get_executor());
/**
* The entry point. We spawn a stackful coroutine using boost::asio::spawn.
*
* The coroutine will actually start running when we call io_context::run().
* It will suspend every time we call one of the asynchronous functions, saving
* all information it needs for resuming. When the asynchronous operation completes,
* the coroutine will resume in the point it was left.
*/
boost::asio::spawn(
ctx.get_executor(),
[&conn, &resolver, params, hostname, company_id](boost::asio::yield_context yield) {
// Hostname resolution
auto endpoints = resolver.async_resolve(hostname, boost::mysql::default_port_string, yield);
// Connect to server. with_diagnostics will turn any thrown exceptions
// into error_with_diagnostics, which contain more info than regular exceptions
conn.async_connect(*endpoints.begin(), params, with_diagnostics(yield));
// We will be using company_id, which is untrusted user input, so we will use a prepared
// statement.
boost::mysql::statement stmt = conn.async_prepare_statement(
"SELECT first_name, last_name, salary FROM employee WHERE company_id = ?",
with_diagnostics(yield)
);
// Execute the statement
boost::mysql::results result;
conn.async_execute(stmt.bind(company_id), result, with_diagnostics(yield));
// Print the employees
for (boost::mysql::row_view employee : result.rows())
{
print_employee(employee);
}
// Notify the MySQL server we want to quit, then close the underlying connection.
conn.async_close(with_diagnostics(yield));
},
// If any exception is thrown in the coroutine body, rethrow it.
[](std::exception_ptr ptr) {
if (ptr)
{
std::rethrow_exception(ptr);
}
}
);
// Don't forget to call run()! Otherwise, your program
// will not spawn the coroutine and will do nothing.
ctx.run();
}
int main(int argc, char** argv)
{
try
{
main_impl(argc, argv);
}
catch (const boost::mysql::error_with_diagnostics& err)
{
// You will only get this type of exceptions if you use with_diagnostics.
// Some errors include additional diagnostics, like server-provided error messages.
// Security note: diagnostics::server_message may contain user-supplied values (e.g. the
// field value that caused the error) and is encoded using to the connection's character set
// (UTF-8 by default). Treat is as untrusted input.
std::cerr << "Error: " << err.what() << '\n'
<< "Server diagnostics: " << err.get_diagnostics().server_message() << std::endl;
return 1;
}
catch (const std::exception& err)
{
std::cerr << "Error: " << err.what() << std::endl;
return 1;
}
}
//]