Skip to content

Commit 9d971db

Browse files
Edouard-chinsodabrew
authored andcommitted
Expose the mysql_set_server_option: (#943)
- Use case: I'd like to be able to do multiple statements query without having to reconnect to the db first. Without this feature, if I want to do a multi statement query **after** the connection is established without the `MULTI_STATEMENTS` flag, I'd have to set the flag on the connection and reconnect - One of the main motivation for this is because Rails is now inserting fixtures inside a multi-statements query. We used the workaround I described above, but it would be great if we could use the mysql function mysql_set_server_option . For more context [Ref](rails/rails#31422 (comment)) - Ref https://dev.mysql.com/doc/refman/5.5/en/mysql-set-server-option.html
1 parent 503429c commit 9d971db

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

ext/mysql2/client.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,23 @@ static VALUE rb_mysql_client_ping(VALUE self) {
10981098
}
10991099
}
11001100

1101+
/* call-seq:
1102+
* client.set_server_option(value)
1103+
*
1104+
* Enables or disables an option for the connection.
1105+
* Read https://dev.mysql.com/doc/refman/5.7/en/mysql-set-server-option.html
1106+
* for more information.
1107+
*/
1108+
static VALUE rb_mysql_client_set_server_option(VALUE self, VALUE value) {
1109+
GET_CLIENT(self);
1110+
1111+
if (mysql_set_server_option(wrapper->client, NUM2INT(value)) == 0) {
1112+
return Qtrue;
1113+
} else {
1114+
return Qfalse;
1115+
}
1116+
}
1117+
11011118
/* call-seq:
11021119
* client.more_results?
11031120
*
@@ -1399,6 +1416,7 @@ void init_mysql2_client() {
13991416
rb_define_method(cMysql2Client, "thread_id", rb_mysql_client_thread_id, 0);
14001417
rb_define_method(cMysql2Client, "ping", rb_mysql_client_ping, 0);
14011418
rb_define_method(cMysql2Client, "select_db", rb_mysql_client_select_db, 1);
1419+
rb_define_method(cMysql2Client, "set_server_option", rb_mysql_client_set_server_option, 1);
14021420
rb_define_method(cMysql2Client, "more_results?", rb_mysql_client_more_results, 0);
14031421
rb_define_method(cMysql2Client, "next_result", rb_mysql_client_next_result, 0);
14041422
rb_define_method(cMysql2Client, "store_result", rb_mysql_client_store_result, 0);
@@ -1528,6 +1546,13 @@ void init_mysql2_client() {
15281546
rb_const_set(cMysql2Client, rb_intern("SECURE_CONNECTION"), LONG2NUM(0));
15291547
#endif
15301548

1549+
#if MYSQL_VERSION_ID >= 40101
1550+
rb_const_set(cMysql2Client, rb_intern("OPTION_MULTI_STATEMENTS_ON"),
1551+
LONG2NUM(MYSQL_OPTION_MULTI_STATEMENTS_ON));
1552+
rb_const_set(cMysql2Client, rb_intern("OPTION_MULTI_STATEMENTS_OFF"),
1553+
LONG2NUM(MYSQL_OPTION_MULTI_STATEMENTS_OFF));
1554+
#endif
1555+
15311556
#ifdef CLIENT_MULTI_STATEMENTS
15321557
rb_const_set(cMysql2Client, rb_intern("MULTI_STATEMENTS"),
15331558
LONG2NUM(CLIENT_MULTI_STATEMENTS));

spec/mysql2/client_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,40 @@ def run_gc
205205
# rubocop:enable Lint/AmbiguousBlockAssociation
206206
end
207207

208+
context "#set_server_option" do
209+
let(:client) do
210+
new_client.tap do |client|
211+
client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
212+
end
213+
end
214+
215+
it 'returns true when multi_statements is enable' do
216+
expect(client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)).to be true
217+
end
218+
219+
it 'returns true when multi_statements is disable' do
220+
expect(client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)).to be true
221+
end
222+
223+
it 'returns false when multi_statements is neither OPTION_MULTI_STATEMENTS_OFF or OPTION_MULTI_STATEMENTS_ON' do
224+
expect(client.set_server_option(344)).to be false
225+
end
226+
227+
it 'enables multiple-statement' do
228+
client.query("SELECT 1;SELECT 2;")
229+
230+
expect(client.next_result).to be true
231+
expect(client.store_result.first).to eql('2' => 2)
232+
expect(client.next_result).to be false
233+
end
234+
235+
it 'disables multiple-statement' do
236+
client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)
237+
238+
expect { client.query("SELECT 1;SELECT 2;") }.to raise_error(Mysql2::Error)
239+
end
240+
end
241+
208242
context "#automatic_close" do
209243
it "is enabled by default" do
210244
expect(new_client.automatic_close?).to be(true)

0 commit comments

Comments
 (0)