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.
33 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
34 #define mysql_field_count mysql_num_fields
48 std::map<std::string,std::string> thisrow;
54 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
55 // MYSQL struct, but does not connect yet.
56 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
59 this->host = thishost;
60 this->user = thisuser;
61 this->pass = thispass;
64 unsigned int timeout = 1;
65 mysql_init(&connection);
66 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
69 // This method connects to the database using the credentials supplied to the constructor, and returns
73 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
76 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
78 bool QueryResult(std::string query)
80 int r = mysql_query(&connection, query.c_str());
83 res = mysql_use_result(&connection);
88 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
89 // the number of effected rows is returned in the return value.
90 unsigned long QueryCount(std::string query)
92 int r = mysql_query(&connection, query.c_str());
95 res = mysql_store_result(&connection);
96 unsigned long rows = mysql_affected_rows(&connection);
97 mysql_free_result(res);
103 // This method fetches a row, if available from the database. You must issue a query
104 // using QueryResult() first! The row's values are returned as a map of std::string
105 // where each item is keyed by the column name.
106 std::map<std::string,std::string> GetRow()
111 row = mysql_fetch_row(res);
114 unsigned int field_count = 0;
115 if(mysql_field_count(&connection) == 0)
117 MYSQL_FIELD *fields = mysql_fetch_fields(res);
118 while (field_count < mysql_field_count(&connection))
120 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
133 mysql_free_result(res);
139 std::string GetError()
141 return mysql_error(&connection);
149 std::string GetHost()
171 typedef std::vector<SQLConnection> ConnectionList;
173 class ModuleSQL : public Module
177 ConnectionList Connections;
180 void ConnectDatabases()
182 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
187 Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
191 Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError());
197 void LoadDatabases(ConfigReader* ThisConf)
199 Srv->Log(DEFAULT,"SQL: Loading database settings");
201 Srv->Log(DEBUG,"Cleared connections");
202 for (int j =0; j < ThisConf->Enumerate("database"); j++)
204 std::string db = ThisConf->ReadValue("database","name",j);
205 std::string user = ThisConf->ReadValue("database","username",j);
206 std::string pass = ThisConf->ReadValue("database","password",j);
207 std::string host = ThisConf->ReadValue("database","hostname",j);
208 std::string id = ThisConf->ReadValue("database","id",j);
209 Srv->Log(DEBUG,"Read database settings");
210 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
212 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
213 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
214 Connections.push_back(ThisSQL);
215 Srv->Log(DEBUG,"Pushed back connection");
221 void ResultType(SQLRequest *r, SQLResult *res)
223 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
225 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
227 bool xr = i->QueryResult(r->GetQuery());
230 res->SetType(SQL_ERROR);
231 res->SetError(i->GetError());
234 res->SetType(SQL_OK);
240 void CountType(SQLRequest *r, SQLResult* res)
242 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
244 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
246 res->SetType(SQL_COUNT);
247 res->SetCount(i->QueryCount(r->GetQuery()));
253 void DoneType(SQLRequest *r, SQLResult* res)
255 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
257 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
259 res->SetType(SQL_DONE);
261 res->SetType(SQL_ERROR);
266 void RowType(SQLRequest *r, SQLResult* res)
268 for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
270 if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
272 log(DEBUG,"*** FOUND MATCHING ROW");
273 std::map<std::string,std::string> row = i->GetRow();
275 res->SetType(SQL_ROW);
278 log(DEBUG,"ROW SIZE IS 0");
279 res->SetType(SQL_END);
286 char* OnRequest(Request* request)
290 SQLResult* Result = new SQLResult();
291 SQLRequest *r = (SQLRequest*)request->GetData();
292 switch (r->GetQueryType())
295 ResultType(r,Result);
307 return (char*)Result;
315 Conf = new ConfigReader();
326 virtual void OnRehash()
329 Conf = new ConfigReader();
333 virtual Version GetVersion()
335 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
340 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
342 class ModuleSQLFactory : public ModuleFactory
353 virtual Module * CreateModule()
355 return new ModuleSQL;
361 extern "C" void * init_module( void )
363 return new ModuleSQLFactory;