From db45e2e69c1a68bf93e2c70d2a301d4ff09bc4b6 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Wed, 13 Nov 2019 13:26:58 +0000 Subject: Improve the logic around connecting to a MySQL server. - The connection timeout can now be customised with . - is now limited to the 1-65535 range. - The MySQL library will now not install a SIGPIPE handler as it would override the default InspIRCd ignore behavopur. - Errors caused by setting the default character set and executing the initial query are now no longer ignored. --- src/modules/extra/m_mysql.cpp | 53 +++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/modules/extra/m_mysql.cpp b/src/modules/extra/m_mysql.cpp index a79ef01ad..fe9bb4cec 100644 --- a/src/modules/extra/m_mysql.cpp +++ b/src/modules/extra/m_mysql.cpp @@ -300,28 +300,43 @@ class SQLConnection : public SQL::Provider // true upon success. bool Connect() { - unsigned int timeout = 1; connection = mysql_init(connection); - mysql_options(connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout); - std::string host = config->getString("host"); - std::string user = config->getString("user"); - std::string pass = config->getString("pass"); - std::string dbname = config->getString("name"); - unsigned int port = config->getUInt("port", 3306); - bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0); - if (!rv) - return rv; - - // Enable character set settings - std::string charset = config->getString("charset"); - if ((!charset.empty()) && (mysql_set_character_set(connection, charset.c_str()))) - ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not set character set to \"%s\"", charset.c_str()); - - std::string initquery; - if (config->readString("initialquery", initquery)) + + // Set the connection timeout. + unsigned int timeout = config->getDuration("timeout", 5, 1, 30); + mysql_options(connection, MYSQL_OPT_CONNECT_TIMEOUT, &timeout); + + // Attempt to connect to the database. + const std::string host = config->getString("host"); + const std::string user = config->getString("user"); + const std::string pass = config->getString("pass"); + const std::string dbname = config->getString("name"); + unsigned int port = config->getUInt("port", 3306, 1, 65535); + if (!mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, CLIENT_IGNORE_SIGPIPE)) + { + ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Unable to connect to the %s MySQL server: %s", + GetId().c_str(), mysql_error(connection)); + return false; + } + + // Set the default character set. + const std::string charset = config->getString("charset"); + if (!charset.empty() && mysql_set_character_set(connection, charset.c_str())) { - mysql_query(connection,initquery.c_str()); + ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not set character set for %s to \"%s\": %s", + GetId().c_str(), charset.c_str(), mysql_error(connection)); + return false; } + + // Execute the initial SQL query. + const std::string initialquery = config->getString("initialquery"); + if (!initialquery.empty() && mysql_real_query(connection, initialquery.data(), initialquery.length())) + { + ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not execute initial query \"%s\" for %s: %s", + initialquery.c_str(), name.c_str(), mysql_error(connection)); + return false; + } + return true; } -- cgit v1.2.3