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:
parent
ac3be4148e
commit
e884f059a4
|
@ -51,7 +51,7 @@ type Engine struct {
|
||||||
|
|
||||||
// NewEngine new a db manager according to the parameter. Currently support four
|
// NewEngine new a db manager according to the parameter. Currently support four
|
||||||
// drivers
|
// 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)
|
dialect, err := dialects.OpenDialect(driverName, dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -62,6 +62,12 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, driverOption := range driverOptions {
|
||||||
|
if err := driverOption(db.DB); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return newEngine(driverName, dataSourceName, dialect, db)
|
return newEngine(driverName, dataSourceName, dialect, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue