]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mysql.cpp
m_ssl_gnutls Strip unrecognized tokens from the priority string if configured to...
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
index 2a7e2f8787d2547ed66472ed14954ad53b27707d..d8dda27a4ef6c91cb4d609f32e8ddfe97ea707ec 100644 (file)
 #include "modules/sql.h"
 
 #ifdef _WIN32
-# pragma comment(lib, "mysqlclient.lib")
-# pragma comment(lib, "advapi32.lib")
-# pragma comment(linker, "/NODEFAULTLIB:LIBCMT")
+# pragma comment(lib, "libmysql.lib")
 #endif
 
 /* VERSION 3 API: With nonblocking (threaded) requests */
 
-/* $ModDesc: SQL Service Provider module for all other m_sql* modules */
 /* $CompileFlags: exec("mysql_config --include") */
 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
 
@@ -92,7 +89,7 @@ struct RQueueItem
        RQueueItem(SQLQuery* Q, MySQLresult* R) : q(Q), r(R) {}
 };
 
-typedef std::map<std::string, SQLConnection*> ConnMap;
+typedef insp::flat_map<std::string, SQLConnection*> ConnMap;
 typedef std::deque<QQueueItem> QueryQueue;
 typedef std::deque<RQueueItem> ResultQueue;
 
@@ -107,11 +104,11 @@ class ModuleSQL : public Module
        ConnMap connections; // main thread only
 
        ModuleSQL();
-       void init();
+       void init() CXX11_OVERRIDE;
        ~ModuleSQL();
-       void OnRehash(User* user);
-       void OnUnloadModule(Module* mod);
-       Version GetVersion();
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
+       void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
+       Version GetVersion() CXX11_OVERRIDE;
 };
 
 class DispatcherThread : public SocketThread
@@ -121,8 +118,8 @@ class DispatcherThread : public SocketThread
  public:
        DispatcherThread(ModuleSQL* CreatorModule) : Parent(CreatorModule) { }
        ~DispatcherThread() { }
-       virtual void Run();
-       virtual void OnNotify();
+       void Run();
+       void OnNotify();
 };
 
 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
@@ -180,7 +177,6 @@ class MySQLresult : public SQLResult
                                rows++;
                        }
                        mysql_free_result(res);
-                       res = NULL;
                }
        }
 
@@ -189,17 +185,17 @@ class MySQLresult : public SQLResult
 
        }
 
-       virtual int Rows()
+       int Rows()
        {
                return rows;
        }
 
-       virtual void GetCols(std::vector<std::string>& result)
+       void GetCols(std::vector<std::string>& result)
        {
                result.assign(colnames.begin(), colnames.end());
        }
 
-       virtual SQLEntry GetValue(int row, int column)
+       SQLEntry GetValue(int row, int column)
        {
                if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))
                {
@@ -208,7 +204,7 @@ class MySQLresult : public SQLResult
                return SQLEntry();
        }
 
-       virtual bool GetRow(SQLEntries& result)
+       bool GetRow(SQLEntries& result)
        {
                if (currentrow < rows)
                {
@@ -259,6 +255,12 @@ class SQLConnection : public SQLProvider
                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))
                {
@@ -329,10 +331,15 @@ class SQLConnection : public SQLProvider
                                if (param < p.size())
                                {
                                        std::string parm = p[param++];
-                                       char buffer[MAXBUF];
-                                       mysql_escape_string(buffer, parm.c_str(), parm.length());
+                                       // In the worst case, each character may need to be encoded as using two bytes,
+                                       // and one byte is the terminating null
+                                       std::vector<char> buffer(parm.length() * 2 + 1);
+
+                                       // The return value of mysql_escape_string() is the length of the encoded string,
+                                       // not including the terminating null
+                                       unsigned long escapedsize = mysql_escape_string(&buffer[0], parm.c_str(), parm.length());
 //                                     mysql_real_escape_string(connection, queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length());
-                                       res.append(buffer);
+                                       res.append(&buffer[0], escapedsize);
                                }
                        }
                }
@@ -358,9 +365,10 @@ class SQLConnection : public SQLProvider
                                if (it != p.end())
                                {
                                        std::string parm = it->second;
-                                       char buffer[MAXBUF];
-                                       mysql_escape_string(buffer, parm.c_str(), parm.length());
-                                       res.append(buffer);
+                                       // NOTE: See above
+                                       std::vector<char> buffer(parm.length() * 2 + 1);
+                                       unsigned long escapedsize = mysql_escape_string(&buffer[0], parm.c_str(), parm.length());
+                                       res.append(&buffer[0], escapedsize);
                                }
                        }
                }
@@ -376,12 +384,7 @@ ModuleSQL::ModuleSQL()
 void ModuleSQL::init()
 {
        Dispatcher = new DispatcherThread(this);
-       ServerInstance->Threads->Start(Dispatcher);
-
-       Implementation eventlist[] = { I_OnRehash, I_OnUnloadModule };
-       ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
-
-       OnRehash(NULL);
+       ServerInstance->Threads.Start(Dispatcher);
 }
 
 ModuleSQL::~ModuleSQL()
@@ -398,7 +401,7 @@ ModuleSQL::~ModuleSQL()
        }
 }
 
-void ModuleSQL::OnRehash(User* user)
+void ModuleSQL::ReadConfig(ConfigStatus& status)
 {
        ConnMap conns;
        ConfigTagList tags = ServerInstance->Config->ConfTags("database");
@@ -431,13 +434,14 @@ void ModuleSQL::OnRehash(User* user)
                i->second->lock.Lock();
                i->second->lock.Unlock();
                // now remove all active queries to this DB
-               for(unsigned int j = qq.size() - 1; j >= 0; j--)
+               for (size_t j = qq.size(); j > 0; j--)
                {
-                       if (qq[j].c == i->second)
+                       size_t k = j - 1;
+                       if (qq[k].c == i->second)
                        {
-                               qq[j].q->OnError(err);
-                               delete qq[j].q;
-                               qq.erase(qq.begin() + j);
+                               qq[k].q->OnError(err);
+                               delete qq[k].q;
+                               qq.erase(qq.begin() + k);
                        }
                }
                // finally, nuke the connection