mirror of
https://github.com/boostorg/mysql.git
synced 2026-01-29 19:52:11 +00:00
154 lines
5.6 KiB
C++
154 lines
5.6 KiB
C++
//
|
|
// Copyright (c) 2019-2022 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_query_sync
|
|
#include <boost/mysql.hpp>
|
|
#include <boost/mysql/handshake_params.hpp>
|
|
#include <boost/mysql/row_view.hpp>
|
|
|
|
#include <boost/asio/io_context.hpp>
|
|
#include <boost/asio/ssl/context.hpp>
|
|
#include <boost/system/system_error.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
#define ASSERT(expr) \
|
|
if (!(expr)) \
|
|
{ \
|
|
std::cerr << "Assertion failed: " #expr << std::endl; \
|
|
exit(1); \
|
|
}
|
|
|
|
/**
|
|
* Prints an employee to std::cout. An employee here is a boost::mysql::row_view,
|
|
* which represents a row returned by a SQL query. row_view objects are an ordered
|
|
* collection of SQL fields, representing each value returned by the query.
|
|
*
|
|
* Indexing a row_view yields a boost::mysql::field_view, which is a variant-like
|
|
* type representing a single value returned by MySQL.
|
|
*/
|
|
void print_employee(const boost::mysql::row& employee)
|
|
{
|
|
std::cout << "Employee '" << employee[0] << " " // first_name (type string)
|
|
<< employee[1] << "' earns " // last_name (type string)
|
|
<< employee[2] << " dollars yearly\n"; // salary (type double)
|
|
}
|
|
|
|
void main_impl(int argc, char** argv)
|
|
{
|
|
if (argc != 4)
|
|
{
|
|
std::cerr << "Usage: " << argv[0] << " <username> <password> <server-hostname>\n";
|
|
exit(1);
|
|
}
|
|
|
|
// The I/O context to perform all operations.
|
|
boost::asio::io_context ctx;
|
|
|
|
/**
|
|
* Connection parameters that tell us how to connect to the MySQL server:
|
|
* database credentials and schema to use.
|
|
*/
|
|
boost::mysql::handshake_params params(
|
|
argv[1], // username
|
|
argv[2], // password
|
|
"boost_mysql_examples" // database to use; leave empty or omit for no database
|
|
);
|
|
|
|
/* We will use SSL in all our examples. To enable SSL, use boost::mysql::tcp_ssl_connection.
|
|
* MySQL 8+ default is to use an authentication method that requires SSL, so we encourage
|
|
* you to use SSL connections if you can.
|
|
*/
|
|
boost::asio::ssl::context ssl_ctx(boost::asio::ssl::context::tls_client);
|
|
|
|
// Represents a single connection over TCP to a MySQL server.
|
|
boost::mysql::tcp_ssl_connection conn(ctx, ssl_ctx);
|
|
|
|
// To establish the connection, we need a TCP endpoint. We have a hostname,
|
|
// so we need to perform hostname resolution. We create a resolver for this.
|
|
boost::asio::ip::tcp::resolver resolver(ctx.get_executor());
|
|
|
|
// Invoke the resolver's appropriate function to perform the resolution.
|
|
const char* hostname = argv[3];
|
|
auto endpoints = resolver.resolve(hostname, boost::mysql::default_port_string);
|
|
|
|
/**
|
|
* Before using the connection, we have to connect to the server by:
|
|
* - Establishing the TCP-level session.
|
|
* - Authenticating to the MySQL server. The SSL handshake is performed as part of this.
|
|
* connection::connect takes care of both.
|
|
*/
|
|
conn.connect(*endpoints.begin(), params);
|
|
|
|
/**
|
|
* To issue a SQL query to the database server, use tcp_ssl_connection::query, which takes
|
|
* the SQL to be executed as parameter and returns a resultset object by lvalue reference.
|
|
*
|
|
* Resultset objects represent the result of a query.
|
|
* They hold metadata describing the fields the resultset holds (in this case, first_name,
|
|
* last_name and salary). Resultsets don't contain the actual data, but have methods to read it.
|
|
*
|
|
* We will get all employees working for 'High Growth Startup'.
|
|
*/
|
|
const char* sql = "SELECT first_name, last_name, salary FROM employee WHERE company_id = 'HGS'";
|
|
boost::mysql::tcp_ssl_resultset result;
|
|
conn.query(sql, result);
|
|
|
|
/**
|
|
* We will use resultset::read_all(), which will read all our rows into
|
|
* a boost::mysql::rows object. This is a matrix-like object, specialized for
|
|
* MySQL fields. Indexing a rows object returns a row_view, which represents
|
|
* an individual row.
|
|
*/
|
|
boost::mysql::rows all_rows;
|
|
result.read_all(all_rows);
|
|
for (boost::mysql::row_view employee : all_rows)
|
|
{
|
|
print_employee(employee);
|
|
}
|
|
|
|
// We can issue any SQL statement, not only SELECTs. In this case, the returned
|
|
// resultset will have no fields and no rows
|
|
sql = "UPDATE employee SET salary = 10000 WHERE first_name = 'Underpaid'";
|
|
conn.query(sql, result);
|
|
ASSERT(
|
|
result.meta().size() == 0
|
|
); // meta() returns a collection containing metadata about the query fields
|
|
|
|
// Check we have updated our poor intern salary
|
|
conn.query("SELECT salary FROM employee WHERE first_name = 'Underpaid'", result);
|
|
result.read_all(all_rows);
|
|
ASSERT(all_rows.size() == 1);
|
|
double salary = all_rows[0][0].as_double();
|
|
ASSERT(salary == 10000);
|
|
|
|
// Close the connection. This notifies the MySQL we want to log out
|
|
// and then closes the underlying socket. This operation implies a network
|
|
// transfer and thus can fail
|
|
conn.close();
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
try
|
|
{
|
|
main_impl(argc, argv);
|
|
}
|
|
catch (const boost::system::system_error& err)
|
|
{
|
|
std::cerr << "Error: " << err.what() << ", error code: " << err.code() << std::endl;
|
|
return 1;
|
|
}
|
|
catch (const std::exception& err)
|
|
{
|
|
std::cerr << "Error: " << err.what() << std::endl;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
//]
|