From 872bdb0caf0cbb0415b406336180adb3ee4e70ad Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Fri, 2 Apr 2010 15:32:58 -0700 Subject: [PATCH] rename method to each, seek dataset to beginning on each call of #each --- ext/mysql2_ext.c | 8 ++++++-- ext/mysql2_ext.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index 3511aff..6add4b7 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -208,7 +208,7 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) { return rowHash; } -static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) { +static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) { VALUE dataset, opts, block; MYSQL_RES * result; unsigned long numRows, i; @@ -217,6 +217,10 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) { rb_scan_args(argc, argv, "01&", &opts, &block); + // force-start at the beginning of the result set for proper + // behavior of #each + mysql_data_seek(result, 0); + numRows = mysql_num_rows(result); if (numRows == 0) { return Qnil; @@ -262,7 +266,7 @@ void Init_mysql2_ext() { cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject); // rb_define_method(cMysql2Result, "fetch_row", rb_mysql_result_fetch_row, -1); // rb_define_method(cMysql2Result, "fetch_rows", rb_mysql_result_fetch_rows, -1); - rb_define_method(cMysql2Result, "each", rb_mysql_result_fetch_rows, -1); + rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1); VALUE mEnumerable = rb_const_get(rb_cObject, rb_intern("Enumerable")); rb_include_module(cMysql2Result, mEnumerable); diff --git a/ext/mysql2_ext.h b/ext/mysql2_ext.h index 5c41cfd..f6c368d 100644 --- a/ext/mysql2_ext.h +++ b/ext/mysql2_ext.h @@ -31,5 +31,5 @@ VALUE cMysql2Result; static ID sym_symbolize_keys; static VALUE rb_mysql_result_to_obj(MYSQL_RES * res); static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self); -static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self); +static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self); void rb_mysql_result_free(void * result); \ No newline at end of file