Skip to content

Commit 70bf629

Browse files
committed
Since streaming is a query option, process it in rb_mysql_result_to_obj instead of rb_mysql_result_each
1 parent 2a7d09d commit 70bf629

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

ext/mysql2/result.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
446446
mysql2_result_wrapper * wrapper;
447447
unsigned long i;
448448
const char * errstr;
449-
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1, streaming = 0;
449+
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1;
450450
MYSQL_FIELD * fields = NULL;
451451

452452
GetMysql2Result(self, wrapper);
@@ -479,11 +479,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
479479
cast = 0;
480480
}
481481

482-
if (rb_hash_aref(opts, sym_stream) == Qtrue) {
483-
streaming = 1;
484-
}
485-
486-
if (streaming && cacheRows) {
482+
if (wrapper->is_streaming && cacheRows) {
487483
rb_warn("cacheRows is ignored if streaming is true");
488484
}
489485

@@ -509,7 +505,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
509505
}
510506

511507
if (wrapper->lastRowProcessed == 0) {
512-
if (streaming) {
508+
if (wrapper->is_streaming) {
513509
/* We can't get number of rows if we're streaming, */
514510
/* until we've finished fetching all rows */
515511
wrapper->numberOfRows = 0;
@@ -524,7 +520,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
524520
}
525521
}
526522

527-
if (streaming) {
523+
if (wrapper->is_streaming) {
528524
if (!wrapper->streamingComplete) {
529525
VALUE row;
530526

@@ -601,12 +597,12 @@ static VALUE rb_mysql_result_count(VALUE self) {
601597
mysql2_result_wrapper *wrapper;
602598

603599
GetMysql2Result(self, wrapper);
600+
if (wrapper->is_streaming) {
601+
return LONG2NUM(wrapper->numberOfRows);
602+
}
603+
604604
if (wrapper->resultFreed) {
605-
if (wrapper->streamingComplete){
606-
return LONG2NUM(wrapper->numberOfRows);
607-
} else {
608-
return LONG2NUM(RARRAY_LEN(wrapper->rows));
609-
}
605+
return LONG2NUM(RARRAY_LEN(wrapper->rows));
610606
} else {
611607
return INT2FIX(mysql_num_rows(wrapper->result));
612608
}
@@ -634,6 +630,10 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
634630

635631
rb_iv_set(obj, "@query_options", options);
636632

633+
/* Options that cannot be changed in results.each(...) { |row| }
634+
* should be processed here. */
635+
wrapper->is_streaming = (rb_hash_aref(options, sym_stream) == Qtrue ? 1 : 0);
636+
637637
return obj;
638638
}
639639

ext/mysql2/result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct {
1212
unsigned int numberOfFields;
1313
unsigned long numberOfRows;
1414
unsigned long lastRowProcessed;
15+
char is_streaming;
1516
char streamingComplete;
1617
char resultFreed;
1718
MYSQL_RES *result;

0 commit comments

Comments
 (0)