Skip to content

Commit 5677e2e

Browse files
committed
Check for errors after streaming results, the connection might have gone away due to read timeout.
1 parent 5f5372c commit 5677e2e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ext/mysql2/result.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
429429
ID db_timezone, app_timezone, dbTz, appTz;
430430
mysql2_result_wrapper * wrapper;
431431
unsigned long i;
432+
const char * errstr;
432433
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1, streaming = 0;
433434
MYSQL_FIELD * fields = NULL;
434435

@@ -492,7 +493,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
492493
}
493494

494495
if (wrapper->lastRowProcessed == 0) {
495-
if(streaming) {
496+
if (streaming) {
496497
/* We can't get number of rows if we're streaming, */
497498
/* until we've finished fetching all rows */
498499
wrapper->numberOfRows = 0;
@@ -508,7 +509,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
508509
}
509510

510511
if (streaming) {
511-
if(!wrapper->streamingComplete) {
512+
if (!wrapper->streamingComplete) {
512513
VALUE row;
513514

514515
fields = mysql_fetch_fields(wrapper->result);
@@ -526,6 +527,13 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
526527

527528
wrapper->numberOfRows = wrapper->lastRowProcessed;
528529
wrapper->streamingComplete = 1;
530+
531+
// Check for errors, the connection might have gone out from under us
532+
// mysql_error returns an empty string if there is no error
533+
errstr = mysql_error(wrapper->client_wrapper->client);
534+
if (errstr[0]) {
535+
rb_raise(cMysql2Error, "%s", errstr);
536+
}
529537
} else {
530538
rb_raise(cMysql2Error, "You have already fetched all the rows for this query and streaming is true. (to reiterate you must requery).");
531539
}

0 commit comments

Comments
 (0)