Open(name string) (Conn, error)
}
+// DriverContext enhances the Driver interface by returning a Connector
+// rather then a single Conn.
+// It separates out the name parsing step from actually connecting to the
+// database. It also gives dialers access to the context by using the
+// Connector.
+type DriverContext interface {
+ // OpenConnector must parse the name in the same format that Driver.Open
+ // parses the name parameter.
+ OpenConnector(name string) (Connector, 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.
return fdriver
}
+type fakeDriverCtx struct {
+ fakeDriver
+}
+
+var _ driver.DriverContext = &fakeDriverCtx{}
+
+func (cc *fakeDriverCtx) OpenConnector(name string) (driver.Connector, error) {
+ return &fakeConnector{name: name}, nil
+}
+
type fakeDB struct {
name string
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
}
}
}
+func TestOpenConnector(t *testing.T) {
+ Register("testctx", &fakeDriverCtx{})
+ db, err := Open("testctx", "people")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+
+ if _, is := db.connector.(*fakeConnector); !is {
+ t.Fatal("not using *fakeConnector")
+ }
+}
+
type ctxOnlyDriver struct {
fakeDriver
}