Skip to content

Commit fcb1a8b

Browse files
committed
Merge pull request #729 from kamipo/support_result_free
Support `Result#free`
2 parents 270ebf3 + d4bd03f commit fcb1a8b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

ext/mysql2/result.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ static void rb_mysql_result_free_result(mysql2_result_wrapper * wrapper) {
104104
wrapper->stmt_wrapper->stmt->bind_result_done = 0;
105105
}
106106

107+
if (wrapper->statement != Qnil) {
108+
decr_mysql2_stmt(wrapper->stmt_wrapper);
109+
}
110+
107111
if (wrapper->result_buffers) {
108112
unsigned int i;
109113
for (i = 0; i < wrapper->numberOfFields; i++) {
@@ -136,13 +140,15 @@ static void rb_mysql_result_free(void *ptr) {
136140
decr_mysql2_client(wrapper->client_wrapper);
137141
}
138142

139-
if (wrapper->statement != Qnil) {
140-
decr_mysql2_stmt(wrapper->stmt_wrapper);
141-
}
142-
143143
xfree(wrapper);
144144
}
145145

146+
static VALUE rb_mysql_result_free_(VALUE self) {
147+
GET_RESULT(self);
148+
rb_mysql_result_free_result(wrapper);
149+
return Qnil;
150+
}
151+
146152
/*
147153
* for small results, this won't hit the network, but there's no
148154
* reliable way for us to tell this so we'll always release the GVL
@@ -511,7 +517,6 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
511517
return rowVal;
512518
}
513519

514-
515520
static VALUE rb_mysql_result_fetch_row(VALUE self, MYSQL_FIELD * fields, const result_each_args *args)
516521
{
517522
VALUE rowVal;
@@ -911,6 +916,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
911916
if (wrapper->lastRowProcessed == 0 && !wrapper->is_streaming) {
912917
wrapper->numberOfRows = wrapper->stmt_wrapper ? mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt) : mysql_num_rows(wrapper->result);
913918
if (wrapper->numberOfRows == 0) {
919+
rb_mysql_result_free_result(wrapper);
914920
wrapper->rows = rb_ary_new();
915921
return wrapper->rows;
916922
}
@@ -1007,6 +1013,7 @@ void init_mysql2_result() {
10071013
cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
10081014
rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1);
10091015
rb_define_method(cMysql2Result, "fields", rb_mysql_result_fetch_fields, 0);
1016+
rb_define_method(cMysql2Result, "free", rb_mysql_result_free_, 0);
10101017
rb_define_method(cMysql2Result, "count", rb_mysql_result_count, 0);
10111018
rb_define_alias(cMysql2Result, "size", "count");
10121019

spec/mysql2/result_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
expect(@result).to respond_to(:each)
2323
end
2424

25+
it "should respond to #free" do
26+
expect(@result).to respond_to(:free)
27+
end
28+
2529
it "should raise a Mysql2::Error exception upon a bad query" do
2630
expect {
2731
@client.query "bad sql"

0 commit comments

Comments
 (0)