From c6114ee028136ab25d3ac3a3cfd93e6f08fbad00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 6 Dec 2024 11:53:57 +0100 Subject: [PATCH] driver: allow configuration of the driver name (#958) * driver: allow configuration of the driver name * Update README * Update README --------- Co-authored-by: lance6716 --- README.md | 19 +++++++++++++++++++ driver/driver.go | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) 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.