Skip to content

Commit

Permalink
Support MySQL connect timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDog authored and rbock committed Dec 10, 2021
1 parent c37c8d3 commit 36d3d30
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/sqlpp11/mysql/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion include/sqlpp11/mysql/connection_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/mysql/usage/Sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 36d3d30

Please sign in to comment.