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 -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;
46 // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
47 // MYSQL struct, but does not connect yet.
48 SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb)
50 this->host = thishost;
51 this->user = thisuser;
52 this->pass = thispass;
54 mysql_init(&connection);
57 // This method connects to the database using the credentials supplied to the constructor, and returns
61 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
64 // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
66 bool QueryResult(std::string query)
68 char escaped_query[query.length()+1];
69 mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
70 int r = mysql_query(&connection, escaped_query);
73 res = mysql_use_result(&connection);
78 // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
79 // the number of effected rows is returned in the return value.
80 unsigned long QueryCount(std::string query)
82 char escaped_query[query.length()+1];
83 mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
84 int r = mysql_query(&connection, escaped_query);
87 res = mysql_store_result(&connection);
88 unsigned long rows = mysql_affected_rows(&connection);
89 mysql_free_result(res);
95 // This method fetches a row, if available from the database. You must issue a query
96 // using QueryResult() first! The row's values are returned as a map of std::string
97 // where each item is keyed by the column name.
98 std::map<std::string,std::string> GetRow()
103 row = mysql_fetch_row(res);
106 unsigned int field_count = 0;
107 if(mysql_field_count(&connection) == 0)
109 MYSQL_FIELD *fields = mysql_fetch_fields(res);
110 while (field_count < mysql_field_count(&connection))
112 thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
125 mysql_free_result(res);
131 std::string GetError()
133 return mysql_error(&connection);
138 mysql_close(&connection);
143 class ModuleSQL : public Module
157 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
161 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
165 virtual void OnRehash()
169 virtual Version GetVersion()
171 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
176 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
178 class ModuleSQLFactory : public ModuleFactory
189 virtual Module * CreateModule()
191 return new ModuleSQL;
197 extern "C" void * init_module( void )
199 return new ModuleSQLFactory;