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 * ---------------------------------------------------
20 #include <mysql/mysql.h>
26 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
27 /* $CompileFlags: -I/usr/local/include -I/usr/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */
29 /** SQLConnection represents one mysql session.
30 * Each session has its own persistent connection to the database.
43 std::map<std::string,std::string> thisrow;
49 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
50 // MYSQL struct, but does not connect yet.
51 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
54 this->host = thishost;
55 this->user = thisuser;
56 this->pass = thispass;
59 mysql_init(&connection);
62 // This method connects to the database using the credentials supplied to the constructor, and returns
66 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
69 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
71 bool QueryResult(std::string query)
73 int r = mysql_query(&connection, query.c_str());
76 res = mysql_use_result(&connection);
81 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
82 // the number of effected rows is returned in the return value.
83 unsigned long QueryCount(std::string query)
85 int r = mysql_query(&connection, query.c_str());
88 res = mysql_store_result(&connection);
89 unsigned long rows = mysql_affected_rows(&connection);
90 mysql_free_result(res);
96 // This method fetches a row, if available from the database. You must issue a query
97 // using QueryResult() first! The row's values are returned as a map of std::string
98 // where each item is keyed by the column name.
99 std::map<std::string,std::string> GetRow()
104 row = mysql_fetch_row(res);
107 unsigned int field_count = 0;
108 if(mysql_field_count(&connection) == 0)
110 MYSQL_FIELD *fields = mysql_fetch_fields(res);
111 while (field_count < mysql_field_count(&connection))
113 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
126 mysql_free_result(res);
132 std::string GetError()
134 return mysql_error(&connection);
142 std::string GetHost()
164 mysql_close(&connection);
168 typedef std::vector<SQLConnection> ConnectionList;
170 class ModuleSQL : public Module
174 ConnectionList Connections;
177 void ConnectDatabases()
179 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
184 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
188 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
194 void LoadDatabases(ConfigReader* Conf)
196 Srv->Log(DEFAULT,"SQL: Loading database settings");
198 for (int j =0; j < Conf->Enumerate("database"); j++)
200 std::string db = Conf->ReadValue("database","name",j);
201 std::string user = Conf->ReadValue("database","username",j);
202 std::string pass = Conf->ReadValue("database","password",j);
203 std::string host = Conf->ReadValue("database","hostname",j);
204 std::string id = Conf->ReadValue("database","id",j);
205 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
207 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
208 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
209 Connections.push_back(ThisSQL);
215 void ResultType(SQLRequest *r, SQLResult *res)
217 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
219 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
221 bool xr = i->QueryResult(r->GetQuery());
224 res->SetType(SQL_ERROR);
225 res->SetError(i->GetError());
228 res->SetType(SQL_OK);
234 void CountType(SQLRequest *r, SQLResult* res)
236 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
238 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
240 res->SetType(SQL_COUNT);
241 res->SetCount(i->QueryCount(r->GetQuery()));
247 void DoneType(SQLRequest *r, SQLResult* res)
249 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
251 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
253 res->SetType(SQL_DONE);
255 res->SetType(SQL_ERROR);
260 void RowType(SQLRequest *r, SQLResult* res)
262 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
264 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
266 log(DEBUG,"*** FOUND MATCHING ROW");
267 std::map<std::string,std::string> row = i->GetRow();
269 res->SetType(SQL_ROW);
272 log(DEBUG,"ROW SIZE IS 0");
273 res->SetType(SQL_END);
280 char* OnRequest(Request* request)
284 SQLResult* Result = new SQLResult();
285 SQLRequest *r = (SQLRequest*)request->GetData();
286 switch (r->GetQueryType())
289 ResultType(r,Result);
301 return (char*)Result;
309 Conf = new ConfigReader();
320 virtual void OnRehash()
323 Conf = new ConfigReader();
327 virtual Version GetVersion()
329 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
334 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
336 class ModuleSQLFactory : public ModuleFactory
347 virtual Module * CreateModule()
349 return new ModuleSQL;
355 extern "C" void * init_module( void )
357 return new ModuleSQLFactory;