From e6358c798b3001e98b6f7f4c3e2d906cf48533b2 Mon Sep 17 00:00:00 2001 From: James Lawrence Date: Sat, 5 Aug 2017 06:04:19 -0400 Subject: database/sql: add OpenDB to directly create a *DB without a DSN. The current Open method limits the ability for driver maintainers to expose options for their drivers by forcing all the configuration to pass through the DSN in order to create a *DB. This CL allows driver maintainers to write their own initialization functions that return a *DB making configuration of the underlying drivers easier. Fixes #20268 Change-Id: Ib10b794f36a201bbb92c23999c8351815d38eedb Reviewed-on: https://go-review.googlesource.com/53430 Reviewed-by: Daniel Theophanes Run-TryBot: Daniel Theophanes TryBot-Result: Gobot Gobot --- src/database/sql/driver/driver.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/database/sql/driver') diff --git a/src/database/sql/driver/driver.go b/src/database/sql/driver/driver.go index 0262ca24ba..f5a2e7c16c 100644 --- a/src/database/sql/driver/driver.go +++ b/src/database/sql/driver/driver.go @@ -55,6 +55,29 @@ type Driver interface { Open(name string) (Conn, error) } +// Connector is an optional interface that drivers can implement. +// It allows drivers to provide more flexible methods to open +// database connections without requiring the use of a DSN string. +type Connector interface { + // Connect returns a connection to the database. + // Connect may return a cached connection (one previously + // closed), but doing so is unnecessary; the sql package + // maintains a pool of idle connections for efficient re-use. + // + // The provided context.Context is for dialing purposes only + // (see net.DialContext) and should not be stored or used for + // other purposes. + // + // The returned connection is only used by one goroutine at a + // time. + Connect(context.Context) (Conn, error) + + // Driver returns the underlying Driver of the Connector, + // mainly to maintain compatibility with the Driver method + // on sql.DB. + Driver() Driver +} + // ErrSkip may be returned by some optional interfaces' methods to // indicate at runtime that the fast path is unavailable and the sql // package should continue as if the optional interface was not -- cgit v1.3