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 * ---------------------------------------------------
24 #include "helperfuncs.h"
27 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
28 /* $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 */
30 /** SQLConnection represents one mysql session.
31 * Each session has its own persistent connection to the database.
34 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
35 #define mysql_field_count mysql_num_fields
49 std::map<std::string,std::string> thisrow;
55 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
56 // MYSQL struct, but does not connect yet.
57 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
60 this->host = thishost;
61 this->user = thisuser;
62 this->pass = thispass;
65 unsigned int timeout = 1;
66 mysql_init(&connection);
67 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
70 // This method connects to the database using the credentials supplied to the constructor, and returns
74 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
77 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
79 bool QueryResult(std::string query)
81 int r = mysql_query(&connection, query.c_str());
84 res = mysql_use_result(&connection);
89 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
90 // the number of effected rows is returned in the return value.
91 unsigned long QueryCount(std::string query)
93 int r = mysql_query(&connection, query.c_str());
96 res = mysql_store_result(&connection);
97 unsigned long rows = mysql_affected_rows(&connection);
98 mysql_free_result(res);
104 // This method fetches a row, if available from the database. You must issue a query
105 // using QueryResult() first! The row's values are returned as a map of std::string
106 // where each item is keyed by the column name.
107 std::map<std::string,std::string> GetRow()
112 row = mysql_fetch_row(res);
115 unsigned int field_count = 0;
116 if(mysql_field_count(&connection) == 0)
118 MYSQL_FIELD *fields = mysql_fetch_fields(res);
119 while (field_count < mysql_field_count(&connection))
121 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
134 mysql_free_result(res);
140 std::string GetError()
142 return mysql_error(&connection);
150 std::string GetHost()
172 typedef std::vector<SQLConnection> ConnectionList;
174 class ModuleSQL : public Module
178 ConnectionList Connections;
181 void ConnectDatabases()
183 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
188 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
192 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
198 void LoadDatabases(ConfigReader* ThisConf)
200 Srv->Log(DEFAULT,"SQL: Loading database settings");
202 Srv->Log(DEBUG,"Cleared connections");
203 for (int j =0; j < ThisConf->Enumerate("database"); j++)
205 std::string db = ThisConf->ReadValue("database","name",j);
206 std::string user = ThisConf->ReadValue("database","username",j);
207 std::string pass = ThisConf->ReadValue("database","password",j);
208 std::string host = ThisConf->ReadValue("database","hostname",j);
209 std::string id = ThisConf->ReadValue("database","id",j);
210 Srv->Log(DEBUG,"Read database settings");
211 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
213 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
214 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
215 Connections.push_back(ThisSQL);
216 Srv->Log(DEBUG,"Pushed back connection");
222 void ResultType(SQLRequest *r, SQLResult *res)
224 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
226 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
228 bool xr = i->QueryResult(r->GetQuery());
231 res->SetType(SQL_ERROR);
232 res->SetError(i->GetError());
235 res->SetType(SQL_OK);
241 void CountType(SQLRequest *r, SQLResult* res)
243 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
245 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
247 res->SetType(SQL_COUNT);
248 res->SetCount(i->QueryCount(r->GetQuery()));
254 void DoneType(SQLRequest *r, SQLResult* res)
256 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
258 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
260 res->SetType(SQL_DONE);
262 res->SetType(SQL_ERROR);
267 void RowType(SQLRequest *r, SQLResult* res)
269 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
271 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
273 log(DEBUG,"*** FOUND MATCHING ROW");
274 std::map<std::string,std::string> row = i->GetRow();
276 res->SetType(SQL_ROW);
279 log(DEBUG,"ROW SIZE IS 0");
280 res->SetType(SQL_END);
287 char* OnRequest(Request* request)
291 SQLResult* Result = new SQLResult();
292 SQLRequest *r = (SQLRequest*)request->GetData();
293 switch (r->GetQueryType())
296 ResultType(r,Result);
308 return (char*)Result;
316 Conf = new ConfigReader();
327 virtual void OnRehash()
330 Conf = new ConfigReader();
334 virtual Version GetVersion()
336 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
341 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
343 class ModuleSQLFactory : public ModuleFactory
354 virtual Module * CreateModule()
356 return new ModuleSQL;
362 extern "C" void * init_module( void )
364 return new ModuleSQLFactory;