Skip to content

Create a mysql.Connect API. #1045

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

Closed
wants to merge 2 commits into from
Closed

Conversation

methane
Copy link
Member

@methane methane commented Dec 26, 2019

Description

This is a counter-proposal to #1042.

You can create a custom Connector object using this new API:

import (
    "database/sql"
    "database/sql/driver"
    "github.com/go-sql-driver/mysql"
)

type CustomConnector struct {
    cfg *mysql.Config
}

func (cc *CustomConnector) Connect(ctx Context) (driver.Conn, error) {
    cc.cfg.Host = getCurrentMySQLHost()
    cfg.User, cfg.Password = getCurrentMySQLAuth()
    return mysql.Connect(ctx, cc.cfg)
}

func (cc *CustomConnector) Driver() driver.Driver {
    return nil
}

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

// You can use this function to create custom connector which dynamically
// change the config. The cfg object must not be changed during calling
// this function.
func Connect(ctx context.Context, cfg *Config) (driver.Conn, error) {

Choose a reason for hiding this comment

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

Why use a pointer here if it's sensitive to concurrent changes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because shallow copy of the config object is not a right way to copy config object.

Co-Authored-By: Márk Sági-Kazár <[email protected]>
@julienschmidt julienschmidt added this to the v1.6.0 milestone Jan 3, 2020
@dolmen
Copy link
Contributor

dolmen commented Jun 1, 2020

This patch extends the API surface while it doesn't seem necessary (at least the description doesn't tell why it is necessary).

As a counter example I implemented years ago a driver that wraps the MySQL driver to load creadentials from the ~/.mylogin.cnf without needing this extended API.
See my packages:

@dolmen
Copy link
Contributor

dolmen commented Jun 1, 2020

If the need is to avoid formatting+parsing of DSNs, the mysql.NewConnector API already exists and the connector can be used with sql.OpenDB. So we don't need to extend the API.

@methane
Copy link
Member Author

methane commented Jun 2, 2020

If the need is to avoid formatting+parsing of DSNs, the mysql.NewConnector API already exists and the connector can be used with sql.OpenDB. So we don't need to extend the API.

Of course, I know it. I am the who proposed it first.

Please read this issue and issue #1042 carefully. The need is not avoiding to parse DSN, but change the configuration (e.g. credential, server IP address, etc) on the fly.

My main intension is to promote custom connector, instead of adding some dynamic (callback) options to the config object.

Adding new API to help custom connector is not my main point. It is just a little helper function. I'm OK to reject this PR if people don't think this helper function is not worth enough.

@methane methane closed this Mar 2, 2021
@methane methane deleted the connect branch March 2, 2021 02:55
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.

4 participants