fix: allow driver options to allow the customisation of the driver after database open call (#2369)

Function can now be passed in to allow customisation to the open database.

For example, with the Oracle database driver `sijms/go-ora` one may want to do the following:

```go
engine, err := xorm.NewEngine("oracle", connectionString, func(db *sql.DB) error {
    return oracle.AddSessionParam(db, "NLS_TIMESTAMP_FORMAT", "RR-MM-DD HH24:MI:SS")
})
```

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2369
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: James Marino <james@marino.io>
Co-committed-by: James Marino <james@marino.io>
This commit is contained in:
James Marino 2024-01-22 08:24:18 +00:00 committed by Lunny Xiao
parent ac3be4148e
commit e884f059a4
1 changed files with 7 additions and 1 deletions

View File

@ -51,7 +51,7 @@ type Engine struct {
// NewEngine new a db manager according to the parameter. Currently support four
// drivers
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
func NewEngine(driverName string, dataSourceName string, driverOptions ...func(db *sql.DB) error) (*Engine, error) {
dialect, err := dialects.OpenDialect(driverName, dataSourceName)
if err != nil {
return nil, err
@ -62,6 +62,12 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
return nil, err
}
for _, driverOption := range driverOptions {
if err := driverOption(db.DB); err != nil {
return nil, err
}
}
return newEngine(driverName, dataSourceName, dialect, db)
}