Skip to content

Commit 6637c9f

Browse files
committed
Should initialize fields before rb_mysql_result_free_result
1 parent 2cabc76 commit 6637c9f

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

ext/mysql2/result.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,15 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
346346
conn_enc = rb_to_encoding(wrapper->encoding);
347347
#endif
348348

349+
if (wrapper->fields == Qnil) {
350+
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
351+
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
352+
}
349353
if (args->asArray) {
350354
rowVal = rb_ary_new2(wrapper->numberOfFields);
351355
} else {
352356
rowVal = rb_hash_new();
353357
}
354-
if (wrapper->fields == Qnil) {
355-
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
356-
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
357-
}
358358

359359
if (wrapper->result_buffers == NULL) {
360360
rb_mysql_result_alloc_result_buffers(self, fields);
@@ -541,16 +541,16 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, MYSQL_FIELD * fields, const r
541541
return Qnil;
542542
}
543543

544+
if (wrapper->fields == Qnil) {
545+
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
546+
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
547+
}
544548
if (args->asArray) {
545549
rowVal = rb_ary_new2(wrapper->numberOfFields);
546550
} else {
547551
rowVal = rb_hash_new();
548552
}
549553
fieldLengths = mysql_fetch_lengths(wrapper->result);
550-
if (wrapper->fields == Qnil) {
551-
wrapper->numberOfFields = mysql_num_fields(wrapper->result);
552-
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
553-
}
554554

555555
for (i = 0; i < wrapper->numberOfFields; i++) {
556556
VALUE field = rb_mysql_result_fetch_field(self, i, args->symbolizeKeys);
@@ -915,11 +915,6 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
915915

916916
if (wrapper->rows == Qnil && !wrapper->is_streaming) {
917917
wrapper->numberOfRows = wrapper->stmt_wrapper ? mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt) : mysql_num_rows(wrapper->result);
918-
if (wrapper->numberOfRows == 0) {
919-
rb_mysql_result_free_result(wrapper);
920-
wrapper->rows = rb_ary_new();
921-
return wrapper->rows;
922-
}
923918
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
924919
}
925920

0 commit comments

Comments
 (0)