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

v1.9.0: No way to set charset when you're not using DSN. #1678

Closed
chrisgavin opened this issue Feb 19, 2025 · 1 comment
Closed

v1.9.0: No way to set charset when you're not using DSN. #1678

chrisgavin opened this issue Feb 19, 2025 · 1 comment

Comments

@chrisgavin
Copy link

Issue description

While upgrading from v1.8.1 to v1.9.0 I noticed that it no longer seems to be possible to set a charset when you're constructing a mysql.Config object yourself rather than parsing a DSN.

In 1.8.1, you would set your charset with something like:

cfg := mysql.NewConfig()
cfg.Params = map[string]string{
	"charset": "utf8mb4",
}

which would be translated to a SET NAMES call in connection.go. This logic seems to have been removed in v1.9.0, and specifying the character set in this way results in an error as it is now translated to SET charset = utf8mb4.

Instead in v1.9.0, there is a charsets option on mysql.Config, which is translated into SET NAMES in connector.go.

Unfortunately this field is private, so the only way of setting it appears to by using mysql.ParseDSN to construct your config, rather than mysql.NewConfig. I was just wondering if it was expected that this field was private. Is there some way of setting it (other than the slightly horrible mysql.ParseDSN(cfg.FormatDSN() + "&charset=utf8mb4"))?

Configuration

Driver version (or git SHA): v1.9.0

Go version: 1.24.0

Server version: MySQL 8.0.41

Server OS: Ubuntu 22.04

@methane
Copy link
Member

methane commented Feb 20, 2025

Sorry about that.

Is there some way of setting it (other than the slightly horrible mysql.ParseDSN(cfg.FormatDSN() + "&charset=utf8mb4"))?

As you said, you can use mysql.ParseDSN("/?charset=utf8mb4") instead of mysql.NewConfig().

FormatDSN + ParseDSN doesn't promise roundtrip.

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

No branches or pull requests

2 participants