mirror of
https://github.com/boostorg/mysql.git
synced 2026-01-28 07:22:26 +00:00
Replaced tabs for spaces
This commit is contained in:
@@ -22,11 +22,11 @@
|
||||
|
||||
void print_employee(const boost::mysql::row& employee)
|
||||
{
|
||||
using boost::mysql::operator<<; // Required for mysql::value objects to be streamable, due to ADL rules
|
||||
std::cout << "Employee '"
|
||||
<< employee.values()[0] << " " // first_name (type std::string_view)
|
||||
<< employee.values()[1] << "' earns " // last_name (type std::string_view)
|
||||
<< employee.values()[2] << " dollars yearly\n"; // salary (type double)
|
||||
using boost::mysql::operator<<; // Required for mysql::value objects to be streamable, due to ADL rules
|
||||
std::cout << "Employee '"
|
||||
<< employee.values()[0] << " " // first_name (type std::string_view)
|
||||
<< employee.values()[1] << "' earns " // last_name (type std::string_view)
|
||||
<< employee.values()[2] << " dollars yearly\n"; // salary (type double)
|
||||
}
|
||||
|
||||
// UNIX sockets are only available in, er, UNIX systems. Typedefs for
|
||||
@@ -37,90 +37,90 @@ void print_employee(const boost::mysql::row& employee)
|
||||
|
||||
void main_impl(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3 && argc != 4)
|
||||
{
|
||||
std::cerr << "Usage: " << argv[0] << " <username> <password> [<socket-path>]\n";
|
||||
exit(1);
|
||||
}
|
||||
if (argc != 3 && argc != 4)
|
||||
{
|
||||
std::cerr << "Usage: " << argv[0] << " <username> <password> [<socket-path>]\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char* socket_path = "/var/run/mysqld/mysqld.sock";
|
||||
if (argc == 4)
|
||||
{
|
||||
socket_path = argv[3];
|
||||
}
|
||||
const char* socket_path = "/var/run/mysqld/mysqld.sock";
|
||||
if (argc == 4)
|
||||
{
|
||||
socket_path = argv[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection parameters that tell us where and how to connect to the MySQL server.
|
||||
* There are two types of parameters:
|
||||
* - UNIX-level connection parameters, identifying the UNIX socket to connect to.
|
||||
* - MySQL level parameters: database credentials and schema to use.
|
||||
*/
|
||||
boost::asio::local::stream_protocol::endpoint ep (socket_path);
|
||||
boost::mysql::connection_params params (
|
||||
argv[1], // username
|
||||
argv[2], // password
|
||||
"mysql_asio_examples" // database to use; leave empty or omit the parameter for no database
|
||||
);
|
||||
// Note: by default, SSL will be used if the server supports it.
|
||||
// connection_params accepts an optional ssl_options argument
|
||||
// determining whether to use SSL or not. See ssl_options and ssl_mode
|
||||
// documentation for further details on SSL.
|
||||
/**
|
||||
* Connection parameters that tell us where and how to connect to the MySQL server.
|
||||
* There are two types of parameters:
|
||||
* - UNIX-level connection parameters, identifying the UNIX socket to connect to.
|
||||
* - MySQL level parameters: database credentials and schema to use.
|
||||
*/
|
||||
boost::asio::local::stream_protocol::endpoint ep (socket_path);
|
||||
boost::mysql::connection_params params (
|
||||
argv[1], // username
|
||||
argv[2], // password
|
||||
"mysql_asio_examples" // database to use; leave empty or omit the parameter for no database
|
||||
);
|
||||
// Note: by default, SSL will be used if the server supports it.
|
||||
// connection_params accepts an optional ssl_options argument
|
||||
// determining whether to use SSL or not. See ssl_options and ssl_mode
|
||||
// documentation for further details on SSL.
|
||||
|
||||
boost::asio::io_context ctx;
|
||||
boost::asio::io_context ctx;
|
||||
|
||||
// Connection to the MySQL server, over a UNIX socket
|
||||
boost::mysql::unix_connection conn (ctx);
|
||||
conn.next_layer().connect(ep); // next_level() returns a boost::asio::local::stream_protocol::socket
|
||||
conn.handshake(params); // Authenticates to the MySQL server
|
||||
// Connection to the MySQL server, over a UNIX socket
|
||||
boost::mysql::unix_connection conn (ctx);
|
||||
conn.next_layer().connect(ep); // next_level() returns a boost::asio::local::stream_protocol::socket
|
||||
conn.handshake(params); // Authenticates to the MySQL server
|
||||
|
||||
const char* sql = "SELECT first_name, last_name, salary FROM employee WHERE company_id = 'HGS'";
|
||||
boost::mysql::unix_resultset result = conn.query(sql);
|
||||
const char* sql = "SELECT first_name, last_name, salary FROM employee WHERE company_id = 'HGS'";
|
||||
boost::mysql::unix_resultset result = conn.query(sql);
|
||||
|
||||
// Get all the rows in the resultset
|
||||
std::vector<boost::mysql::owning_row> employees = result.fetch_all();
|
||||
for (const auto& employee: employees)
|
||||
{
|
||||
print_employee(employee);
|
||||
}
|
||||
// Get all the rows in the resultset
|
||||
std::vector<boost::mysql::owning_row> employees = result.fetch_all();
|
||||
for (const auto& employee: employees)
|
||||
{
|
||||
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'";
|
||||
result = conn.query(sql);
|
||||
assert(result.fields().size() == 0); // fields() returns a vector containing metadata about the query fields
|
||||
// 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'";
|
||||
result = conn.query(sql);
|
||||
assert(result.fields().size() == 0); // fields() returns a vector containing metadata about the query fields
|
||||
|
||||
// Check we have updated our poor intern salary
|
||||
result = conn.query("SELECT salary FROM employee WHERE first_name = 'Underpaid'");
|
||||
auto rows = result.fetch_all();
|
||||
assert(rows.size() == 1);
|
||||
[[maybe_unused]] auto salary = std::get<double>(rows[0].values()[0]);
|
||||
assert(salary == 10000);
|
||||
// Check we have updated our poor intern salary
|
||||
result = conn.query("SELECT salary FROM employee WHERE first_name = 'Underpaid'");
|
||||
auto rows = result.fetch_all();
|
||||
assert(rows.size() == 1);
|
||||
[[maybe_unused]] auto salary = std::get<double>(rows[0].values()[0]);
|
||||
assert(salary == 10000);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void main_impl(int, char**)
|
||||
{
|
||||
std::cout << "Sorry, your system does not support UNIX sockets" << std::endl;
|
||||
std::cout << "Sorry, your system does not support UNIX sockets" << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user