]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_mysql.cpp
Remove m_sqlv2.h from these modules, they both use v3 now.
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* Stop mysql wanting to use long long */
15 #define NO_CLIENT_LONG_LONG
16
17 #include "inspircd.h"
18 #include <mysql.h>
19 #include "sql.h"
20
21 #ifdef WINDOWS
22 #pragma comment(lib, "mysqlclient.lib")
23 #endif
24
25 /* VERSION 3 API: With nonblocking (threaded) requests */
26
27 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
28 /* $CompileFlags: exec("mysql_config --include") */
29 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
30
31 /* THE NONBLOCKING MYSQL API!
32  *
33  * MySQL provides no nonblocking (asyncronous) API of its own, and its developers recommend
34  * that instead, you should thread your program. This is what i've done here to allow for
35  * asyncronous SQL requests via mysql. The way this works is as follows:
36  *
37  * The module spawns a thread via class Thread, and performs its mysql queries in this thread,
38  * using a queue with priorities. There is a mutex on either end which prevents two threads
39  * adjusting the queue at the same time, and crashing the ircd. Every 50 milliseconds, the
40  * worker thread wakes up, and checks if there is a request at the head of its queue.
41  * If there is, it processes this request, blocking the worker thread but leaving the ircd
42  * thread to go about its business as usual. During this period, the ircd thread is able
43  * to insert futher pending requests into the queue.
44  *
45  * Once the processing of a request is complete, it is removed from the incoming queue to
46  * an outgoing queue, and initialized as a 'response'. The worker thread then signals the
47  * ircd thread (via a loopback socket) of the fact a result is available, by sending the
48  * connection ID through the connection.
49  *
50  * The ircd thread then mutexes the queue once more, reads the outbound response off the head
51  * of the queue, and sends it on its way to the original calling module.
52  *
53  * XXX: You might be asking "why doesnt he just send the response from within the worker thread?"
54  * The answer to this is simple. The majority of InspIRCd, and in fact most ircd's are not
55  * threadsafe. This module is designed to be threadsafe and is careful with its use of threads,
56  * however, if we were to call a module's OnRequest even from within a thread which was not the
57  * one the module was originally instantiated upon, there is a chance of all hell breaking loose
58  * if a module is ever put in a re-enterant state (stack corruption could occur, crashes, data
59  * corruption, and worse, so DONT think about it until the day comes when InspIRCd is 100%
60  * gauranteed threadsafe!)
61  *
62  * For a diagram of this system please see http://wiki.inspircd.org/Mysql2
63  */
64
65 class SQLConnection;
66 class MySQLresult;
67 class DispatcherThread;
68
69 struct QQueueItem
70 {
71         SQLQuery* q;
72         std::string query;
73         SQLConnection* c;
74         QQueueItem(SQLQuery* Q, const std::string& S, SQLConnection* C) : q(Q), query(S), c(C) {}
75 };
76
77 struct RQueueItem
78 {
79         SQLQuery* q;
80         MySQLresult* r;
81         RQueueItem(SQLQuery* Q, MySQLresult* R) : q(Q), r(R) {}
82 };
83
84 typedef std::map<std::string, SQLConnection*> ConnMap;
85 typedef std::deque<QQueueItem> QueryQueue;
86 typedef std::deque<RQueueItem> ResultQueue;
87
88 /** MySQL module
89  *  */
90 class ModuleSQL : public Module
91 {
92  public:
93         DispatcherThread* Dispatcher;
94         QueryQueue qq;       // MUST HOLD MUTEX
95         ResultQueue rq;      // MUST HOLD MUTEX
96         ConnMap connections; // main thread only
97
98         ModuleSQL();
99         void init();
100         ~ModuleSQL();
101         void OnRehash(User* user);
102         void OnUnloadModule(Module* mod);
103         Version GetVersion();
104 };
105
106 class DispatcherThread : public SocketThread
107 {
108  private:
109         ModuleSQL* const Parent;
110  public:
111         DispatcherThread(ModuleSQL* CreatorModule) : Parent(CreatorModule) { }
112         ~DispatcherThread() { }
113         virtual void Run();
114         virtual void OnNotify();
115 };
116
117 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
118 #define mysql_field_count mysql_num_fields
119 #endif
120
121 /** Represents a mysql result set
122  */
123 class MySQLresult : public SQLResult
124 {
125  public:
126         SQLerror err;
127         int currentrow;
128         int rows;
129         std::vector<std::string> colnames;
130         std::vector<SQLEntries> fieldlists;
131
132         MySQLresult(MYSQL_RES* res, int affected_rows) : err(SQL_NO_ERROR), currentrow(0), rows(0)
133         {
134                 if (affected_rows >= 1)
135                 {
136                         rows = affected_rows;
137                         fieldlists.resize(rows);
138                 }
139                 unsigned int field_count = 0;
140                 if (res)
141                 {
142                         MYSQL_ROW row;
143                         int n = 0;
144                         while ((row = mysql_fetch_row(res)))
145                         {
146                                 if (fieldlists.size() < (unsigned int)rows+1)
147                                 {
148                                         fieldlists.resize(fieldlists.size()+1);
149                                 }
150                                 field_count = 0;
151                                 MYSQL_FIELD *fields = mysql_fetch_fields(res);
152                                 if(mysql_num_fields(res) == 0)
153                                         break;
154                                 if (fields && mysql_num_fields(res))
155                                 {
156                                         colnames.clear();
157                                         while (field_count < mysql_num_fields(res))
158                                         {
159                                                 std::string a = (fields[field_count].name ? fields[field_count].name : "");
160                                                 if (row[field_count])
161                                                         fieldlists[n].push_back(SQLEntry(row[field_count]));
162                                                 else
163                                                         fieldlists[n].push_back(SQLEntry());
164                                                 colnames.push_back(a);
165                                                 field_count++;
166                                         }
167                                         n++;
168                                 }
169                                 rows++;
170                         }
171                         mysql_free_result(res);
172                         res = NULL;
173                 }
174         }
175
176         MySQLresult(SQLerror& e) : err(e)
177         {
178
179         }
180
181         ~MySQLresult()
182         {
183         }
184
185         virtual int Rows()
186         {
187                 return rows;
188         }
189
190         virtual void GetCols(std::vector<std::string>& result)
191         {
192                 result.assign(colnames.begin(), colnames.end());
193         }
194
195         virtual SQLEntry GetValue(int row, int column)
196         {
197                 if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))
198                 {
199                         return fieldlists[row][column];
200                 }
201                 return SQLEntry();
202         }
203
204         virtual bool GetRow(SQLEntries& result)
205         {
206                 if (currentrow < rows)
207                 {
208                         result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
209                         currentrow++;
210                         return true;
211                 }
212                 else
213                 {
214                         result.clear();
215                         return false;
216                 }
217         }
218 };
219
220 /** Represents a connection to a mysql database
221  */
222 class SQLConnection : public SQLProvider
223 {
224  public:
225         reference<ConfigTag> config;
226         MYSQL *connection;
227         Mutex lock;
228
229         // This constructor creates an SQLConnection object with the given credentials, but does not connect yet.
230         SQLConnection(Module* p, ConfigTag* tag) : SQLProvider(p, "SQL/" + tag->getString("id")),
231                 config(tag), connection(NULL)
232         {
233         }
234
235         ~SQLConnection()
236         {
237                 Close();
238         }
239
240         // This method connects to the database using the credentials supplied to the constructor, and returns
241         // true upon success.
242         bool Connect()
243         {
244                 unsigned int timeout = 1;
245                 connection = mysql_init(connection);
246                 mysql_options(connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
247                 std::string host = config->getString("host");
248                 std::string user = config->getString("user");
249                 std::string pass = config->getString("pass");
250                 std::string dbname = config->getString("name");
251                 int port = config->getInt("port");
252                 bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0);
253                 if (!rv)
254                         return rv;
255                 std::string initquery;
256                 if (config->readString("initialquery", initquery))
257                 {
258                         mysql_query(connection,initquery.c_str());
259                 }
260                 return true;
261         }
262
263         ModuleSQL* Parent()
264         {
265                 return (ModuleSQL*)(Module*)creator;
266         }
267
268         MySQLresult* DoBlockingQuery(const std::string& query)
269         {
270
271                 /* Parse the command string and dispatch it to mysql */
272                 if (CheckConnection() && !mysql_real_query(connection, query.data(), query.length()))
273                 {
274                         /* Successfull query */
275                         MYSQL_RES* res = mysql_use_result(connection);
276                         unsigned long rows = mysql_affected_rows(connection);
277                         return new MySQLresult(res, rows);
278                 }
279                 else
280                 {
281                         /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
282                          * possible error numbers and error messages */
283                         SQLerror e(SQL_QREPLY_FAIL, ConvToStr(mysql_errno(connection)) + std::string(": ") + mysql_error(connection));
284                         return new MySQLresult(e);
285                 }
286         }
287
288         bool CheckConnection()
289         {
290                 if (!connection || mysql_ping(connection) != 0)
291                         return Connect();
292                 return true;
293         }
294
295         std::string GetError()
296         {
297                 return mysql_error(connection);
298         }
299
300         void Close()
301         {
302                 mysql_close(connection);
303         }
304
305         void submit(SQLQuery* q, const std::string& qs)
306         {
307                 Parent()->Dispatcher->LockQueue();
308                 Parent()->qq.push_back(QQueueItem(q, qs, this));
309                 Parent()->Dispatcher->UnlockQueueWakeup();
310         }
311
312         void submit(SQLQuery* call, const std::string& q, const ParamL& p)
313         {
314                 std::string res;
315                 unsigned int param = 0;
316                 for(std::string::size_type i = 0; i < q.length(); i++)
317                 {
318                         if (q[i] != '?')
319                                 res.push_back(q[i]);
320                         else
321                         {
322                                 if (param < p.size())
323                                 {
324                                         std::string parm = p[param++];
325                                         char buffer[MAXBUF];
326                                         mysql_escape_string(buffer, parm.c_str(), parm.length());
327 //                                      mysql_real_escape_string(connection, queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length());
328                                         res.append(buffer);
329                                 }
330                         }
331                 }
332                 submit(call, res);
333         }
334
335         void submit(SQLQuery* call, const std::string& q, const ParamM& p)
336         {
337                 std::string res;
338                 for(std::string::size_type i = 0; i < q.length(); i++)
339                 {
340                         if (q[i] != '$')
341                                 res.push_back(q[i]);
342                         else
343                         {
344                                 std::string field;
345                                 i++;
346                                 while (i < q.length() && isalnum(q[i]))
347                                         field.push_back(q[i++]);
348                                 i--;
349
350                                 ParamM::const_iterator it = p.find(field);
351                                 if (it != p.end())
352                                 {
353                                         std::string parm = it->second;
354                                         char buffer[MAXBUF];
355                                         mysql_escape_string(buffer, parm.c_str(), parm.length());
356                                         res.append(buffer);
357                                 }
358                         }
359                 }
360                 submit(call, res);
361         }
362 };
363
364 ModuleSQL::ModuleSQL()
365 {
366         Dispatcher = NULL;
367 }
368
369 void ModuleSQL::init()
370 {
371         Dispatcher = new DispatcherThread(this);
372         ServerInstance->Threads->Start(Dispatcher);
373
374         Implementation eventlist[] = { I_OnRehash, I_OnUnloadModule };
375         ServerInstance->Modules->Attach(eventlist, this, 2);
376
377         OnRehash(NULL);
378 }
379
380 ModuleSQL::~ModuleSQL()
381 {
382         if (Dispatcher)
383         {
384                 Dispatcher->join();
385                 Dispatcher->OnNotify();
386                 delete Dispatcher;
387         }
388         for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
389         {
390                 delete i->second;
391         }
392 }
393
394 void ModuleSQL::OnRehash(User* user)
395 {
396         ConnMap conns;
397         ConfigTagList tags = ServerInstance->Config->ConfTags("database");
398         for(ConfigIter i = tags.first; i != tags.second; i++)
399         {
400                 if (i->second->getString("module", "mysql") != "mysql")
401                         continue;
402                 std::string id = i->second->getString("id");
403                 ConnMap::iterator curr = connections.find(id);
404                 if (curr == connections.end())
405                 {
406                         SQLConnection* conn = new SQLConnection(this, i->second);
407                         conns.insert(std::make_pair(id, conn));
408                         ServerInstance->Modules->AddService(*conn);
409                 }
410                 else
411                 {
412                         conns.insert(*curr);
413                         connections.erase(curr);
414                 }
415         }
416
417         // now clean up the deleted databases
418         Dispatcher->LockQueue();
419         SQLerror err(SQL_BAD_DBID);
420         for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
421         {
422                 ServerInstance->Modules->DelService(*i->second);
423                 // it might be running a query on this database. Wait for that to complete
424                 i->second->lock.Lock();
425                 i->second->lock.Unlock();
426                 // now remove all active queries to this DB
427                 for(unsigned int j = qq.size() - 1; j >= 0; j--)
428                 {
429                         if (qq[j].c == i->second)
430                         {
431                                 qq[j].q->OnError(err);
432                                 delete qq[j].q;
433                                 qq.erase(qq.begin() + j);
434                         }
435                 }
436                 // finally, nuke the connection
437                 delete i->second;
438         }
439         Dispatcher->UnlockQueue();
440         connections.swap(conns);
441 }
442
443 void ModuleSQL::OnUnloadModule(Module* mod)
444 {
445         SQLerror err(SQL_BAD_DBID);
446         Dispatcher->LockQueue();
447         unsigned int i = qq.size();
448         while (i > 0)
449         {
450                 i--;
451                 if (qq[i].q->creator == mod)
452                 {
453                         if (i == 0)
454                         {
455                                 // need to wait until the query is done
456                                 // (the result will be discarded)
457                                 qq[i].c->lock.Lock();
458                                 qq[i].c->lock.Unlock();
459                         }
460                         qq[i].q->OnError(err);
461                         delete qq[i].q;
462                         qq.erase(qq.begin() + i);
463                 }
464         }
465         Dispatcher->UnlockQueue();
466         // clean up any result queue entries
467         Dispatcher->OnNotify();
468 }
469
470 Version ModuleSQL::GetVersion()
471 {
472         return Version("MySQL support", VF_VENDOR);
473 }
474
475 void DispatcherThread::Run()
476 {
477         this->LockQueue();
478         while (!this->GetExitFlag())
479         {
480                 if (!Parent->qq.empty())
481                 {
482                         QQueueItem i = Parent->qq.front();
483                         i.c->lock.Lock();
484                         this->UnlockQueue();
485                         MySQLresult* res = i.c->DoBlockingQuery(i.query);
486                         i.c->lock.Unlock();
487
488                         /*
489                          * At this point, the main thread could be working on:
490                          *  Rehash - delete i.c out from under us. We don't care about that.
491                          *  UnloadModule - delete i.q and the qq item. Need to avoid reporting results.
492                          */
493
494                         this->LockQueue();
495                         if (Parent->qq.front().q == i.q)
496                         {
497                                 Parent->qq.pop_front();
498                                 Parent->rq.push_back(RQueueItem(i.q, res));
499                                 NotifyParent();
500                         }
501                         else
502                         {
503                                 // UnloadModule ate the query
504                                 delete res;
505                         }
506                 }
507                 else
508                 {
509                         /* We know the queue is empty, we can safely hang this thread until
510                          * something happens
511                          */
512                         this->WaitForQueue();
513                 }
514         }
515         this->UnlockQueue();
516 }
517
518 void DispatcherThread::OnNotify()
519 {
520         // this could unlock during the dispatch, but OnResult isn't expected to take that long
521         this->LockQueue();
522         for(ResultQueue::iterator i = Parent->rq.begin(); i != Parent->rq.end(); i++)
523         {
524                 MySQLresult* res = i->r;
525                 if (res->err.id == SQL_NO_ERROR)
526                         i->q->OnResult(*res);
527                 else
528                         i->q->OnError(res->err);
529                 delete i->q;
530                 delete i->r;
531         }
532         Parent->rq.clear();
533         this->UnlockQueue();
534 }
535
536 MODULE_INIT(ModuleSQL)