summaryrefslogtreecommitdiff
path: root/src/modules/extra/m_sql.cpp
blob: 62d4096eb89218a8aaa2e6853c683021c3d1b8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */


#include <stdio.h>
#include <string>
#include <mysql/mysql.h>
#include "users.h"
#include "channels.h"
#include "modules.h"

/* $ModDesc: m_filter with regexps */
/* $CompileFlags: -I/usr/local/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */

/** SQLConnection represents one mysql session.
 * Each session has its own persistent connection to the database.
 */
class SQLConnection
{
 protected:

	MYSQL connection;
	MYSQL_RES *res;
	MYSQL_ROW row;
	std::string host;
	std::string user;
	std::string pass;
	std::string db;
	std::map<std::string,std::string> thisrow;

 public:

	// This constructor creates an SQLConnection object with the given credentials, and creates the underlying
	// MYSQL struct, but does not connect yet.
	SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb)
	{
		this->host = thishost;
		this->user = thisuser;
		this->pass = thispass;
		this->db = thisdb;
		mysql_init(&connection);
	}

	// This method connects to the database using the credentials supplied to the constructor, and returns
	// true upon success.
	bool Connect()
	{
		return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
	}

	// This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
	// multiple rows.
	bool QueryResult(std::string query)
	{
		char escaped_query[query.length()+1];
		mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
		int r = mysql_query(&connection, escaped_query);
		if (!r)
		{
			res = mysql_use_result(&connection);
		}
		return (!r);
	}

	// This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
	// the number of effected rows is returned in the return value.
	unsigned long QueryCount(std::string query)
	{
		char escaped_query[query.length()+1];
		mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
	        int r = mysql_query(&connection, escaped_query);
	        if (!r)
	        {
	                res = mysql_store_result(&connection);
	                unsigned long rows = mysql_affected_rows(&connection);
	                mysql_free_result(res);
	                return rows;
	        }
	        return 0;
	}

	// This method fetches a row, if available from the database. You must issue a query
	// using QueryResult() first! The row's values are returned as a map of std::string
	// where each item is keyed by the column name.
	std::map<std::string,std::string> GetRow()
	{
		thisrow.clear();
		if (res)
		{
			row = mysql_fetch_row(res);
			if (row)
			{
				unsigned int field_count = 0;
				if(mysql_field_count(&connection) == 0)
					return thisrow;
				MYSQL_FIELD *fields = mysql_fetch_fields(res);
				while (field_count < mysql_field_count(&connection))
				{
					thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
					field_count++;
				}
				return thisrow;
			}
		}
		return thisrow;
	}

	bool QueryDone()
	{
		if (res)
		{
			mysql_free_result(res);
			return true;
		}
		else return false;
	}

	std::string GetError()
	{
		return mysql_error(&connection);
	}

	~SQLConnection()
	{
		mysql_close(&connection);
	}
};


class ModuleSQL : public Module
{
	Server *Srv;
	ConfigReader *Conf;
 
 public:
	ModuleSQL()
	{
	}
	
	virtual ~ModuleSQL()
	{
	}
	
	virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
	{
	}
	
	virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
	{
	}
	
	virtual void OnRehash()
	{
	}
	
	virtual Version GetVersion()
	{
		return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
	}
	
};

// stuff down here is the module-factory stuff. For basic modules you can ignore this.

class ModuleSQLFactory : public ModuleFactory
{
 public:
	ModuleSQLFactory()
	{
	}
	
	~ModuleSQLFactory()
	{
	}
	
	virtual Module * CreateModule()
	{
		return new ModuleSQL;
	}
	
};


extern "C" void * init_module( void )
{
	return new ModuleSQLFactory;
}