Skip to content

Commit 9cfde46

Browse files
Expose db attribute of MYSQL client struct
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 25c42c7 commit 9cfde46

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
@@ -1259,6 +1259,26 @@ static VALUE rb_mysql_client_encoding(VALUE self) {
12591259
return wrapper->encoding;
12601260
}
12611261

1262+
/* call-seq:
1263+
* client.db
1264+
*
1265+
* Returns the currently selected db.
1266+
*
1267+
* The result may be stale if `session_track_schema` is disabled. Read
1268+
* https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html for more
1269+
* information.
1270+
*/
1271+
static VALUE rb_mysql_client_db(VALUE self) {
1272+
GET_CLIENT(self);
1273+
1274+
char *db = wrapper->client->db;
1275+
if (!db) {
1276+
return Qnil;
1277+
}
1278+
1279+
return rb_str_new_cstr(wrapper->client->db);
1280+
}
1281+
12621282
/* call-seq:
12631283
* client.automatic_close?
12641284
*
@@ -1500,6 +1520,7 @@ void init_mysql2_client() {
15001520
rb_define_method(cMysql2Client, "ssl_cipher", rb_mysql_get_ssl_cipher, 0);
15011521
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
15021522
rb_define_method(cMysql2Client, "session_track", rb_mysql_client_session_track, 1);
1523+
rb_define_method(cMysql2Client, "db", rb_mysql_client_db, 0);
15031524

15041525
rb_define_private_method(cMysql2Client, "connect_timeout=", set_connect_timeout, 1);
15051526
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
@@ -1144,6 +1144,49 @@ def run_gc
11441144
end
11451145
end
11461146

1147+
context 'db' do
1148+
before(:example) do
1149+
2.times do |i|
1150+
@client.query("CREATE DATABASE test_db#{i}")
1151+
end
1152+
end
1153+
1154+
after(:example) do
1155+
2.times do |i|
1156+
@client.query("DROP DATABASE test_db#{i}")
1157+
end
1158+
end
1159+
1160+
it "should be `nil` when no database is selected" do
1161+
client = new_client(database: nil)
1162+
expect(client.db).to eq(nil)
1163+
end
1164+
1165+
it "should reflect the initially connected database" do
1166+
client = new_client(database: 'test_db0')
1167+
expect(client.db).to eq('test_db0')
1168+
end
1169+
1170+
context "when session tracking is on" do
1171+
it "should change to reflect currently selected database" do
1172+
client = new_client(database: 'test_db0')
1173+
client.query('SET session_track_schema=on')
1174+
expect { client.query('USE test_db1') }.to change {
1175+
client.db
1176+
}.from('test_db0').to('test_db1')
1177+
end
1178+
end
1179+
1180+
context "when session tracking is off" do
1181+
it "does not change when a new database is selected" do
1182+
client = new_client(database: 'test_db0')
1183+
client.query('SET session_track_schema=off')
1184+
expect(client.db).to eq('test_db0')
1185+
expect { client.query('USE test_db1') }.not_to change { client.db }
1186+
end
1187+
end
1188+
end
1189+
11471190
it "#thread_id should return a boolean" do
11481191
expect(@client.ping).to eql(true)
11491192
@client.close

0 commit comments

Comments
 (0)