]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mysql.cpp
Change Windows libraries to be dynamically linked
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
index 86d9273c3bc99f1d5d6b4990fed3fca984723026..01b1553b0b1d36b0e15433f5b8bb17793ca22cd7 100644 (file)
 #include <mysql.h>
 #include "sql.h"
 
-#ifdef WINDOWS
-# pragma comment(lib, "mysqlclient.lib")
-# pragma comment(lib, "advapi32.lib")
-# pragma comment(linker, "/NODEFAULTLIB:LIBCMT")
+#ifdef _WIN32
+# pragma comment(lib, "libmysql.lib")
 #endif
 
 /* VERSION 3 API: With nonblocking (threaded) requests */
@@ -180,7 +178,6 @@ class MySQLresult : public SQLResult
                                rows++;
                        }
                        mysql_free_result(res);
-                       res = NULL;
                }
        }
 
@@ -291,7 +288,7 @@ class SQLConnection : public SQLProvider
                {
                        /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
                         * possible error numbers and error messages */
-                       SQLerror e(SQL_QREPLY_FAIL, ConvToStr(mysql_errno(connection)) + std::string(": ") + mysql_error(connection));
+                       SQLerror e(SQL_QREPLY_FAIL, ConvToStr(mysql_errno(connection)) + ": " + mysql_error(connection));
                        return new MySQLresult(e);
                }
        }
@@ -333,10 +330,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);
                                }
                        }
                }
@@ -362,9 +364,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);
                                }
                        }
                }
@@ -383,7 +386,7 @@ void ModuleSQL::init()
        ServerInstance->Threads->Start(Dispatcher);
 
        Implementation eventlist[] = { I_OnRehash, I_OnUnloadModule };
-       ServerInstance->Modules->Attach(eventlist, this, 2);
+       ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
 
        OnRehash(NULL);
 }
@@ -435,13 +438,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
@@ -503,7 +507,7 @@ void DispatcherThread::Run()
                         */
 
                        this->LockQueue();
-                       if (Parent->qq.front().q == i.q)
+                       if (!Parent->qq.empty() && Parent->qq.front().q == i.q)
                        {
                                Parent->qq.pop_front();
                                Parent->rq.push_back(RQueueItem(i.q, res));