Sat, 29 Nov 2008 00:51:14 +0000
Bugfix - the size of strings for bind params pointed to the same memory
1 | 1 | #include "dbd_mysql.h" |
2 | ||
3 | static lua_push_type_t mysql_to_lua_push(unsigned int mysql_type) { | |
4 | lua_push_type_t lua_type; | |
5 | ||
6 | switch(mysql_type) { | |
7 | case MYSQL_TYPE_NULL: | |
8 | lua_type = LUA_PUSH_NIL; | |
9 | break; | |
10 | ||
11 | case MYSQL_TYPE_TINY: | |
12 | case MYSQL_TYPE_SHORT: | |
13 | case MYSQL_TYPE_LONG: | |
14 | lua_type = LUA_PUSH_INTEGER; | |
15 | break; | |
16 | ||
17 | case MYSQL_TYPE_DOUBLE: | |
18 | case MYSQL_TYPE_LONGLONG: | |
19 | lua_type = LUA_PUSH_NUMBER; | |
20 | break; | |
21 | ||
22 | default: | |
23 | lua_type = LUA_PUSH_STRING; | |
24 | } | |
25 | ||
26 | return lua_type; | |
27 | } | |
28 | ||
2 | 29 | /* |
30 | * success = statement:close() | |
31 | */ | |
1 | 32 | static int statement_close(lua_State *L) { |
33 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
34 | ||
35 | if (statement->metadata) { | |
36 | mysql_free_result(statement->metadata); | |
3 | 37 | statement->metadata = NULL; |
1 | 38 | } |
39 | ||
40 | if (statement->stmt) { | |
3 | 41 | mysql_stmt_close(statement->stmt); |
42 | statement->stmt = NULL; | |
1 | 43 | } |
44 | ||
3 | 45 | lua_pushboolean(L, 1); |
1 | 46 | return 1; |
47 | } | |
48 | ||
2 | 49 | /* |
3 | 50 | * success,err = statement:execute(...) |
2 | 51 | */ |
1 | 52 | static int statement_execute(lua_State *L) { |
53 | int n = lua_gettop(L); | |
54 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
55 | int num_bind_params = n - 1; | |
3 | 56 | int expected_params; |
1 | 57 | |
58 | MYSQL_BIND *bind = NULL; | |
59 | MYSQL_RES *metadata = NULL; | |
60 | ||
61 | char *error_message = NULL; | |
62 | ||
63 | int p; | |
64 | ||
3 | 65 | if (!statement->stmt) { |
66 | lua_pushboolean(L, 0); | |
4 | 67 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 68 | return 2; |
69 | } | |
70 | ||
71 | expected_params = mysql_stmt_param_count(statement->stmt); | |
72 | ||
73 | if (expected_params != num_bind_params) { | |
74 | /* | |
75 | * mysql_stmt_bind_param does not handle this conndition, | |
76 | * and the client library will segfault if these do no match | |
77 | */ | |
78 | lua_pushboolean(L, 0); | |
4 | 79 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); |
3 | 80 | return 2; |
81 | } | |
82 | ||
1 | 83 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); |
84 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
85 | ||
86 | for (p = 2; p <= n; p++) { | |
87 | int type = lua_type(L, p); | |
88 | int i = p - 2; | |
89 | ||
90 | const char *str = NULL; | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
91 | size_t *str_len = NULL; |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
92 | double *num = NULL; |
1 | 93 | |
94 | switch(type) { | |
95 | case LUA_TNIL: | |
96 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
97 | bind[i].is_null = (my_bool*)1; | |
98 | break; | |
99 | ||
100 | case LUA_TNUMBER: | |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
101 | /* |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
102 | * num needs to be it's own |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
103 | * memory here |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
104 | */ |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
105 | num = (double *)malloc(sizeof(double)); |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
106 | *num = luaL_checknumber(L, p); |
1 | 107 | |
108 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
109 | bind[i].is_null = (my_bool*)0; | |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
110 | bind[i].buffer = (char *)num; |
1 | 111 | bind[i].length = 0; |
112 | break; | |
113 | ||
114 | case LUA_TSTRING: | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
115 | str_len = malloc(sizeof(size_t)); |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
116 | str = luaL_checklstring(L, p, str_len); |
1 | 117 | |
118 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
119 | bind[i].is_null = (my_bool*)0; | |
120 | bind[i].buffer = (char *)str; | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
121 | bind[i].length = str_len; |
1 | 122 | break; |
123 | ||
124 | default: | |
4 | 125 | error_message = DBI_ERR_BINDING_UNKNOWN; |
1 | 126 | goto cleanup; |
127 | } | |
128 | } | |
129 | ||
130 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 131 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 132 | goto cleanup; |
133 | } | |
134 | ||
135 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 136 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 137 | goto cleanup; |
138 | } | |
139 | ||
140 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
141 | ||
142 | cleanup: | |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
143 | if (bind) { |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
144 | int i; |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
145 | |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
146 | for (i = 0; i < num_bind_params; i++) { |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
147 | /* |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
148 | * Free the memory associated with |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
149 | * the allocation of double and string |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
150 | * bind params. If the interface are |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
151 | * extended with other types they |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
152 | * will need to be added here |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
153 | */ |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
154 | if (bind[i].buffer_type == MYSQL_TYPE_DOUBLE) { |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
155 | if (bind[i].buffer) |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
156 | free(bind[i].buffer); |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
157 | } else if (bind[i].buffer_type == MYSQL_TYPE_STRING) { |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
158 | if (bind[i].length) |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
159 | free(bind[i].length); |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
160 | } |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
161 | } |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
162 | |
1 | 163 | free(bind); |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
164 | } |
1 | 165 | |
166 | if (error_message) { | |
3 | 167 | lua_pushboolean(L, 0); |
168 | lua_pushfstring(L, error_message, mysql_stmt_error(statement->stmt)); | |
169 | return 2; | |
1 | 170 | } |
171 | ||
172 | statement->metadata = metadata; | |
173 | ||
3 | 174 | lua_pushboolean(L, 1); |
1 | 175 | return 1; |
176 | } | |
177 | ||
178 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
179 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
180 | int column_count; | |
181 | MYSQL_BIND *bind = NULL; | |
182 | const char *error_message = NULL; | |
183 | ||
184 | if (!statement->stmt) { | |
4 | 185 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 186 | return 0; |
187 | } | |
188 | ||
189 | if (!statement->metadata) { | |
4 | 190 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 191 | return 0; |
1 | 192 | } |
193 | ||
194 | if (!statement->metadata) { | |
195 | lua_pushnil(L); | |
196 | return 1; | |
197 | } | |
198 | ||
199 | column_count = mysql_num_fields(statement->metadata); | |
200 | ||
201 | if (column_count > 0) { | |
202 | int i; | |
203 | MYSQL_FIELD *fields; | |
204 | ||
205 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
206 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
207 | ||
208 | fields = mysql_fetch_fields(statement->metadata); | |
209 | ||
210 | for (i = 0; i < column_count; i++) { | |
211 | unsigned int length = fields[i].length; | |
212 | ||
213 | char *buffer = (char *)malloc(length); | |
214 | memset(buffer, 0, length); | |
215 | ||
216 | bind[i].buffer_type = fields[i].type; | |
217 | bind[i].buffer = buffer; | |
218 | bind[i].buffer_length = length; | |
219 | } | |
220 | ||
221 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 222 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 223 | goto cleanup; |
224 | } | |
225 | ||
226 | if (!mysql_stmt_fetch(statement->stmt)) { | |
227 | int d = 1; | |
228 | ||
229 | lua_newtable(L); | |
230 | for (i = 0; i < column_count; i++) { | |
231 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
232 | const char *name = fields[i].name; | |
233 | ||
234 | if (lua_push == LUA_PUSH_NIL) { | |
235 | if (named_columns) { | |
236 | LUA_PUSH_ATTRIB_NIL(name); | |
237 | } else { | |
238 | LUA_PUSH_ARRAY_NIL(d); | |
239 | } | |
240 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
241 | if (named_columns) { | |
242 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
243 | } else { | |
244 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
245 | } | |
246 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
247 | if (named_columns) { | |
248 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
249 | } else { | |
250 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
251 | } | |
252 | } else if (lua_push == LUA_PUSH_STRING) { | |
253 | if (named_columns) { | |
254 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
255 | } else { | |
256 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
257 | } | |
258 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
259 | if (named_columns) { | |
260 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
261 | } else { | |
262 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
263 | } | |
264 | } else { | |
4 | 265 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 266 | } |
267 | } | |
268 | } else { | |
269 | lua_pushnil(L); | |
270 | } | |
271 | } | |
272 | ||
273 | cleanup: | |
274 | if (bind) { | |
275 | int i; | |
276 | ||
277 | for (i = 0; i < column_count; i++) { | |
278 | free(bind[i].buffer); | |
279 | } | |
280 | ||
281 | free(bind); | |
282 | } | |
283 | ||
284 | if (error_message) { | |
285 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
286 | return 0; | |
287 | } | |
288 | ||
289 | return 1; | |
290 | } | |
291 | ||
2 | 292 | /* |
293 | * array = statement:fetch() | |
294 | */ | |
1 | 295 | static int statement_fetch(lua_State *L) { |
296 | return statement_fetch_impl(L, 0); | |
297 | } | |
298 | ||
2 | 299 | /* |
300 | * hashmap = statement:fetchtable() | |
301 | */ | |
1 | 302 | static int statement_fetchtable(lua_State *L) { |
303 | return statement_fetch_impl(L, 1); | |
304 | } | |
305 | ||
2 | 306 | /* |
307 | * __gc | |
308 | */ | |
1 | 309 | static int statement_gc(lua_State *L) { |
310 | /* always free the handle */ | |
311 | statement_close(L); | |
312 | ||
313 | return 0; | |
314 | } | |
315 | ||
316 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
317 | unsigned long sql_len = strlen(sql_query); | |
318 | ||
319 | statement_t *statement = NULL; | |
320 | ||
321 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
322 | ||
323 | if (!stmt) { | |
3 | 324 | lua_pushnil(L); |
4 | 325 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 326 | return 2; |
1 | 327 | } |
328 | ||
329 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 330 | lua_pushnil(L); |
4 | 331 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 332 | return 2; |
1 | 333 | } |
334 | ||
335 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
336 | statement->mysql = conn->mysql; | |
337 | statement->stmt = stmt; | |
338 | statement->metadata = NULL; | |
339 | ||
340 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
341 | lua_setmetatable(L, -2); | |
342 | ||
343 | return 1; | |
344 | } | |
345 | ||
346 | int dbd_mysql_statement(lua_State *L) { | |
2 | 347 | static const luaL_Reg statement_methods[] = { |
348 | {"close", statement_close}, | |
349 | {"execute", statement_execute}, | |
350 | {"fetch", statement_fetch}, | |
351 | {"fetchtable", statement_fetchtable}, | |
352 | {NULL, NULL} | |
353 | }; | |
354 | ||
355 | static const luaL_Reg statement_class_methods[] = { | |
356 | {NULL, NULL} | |
357 | }; | |
358 | ||
1 | 359 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
360 | luaL_register(L, 0, statement_methods); | |
361 | lua_pushvalue(L,-1); | |
362 | lua_setfield(L, -2, "__index"); | |
363 | ||
364 | lua_pushcfunction(L, statement_gc); | |
365 | lua_setfield(L, -2, "__gc"); | |
366 | ||
367 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
368 | ||
369 | return 1; | |
370 | } |