Skip to content

On connect, set all variables in a single SET statement #1099

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ type mysqlConn struct {

// Handles parameters set in DSN after the connection is established
func (mc *mysqlConn) handleParams() (err error) {
var params []string
for param, val := range mc.cfg.Params {
switch param {
// Charset
// Charset: character_set_connection, character_set_client, character_set_results
case "charset":
charsets := strings.Split(val, ",")
for i := range charsets {
Expand All @@ -63,12 +64,16 @@ func (mc *mysqlConn) handleParams() (err error) {
return
}

// System Vars
// Other system vars
default:
err = mc.exec("SET " + param + "=" + val + "")
if err != nil {
return
}
params = append(params, param+"="+val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (not a must): This allocates a new string for every param. Maybe using strings.Builder (and adding the , here) is the better approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there are improvement in this area. For this important pull request I wanted to apply the minimum change required. The string concatenation problem was in the original code, so I didn't want to change that and divert the reviewers from the main issue.

Now that this structural change is merged (thanks!), I am now able to propose some further implementation improvements.

}
}

if len(params) > 0 {
err = mc.exec("SET " + strings.Join(params, ","))
if err != nil {
return
}
}

Expand Down