File: mod_sql_mysql.c

package info (click to toggle)
spl 1.0~pre6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,364 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (266 lines) | stat: -rw-r--r-- 7,344 bytes parent folder | download | duplicates (3)
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
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_sql_mysql.c: Module for MySQL bindings
 */

/**
 * This module implements the MySQL database driver.
 *
 * Load this module (and the [[sql:]] module) and pass "mysql" as backend
 * driver name to [[sql:sql_connect()]];
 *
 * The string describing the database connection (the 2nd argument to
 * [[sql:sql_connect()]]) is a coma seperated list of the following tokens:
 *
 *	host={hostname}
 *	user={username}
 *	pass={password}
 *	sock={socket_filename}
 *	db={database_name}
 *	port={tcp_port_number}
 *
 *	compress             (set MySQL CLIENT_COMPRESS flag)
 *	ignore_space         (set MySQL CLIENT_IGNORE_SPACE flag)
 *	multi_statements     (set MySQL CLIENT_MULTI_STATEMENTS flag)
 *
 * Whitespaces are not allowed. E.g.:
 *
 *	var db = sql_connect("mysql", "host=dbserver,user=dbuser");
 *
 *	var r = sql(db, "show databases");
 *
 *	foreach i (r)
 *		debug pop r[i];
 *
 */

#include <mysql/mysql.h>
#include <stdlib.h>
#include <unistd.h>

#include "spl.h"
#include "mod_sql.h"

#include "compat.h"

extern void SPL_ABI(spl_mod_sql_mysql_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_sql_mysql_done)(struct spl_vm *vm, struct spl_module *mod);

struct mysql_backend_data {
	MYSQL mysql;
};

static struct spl_node *sql_mysql_query_callback(struct spl_task *task, void *backend_data, const char *query)
{
	struct mysql_backend_data *mbd = backend_data;
	struct spl_node *result = spl_get(0);

	MYSQL_ROW row;
	MYSQL_RES *res;
	MYSQL_FIELD *fields;
	unsigned int num_fields;

	if (mysql_query(&mbd->mysql, query))
		goto got_error;

	res = mysql_store_result(&mbd->mysql);

	if (res)
	{
		num_fields = mysql_num_fields(res);
		fields = mysql_fetch_fields(res);

		while ( (row = mysql_fetch_row(res)) )
		{
			struct spl_node *n = spl_get(0);

			for (unsigned int i=0; i < num_fields; i++)
			{
				char *name_base = strrchr(fields[i].name, '.');
				name_base = name_base ? name_base+1 : fields[i].name;

				spl_create(task, n, name_base, row[i] ?
						SPL_NEW_STRING_DUP(row[i]) : spl_get(0), SPL_CREATE_LOCAL);
			}

			spl_create(task, result, NULL, n, SPL_CREATE_LOCAL);
		}

		mysql_free_result(res);
	} else
	if(mysql_field_count(&mbd->mysql) != 0)
		goto got_error;

	return result;

got_error:
	spl_put(task->vm, result);
	spl_clib_exception(task, "SqlEx", "description",
		SPL_NEW_SPL_STRING(spl_string_printf(0, 0, 0,
			"MySQL: SQL Error on '%s': %s!\n",
			query, mysql_error(&mbd->mysql))),
		NULL);
	return 0;
}

static void sql_mysql_close_callback(struct spl_vm *vm UNUSED, void *backend_data)
{
	struct mysql_backend_data *mbd = backend_data;
	mysql_close(&mbd->mysql);
	free(mbd);
}

