Skip to content

Commit

Permalink
driver: allow configuration of the driver name (#958)
Browse files Browse the repository at this point in the history
* driver: allow configuration of the driver name

* Update README

* Update README

---------

Co-authored-by: lance6716 <[email protected]>
  • Loading branch information
dveeden and lance6716 authored Dec 6, 2024
1 parent 615af30 commit c6114ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:3306/test",
}))
```

### Custom NamedValueChecker

Golang allows for custom handling of query arguments before they are passed to the driver
Expand Down
4 changes: 3 additions & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c6114ee

Please sign in to comment.