Skip to content

Commit 8733029

Browse files
committed
Reset rows on second iteration (and seek to beginning)
1 parent 0faa0a7 commit 8733029

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
@@ -834,15 +834,17 @@ static VALUE rb_mysql_result_each_(VALUE self,
834834

835835
if (row == Qnil) {
836836
/* we don't need the mysql C dataset around anymore, peace it */
837-
rb_mysql_result_free_result(wrapper);
837+
if (args->cacheRows) {
838+
rb_mysql_result_free_result(wrapper);
839+
}
838840
return Qnil;
839841
}
840842

841843
if (args->block_given != Qnil) {
842844
rb_yield(row);
843845
}
844846
}
845-
if (wrapper->lastRowProcessed == wrapper->numberOfRows) {
847+
if (wrapper->lastRowProcessed == wrapper->numberOfRows && args->cacheRows) {
846848
/* we don't need the mysql C dataset around anymore, peace it */
847849
rb_mysql_result_free_result(wrapper);
848850
}
@@ -917,6 +919,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
917919
if (wrapper->rows == Qnil && !wrapper->is_streaming) {
918920
wrapper->numberOfRows = wrapper->stmt_wrapper ? mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt) : mysql_num_rows(wrapper->result);
919921
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
922+
} else if (wrapper->rows && !cacheRows) {
923+
mysql_data_seek(wrapper->result, 0);
924+
wrapper->lastRowProcessed = 0;
925+
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
920926
}
921927

922928
// Backward compat

0 commit comments

Comments
 (0)