From 87ce8d8083f32215eed711eeb9b797001ac52d5c Mon Sep 17 00:00:00 2001 From: Richard Larocque Date: Wed, 23 Feb 2022 12:52:12 -0800 Subject: [PATCH] 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. --- ext/mysql2/client.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 04bf2cc8..8bb737e0 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -1259,6 +1259,22 @@ static VALUE rb_mysql_client_encoding(VALUE self) { return wrapper->encoding; } +/* call-seq: + * client.db + * + * Returns the currently selected db. + */ +static VALUE rb_mysql_client_db(VALUE self) { + GET_CLIENT(self); + + char *db = wrapper->client->db; + if (!db) { + return Qnil; + } + + return rb_str_new_cstr(wrapper->client->db); +} + /* call-seq: * client.automatic_close? * @@ -1500,6 +1516,7 @@ void init_mysql2_client() { rb_define_method(cMysql2Client, "ssl_cipher", rb_mysql_get_ssl_cipher, 0); rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0); rb_define_method(cMysql2Client, "session_track", rb_mysql_client_session_track, 1); + rb_define_method(cMysql2Client, "db", rb_mysql_client_db, 0); rb_define_private_method(cMysql2Client, "connect_timeout=", set_connect_timeout, 1); rb_define_private_method(cMysql2Client, "read_timeout=", set_read_timeout, 1);