allow passing a block to fetch_rows

This commit is contained in:
Brian Lopez 2010-03-30 09:14:27 -07:00
parent ccbbb94ebb
commit 1eef152459
2 changed files with 29 additions and 15 deletions

View File

@ -97,7 +97,6 @@ static VALUE rb_mysql_result_fetch_row(VALUE self) {
fields = mysql_fetch_fields(result); fields = mysql_fetch_fields(result);
rowHash = rb_hash_new(); rowHash = rb_hash_new();
// TODO: yield to a passed block
for (i = 0; i < numFields; i++) { for (i = 0; i < numFields; i++) {
VALUE key = rb_str_new(fields[i].name, fields[i].name_length); VALUE key = rb_str_new(fields[i].name, fields[i].name_length);
if (row[i]) { if (row[i]) {
@ -109,27 +108,42 @@ static VALUE rb_mysql_result_fetch_row(VALUE self) {
return rowHash; return rowHash;
} }
static VALUE rb_mysql_result_fetch_rows(VALUE self) { static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
VALUE dataset; VALUE dataset, block;
MYSQL_RES * result; MYSQL_RES * result;
unsigned long numRows, i; unsigned long numRows, i;
GetMySQLResult(self, result); GetMySQLResult(self, result);
rb_scan_args(argc, argv, "0&", &block);
numRows = mysql_num_rows(result); numRows = mysql_num_rows(result);
if (numRows == 0) { if (numRows == 0) {
return Qnil; return Qnil;
} }
dataset = rb_ary_new2(numRows); // TODO: allow yielding datasets of configurable size
for (i = 0; i < numRows; i++) { // like find_in_batches from AR...
VALUE row = rb_mysql_result_fetch_row(self); if (block != Qnil) {
if (row == Qnil) { for (i = 0; i < numRows; i++) {
return Qnil; VALUE row = rb_mysql_result_fetch_row(self);
if (row == Qnil) {
return Qnil;
}
rb_yield(row);
} }
rb_ary_store(dataset, i, row); } else {
dataset = rb_ary_new2(numRows);
for (i = 0; i < numRows; i++) {
VALUE row = rb_mysql_result_fetch_row(self);
if (row == Qnil) {
return Qnil;
}
rb_ary_store(dataset, i, row);
}
return dataset;
} }
return dataset; return Qnil;
} }
/* Ruby Extension initializer */ /* Ruby Extension initializer */
@ -143,7 +157,7 @@ void Init_mysql_duce_ext() {
cMySQLResult = rb_define_class_under(mMySQL, "Result", rb_cObject); cMySQLResult = rb_define_class_under(mMySQL, "Result", rb_cObject);
rb_define_method(cMySQLResult, "fetch_row", rb_mysql_result_fetch_row, 0); rb_define_method(cMySQLResult, "fetch_row", rb_mysql_result_fetch_row, 0);
rb_define_method(cMySQLResult, "fetch_rows", rb_mysql_result_fetch_rows, 0); rb_define_method(cMySQLResult, "fetch_rows", rb_mysql_result_fetch_rows, -1);
#ifdef HAVE_RUBY_ENCODING_H #ifdef HAVE_RUBY_ENCODING_H
utf8Encoding = rb_enc_find_index("UTF-8"); utf8Encoding = rb_enc_find_index("UTF-8");

View File

@ -27,4 +27,4 @@ VALUE cMySQLResult;
static VALUE rb_mysql_result_to_obj(MYSQL_RES * res); static VALUE rb_mysql_result_to_obj(MYSQL_RES * res);
void rb_mysql_result_free(void * result); void rb_mysql_result_free(void * result);
static VALUE rb_mysql_result_fetch_row(VALUE self); static VALUE rb_mysql_result_fetch_row(VALUE self);
static VALUE rb_mysql_result_fetch_rows(VALUE self); static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self);