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 * ---------------------------------------------------
25 #include "helperfuncs.h"
28 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
29 /* $CompileFlags: -I/usr/local/include/mysql -I/usr/include/mysql -I/usr/local/include -I/usr/include */
30 /* $LinkerFlags: -L/usr/local/lib/mysql -Wl,--rpath -Wl,/usr/local/lib/mysql -L/usr/lib/mysql -Wl,--rpath -Wl,/usr/lib/mysql -lmysqlclient */
32 /** SQLConnection represents one mysql session.
33 * Each session has its own persistent connection to the database.
36 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
37 #define mysql_field_count mysql_num_fields
51 std::map<std::string,std::string> thisrow;
57 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
58 // MYSQL struct, but does not connect yet.
59 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
62 this->host = thishost;
63 this->user = thisuser;
64 this->pass = thispass;
67 unsigned int timeout = 1;
68 mysql_init(&connection);
69 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
72 // This method connects to the database using the credentials supplied to the constructor, and returns
76 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
79 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
81 bool QueryResult(std::string query)
83 int r = mysql_query(&connection, query.c_str());
86 res = mysql_use_result(&connection);
91 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
92 // the number of effected rows is returned in the return value.
93 unsigned long QueryCount(std::string query)
95 int r = mysql_query(&connection, query.c_str());
98 res = mysql_store_result(&connection);
99 unsigned long rows = mysql_affected_rows(&connection);
100 mysql_free_result(res);
106 // This method fetches a row, if available from the database. You must issue a query
107 // using QueryResult() first! The row's values are returned as a map of std::string
108 // where each item is keyed by the column name.
109 std::map<std::string,std::string> GetRow()
114 row = mysql_fetch_row(res);
117 unsigned int field_count = 0;
118 MYSQL_FIELD *fields = mysql_fetch_fields(res);
119 if(mysql_field_count(&connection) == 0)
121 if (fields && mysql_field_count(&connection))
123 while (field_count < mysql_field_count(&connection))
125 std::string a = (fields[field_count].name ? fields[field_count].name : "");
126 std::string b = (row[field_count] ? row[field_count] : "");
141 mysql_free_result(res);
147 std::string GetError()
149 return mysql_error(&connection);
157 std::string GetHost()
179 typedef std::vector<SQLConnection> ConnectionList;
181 class ModuleSQL : public Module
185 ConnectionList Connections;
188 void ConnectDatabases()
190 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
195 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
199 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
205 void LoadDatabases(ConfigReader* ThisConf)
207 Srv->Log(DEFAULT,"SQL: Loading database settings");
209 Srv->Log(DEBUG,"Cleared connections");
210 for (int j =0; j < ThisConf->Enumerate("database"); j++)
212 std::string db = ThisConf->ReadValue("database","name",j);
213 std::string user = ThisConf->ReadValue("database","username",j);
214 std::string pass = ThisConf->ReadValue("database","password",j);
215 std::string host = ThisConf->ReadValue("database","hostname",j);
216 std::string id = ThisConf->ReadValue("database","id",j);
217 Srv->Log(DEBUG,"Read database settings");
218 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
220 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
221 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
222 Connections.push_back(ThisSQL);
223 Srv->Log(DEBUG,"Pushed back connection");
229 void ResultType(SQLRequest *r, SQLResult *res)
231 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
233 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
235 bool xr = i->QueryResult(r->GetQuery());
238 res->SetType(SQL_ERROR);
239 res->SetError(i->GetError());
242 res->SetType(SQL_OK);
248 void CountType(SQLRequest *r, SQLResult* res)
250 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
252 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
254 res->SetType(SQL_COUNT);
255 res->SetCount(i->QueryCount(r->GetQuery()));
261 void DoneType(SQLRequest *r, SQLResult* res)
263 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
265 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
267 res->SetType(SQL_DONE);
269 res->SetType(SQL_ERROR);
274 void RowType(SQLRequest *r, SQLResult* res)
276 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
278 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
280 log(DEBUG,"*** FOUND MATCHING ROW");
281 std::map<std::string,std::string> row = i->GetRow();
283 res->SetType(SQL_ROW);
286 log(DEBUG,"ROW SIZE IS 0");
287 res->SetType(SQL_END);
294 void Implements(char* List)
296 List[I_OnRehash] = List[I_OnRequest] = 1;
299 char* OnRequest(Request* request)
303 SQLResult* Result = new SQLResult();
304 SQLRequest *r = (SQLRequest*)request->GetData();
305 switch (r->GetQueryType())
308 ResultType(r,Result);
320 return (char*)Result;
325 ModuleSQL(Server* Me)
329 Conf = new ConfigReader();
339 virtual void OnRehash(std::string parameter)
342 Conf = new ConfigReader();
346 virtual Version GetVersion()
348 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
353 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
355 class ModuleSQLFactory : public ModuleFactory
366 virtual Module * CreateModule(Server* Me)
368 return new ModuleSQL(Me);
374 extern "C" void * init_module( void )
376 return new ModuleSQLFactory;