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: -pthread `mysql_config --include` */
33 /* $LinkerFlags: -pthread `mysql_config --libs_r` `perl ../mysql_rpath.pl` */
35 /** SQLConnection represents one mysql session.
36 * Each session has its own persistent connection to the database.
39 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
40 #define mysql_field_count mysql_num_fields
43 class SQLConnection : public classbase
54 std::map<std::string,std::string> thisrow;
60 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
61 // MYSQL struct, but does not connect yet.
62 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
65 this->host = thishost;
66 this->user = thisuser;
67 this->pass = thispass;
72 // This method connects to the database using the credentials supplied to the constructor, and returns
76 unsigned int timeout = 1;
77 mysql_init(&connection);
78 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
79 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
82 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
84 bool QueryResult(std::string query)
86 if (!CheckConnection()) return false;
88 int r = mysql_query(&connection, query.c_str());
91 res = mysql_use_result(&connection);
96 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
97 // the number of effected rows is returned in the return value.
98 long QueryCount(std::string query)
100 /* If the connection is down, we return a negative value - New to 1.1 */
101 if (!CheckConnection()) return -1;
103 int r = mysql_query(&connection, query.c_str());
106 res = mysql_store_result(&connection);
107 unsigned long rows = mysql_affected_rows(&connection);
108 mysql_free_result(res);
114 // This method fetches a row, if available from the database. You must issue a query
115 // using QueryResult() first! The row's values are returned as a map of std::string
116 // where each item is keyed by the column name.
117 std::map<std::string,std::string> GetRow()
122 row = mysql_fetch_row(res);
125 unsigned int field_count = 0;
126 MYSQL_FIELD *fields = mysql_fetch_fields(res);
127 if(mysql_field_count(&connection) == 0)
129 if (fields && mysql_field_count(&connection))
131 while (field_count < mysql_field_count(&connection))
133 std::string a = (fields[field_count].name ? fields[field_count].name : "");
134 std::string b = (row[field_count] ? row[field_count] : "");
149 mysql_free_result(res);
156 bool ConnectionLost()
159 return (mysql_ping(&connection) != 0);
164 bool CheckConnection()
166 if (ConnectionLost()) {
172 std::string GetError()
174 return mysql_error(&connection);
182 std::string GetHost()
204 typedef std::vector<SQLConnection> ConnectionList;
206 class ModuleSQL : public Module
210 ConnectionList Connections;
213 void ConnectDatabases()
215 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
220 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
224 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
230 void LoadDatabases(ConfigReader* ThisConf)
232 Srv->Log(DEFAULT,"SQL: Loading database settings");
234 Srv->Log(DEBUG,"Cleared connections");
235 for (int j =0; j < ThisConf->Enumerate("database"); j++)
237 std::string db = ThisConf->ReadValue("database","name",j);
238 std::string user = ThisConf->ReadValue("database","username",j);
239 std::string pass = ThisConf->ReadValue("database","password",j);
240 std::string host = ThisConf->ReadValue("database","hostname",j);
241 std::string id = ThisConf->ReadValue("database","id",j);
242 Srv->Log(DEBUG,"Read database settings");
243 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
245 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
246 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
247 Connections.push_back(ThisSQL);
248 Srv->Log(DEBUG,"Pushed back connection");
254 void ResultType(SQLRequest *r, SQLResult *res)
256 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
258 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
260 bool xr = i->QueryResult(r->GetQuery());
263 res->SetType(SQL_ERROR);
264 res->SetError(i->GetError());
267 res->SetType(SQL_OK);
273 void CountType(SQLRequest *r, SQLResult* res)
275 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
277 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
279 res->SetType(SQL_COUNT);
280 res->SetCount(i->QueryCount(r->GetQuery()));
286 void DoneType(SQLRequest *r, SQLResult* res)
288 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
290 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
292 res->SetType(SQL_DONE);
294 res->SetType(SQL_ERROR);
299 void RowType(SQLRequest *r, SQLResult* res)
301 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
303 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
305 log(DEBUG,"*** FOUND MATCHING ROW");
306 std::map<std::string,std::string> row = i->GetRow();
308 res->SetType(SQL_ROW);
311 log(DEBUG,"ROW SIZE IS 0");
312 res->SetType(SQL_END);
319 void Implements(char* List)
321 List[I_OnRehash] = List[I_OnRequest] = 1;
324 char* OnRequest(Request* request)
328 SQLResult* Result = new SQLResult();
329 SQLRequest *r = (SQLRequest*)request->GetData();
330 switch (r->GetQueryType())
333 ResultType(r,Result);
345 return (char*)Result;
350 ModuleSQL(Server* Me)
354 Conf = new ConfigReader();
364 virtual void OnRehash(const std::string ¶meter)
367 Conf = new ConfigReader();
371 virtual Version GetVersion()
373 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
378 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
380 class ModuleSQLFactory : public ModuleFactory
391 virtual Module * CreateModule(Server* Me)
393 return new ModuleSQL(Me);
399 extern "C" void * init_module( void )
401 return new ModuleSQLFactory;