1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire 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 if(mysql_field_count(&connection) == 0)
120 MYSQL_FIELD *fields = mysql_fetch_fields(res);
121 while (field_count < mysql_field_count(&connection))
123 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
136 mysql_free_result(res);
142 std::string GetError()
144 return mysql_error(&connection);
152 std::string GetHost()
174 typedef std::vector<SQLConnection> ConnectionList;
176 class ModuleSQL : public Module
180 ConnectionList Connections;
183 void ConnectDatabases()
185 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
190 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
194 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
200 void LoadDatabases(ConfigReader* ThisConf)
202 Srv->Log(DEFAULT,"SQL: Loading database settings");
204 Srv->Log(DEBUG,"Cleared connections");
205 for (int j =0; j < ThisConf->Enumerate("database"); j++)
207 std::string db = ThisConf->ReadValue("database","name",j);
208 std::string user = ThisConf->ReadValue("database","username",j);
209 std::string pass = ThisConf->ReadValue("database","password",j);
210 std::string host = ThisConf->ReadValue("database","hostname",j);
211 std::string id = ThisConf->ReadValue("database","id",j);
212 Srv->Log(DEBUG,"Read database settings");
213 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
215 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
216 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
217 Connections.push_back(ThisSQL);
218 Srv->Log(DEBUG,"Pushed back connection");
224 void ResultType(SQLRequest *r, SQLResult *res)
226 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
228 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
230 bool xr = i->QueryResult(r->GetQuery());
233 res->SetType(SQL_ERROR);
234 res->SetError(i->GetError());
237 res->SetType(SQL_OK);
243 void CountType(SQLRequest *r, SQLResult* res)
245 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
247 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
249 res->SetType(SQL_COUNT);
250 res->SetCount(i->QueryCount(r->GetQuery()));
256 void DoneType(SQLRequest *r, SQLResult* res)
258 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
260 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
262 res->SetType(SQL_DONE);
264 res->SetType(SQL_ERROR);
269 void RowType(SQLRequest *r, SQLResult* res)
271 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
273 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
275 log(DEBUG,"*** FOUND MATCHING ROW");
276 std::map<std::string,std::string> row = i->GetRow();
278 res->SetType(SQL_ROW);
281 log(DEBUG,"ROW SIZE IS 0");
282 res->SetType(SQL_END);
289 char* OnRequest(Request* request)
293 SQLResult* Result = new SQLResult();
294 SQLRequest *r = (SQLRequest*)request->GetData();
295 switch (r->GetQueryType())
298 ResultType(r,Result);
310 return (char*)Result;
315 ModuleSQL(Server* Me)
319 Conf = new ConfigReader();
329 virtual void OnRehash(std::string parameter)
332 Conf = new ConfigReader();
336 virtual Version GetVersion()
338 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
343 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
345 class ModuleSQLFactory : public ModuleFactory
356 virtual Module * CreateModule(Server* Me)
358 return new ModuleSQL(Me);
364 extern "C" void * init_module( void )
366 return new ModuleSQLFactory;