From 36d3d30254dc0b142571168b32ad3072741c6102 Mon Sep 17 00:00:00 2001 From: Andreas Sommer Date: Wed, 8 Dec 2021 19:21:54 +0100 Subject: [PATCH] Support MySQL connect timeout option --- include/sqlpp11/mysql/connection.h | 6 ++++++ include/sqlpp11/mysql/connection_config.h | 3 ++- tests/mysql/usage/Sample.cpp | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/sqlpp11/mysql/connection.h b/include/sqlpp11/mysql/connection.h index 01365d02e..b25f929f5 100644 --- a/include/sqlpp11/mysql/connection.h +++ b/include/sqlpp11/mysql/connection.h @@ -71,6 +71,12 @@ namespace sqlpp inline void connect(MYSQL* mysql, const connection_config& config) { + if (config.connect_timeout_seconds != 0 && + mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &config.connect_timeout_seconds)) + { + throw sqlpp::exception("MySQL: could not set option MYSQL_OPT_CONNECT_TIMEOUT"); + } + 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, diff --git a/include/sqlpp11/mysql/connection_config.h b/include/sqlpp11/mysql/connection_config.h index cd695510d..72d787488 100644 --- a/include/sqlpp11/mysql/connection_config.h +++ b/include/sqlpp11/mysql/connection_config.h @@ -47,12 +47,13 @@ namespace sqlpp std::string charset = "utf8"; bool auto_reconnect = true; bool debug = false; + unsigned int connect_timeout_seconds = 0; // 0 = do not override MySQL library default 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.auto_reconnect == auto_reconnect and - other.debug == debug); + other.debug == debug and other.connect_timeout_seconds == connect_timeout_seconds); } bool operator!=(const connection_config& other) const diff --git a/tests/mysql/usage/Sample.cpp b/tests/mysql/usage/Sample.cpp index a317d1c8b..f4f0b12d2 100644 --- a/tests/mysql/usage/Sample.cpp +++ b/tests/mysql/usage/Sample.cpp @@ -43,6 +43,7 @@ int Sample(int, char*[]) config->user = "root"; config->database = "sqlpp_mysql"; config->debug = true; + config->connect_timeout_seconds = 5; try { mysql::connection db(config);