Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose db attribute of MYSQL client struct #1245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

richardlarocque
Copy link

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.

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.
@richardlarocque
Copy link
Author

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 #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.

@richardlarocque
Copy link
Author

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!

@sodabrew
Copy link
Collaborator

sodabrew commented May 2, 2022

Thanks for the nudge!

@sodabrew
Copy link
Collaborator

sodabrew commented Dec 4, 2024

Catching up on very old PRs! This looks OK, but it should be named database to be consistent with the constructor:

mysql2/lib/mysql2/client.rb

Lines 168 to 169 in 58f8d00

warn "The options :user, :pass, :hostname, :dbname, :db, and :sock are deprecated and will be removed at some point in the future."
warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants