MyCoSQL is a C++20 client for the MySQL and MariaDB database servers, based on the proposed Capy and Corosio libraries. It is coroutines-only.
Important: not a Boost library yet. Might get proposed in the future!
Still in development, but usable. If you find any issues or have any feedback, please open an issue!
This library is a full implementation of the MySQL client/server protocol. It is similar in scope to the official libmysqlclient, but natively compatible with Capy/Corosio, safer and more expressive. MyCoSQL does not use libmysqlclient.
- Fully async, but thanks to coroutines, as easy to use as synchronous code.
- Fully compatible with Capy/Corosio.
- Coroutines-native, optimized for C++20 coroutines.
- Advanced features like connection pooling, client-side SQL formatting and pipelining.
This simple example connects to a database server, issues a query and retrieves the generated rows:
#include <boost/mysql/any_address.hpp>
#include <boost/mysql/any_connection.hpp>
#include <boost/mysql/connect_params.hpp>
#include <boost/mysql/diagnostics.hpp>
#include <boost/mysql/results.hpp>
#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/ex/this_coro.hpp>
#include <boost/capy/task.hpp>
#include <boost/corosio/io_context.hpp>
#include <iostream>
namespace mysql = boost::mysql;
namespace corosio = boost::corosio;
namespace capy = boost::capy;
capy::task<> main_impl()
{
// Represents a connection to the MySQL server.
mysql::any_connection conn(co_await capy::this_coro::executor);
// The hostname, username, password and database to use.
mysql::connect_params params{
.server_address = mysql::host_and_port("localhost"),
.username = "root",
.password = "",
};
// Connect to the server. This performs hostname resolution,
// TCP connection, TLS handshake and MySQL handshake.
if (auto [ec, diag] = co_await conn.connect(params); ec)
{
std::cerr << "Error connecting: " << ec << ": " << diag.server_message() << std::endl;
exit(1);
}
// Issue the SQL query to the server.
// results contains the rows returned by the query.
mysql::results result;
if (auto [ec, diag] = co_await conn.execute("SELECT 'Hello world!'", result); ec)
{
std::cerr << "Error executing query: " << ec << ": " << diag.server_message() << std::endl;
exit(1);
}
// Print the first field in the first row.
std::cout << result.rows().at(0).at(0) << std::endl;
// Close the connection. This notifies the server that
// we want to terminate the session, then closes the socket.
if (auto [ec, diag] = co_await conn.close(); ec)
{
std::cerr << "Error closing connection: " << ec << ": " << diag.server_message() << std::endl;
exit(1);
}
}
int main(int argc, char** argv)
{
// The I/O context, required to run I/O operations.
corosio::io_context ctx;
// Launch the coroutine
capy::run_async(ctx.get_executor())(main_impl());
// Run until the coroutine completes
ctx.run();
}To use this library, you need:
- Boost (latest stable version).
- Capy/Corosio.
- A C++20-capable compiler.
- OpenSSL.
The library is intended to be built within the Boost source tree.
Consume it via CMake as you would a Boost library:
project(my_example LANGUAGES CXX)
find_package(boost_mycosql REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE Boost::mycosql)- Text queries (execution of text SQL queries and data retrieval). MySQL refers to this as the "text protocol", as all information is passed using text (as opposed to prepared statements, see below).
- Prepared statements. MySQL refers to this as the "binary protocol", as the result of executing a prepared statement is sent in binary format rather than in text.
- Stored procedures.
- Authentication methods (authentication plugins):
mysql_native_passwordandcaching_sha2_password. These are the default methods in MySQL 5/MariaDB and MySQL 8, respectively. - Encrypted connections (TLS).
- TCP and UNIX socket transports.
- Connection pools.
- Client-side SQL formatting.
- Pipelines.