Skip to content

Commit 0f38974

Browse files
Expose db attribute of MYSQL client struct (#1245)
Adds an accessor method that returns the current value of the `MSYQL` client struct's `db` attribute. The `MYSQL` client struct includes a field `char *db`. When the `session_track_schema` setting is enabled, this field will be updated using information from server-provided "OK" packets, keeping it in sync as the client switches between databases.
1 parent b63d2e8 commit 0f38974

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

ext/mysql2/client.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,26 @@ static VALUE rb_mysql_client_encoding(VALUE self) {
13341334
return wrapper->encoding;
13351335
}
13361336

1337+
/* call-seq:
1338+
* client.database
1339+
*
1340+
* Returns the currently selected database.
1341+
*
1342+
* The result may be stale if `session_track_schema` is disabled. Read
1343+
* https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html for more
1344+
* information.
1345+
*/
1346+
static VALUE rb_mysql_client_database(VALUE self) {
1347+
GET_CLIENT(self);
1348+
1349+
char *db = wrapper->client->db;
1350+
if (!db) {
1351+
return Qnil;
1352+
}
1353+
1354+
return rb_str_new_cstr(wrapper->client->db);
1355+
}
1356+
13371357
/* call-seq:
13381358
* client.automatic_close?
13391359
*
@@ -1606,6 +1626,7 @@ void init_mysql2_client() {
16061626
rb_define_method(cMysql2Client, "ssl_cipher", rb_mysql_get_ssl_cipher, 0);
16071627
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
16081628
rb_define_method(cMysql2Client, "session_track", rb_mysql_client_session_track, 1);
1629+
rb_define_method(cMysql2Client, "database", rb_mysql_client_database, 0);
16091630

16101631
rb_define_private_method(cMysql2Client, "connect_timeout=", set_connect_timeout, 1);
16111632
rb_define_private_method(cMysql2Client, "read_timeout=", set_read_timeout, 1);

spec/mysql2/client_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,49 @@ def run_gc
11811181
end
11821182
end
11831183

1184+
context 'database' do
1185+
before(:example) do
1186+
2.times do |i|
1187+
@client.query("CREATE DATABASE test_db#{i}")
1188+
end
1189+
end
1190+
1191+
after(:example) do
1192+
2.times do |i|
1193+
@client.query("DROP DATABASE test_db#{i}")
1194+
end
1195+
end
1196+
1197+
it "should be `nil` when no database is selected" do
1198+
client = new_client(database: nil)
1199+
expect(client.database).to eq(nil)
1200+
end
1201+
1202+
it "should reflect the initially connected database" do
1203+
client = new_client(database: 'test_db0')
1204+
expect(client.database).to eq('test_db0')
1205+
end
1206+
1207+
context "when session tracking is on" do
1208+
it "should change to reflect currently selected database" do
1209+
client = new_client(database: 'test_db0')
1210+
client.query('SET session_track_schema=on')
1211+
expect { client.query('USE test_db1') }.to change {
1212+
client.database
1213+
}.from('test_db0').to('test_db1')
1214+
end
1215+
end
1216+
1217+
context "when session tracking is off" do
1218+
it "does not change when a new database is selected" do
1219+
client = new_client(database: 'test_db0')
1220+
client.query('SET session_track_schema=off')
1221+
expect(client.database).to eq('test_db0')
1222+
expect { client.query('USE test_db1') }.not_to(change { client.database })
1223+
end
1224+
end
1225+
end
1226+
11841227
it "#thread_id should return a boolean" do
11851228
expect(@client.ping).to eql(true)
11861229
@client.close

0 commit comments

Comments
 (0)