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 typedef std::vector<SQLConnection> ConnectionList;
166 class ModuleSQL : public Module
170 ConnectionList Connections;
173 void ConnectDatabases()
175 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
180 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
184 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
190 void LoadDatabases(ConfigReader* Conf)
192 Srv->Log(DEFAULT,"SQL: Loading database settings");
194 Srv->Log(DEBUG,"Cleared connections");
195 for (int j =0; j < Conf->Enumerate("database"); j++)
197 std::string db = Conf->ReadValue("database","name",j);
198 std::string user = Conf->ReadValue("database","username",j);
199 std::string pass = Conf->ReadValue("database","password",j);
200 std::string host = Conf->ReadValue("database","hostname",j);
201 std::string id = Conf->ReadValue("database","id",j);
202 Srv->Log(DEBUG,"Read database settings");
203 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
205 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
206 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
207 Connections.push_back(ThisSQL);
208 Srv->Log(DEBUG,"Pushed back connection");
214 void ResultType(SQLRequest *r, SQLResult *res)
216 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
218 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
220 bool xr = i->QueryResult(r->GetQuery());
223 res->SetType(SQL_ERROR);
224 res->SetError(i->GetError());
227 res->SetType(SQL_OK);
233 void CountType(SQLRequest *r, SQLResult* res)
235 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
237 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
239 res->SetType(SQL_COUNT);
240 res->SetCount(i->QueryCount(r->GetQuery()));
246 void DoneType(SQLRequest *r, SQLResult* res)
248 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
250 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
252 res->SetType(SQL_DONE);
254 res->SetType(SQL_ERROR);
259 void RowType(SQLRequest *r, SQLResult* res)
261 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
263 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
265 log(DEBUG,"*** FOUND MATCHING ROW");
266 std::map<std::string,std::string> row = i->GetRow();
268 res->SetType(SQL_ROW);
271 log(DEBUG,"ROW SIZE IS 0");
272 res->SetType(SQL_END);
279 char* OnRequest(Request* request)
283 SQLResult* Result = new SQLResult();
284 SQLRequest *r = (SQLRequest*)request->GetData();
285 switch (r->GetQueryType())
288 ResultType(r,Result);
300 return (char*)Result;
308 Conf = new ConfigReader();
319 virtual void OnRehash()
322 Conf = new ConfigReader();
326 virtual Version GetVersion()
328 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
333 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
335 class ModuleSQLFactory : public ModuleFactory
346 virtual Module * CreateModule()
348 return new ModuleSQL;
354 extern "C" void * init_module( void )
356 return new ModuleSQLFactory;