Skip to content

Commit b009d7e

Browse files
authored
Fix Client#affected_rows for queries with no affected rows (#1417)
Fix: #1414 e.g. `SELECT sleep(0.1)` or `SELECT 1`.
1 parent 387e13a commit b009d7e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

ext/mysql2/client.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
673673
wrapper->active_fiber = Qnil;
674674
rb_raise_mysql2_error(wrapper);
675675
}
676-
wrapper->affected_rows = mysql_affected_rows(wrapper->client);
677676

678677
is_streaming = rb_hash_aref(rb_ivar_get(self, intern_current_query_options), sym_stream);
679678
if (is_streaming == Qtrue) {
@@ -682,6 +681,8 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
682681
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
683682
}
684683

684+
wrapper->affected_rows = mysql_affected_rows(wrapper->client);
685+
685686
if (result == NULL) {
686687
if (mysql_errno(wrapper->client) != 0) {
687688
wrapper->active_fiber = Qnil;
@@ -1165,7 +1166,7 @@ static VALUE rb_mysql_client_affected_rows(VALUE self) {
11651166

11661167
REQUIRE_CONNECTED(wrapper);
11671168
retVal = wrapper->affected_rows;
1168-
if (retVal == (uint64_t)-1) {
1169+
if (retVal == (my_ulonglong)-1) {
11691170
rb_raise_mysql2_error(wrapper);
11701171
}
11711172
return ULL2NUM(retVal);

spec/mysql2/client_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,11 @@ def run_gc
10931093
end
10941094
end
10951095

1096+
it "#affected_rows when no rows were affected returns 1" do
1097+
@client.query "SELECT sleep(0.01)"
1098+
expect(@client.affected_rows).to eq(1)
1099+
end
1100+
10961101
it "should respond to #thread_id" do
10971102
expect(@client).to respond_to(:thread_id)
10981103
end

0 commit comments

Comments
 (0)