Created
May 11, 2022 04:45
-
-
Save klemens-morgenstern/0195a502340bd33029437d33eb914f2c to your computer and use it in GitHub Desktop.
Review test mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/asio.hpp> | |
#include <boost/mysql.hpp> | |
using namespace boost; | |
struct printer | |
{ | |
template<typename T> | |
auto & operator()(const T & t) | |
{ | |
return std::cout << t; | |
} | |
template<typename Clock, typename Duration> | |
auto & operator()(const std::chrono::time_point<Clock, Duration> & t) | |
{ | |
return std::cout << "<timestamp>"; | |
} | |
template<typename Clock, typename Duration> | |
auto & operator()(const std::chrono::duration<Clock, Duration> & t) | |
{ | |
return std::cout << "<duration>"; | |
} | |
}; | |
using executor_type = asio::use_awaitable_t<>::executor_with_default<asio::any_io_executor>; | |
template<typename Stream> | |
asio::awaitable<void> print_result(mysql::resultset<Stream> & res) | |
{ | |
for (const boost::mysql::row& row : co_await res.async_read_all()) | |
{ | |
std::cout << "Row: "; | |
for (const auto & col : row.values()) | |
visit(printer{}, col.to_variant()) << ", "; | |
std::cout << std::endl; | |
} | |
} | |
asio::awaitable<void> test() | |
{ | |
using socket_type = asio::ip::tcp::socket::rebind_executor<executor_type>::other; | |
using resolv_type = asio::ip::tcp::resolver::rebind_executor<executor_type>::other; | |
using mysql_conn = mysql::connection<socket_type>; | |
auto exec = co_await asio::this_coro::executor; | |
mysql_conn conn(exec); | |
resolv_type res(exec); | |
auto itr = co_await res.async_resolve("localhost", "mysql"); | |
co_await conn.async_connect(*itr.begin(), mysql::connection_params("root", "mypass", "review")); | |
// language=mysql | |
auto req = co_await conn.async_query(R"(select 'Hello World!')"); | |
co_await print_result(req); | |
// language=mysql | |
req = co_await conn.async_query(R"(create database if not exists review;)"); | |
co_await print_result(req); | |
// language=mysql | |
req = co_await conn.async_query(R"(create table if not exists users( | |
user_id INT AUTO_INCREMENT PRIMARY KEY, | |
username VARCHAR(40), | |
password VARCHAR(255)))"); | |
co_await print_result(req); | |
auto prep = co_await conn.async_prepare_statement(R"(INSERT INTO users(username, password) VALUES (?, ?), (?, ?))"); | |
req = co_await prep.async_execute(mysql::make_values("admin", "admin", "root", "password")); | |
co_await print_result(req); | |
req = co_await conn.async_query(R"(select * from users)"); | |
co_await print_result(req); | |
req = co_await conn.async_query(R"(drop table users)"); | |
co_await print_result(req); | |
co_await conn.async_quit(); | |
co_return ; | |
} | |
int main() | |
{ | |
// docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -p 3306:3306 -d mariadb | |
asio::io_context ctx; | |
asio::co_spawn(ctx, test(), [](auto e) {if (e) std::rethrow_exception(e);}); | |
asio::steady_timer tim{ctx}; | |
ctx.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment