From b44b2feb914d866250f2efb744b24de0ce8c161a Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Tue, 14 Nov 2017 08:53:56 -0800 Subject: database/sql: allow OpenConnector in a driver.Driver interface While driver.Connector was previously added to allow non-string connection arguments and access to the context, most users of the sql package will continue to rely on a string DSN. Allow drivers to implement a string DSN to Connector interface that both allows a single parsing of the string DSN and uses the Connector interface which passes available context to the driver dialer. Fixes #22713 Change-Id: Ia0b862262f4c4670effe2538d0d6d43733fea18d Reviewed-on: https://go-review.googlesource.com/77550 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor --- src/database/sql/sql.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/database/sql/sql.go') diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index 30b4ad3609..1192eaae26 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -662,6 +662,14 @@ func Open(driverName, dataSourceName string) (*DB, error) { return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName) } + if driverCtx, ok := driveri.(driver.DriverContext); ok { + connector, err := driverCtx.OpenConnector(dataSourceName) + if err != nil { + return nil, err + } + return OpenDB(connector), nil + } + return OpenDB(dsnConnector{dsn: dataSourceName, driver: driveri}), nil } -- cgit v1.3