summaryrefslogtreecommitdiffabout
path: root/src/mysql.c
blob: 1391743d961192bad571db5322617372bb5f4e1c (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* This file is part of vmod-dbrw
   Copyright (C) 2013-2018 Sergey Poznyakoff

   Vmod-dbrw is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Vmod-dbrw is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with vmod-dbrw.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "dbrw.h"
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
#include <mysql/mysqld_error.h>

struct vmod_mysql_data
{
	MYSQL mysql;
	MYSQL_RES *result;
	char *buffer;
	size_t bufsize;
};
  

static void
check_errno(struct dbrw_connection *conn, char const *query)
{
	struct vmod_mysql_data *mp = conn->data;

	dbrw_error("query failed: %s", query);
	dbrw_error("%s", mysql_error(&mp->mysql));
	switch (mysql_errno(&mp->mysql)) {
	case CR_SERVER_GONE_ERROR:
	case CR_SERVER_LOST:
	case ER_SERVER_SHUTDOWN:
	case ER_ABORTING_CONNECTION:
		sql_disconnect(conn);
		if (conn->state == state_error) {
			conn->state = state_disabled;
			dbrw_error("disabling MySQL connection");
		}
		break;
	case ER_UNKNOWN_COM_ERROR:
	case ER_ACCESS_DENIED_ERROR:
	case ER_BAD_DB_ERROR:
	case ER_WRONG_DB_NAME:
	case ER_BAD_FIELD_ERROR:
	case ER_BAD_HOST_ERROR:
	case ER_BAD_TABLE_ERROR:
	case ER_WRONG_FIELD_SPEC:
	case ER_FIELD_SPECIFIED_TWICE:
	case ER_NO_SUCH_TABLE:
	case ER_NOT_ALLOWED_COMMAND:
		dbrw_error("disabling MySQL connection");
		sql_disconnect(conn);
		conn->state = state_disabled;
		break;
	case ER_EMPTY_QUERY:
	case ER_PARSE_ERROR:
	default:
		break;
	}
}

/* ************************************************************************* */
/* Interface routines */

static int
s_mysql_init(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = calloc(1, sizeof (*mp));
	if (!mp) {
		dbrw_error("not enough memory");
		return -1;
	}
	conn->data = mp;
	return 0;
}

static void
s_mysql_destroy(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	free(mp->buffer);
	free(mp);
	conn->data = NULL;
}
  

static int
s_mysql_connect(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	char *host, *socket_name = NULL;
	char *s;
	int port = 0;
	
	mysql_init(&mp->mysql);

	host = findparam(conn->conf->param, "server");
	if (host && host[0] == '/') {
		host = "localhost";
		socket_name = host;
	}

	s = findparam(conn->conf->param, "port");
	if (s)
		port = atoi(s);

	s = findparam(conn->conf->param, "config");
	if (s)
		mysql_options(&mp->mysql, MYSQL_READ_DEFAULT_FILE, s);

	s = findparam(conn->conf->param, "group");
	if (s)
		mysql_options(&mp->mysql, MYSQL_READ_DEFAULT_GROUP, s);

	s = findparam(conn->conf->param, "cacert");
	if (s)
		mysql_ssl_set(&mp->mysql, NULL, NULL, s, NULL, NULL);
	debug(conn->conf, 1, ("connecting to database"));
	if (!mysql_real_connect(&mp->mysql, 
				host,
				findparam(conn->conf->param, "user"),
				findparam(conn->conf->param, "password"),
				findparam(conn->conf->param, "database"),
				port,
				socket_name,
				CLIENT_MULTI_RESULTS)) {
		dbrw_error("cannot connect: %s", mysql_error(&mp->mysql));
		return -1;
	}
	debug(conn->conf, 1, ("connected to database"));
  
	return 0;
}

static int 
s_mysql_disconnect(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	mysql_close(&mp->mysql);
	return 0;
}

static int
s_mysql_query(struct dbrw_connection *conn, const char *query)
{
	struct vmod_mysql_data *mp = conn->data;
	int rc;
	int i;
	MYSQL *mysql;
	
	for (i = 0; i < 10; i++) {
		rc = mysql_query(&mp->mysql, query);
		if (rc) {
			check_errno(conn, query);
			if (conn->state != state_init)
				return -1;
			sql_connect(conn);
			if (conn->state != state_connected)
				return -1;
			continue;
		}
		mp->result = mysql_store_result(&mp->mysql);
		if (mp->result) {
			conn->state = state_result;
		        rc = 0;
		} else if (mysql_field_count(&mp->mysql)) {
			dbrw_error("cannot store result: %s",
				    mysql_error(&mp->mysql));
			conn->state = state_error;
			rc = 1;
		} else
			rc = 0;
		break;
	}
	return rc;
}

static int
s_mysql_free_result(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	mysql_free_result(mp->result);
	mp->result = NULL;
	return 0;
}

static unsigned
s_mysql_num_fields(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	return mysql_num_fields(mp->result);
}

static unsigned
s_mysql_num_tuples(struct dbrw_connection *conn)
{
	struct vmod_mysql_data *mp = conn->data;
	return mysql_num_rows(mp->result);
}

static const char *
s_mysql_get_column(struct dbrw_connection *conn, size_t nrow, size_t ncol)
{
	struct vmod_mysql_data *mp = conn->data;
	MYSQL_ROW row;

	if (nrow >= mysql_num_rows(mp->result) ||
	    ncol >= mysql_num_fields (mp->result))
		return NULL;
  
	mysql_data_seek(mp->result, nrow);
	row = mysql_fetch_row(mp->result);
	if (!row)
		return NULL;
	return row[ncol];
}

static char *
s_mysql_escape (struct dbrw_connection *conn, const char *arg)
{
	struct vmod_mysql_data *mp = conn->data;
	size_t len, size;
	char *p;

	switch (conn->state) {
	case state_connected:
	case state_result:
		break;

	case state_error:
	case state_init:
		if (sql_connect(conn))
			return NULL;
		break;

	default:
		return NULL;
	}

	len = strlen(arg);
	size = 2 * len + 1;
	if (mp->bufsize < size) {
		p = realloc(mp->buffer, size);
		if (!p) {
			dbrw_error("not enough memory");
			return NULL;
		}
		mp->buffer = p;
		mp->bufsize = size;
	}
			 
	mysql_real_escape_string(&mp->mysql, mp->buffer, arg, len);

	p = strdup(mp->buffer);
	if (!p)
		dbrw_error("not enough memory");
	return p;
}

static int
s_mysql_idle_timeout(struct dbrw_connection *conn)
{
	const char *res;
	int n;
	if (sql_connect(conn) || conn->state != state_connected)
		return -1;
	if (sql_query(conn,
		      "SHOW VARIABLES WHERE Variable_name = 'wait_timeout'"))
		return -1;
	res = sql_get_column(conn, 0, 1);
	if (res)
		n = atoi(res);
	else
		n = -1;
	sql_free_result(conn);
	return n;
}


struct dbrw_backend mysql_backend = {
	"mysql",
	s_mysql_init,
	s_mysql_destroy,
	s_mysql_connect,
	s_mysql_disconnect,
	s_mysql_escape,
	s_mysql_query,
	s_mysql_num_tuples,
	s_mysql_num_fields,
	s_mysql_free_result,
	s_mysql_get_column,
	s_mysql_idle_timeout
};

Return to:

Send suggestions and report system problems to the System administrator.