Skip to content

Commit 4463cac

Browse files
authored
Update affected_rows after calling mysql_next_result (#1412)
Fix: #1411 Followup: #1383 When using multi statement, we need to update the affected rows after each call to `mysql_next_result`.
1 parent c79b3c1 commit 4463cac

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ext/mysql2/client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ static VALUE rb_mysql_client_next_result(VALUE self)
12821282
int ret;
12831283
GET_CLIENT(self);
12841284
ret = mysql_next_result(wrapper->client);
1285+
wrapper->affected_rows = mysql_affected_rows(wrapper->client);
12851286
if (ret > 0) {
12861287
rb_raise_mysql2_error(wrapper);
12871288
return Qfalse;

spec/mysql2/client_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,12 +1064,23 @@ def run_gc
10641064
end
10651065

10661066
it "#affected_rows should return a Fixnum, from the last INSERT/UPDATE" do
1067-
@client.query "INSERT INTO lastIdTest (blah) VALUES (1234)"
1068-
expect(@client.affected_rows).to eql(1)
1067+
@client.query "INSERT INTO lastIdTest (blah) VALUES (1234), (5678)"
1068+
expect(@client.affected_rows).to eql(2)
10691069
@client.query "UPDATE lastIdTest SET blah=4321 WHERE id=1"
10701070
expect(@client.affected_rows).to eql(1)
10711071
end
10721072

1073+
it "#affected_rows with multi statements returns the last result's affected_rows" do
1074+
@client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
1075+
1076+
@client.query("INSERT INTO lastIdTest (blah) VALUES (1234), (5678); UPDATE lastIdTest SET blah=4321 WHERE id=1")
1077+
expect(@client.affected_rows).to eq(2)
1078+
expect(@client.next_result).to eq(true)
1079+
expect(@client.affected_rows).to eq(1)
1080+
ensure
1081+
@client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)
1082+
end
1083+
10731084
it "#affected_rows isn't cleared by Statement#close" do
10741085
stmt = @client.prepare("INSERT INTO lastIdTest (blah) VALUES (1234)")
10751086

0 commit comments

Comments
 (0)