Permalink
Browse files

move myql_fetch_fields outside of the row fetch loop. Fixes #92

  • Loading branch information...
1 parent a917c6a commit cd443a94ecab37fb72bc9b9bf9f6f7537d03b838 @tenderlove tenderlove committed Mar 29, 2012
Showing with 8 additions and 5 deletions.
  1. +8 −5 ext/mysql2/result.c
View
@@ -163,11 +163,10 @@ static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_e
#endif
-static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezone, int symbolizeKeys, int asArray, int castBool, int cast) {
+static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezone, int symbolizeKeys, int asArray, int castBool, int cast, MYSQL_FIELD * fields) {
VALUE rowVal;
mysql2_result_wrapper * wrapper;
MYSQL_ROW row;
- MYSQL_FIELD * fields = NULL;
unsigned int i = 0;
unsigned long * fieldLengths;
void * ptr;
@@ -193,7 +192,6 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
} else {
rowVal = rb_hash_new();
}
- fields = mysql_fetch_fields(wrapper->result);
fieldLengths = mysql_fetch_lengths(wrapper->result);
if (wrapper->fields == Qnil) {
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
@@ -395,6 +393,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
mysql2_result_wrapper * wrapper;
unsigned long i;
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1, streaming = 0;
+ MYSQL_FIELD * fields = NULL;
GetMysql2Result(self, wrapper);
@@ -474,8 +473,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
if(!wrapper->streamingComplete) {
VALUE row;
+ fields = mysql_fetch_fields(wrapper->result);
+
do {
- row = rb_mysql_result_fetch_row(self, db_timezone, app_timezone, symbolizeKeys, asArray, castBool, cast);
+ row = rb_mysql_result_fetch_row(self, db_timezone, app_timezone, symbolizeKeys, asArray, castBool, cast, fields);
if (block != Qnil) {
rb_yield(row);
@@ -500,12 +501,14 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
} else {
unsigned long rowsProcessed = 0;
rowsProcessed = RARRAY_LEN(wrapper->rows);
+ fields = mysql_fetch_fields(wrapper->result);
+
for (i = 0; i < wrapper->numberOfRows; i++) {
VALUE row;
if (cacheRows && i < rowsProcessed) {
row = rb_ary_entry(wrapper->rows, i);
} else {
- row = rb_mysql_result_fetch_row(self, db_timezone, app_timezone, symbolizeKeys, asArray, castBool, cast);
+ row = rb_mysql_result_fetch_row(self, db_timezone, app_timezone, symbolizeKeys, asArray, castBool, cast, fields);
if (cacheRows) {
rb_ary_store(wrapper->rows, i, row);
}

0 comments on commit cd443a9

Please sign in to comment.