1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
26 #include "helperfuncs.h"
29 /* VERSION 2 API: With nonblocking (threaded) requests */
31 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
32 /* $CompileFlags: `mysql_config --include` */
33 /* $LinkerFlags: `mysql_config --libs_r` `perl ../mysql_rpath.pl` */
38 extern InspIRCd* ServerInstance;
39 typedef std::map<std::string, SQLConnection*> ConnMap;
42 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
43 #define mysql_field_count mysql_num_fields
46 class QueryQueue : public classbase
49 typedef std::deque<SQLrequest> ReqDeque;
51 ReqDeque priority; /* The priority queue */
52 ReqDeque normal; /* The 'normal' queue */
53 enum { PRI, NOR, NON } which; /* Which queue the currently active element is at the front of */
61 void push(const SQLrequest &q)
63 log(DEBUG, "QueryQueue::push(): Adding %s query to queue: %s", ((q.pri) ? "priority" : "non-priority"), q.query.q.c_str());
66 priority.push_back(q);
73 if((which == PRI) && priority.size())
77 else if((which == NOR) && normal.size())
85 /* Silently do nothing if there was no element to pop() */
93 return priority.front();
95 return normal.front();
100 return priority.front();
106 return normal.front();
109 /* This will probably result in a segfault,
110 * but the caller should have checked totalsize()
111 * first so..meh - moron :p
114 return priority.front();
118 std::pair<int, int> size()
120 return std::make_pair(priority.size(), normal.size());
125 return priority.size() + normal.size();
128 void PurgeModule(Module* mod)
130 DoPurgeModule(mod, priority);
131 DoPurgeModule(mod, normal);
135 void DoPurgeModule(Module* mod, ReqDeque& q)
137 for(ReqDeque::iterator iter = q.begin(); iter != q.end(); iter++)
139 if(iter->GetSource() == mod)
141 if(iter->id == front().id)
143 /* It's the currently active query.. :x */
144 iter->SetSource(NULL);
148 /* It hasn't been executed yet..just remove it */
149 iter = q.erase(iter);
158 class SQLConnection : public classbase
169 std::map<std::string,std::string> thisrow;
175 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
176 // MYSQL struct, but does not connect yet.
177 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
179 this->Enabled = true;
180 this->host = thishost;
181 this->user = thisuser;
182 this->pass = thispass;
187 // This method connects to the database using the credentials supplied to the constructor, and returns
188 // true upon success.
191 unsigned int timeout = 1;
192 mysql_init(&connection);
193 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
194 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
197 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
199 bool QueryResult(std::string query)
201 if (!CheckConnection()) return false;
203 int r = mysql_query(&connection, query.c_str());
206 res = mysql_use_result(&connection);
211 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
212 // the number of effected rows is returned in the return value.
213 long QueryCount(std::string query)
215 /* If the connection is down, we return a negative value - New to 1.1 */
216 if (!CheckConnection()) return -1;
218 int r = mysql_query(&connection, query.c_str());
221 res = mysql_store_result(&connection);
222 unsigned long rows = mysql_affected_rows(&connection);
223 mysql_free_result(res);
229 // This method fetches a row, if available from the database. You must issue a query
230 // using QueryResult() first! The row's values are returned as a map of std::string
231 // where each item is keyed by the column name.
232 std::map<std::string,std::string> GetRow()
237 row = mysql_fetch_row(res);
240 unsigned int field_count = 0;
241 MYSQL_FIELD *fields = mysql_fetch_fields(res);
242 if(mysql_field_count(&connection) == 0)
244 if (fields && mysql_field_count(&connection))
246 while (field_count < mysql_field_count(&connection))
248 std::string a = (fields[field_count].name ? fields[field_count].name : "");
249 std::string b = (row[field_count] ? row[field_count] : "");
264 mysql_free_result(res);
271 bool ConnectionLost()
274 return (mysql_ping(&connection) != 0);
279 bool CheckConnection()
281 if (ConnectionLost()) {
287 std::string GetError()
289 return mysql_error(&connection);
297 std::string GetHost()
321 void ConnectDatabases(Server* Srv)
323 for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
326 if (i->second->Connect())
328 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->second->GetHost());
332 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
333 i->second->Disable();
339 void LoadDatabases(ConfigReader* ThisConf, Server* Srv)
341 Srv->Log(DEFAULT,"SQL: Loading database settings");
343 Srv->Log(DEBUG,"Cleared connections");
344 for (int j =0; j < ThisConf->Enumerate("database"); j++)
346 std::string db = ThisConf->ReadValue("database","name",j);
347 std::string user = ThisConf->ReadValue("database","username",j);
348 std::string pass = ThisConf->ReadValue("database","password",j);
349 std::string host = ThisConf->ReadValue("database","hostname",j);
350 std::string id = ThisConf->ReadValue("database","id",j);
351 Srv->Log(DEBUG,"Read database settings");
352 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
354 SQLConnection* ThisSQL = new SQLConnection(host,user,pass,db,atoi(id.c_str()));
355 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL->GetHost());
356 Connections[id] = ThisSQL;
357 Srv->Log(DEBUG,"Pushed back connection");
360 ConnectDatabases(Srv);
363 void* DispatcherThread(void* arg);
365 class ModuleSQL : public Module
370 pthread_t Dispatcher;
372 void Implements(char* List)
374 List[I_OnRehash] = List[I_OnRequest] = 1;
377 char* OnRequest(Request* request)
382 ModuleSQL(Server* Me)
386 Conf = new ConfigReader();
387 pthread_attr_t attribs;
388 pthread_attr_init(&attribs);
389 pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
390 if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
392 log(DEBUG,"m_mysql: Failed to create dispatcher thread: %s", strerror(errno));
401 virtual void OnRehash(const std::string ¶meter)
403 /* TODO: set rehash bool here, which makes the dispatcher thread rehash at next opportunity */
406 virtual Version GetVersion()
408 return Version(1,1,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
413 void* DispatcherThread(void* arg)
415 ModuleSQL* thismodule = (ModuleSQL*)arg;
416 LoadDatabases(thismodule->Conf, thismodule->Srv);
422 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
424 class ModuleSQLFactory : public ModuleFactory
435 virtual Module * CreateModule(Server* Me)
437 return new ModuleSQL(Me);
443 extern "C" void * init_module( void )
445 return new ModuleSQLFactory;