284 lua_pushboolean(L, 1); |
284 lua_pushboolean(L, 1); |
285 return 1; |
285 return 1; |
286 } |
286 } |
287 |
287 |
288 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
288 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
289 int column_count; |
289 int column_count, fetch_result_ok; |
290 MYSQL_BIND *bind = NULL; |
290 MYSQL_BIND *bind = NULL; |
|
291 unsigned long *real_length = NULL; |
291 const char *error_message = NULL; |
292 const char *error_message = NULL; |
292 |
293 |
293 if (!statement->stmt) { |
294 if (!statement->stmt) { |
294 luaL_error(L, DBI_ERR_FETCH_INVALID); |
295 luaL_error(L, DBI_ERR_FETCH_INVALID); |
295 return 0; |
296 return 0; |
304 |
305 |
305 if (column_count > 0) { |
306 if (column_count > 0) { |
306 int i; |
307 int i; |
307 MYSQL_FIELD *fields; |
308 MYSQL_FIELD *fields; |
308 |
309 |
|
310 real_length = calloc(column_count, sizeof(unsigned long)); |
|
311 |
309 bind = malloc(sizeof(MYSQL_BIND) * column_count); |
312 bind = malloc(sizeof(MYSQL_BIND) * column_count); |
310 memset(bind, 0, sizeof(MYSQL_BIND) * column_count); |
313 memset(bind, 0, sizeof(MYSQL_BIND) * column_count); |
311 |
314 |
312 fields = mysql_fetch_fields(statement->metadata); |
315 fields = mysql_fetch_fields(statement->metadata); |
313 |
316 |
314 for (i = 0; i < column_count; i++) { |
317 for (i = 0; i < column_count; i++) { |
315 unsigned int length = mysql_buffer_size(&fields[i]); |
318 unsigned int length = mysql_buffer_size(&fields[i]); |
316 char *buffer = (char *)malloc(length); |
319 if (length > sizeof(MYSQL_TIME)) { |
317 memset(buffer, 0, length); |
320 bind[i].buffer = NULL; |
|
321 bind[i].buffer_length = 0; |
|
322 } else { |
|
323 char *buffer = (char *)malloc(length); |
|
324 memset(buffer, 0, length); |
|
325 |
|
326 bind[i].buffer = buffer; |
|
327 bind[i].buffer_length = length; |
|
328 } |
318 |
329 |
319 bind[i].buffer_type = fields[i].type; |
330 bind[i].buffer_type = fields[i].type; |
320 bind[i].buffer = buffer; |
331 bind[i].length = &real_length[i]; |
321 bind[i].buffer_length = length; |
|
322 } |
332 } |
323 |
333 |
324 if (mysql_stmt_bind_result(statement->stmt, bind)) { |
334 if (mysql_stmt_bind_result(statement->stmt, bind)) { |
325 error_message = DBI_ERR_BINDING_RESULTS; |
335 error_message = DBI_ERR_BINDING_RESULTS; |
326 goto cleanup; |
336 goto cleanup; |
327 } |
337 } |
328 |
338 |
329 if (!mysql_stmt_fetch(statement->stmt)) { |
339 fetch_result_ok = mysql_stmt_fetch(statement->stmt); |
|
340 if (fetch_result_ok == 0 || fetch_result_ok == MYSQL_DATA_TRUNCATED) { |
330 int d = 1; |
341 int d = 1; |
331 |
342 |
332 lua_newtable(L); |
343 lua_newtable(L); |
333 for (i = 0; i < column_count; i++) { |
344 for (i = 0; i < column_count; i++) { |
334 lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); |
345 lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); |
335 const char *name = fields[i].name; |
346 const char *name = fields[i].name; |
|
347 |
|
348 if (bind[i].buffer == NULL) { |
|
349 char *buffer = (char *)malloc(real_length[i]); |
|
350 memset(buffer, 0, real_length[i]); |
|
351 |
|
352 bind[i].buffer = buffer; |
|
353 bind[i].buffer_length = real_length[i]; |
|
354 mysql_stmt_fetch_column(statement->stmt, bind, i, 0); |
|
355 } |
336 |
356 |
337 if (lua_push == LUA_PUSH_NIL) { |
357 if (lua_push == LUA_PUSH_NIL) { |
338 if (named_columns) { |
358 if (named_columns) { |
339 LUA_PUSH_ATTRIB_NIL(name); |
359 LUA_PUSH_ATTRIB_NIL(name); |
340 } else { |
360 } else { |