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 * ---------------------------------------------------
26 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
27 /* $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 */
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 unsigned int timeout = 1;
60 mysql_init(&connection);
61 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
64 // This method connects to the database using the credentials supplied to the constructor, and returns
68 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
71 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
73 bool QueryResult(std::string query)
75 int r = mysql_query(&connection, query.c_str());
78 res = mysql_use_result(&connection);
83 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
84 // the number of effected rows is returned in the return value.
85 unsigned long QueryCount(std::string query)
87 int r = mysql_query(&connection, query.c_str());
90 res = mysql_store_result(&connection);
91 unsigned long rows = mysql_affected_rows(&connection);
92 mysql_free_result(res);
98 // This method fetches a row, if available from the database. You must issue a query
99 // using QueryResult() first! The row's values are returned as a map of std::string
100 // where each item is keyed by the column name.
101 std::map<std::string,std::string> GetRow()
106 row = mysql_fetch_row(res);
109 unsigned int field_count = 0;
110 if(mysql_field_count(&connection) == 0)
112 MYSQL_FIELD *fields = mysql_fetch_fields(res);
113 while (field_count < mysql_field_count(&connection))
115 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
128 mysql_free_result(res);
134 std::string GetError()
136 return mysql_error(&connection);
144 std::string GetHost()
166 typedef std::vector<SQLConnection> ConnectionList;
168 class ModuleSQL : public Module
172 ConnectionList Connections;
175 void ConnectDatabases()
177 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
182 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
186 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
192 void LoadDatabases(ConfigReader* ThisConf)
194 Srv->Log(DEFAULT,"SQL: Loading database settings");
196 Srv->Log(DEBUG,"Cleared connections");
197 for (int j =0; j < ThisConf->Enumerate("database"); j++)
199 std::string db = ThisConf->ReadValue("database","name",j);
200 std::string user = ThisConf->ReadValue("database","username",j);
201 std::string pass = ThisConf->ReadValue("database","password",j);
202 std::string host = ThisConf->ReadValue("database","hostname",j);
203 std::string id = ThisConf->ReadValue("database","id",j);
204 Srv->Log(DEBUG,"Read database settings");
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);
210 Srv->Log(DEBUG,"Pushed back connection");
216 void ResultType(SQLRequest *r, SQLResult *res)
218 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
220 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
222 bool xr = i->QueryResult(r->GetQuery());
225 res->SetType(SQL_ERROR);
226 res->SetError(i->GetError());
229 res->SetType(SQL_OK);
235 void CountType(SQLRequest *r, SQLResult* res)
237 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
239 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
241 res->SetType(SQL_COUNT);
242 res->SetCount(i->QueryCount(r->GetQuery()));
248 void DoneType(SQLRequest *r, SQLResult* res)
250 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
252 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
254 res->SetType(SQL_DONE);
256 res->SetType(SQL_ERROR);
261 void RowType(SQLRequest *r, SQLResult* res)
263 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
265 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
267 log(DEBUG,"*** FOUND MATCHING ROW");
268 std::map<std::string,std::string> row = i->GetRow();
270 res->SetType(SQL_ROW);
273 log(DEBUG,"ROW SIZE IS 0");
274 res->SetType(SQL_END);
281 char* OnRequest(Request* request)
285 SQLResult* Result = new SQLResult();
286 SQLRequest *r = (SQLRequest*)request->GetData();
287 switch (r->GetQueryType())
290 ResultType(r,Result);
302 return (char*)Result;
310 Conf = new ConfigReader();
321 virtual void OnRehash()
324 Conf = new ConfigReader();
328 virtual Version GetVersion()
330 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
335 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
337 class ModuleSQLFactory : public ModuleFactory
348 virtual Module * CreateModule()
350 return new ModuleSQL;
356 extern "C" void * init_module( void )
358 return new ModuleSQLFactory;