mysql2
mysql2 copied to clipboard
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.
I'll admit I'm not very familiar with the MySQL client libraries or Ruby extensions, so noob mistakes are a strong possibility here.
I started working on this when I saw https://github.com/brianmario/mysql2/pull/1092 didn't work the way I expected. The call to c.session_track(Mysql2::Client::SESSION_TRACK_SCHEMA) would return the current schema if it was run immediately after a USE statement, but would otherwise return nil.
I dug into the client code on the MySQL side and found this: https://github.com/mysql/mysql-server/blob/0cd98bdf981583a1cf4cb526581fc16e23bb839b/sql-common/client.c#L929-L942
It's updating the db attribute whenever it gets back a SESSION_TRACK_SCHEMA change notification. Unlike the "info" that only lasts until the next "OK" packet, this information sticks around.
Hence this PR to expose the db field.
Now, does the MySQL client library guarantee that it keeps this db field up to date and it's safe to take a dependency on it? I'm not sure. I wasn't able to turn up any docs that say so. I'm just expecting this to work based on my reading of the code.
Hi @sodabrew. This is a friendly reminder of PR. You mentioned in an earlier iteration that you'd be willing to review. If you can take a look at this one (now with tests!) I'd appreciate it.
Thanks!
Thanks for the nudge!