diff --git a/README.md b/README.md index facc91b4b..5fed0b27f 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,25 @@ func main() { } ``` +### Custom Driver Name + +A custom driver name can be set via build options: `-ldflags '-X "github.com/go-mysql-org/go-mysql/driver.driverName=gomysql"'`. + +This can be useful when using [GORM](https://gorm.io/docs/connecting_to_the_database.html#Customize-Driver): + +```go +import ( + _ "github.com/go-mysql-org/go-mysql/driver" + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +db, err := gorm.Open(mysql.New(mysql.Config{ + DriverName: "gomysql", + DSN: "gorm:gorm@127.0.0.1:3306/test", +})) +``` + ### Custom NamedValueChecker Golang allows for custom handling of query arguments before they are passed to the driver diff --git a/driver/driver.go b/driver/driver.go index 4054db2af..fa36dc8e0 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -389,13 +389,15 @@ func (r *rows) Next(dest []sqldriver.Value) error { return nil } +var driverName = "mysql" + func init() { options["compress"] = CompressOption options["collation"] = CollationOption options["readTimeout"] = ReadTimeoutOption options["writeTimeout"] = WriteTimeoutOption - sql.Register("mysql", driver{}) + sql.Register(driverName, driver{}) } // SetCustomTLSConfig sets a custom TLSConfig for the address (host:port) of the supplied DSN.