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 -L/usr/local/lib/mysql -L/usr/lib/mysql -L/usr/local/lib -lmysqlclient */
31 /** SQLConnection represents one mysql session.
32 * Each session has its own persistent connection to the database.
35 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
36 #define mysql_field_count mysql_num_fields
50 std::map<std::string,std::string> thisrow;
56 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
57 // MYSQL struct, but does not connect yet.
58 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
61 this->host = thishost;
62 this->user = thisuser;
63 this->pass = thispass;
66 unsigned int timeout = 1;
67 mysql_init(&connection);
68 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
71 // This method connects to the database using the credentials supplied to the constructor, and returns
75 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
78 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
80 bool QueryResult(std::string query)
82 int r = mysql_query(&connection, query.c_str());
85 res = mysql_use_result(&connection);
90 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
91 // the number of effected rows is returned in the return value.
92 unsigned long QueryCount(std::string query)
94 int r = mysql_query(&connection, query.c_str());
97 res = mysql_store_result(&connection);
98 unsigned long rows = mysql_affected_rows(&connection);
99 mysql_free_result(res);
105 // This method fetches a row, if available from the database. You must issue a query
106 // using QueryResult() first! The row's values are returned as a map of std::string
107 // where each item is keyed by the column name.
108 std::map<std::string,std::string> GetRow()
113 row = mysql_fetch_row(res);
116 unsigned int field_count = 0;
117 if(mysql_field_count(&connection) == 0)
119 MYSQL_FIELD *fields = mysql_fetch_fields(res);
120 while (field_count < mysql_field_count(&connection))
122 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
135 mysql_free_result(res);
141 std::string GetError()
143 return mysql_error(&connection);
151 std::string GetHost()
173 typedef std::vector<SQLConnection> ConnectionList;
175 class ModuleSQL : public Module
179 ConnectionList Connections;
182 void ConnectDatabases()
184 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
189 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
193 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
199 void LoadDatabases(ConfigReader* ThisConf)
201 Srv->Log(DEFAULT,"SQL: Loading database settings");
203 Srv->Log(DEBUG,"Cleared connections");
204 for (int j =0; j < ThisConf->Enumerate("database"); j++)
206 std::string db = ThisConf->ReadValue("database","name",j);
207 std::string user = ThisConf->ReadValue("database","username",j);
208 std::string pass = ThisConf->ReadValue("database","password",j);
209 std::string host = ThisConf->ReadValue("database","hostname",j);
210 std::string id = ThisConf->ReadValue("database","id",j);
211 Srv->Log(DEBUG,"Read database settings");
212 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
214 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
215 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
216 Connections.push_back(ThisSQL);
217 Srv->Log(DEBUG,"Pushed back connection");
223 void ResultType(SQLRequest *r, SQLResult *res)
225 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
227 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
229 bool xr = i->QueryResult(r->GetQuery());
232 res->SetType(SQL_ERROR);
233 res->SetError(i->GetError());
236 res->SetType(SQL_OK);
242 void CountType(SQLRequest *r, SQLResult* res)
244 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
246 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
248 res->SetType(SQL_COUNT);
249 res->SetCount(i->QueryCount(r->GetQuery()));
255 void DoneType(SQLRequest *r, SQLResult* res)
257 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
259 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
261 res->SetType(SQL_DONE);
263 res->SetType(SQL_ERROR);
268 void RowType(SQLRequest *r, SQLResult* res)
270 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
272 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
274 log(DEBUG,"*** FOUND MATCHING ROW");
275 std::map<std::string,std::string> row = i->GetRow();
277 res->SetType(SQL_ROW);
280 log(DEBUG,"ROW SIZE IS 0");
281 res->SetType(SQL_END);
288 char* OnRequest(Request* request)
292 SQLResult* Result = new SQLResult();
293 SQLRequest *r = (SQLRequest*)request->GetData();
294 switch (r->GetQueryType())
297 ResultType(r,Result);
309 return (char*)Result;
317 Conf = new ConfigReader();
328 virtual void OnRehash()
331 Conf = new ConfigReader();
335 virtual Version GetVersion()
337 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
342 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
344 class ModuleSQLFactory : public ModuleFactory
355 virtual Module * CreateModule()
357 return new ModuleSQL;
363 extern "C" void * init_module( void )
365 return new ModuleSQLFactory;