static void sql_mysql_open_callback(struct spl_task *task, struct spl_node *node, const char *data)
{
	const char *original_data = data;
	const char *config_host = 0;
	const char *config_user = 0;
	const char *config_pass = 0;
	const char *config_sock = 0;
	const char *config_db = 0;
	int config_port = 0;
	int config_flag = 0;

	while (*data) {
		int tok_name_len = strcspn(data, "=,");
		char *tok_name = my_alloca(tok_name_len+1);
		memcpy(tok_name, data, tok_name_len);
		tok_name[tok_name_len] = 0;
		data += tok_name_len;

		if (*data == '=')
		{
			data++;

			int tok_value_len = strcspn(data, ",");
			char *tok_value = my_alloca(tok_value_len+1);
			memcpy(tok_value, data, tok_value_len);
			tok_value[tok_value_len] = 0;
			data += tok_value_len;

			if (!strcmp("host", tok_name))
				config_host = tok_value;
			else
			if (!strcmp("user", tok_name))
				config_user = tok_value;
			else
			if (!strcmp("pass", tok_name))
				config_pass = tok_value;
			else
			if (!strcmp("sock", tok_name))
				config_sock = tok_value;
			else
			if (!strcmp("db", tok_name))
				config_db = tok_value;
			else
			if (!strcmp("port", tok_name))
				config_port = atoi(tok_value);
			else
				goto parser_error;
		} else
		if (!strcmp("compress", tok_name))
			config_flag |= CLIENT_COMPRESS;
		else
		if (!strcmp("ignore_space", tok_name))
			config_flag |= CLIENT_IGNORE_SPACE;
		else
		if (!strcmp("multi_statements", tok_name))
#ifdef CLIENT_MULTI_STATEMENTS
			config_flag |= CLIENT_MULTI_STATEMENTS;
#else
			config_flag |= 0;
#endif
		else {
parser_error:
			spl_clib_exception(task, "SqlEx", "description",
				SPL_NEW_SPL_STRING(spl_string_printf(0, 0, 0,
					"MySQL: Error parsing database "
					"connection description '%s'!\n",
					original_data)),
				NULL);
			return;
		}

		if (*data == ',')
			data++;
	}

	struct mysql_backend_data *mbd = malloc(sizeof(struct mysql_backend_data));

	mysql_init(&mbd->mysql);
	if (!mysql_real_connect(&mbd->mysql, config_host, config_user,
		config_pass, config_db, config_port, config_sock, config_flag))
	{
		spl_clib_exception(task, "SqlEx", "description",
			SPL_NEW_SPL_STRING(spl_string_printf(0, 0, 0,
				"MySQL: Can't open database %s: %s!\n",
				original_data, mysql_error(&mbd->mysql))),
			NULL);
		mysql_close(&mbd->mysql);
		free(mbd);
		return;
	}

	struct sql_hnode_data *hnd = malloc(sizeof(struct sql_hnode_data));

	hnd->backend_data = mbd;
	hnd->query_callback = sql_mysql_query_callback;
	hnd->close_callback = sql_mysql_close_callback;

	node->hnode_data = hnd;
}

/**
 * This function encodes a string to be used in an MySQL query. I.e. the string
 * will be put in single quotes and special characters inside the string is
 * quoted with backslash sequences.
 *
 * Always use this function instead of [[sql:encode_sql()]] for quoting strings
 * in MySQL queries unless you are running MySQL in the ANSI_QUOTES mode.
 *
 * This function is designed to be used with the encoding/quoting operator (::).
 */
// builtin encode_mysql(text)
static struct spl_node *handler_encode_mysql(struct spl_task *task, void *data UNUSED)
{
	char *text = spl_clib_get_string(task);
	int text_len = strlen(text);

	char *newtext = malloc(text_len*2+3);
	int newtext_len = mysql_escape_string(newtext+1, text, text_len);
	newtext = realloc(newtext, newtext_len+3);

	newtext[0] = newtext[newtext_len+1] = '\'';
	newtext[newtext_len+2] = 0;

	return SPL_NEW_STRING(newtext);
}

void SPL_ABI(spl_mod_sql_mysql_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore)
{
	if (!restore) spl_module_load(vm, "sql", 0);
	spl_clib_reg(vm, "encode_mysql", handler_encode_mysql, 0);
	sql_register_backend(vm, "mysql", sql_mysql_open_callback);
}

void SPL_ABI(spl_mod_sql_mysql_done)(struct spl_vm *vm, struct spl_module *mod UNUSED)
{
	sql_unregister_backend(vm, "mysql");
}