Mon, 01 Dec 2008 07:22:44 +0000
Added new typechecks and errors
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; | |
10 | 62 | char *errstr = NULL; |
1 | 63 | |
64 | int p; | |
65 | ||
3 | 66 | if (!statement->stmt) { |
67 | lua_pushboolean(L, 0); | |
4 | 68 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 69 | return 2; |
70 | } | |
71 | ||
72 | expected_params = mysql_stmt_param_count(statement->stmt); | |
73 | ||
74 | if (expected_params != num_bind_params) { | |
75 | /* | |
10 | 76 | * mysql_stmt_bind_param does not handle this condition, |
3 | 77 | * and the client library will segfault if these do no match |
78 | */ | |
79 | lua_pushboolean(L, 0); | |
4 | 80 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); |
3 | 81 | return 2; |
82 | } | |
83 | ||
1 | 84 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); |
85 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
86 | ||
87 | for (p = 2; p <= n; p++) { | |
88 | int type = lua_type(L, p); | |
89 | int i = p - 2; | |
90 | ||
91 | 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
|
92 | 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
|
93 | double *num = NULL; |
9 | 94 | int *boolean = NULL; |
10 | 95 | char err[64]; |
1 | 96 | |
97 | switch(type) { | |
98 | case LUA_TNIL: | |
99 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
100 | bind[i].is_null = (my_bool*)1; | |
101 | break; | |
102 | ||
9 | 103 | case LUA_TBOOLEAN: |
104 | boolean = (int *)malloc(sizeof(int)); | |
105 | *boolean = lua_toboolean(L, p); | |
106 | bind[i].buffer_type = MYSQL_TYPE_LONG; | |
107 | bind[i].is_null = (my_bool*)0; | |
108 | bind[i].buffer = (char *)boolean; | |
109 | bind[i].length = 0; | |
110 | break; | |
111 | ||
1 | 112 | 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
|
113 | /* |
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
|
114 | * 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
|
115 | * 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
|
116 | */ |
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
|
117 | num = (double *)malloc(sizeof(double)); |
9 | 118 | *num = lua_tonumber(L, p); |
1 | 119 | |
120 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
121 | 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
|
122 | bind[i].buffer = (char *)num; |
1 | 123 | bind[i].length = 0; |
124 | break; | |
125 | ||
126 | 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
|
127 | str_len = malloc(sizeof(size_t)); |
9 | 128 | str = lua_tolstring(L, p, str_len); |
1 | 129 | |
130 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
131 | bind[i].is_null = (my_bool*)0; | |
132 | 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
|
133 | bind[i].length = str_len; |
1 | 134 | break; |
135 | ||
136 | default: | |
10 | 137 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); |
138 | errstr = err; | |
139 | error_message = DBI_ERR_BINDING_PARAMS; | |
1 | 140 | goto cleanup; |
141 | } | |
142 | } | |
143 | ||
144 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 145 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 146 | goto cleanup; |
147 | } | |
148 | ||
149 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 150 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 151 | goto cleanup; |
152 | } | |
153 | ||
154 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
155 | ||
156 | 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
|
157 | 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
|
158 | 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
|
159 | |
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 | 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
|
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 | * 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
|
163 | * 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
|
164 | * 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
|
165 | * 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
|
166 | * 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
|
167 | */ |
9 | 168 | if (bind[i].buffer_type == MYSQL_TYPE_DOUBLE || bind[i].buffer_type == MYSQL_TYPE_LONG) { |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
169 | 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
|
170 | 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
|
171 | } 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
|
172 | 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
|
173 | 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
|
174 | } |
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
|
175 | } |
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
|
176 | |
1 | 177 | 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
|
178 | } |
1 | 179 | |
180 | if (error_message) { | |
3 | 181 | lua_pushboolean(L, 0); |
10 | 182 | lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt)); |
3 | 183 | return 2; |
1 | 184 | } |
185 | ||
186 | statement->metadata = metadata; | |
187 | ||
3 | 188 | lua_pushboolean(L, 1); |
1 | 189 | return 1; |
190 | } | |
191 | ||
192 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
193 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
194 | int column_count; | |
195 | MYSQL_BIND *bind = NULL; | |
196 | const char *error_message = NULL; | |
197 | ||
198 | if (!statement->stmt) { | |
4 | 199 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 200 | return 0; |
201 | } | |
202 | ||
203 | if (!statement->metadata) { | |
4 | 204 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 205 | return 0; |
1 | 206 | } |
207 | ||
208 | if (!statement->metadata) { | |
209 | lua_pushnil(L); | |
210 | return 1; | |
211 | } | |
212 | ||
213 | column_count = mysql_num_fields(statement->metadata); | |
214 | ||
215 | if (column_count > 0) { | |
216 | int i; | |
217 | MYSQL_FIELD *fields; | |
218 | ||
219 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
220 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
221 | ||
222 | fields = mysql_fetch_fields(statement->metadata); | |
223 | ||
224 | for (i = 0; i < column_count; i++) { | |
225 | unsigned int length = fields[i].length; | |
226 | ||
227 | char *buffer = (char *)malloc(length); | |
228 | memset(buffer, 0, length); | |
229 | ||
230 | bind[i].buffer_type = fields[i].type; | |
231 | bind[i].buffer = buffer; | |
232 | bind[i].buffer_length = length; | |
233 | } | |
234 | ||
235 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 236 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 237 | goto cleanup; |
238 | } | |
239 | ||
240 | if (!mysql_stmt_fetch(statement->stmt)) { | |
241 | int d = 1; | |
242 | ||
243 | lua_newtable(L); | |
244 | for (i = 0; i < column_count; i++) { | |
245 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
246 | const char *name = fields[i].name; | |
247 | ||
248 | if (lua_push == LUA_PUSH_NIL) { | |
249 | if (named_columns) { | |
250 | LUA_PUSH_ATTRIB_NIL(name); | |
251 | } else { | |
252 | LUA_PUSH_ARRAY_NIL(d); | |
253 | } | |
254 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
255 | if (named_columns) { | |
256 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
257 | } else { | |
258 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
259 | } | |
260 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
261 | if (named_columns) { | |
262 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
263 | } else { | |
264 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
265 | } | |
266 | } else if (lua_push == LUA_PUSH_STRING) { | |
267 | if (named_columns) { | |
268 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
269 | } else { | |
270 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
271 | } | |
272 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
273 | if (named_columns) { | |
274 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
275 | } else { | |
276 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
277 | } | |
278 | } else { | |
4 | 279 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 280 | } |
281 | } | |
282 | } else { | |
283 | lua_pushnil(L); | |
284 | } | |
285 | } | |
286 | ||
287 | cleanup: | |
288 | if (bind) { | |
289 | int i; | |
290 | ||
291 | for (i = 0; i < column_count; i++) { | |
292 | free(bind[i].buffer); | |
293 | } | |
294 | ||
295 | free(bind); | |
296 | } | |
297 | ||
298 | if (error_message) { | |
299 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
300 | return 0; | |
301 | } | |
302 | ||
303 | return 1; | |
304 | } | |
305 | ||
2 | 306 | /* |
307 | * array = statement:fetch() | |
308 | */ | |
1 | 309 | static int statement_fetch(lua_State *L) { |
310 | return statement_fetch_impl(L, 0); | |
311 | } | |
312 | ||
2 | 313 | /* |
314 | * hashmap = statement:fetchtable() | |
315 | */ | |
1 | 316 | static int statement_fetchtable(lua_State *L) { |
317 | return statement_fetch_impl(L, 1); | |
318 | } | |
319 | ||
2 | 320 | /* |
321 | * __gc | |
322 | */ | |
1 | 323 | static int statement_gc(lua_State *L) { |
324 | /* always free the handle */ | |
325 | statement_close(L); | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
331 | unsigned long sql_len = strlen(sql_query); | |
332 | ||
333 | statement_t *statement = NULL; | |
334 | ||
335 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
336 | ||
337 | if (!stmt) { | |
3 | 338 | lua_pushnil(L); |
4 | 339 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 340 | return 2; |
1 | 341 | } |
342 | ||
343 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 344 | lua_pushnil(L); |
4 | 345 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 346 | return 2; |
1 | 347 | } |
348 | ||
349 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
350 | statement->mysql = conn->mysql; | |
351 | statement->stmt = stmt; | |
352 | statement->metadata = NULL; | |
353 | ||
354 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
355 | lua_setmetatable(L, -2); | |
356 | ||
357 | return 1; | |
358 | } | |
359 | ||
360 | int dbd_mysql_statement(lua_State *L) { | |
2 | 361 | static const luaL_Reg statement_methods[] = { |
362 | {"close", statement_close}, | |
363 | {"execute", statement_execute}, | |
364 | {"fetch", statement_fetch}, | |
365 | {"fetchtable", statement_fetchtable}, | |
366 | {NULL, NULL} | |
367 | }; | |
368 | ||
369 | static const luaL_Reg statement_class_methods[] = { | |
370 | {NULL, NULL} | |
371 | }; | |
372 | ||
1 | 373 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
374 | luaL_register(L, 0, statement_methods); | |
375 | lua_pushvalue(L,-1); | |
376 | lua_setfield(L, -2, "__index"); | |
377 | ||
378 | lua_pushcfunction(L, statement_gc); | |
379 | lua_setfield(L, -2, "__gc"); | |
380 | ||
381 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
382 | ||
383 | return 1; | |
384 | } |