From e884f059a4c1d488816d2cb4b10b7ae18170f18d Mon Sep 17 00:00:00 2001 From: James Marino Date: Mon, 22 Jan 2024 08:24:18 +0000 Subject: [PATCH] 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 Reviewed-on: https://gitea.com/xorm/xorm/pulls/2369 Reviewed-by: Lunny Xiao Co-authored-by: James Marino Co-committed-by: James Marino --- engine.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/engine.go b/engine.go index 459c2f44..e16bb3e8 100644 --- a/engine.go +++ b/engine.go @@ -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) }