]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sql.cpp
Added preliminary m_sql.cpp
[user/henk/code/inspircd.git] / src / modules / extra / m_sql.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17
18 #include <stdio.h>
19 #include <string>
20 #include <mysql/mysql.h>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24
25 /* $ModDesc: m_filter with regexps */
26 /* $CompileFlags: -I/usr/local/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */
27
28 /** SQLConnection represents one mysql session.
29  * Each session has its own persistent connection to the database.
30  */
31 class SQLConnection
32 {
33  protected:
34
35         MYSQL connection;
36         MYSQL_RES *res;
37         MYSQL_ROW row;
38         std::string host;
39         std::string user;
40         std::string pass;
41         std::string db;
42         std::map<std::string,std::string> thisrow;
43
44  public:
45
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)
49         {
50                 this->host = thishost;
51                 this->user = thisuser;
52                 this->pass = thispass;
53                 this->db = thisdb;
54                 mysql_init(&connection);
55         }
56
57         // This method connects to the database using the credentials supplied to the constructor, and returns
58         // true upon success.
59         bool Connect()
60         {
61                 return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
62         }
63
64         // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
65         // multiple rows.
66         bool QueryResult(std::string query)
67         {
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);
71                 if (!r)
72                 {
73                         res = mysql_use_result(&connection);
74                 }
75                 return (!r);
76         }
77
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)
81         {
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);
85                 if (!r)
86                 {
87                         res = mysql_store_result(&connection);
88                         unsigned long rows = mysql_affected_rows(&connection);
89                         mysql_free_result(res);
90                         return rows;
91                 }
92                 return 0;
93         }
94
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()
99         {
100                 thisrow.clear();
101                 if (res)
102                 {
103                         row = mysql_fetch_row(res);
104                         if (row)
105                         {
106                                 unsigned int field_count = 0;
107                                 if(mysql_field_count(&connection) == 0)
108                                         return thisrow;
109                                 MYSQL_FIELD *fields = mysql_fetch_fields(res);
110                                 while (field_count < mysql_field_count(&connection))
111                                 {
112                                         thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
113                                         field_count++;
114                                 }
115                                 return thisrow;
116                         }
117                 }
118                 return thisrow;
119         }
120
121         bool QueryDone()
122         {
123                 if (res)
124                 {
125                         mysql_free_result(res);
126                         return true;
127                 }
128                 else return false;
129         }
130
131         std::string GetError()
132         {
133                 return mysql_error(&connection);
134         }
135
136         ~SQLConnection()
137         {
138                 mysql_close(&connection);
139         }
140 };
141
142
143 class ModuleSQL : public Module
144 {
145         Server *Srv;
146         ConfigReader *Conf;
147  
148  public:
149         ModuleSQL()
150         {
151         }
152         
153         virtual ~ModuleSQL()
154         {
155         }
156         
157         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
158         {
159         }
160         
161         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
162         {
163         }
164         
165         virtual void OnRehash()
166         {
167         }
168         
169         virtual Version GetVersion()
170         {
171                 return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
172         }
173         
174 };
175
176 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
177
178 class ModuleSQLFactory : public ModuleFactory
179 {
180  public:
181         ModuleSQLFactory()
182         {
183         }
184         
185         ~ModuleSQLFactory()
186         {
187         }
188         
189         virtual Module * CreateModule()
190         {
191                 return new ModuleSQL;
192         }
193         
194 };
195
196
197 extern "C" void * init_module( void )
198 {
199         return new ModuleSQLFactory;
200 }
201