From e6bd90c489bb42394247a2cb9cd1838bb55e1654 Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Wed, 19 Jun 2024 19:32:17 +0100 Subject: [PATCH] MySQL SSL & read_timeout options, and bind blob result bugfix (#577) --- include/sqlpp11/mysql/bind_result.h | 7 +++-- include/sqlpp11/mysql/connection_config.h | 8 ++++- .../sqlpp11/mysql/detail/connection_handle.h | 30 +++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/sqlpp11/mysql/bind_result.h b/include/sqlpp11/mysql/bind_result.h index ad859fcf..d63329a4 100644 --- a/include/sqlpp11/mysql/bind_result.h +++ b/include/sqlpp11/mysql/bind_result.h @@ -210,7 +210,7 @@ namespace sqlpp param.is_unsigned = false; param.error = &meta_data.bound_error; } - void _bind_blob_result(size_t index, const char** value, size_t* len) + void _bind_blob_result(size_t index, const uint8_t** value, size_t* len) { if (_handle->debug) std::cerr << "MySQL debug: binding text result " << static_cast(*value) << " at index: " << index @@ -220,7 +220,7 @@ namespace sqlpp meta_data.index = index; meta_data.len = len; meta_data.is_null = nullptr; - meta_data.text_buffer = value; + meta_data.text_buffer = reinterpret_cast(value); if (meta_data.bound_text_buffer.empty()) meta_data.bound_text_buffer.resize(8); @@ -318,6 +318,9 @@ namespace sqlpp void _post_bind_text_result(size_t /* index */, const char** /* text */, size_t* /* len */) { } + void _post_bind_blob_result(size_t /* index */, const uint8_t** /* value */, size_t* /* len */) + { + } void _post_bind_date_result(size_t index, ::sqlpp::chrono::day_point* value, bool* is_null) { if (_handle->debug) diff --git a/include/sqlpp11/mysql/connection_config.h b/include/sqlpp11/mysql/connection_config.h index a518a326..20e091e0 100644 --- a/include/sqlpp11/mysql/connection_config.h +++ b/include/sqlpp11/mysql/connection_config.h @@ -44,12 +44,18 @@ namespace sqlpp std::string charset{"utf8"}; bool debug{false}; unsigned int connect_timeout_seconds{0}; // 0 = do not override MySQL library default + bool ssl{false}; + std::string ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher; + unsigned int read_timeout{0}; bool operator==(const connection_config& other) const { return (other.host == host and other.user == user and other.password == password and other.database == database and other.charset == charset and other.debug == debug and - other.connect_timeout_seconds == connect_timeout_seconds); + other.connect_timeout_seconds == connect_timeout_seconds and other.ssl == ssl and + other.ssl_key == ssl_key and other.ssl_cert == ssl_cert and other.ssl_ca == ssl_ca and + other.ssl_capath == ssl_capath and other.ssl_cipher == ssl_cipher and + other.read_timeout == read_timeout); } bool operator!=(const connection_config& other) const diff --git a/include/sqlpp11/mysql/detail/connection_handle.h b/include/sqlpp11/mysql/detail/connection_handle.h index 471a3784..5dc8a37a 100644 --- a/include/sqlpp11/mysql/detail/connection_handle.h +++ b/include/sqlpp11/mysql/detail/connection_handle.h @@ -46,6 +46,36 @@ namespace sqlpp throw sqlpp::exception{"MySQL: could not set option MYSQL_OPT_CONNECT_TIMEOUT"}; } + if (config.read_timeout > 0 && + mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, &config.read_timeout)) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_READ_TIMEOUT"); + } + + if (config.ssl) + { + if (!config.ssl_key.empty() && mysql_options(mysql, MYSQL_OPT_SSL_KEY, config.ssl_key.c_str())) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_SSL_KEY"); + } + if (!config.ssl_cert.empty() && mysql_options(mysql, MYSQL_OPT_SSL_CERT, config.ssl_cert.c_str())) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_SSL_CERT"); + } + if (!config.ssl_ca.empty() && mysql_options(mysql, MYSQL_OPT_SSL_CA, config.ssl_ca.c_str())) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_SSL_CA"); + } + if (!config.ssl_capath.empty() && mysql_options(mysql, MYSQL_OPT_SSL_CAPATH, config.ssl_capath.c_str())) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_SSL_CAPATH"); + } + if (!config.ssl_cipher.empty() && mysql_options(mysql, MYSQL_OPT_SSL_CIPHER, config.ssl_cipher.c_str())) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_SSL_CIPHER"); + } + } + if (!mysql_real_connect(mysql, config.host.empty() ? nullptr : config.host.c_str(), config.user.empty() ? nullptr : config.user.c_str(), config.password.empty() ? nullptr : config.password.c_str(), nullptr, config.port,