Skip to content

Commit c6114ee

Browse files
dveedenlance6716
andauthored
driver: allow configuration of the driver name (#958)
* driver: allow configuration of the driver name * Update README * Update README --------- Co-authored-by: lance6716 <[email protected]>
1 parent 615af30 commit c6114ee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,25 @@ func main() {
460460
}
461461
```
462462

463+
### Custom Driver Name
464+
465+
A custom driver name can be set via build options: `-ldflags '-X "github.com/go-mysql-org/go-mysql/driver.driverName=gomysql"'`.
466+
467+
This can be useful when using [GORM](https://gorm.io/docs/connecting_to_the_database.html#Customize-Driver):
468+
469+
```go
470+
import (
471+
_ "github.com/go-mysql-org/go-mysql/driver"
472+
"gorm.io/driver/mysql"
473+
"gorm.io/gorm"
474+
)
475+
476+
db, err := gorm.Open(mysql.New(mysql.Config{
477+
DriverName: "gomysql",
478+
DSN: "gorm:[email protected]:3306/test",
479+
}))
480+
```
481+
463482
### Custom NamedValueChecker
464483

465484
Golang allows for custom handling of query arguments before they are passed to the driver

driver/driver.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,15 @@ func (r *rows) Next(dest []sqldriver.Value) error {
389389
return nil
390390
}
391391

392+
var driverName = "mysql"
393+
392394
func init() {
393395
options["compress"] = CompressOption
394396
options["collation"] = CollationOption
395397
options["readTimeout"] = ReadTimeoutOption
396398
options["writeTimeout"] = WriteTimeoutOption
397399

398-
sql.Register("mysql", driver{})
400+
sql.Register(driverName, driver{})
399401
}
400402

401403
// SetCustomTLSConfig sets a custom TLSConfig for the address (host:port) of the supplied DSN.

0 commit comments

Comments
 (0)