@@ -780,13 +780,11 @@ static VALUE rb_mysql_result_each_nonstmt(VALUE self, const result_each_args* ar
780
780
wrapper -> numberOfRows ++ ;
781
781
if (args -> block_given != Qnil ) {
782
782
rb_yield (row );
783
- wrapper -> lastRowProcessed ++ ;
784
783
}
785
784
}
786
785
} while (row != Qnil );
787
786
788
787
rb_mysql_result_free_result (wrapper );
789
- wrapper -> numberOfRows = wrapper -> lastRowProcessed ;
790
788
wrapper -> streamingComplete = 1 ;
791
789
792
790
// 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
799
797
rb_raise (cMysql2Error , "You have already fetched all the rows for this query and streaming is true. (to reiterate you must requery)." );
800
798
}
801
799
} 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
+
802
809
if (args -> cacheRows && wrapper -> lastRowProcessed == wrapper -> numberOfRows ) {
803
810
/* we've already read the entire dataset from the C result into our */
804
811
/* 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
816
823
row = rb_ary_entry (wrapper -> rows , i );
817
824
} else {
818
825
row = rb_mysql_result_fetch_row (self , args -> db_timezone , args -> app_timezone , args -> symbolizeKeys , args -> asArray , args -> castBool , args -> cast , fields );
819
-
820
826
if (args -> cacheRows ) {
821
827
rb_ary_store (wrapper -> rows , i , row );
822
828
}
@@ -990,20 +996,13 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
990
996
app_timezone = Qnil ;
991
997
}
992
998
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 ) {
998
1002
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 ;
1006
1004
}
1005
+ wrapper -> rows = rb_ary_new2 (wrapper -> numberOfRows );
1007
1006
}
1008
1007
1009
1008
// Backward compat
0 commit comments