summaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorPeter Powell <petpow@saberuk.com>2019-11-13 13:26:58 +0000
committerPeter Powell <petpow@saberuk.com>2019-11-13 13:26:58 +0000
commitdb45e2e69c1a68bf93e2c70d2a301d4ff09bc4b6 (patch)
treedf4444e3cf9daca6efbb4afebf9ea3532852fd0b /src/modules
parent43fb2f9972de22cafb93f2a8a67039176095f4c0 (diff)
Improve the logic around connecting to a MySQL server.
- The connection timeout can now be customised with <database:timeout>. - <database:port> 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.
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/extra/m_mysql.cpp53
1 files 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;
}