Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 4885f9a

Browse files
committed
Reset rows on second iteration (and seek to beginning)
1 parent 66dba82 commit 4885f9a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

ext/mysql2/result.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
531531
return wrapper->rows;
532532
}
533533
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
534+
} else if (!cacheRows && wrapper->lastRowProcessed == wrapper->numberOfRows) {
535+
mysql_data_seek(wrapper->result, 0);
536+
wrapper->lastRowProcessed = 0;
537+
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
534538
}
535539

536540
if (cacheRows && wrapper->lastRowProcessed == wrapper->numberOfRows) {
@@ -558,15 +562,17 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
558562

559563
if (row == Qnil) {
560564
/* we don't need the mysql C dataset around anymore, peace it */
561-
rb_mysql_result_free_result(wrapper);
565+
if (cacheRows) {
566+
rb_mysql_result_free_result(wrapper);
567+
}
562568
return Qnil;
563569
}
564570

565571
if (block != Qnil) {
566572
rb_yield(row);
567573
}
568574
}
569-
if (wrapper->lastRowProcessed == wrapper->numberOfRows) {
575+
if (wrapper->lastRowProcessed == wrapper->numberOfRows && cacheRows) {
570576
/* we don't need the mysql C dataset around anymore, peace it */
571577
rb_mysql_result_free_result(wrapper);
572578
}

0 commit comments

Comments
 (0)