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>
25 /* $ModDesc: m_filter with regexps */
26 /* $CompileFlags: -I/usr/local/include -I/usr/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */
28 /** SQLConnection represents one mysql session.
29 * Each session has its own persistent connection to the database.
42 std::map<std::string,std::string> thisrow;
48 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
49 // MYSQL struct, but does not connect yet.
50 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb, long myid)
53 this->host = thishost;
54 this->user = thisuser;
55 this->pass = thispass;
58 mysql_init(&connection);
61 // This method connects to the database using the credentials supplied to the constructor, and returns
65 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
68 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
70 bool QueryResult(std::string query)
72 char escaped_query[query.length()+1];
73 mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
74 int r = mysql_query(&connection, escaped_query);
77 res = mysql_use_result(&connection);
82 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
83 // the number of effected rows is returned in the return value.
84 unsigned long QueryCount(std::string query)
86 char escaped_query[query.length()+1];
87 mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
88 int r = mysql_query(&connection, escaped_query);
91 res = mysql_store_result(&connection);
92 unsigned long rows = mysql_affected_rows(&connection);
93 mysql_free_result(res);
99 // This method fetches a row, if available from the database. You must issue a query
100 // using QueryResult() first! The row's values are returned as a map of std::string
101 // where each item is keyed by the column name.
102 std::map<std::string,std::string> GetRow()
107 row = mysql_fetch_row(res);
110 unsigned int field_count = 0;
111 if(mysql_field_count(&connection) == 0)
113 MYSQL_FIELD *fields = mysql_fetch_fields(res);
114 while (field_count < mysql_field_count(&connection))
116 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
129 mysql_free_result(res);
135 std::string GetError()
137 return mysql_error(&connection);
145 std::string GetHost()
167 mysql_close(&connection);
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* Conf)
199 Srv->Log(DEFAULT,"SQL: Loading database settings");
201 for (int j =0; j < Conf->Enumerate("database"); j++)
203 std::string db = Conf->ReadValue("database","name",j);
204 std::string user = Conf->ReadValue("database","username",j);
205 std::string pass = Conf->ReadValue("database","password",j);
206 std::string host = Conf->ReadValue("database","hostname",j);
207 std::string id = Conf->ReadValue("database","id",j);
208 if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
210 SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
211 Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
212 Connections.push_back(ThisSQL);
221 Conf = new ConfigReader();
232 virtual void OnRehash()
235 Conf = new ConfigReader();
239 virtual Version GetVersion()
241 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
246 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
248 class ModuleSQLFactory : public ModuleFactory
259 virtual Module * CreateModule()
261 return new ModuleSQL;
267 extern "C" void * init_module( void )
269 return new ModuleSQLFactory;