Skip to content

Commit 563b120

Browse files
committed
fix streaming results and row counts
1 parent 556a0e1 commit 563b120

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

ext/mysql2/result.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,11 @@ static VALUE rb_mysql_result_each_nonstmt(VALUE self, const result_each_args* ar
780780
wrapper->numberOfRows++;
781781
if (args->block_given != Qnil) {
782782
rb_yield(row);
783-
wrapper->lastRowProcessed++;
784783
}
785784
}
786785
} while(row != Qnil);
787786

788787
rb_mysql_result_free_result(wrapper);
789-
wrapper->numberOfRows = wrapper->lastRowProcessed;
790788
wrapper->streamingComplete = 1;
791789

792790
// Check for errors, the connection might have gone out from under us
@@ -799,6 +797,15 @@ static VALUE rb_mysql_result_each_nonstmt(VALUE self, const result_each_args* ar
799797
rb_raise(cMysql2Error, "You have already fetched all the rows for this query and streaming is true. (to reiterate you must requery).");
800798
}
801799
} else {
800+
if (wrapper->lastRowProcessed == 0) {
801+
wrapper->numberOfRows = mysql_num_rows(wrapper->result);
802+
if (wrapper->numberOfRows == 0) {
803+
wrapper->rows = rb_ary_new();
804+
return wrapper->rows;
805+
}
806+
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
807+
}
808+
802809
if (args->cacheRows && wrapper->lastRowProcessed == wrapper->numberOfRows) {
803810
/* we've already read the entire dataset from the C result into our */
804811
/* internal array. Lets hand that over to the user since it's ready to go */
@@ -816,7 +823,6 @@ static VALUE rb_mysql_result_each_nonstmt(VALUE self, const result_each_args* ar
816823
row = rb_ary_entry(wrapper->rows, i);
817824
} else {
818825
row = rb_mysql_result_fetch_row(self, args->db_timezone, args->app_timezone, args->symbolizeKeys, args->asArray, args->castBool, args->cast, fields);
819-
820826
if (args->cacheRows) {
821827
rb_ary_store(wrapper->rows, i, row);
822828
}
@@ -990,20 +996,13 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
990996
app_timezone = Qnil;
991997
}
992998

993-
if (wrapper->lastRowProcessed == 0) {
994-
if(args.streaming) {
995-
// We can't get number of rows if we're streaming,
996-
// until we've finished fetching all rows
997-
wrapper->numberOfRows = 0;
999+
if (wrapper->lastRowProcessed == 0 && !wrapper->is_streaming) {
1000+
wrapper->numberOfRows = wrapper->stmt ? mysql_stmt_num_rows(wrapper->stmt) : mysql_num_rows(wrapper->result);
1001+
if (wrapper->numberOfRows == 0) {
9981002
wrapper->rows = rb_ary_new();
999-
} else {
1000-
wrapper->numberOfRows = wrapper->stmt ? mysql_stmt_num_rows(wrapper->stmt) : mysql_num_rows(wrapper->result);
1001-
if (wrapper->numberOfRows == 0) {
1002-
wrapper->rows = rb_ary_new();
1003-
return wrapper->rows;
1004-
}
1005-
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
1003+
return wrapper->rows;
10061004
}
1005+
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
10071006
}
10081007

10091008
// Backward compat

0 commit comments

Comments
 (0)