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 <kardianos@gmail.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
James Lawrence 2017-08-05 06:04:19 -04:00 committed by Daniel Theophanes
parent 6936671ed1
commit e6358c798b
3 changed files with 84 additions and 20 deletions

View file

@ -81,6 +81,13 @@ func newTestDB(t testing.TB, name string) *DB {
return db
}
func TestOpenDB(t *testing.T) {
db := OpenDB(dsnConnector{dsn: fakeDBName, driver: fdriver})
if db.Driver() != fdriver {
t.Fatalf("OpenDB should return the driver of the Connector")
}
}
func TestDriverPanic(t *testing.T) {
// Test that if driver panics, database/sql does not deadlock.
db, err := Open("test", fakeDBName)
@ -1672,7 +1679,7 @@ func TestIssue4902(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
driver := db.driver.(*fakeDriver)
driver := db.Driver().(*fakeDriver)
opens0 := driver.openCount
var stmt *Stmt
@ -1765,7 +1772,7 @@ func TestMaxOpenConns(t *testing.T) {
db := newTestDB(t, "magicquery")
defer closeDB(t, db)
driver := db.driver.(*fakeDriver)
driver := db.Driver().(*fakeDriver)
// Force the number of open connections to 0 so we can get an accurate
// count for the test
@ -2057,7 +2064,7 @@ func TestConnMaxLifetime(t *testing.T) {
db := newTestDB(t, "magicquery")
defer closeDB(t, db)
driver := db.driver.(*fakeDriver)
driver := db.Driver().(*fakeDriver)
// Force the number of open connections to 0 so we can get an accurate
// count for the test
@ -2146,7 +2153,7 @@ func TestStmtCloseDeps(t *testing.T) {
db := newTestDB(t, "magicquery")
defer closeDB(t, db)
driver := db.driver.(*fakeDriver)
driver := db.Driver().(*fakeDriver)
driver.mu.Lock()
opens0 := driver.openCount
@ -3071,7 +3078,7 @@ func TestIssue6081(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
drv := db.driver.(*fakeDriver)
drv := db.Driver().(*fakeDriver)
drv.mu.Lock()
opens0 := drv.openCount
closes0 := drv.closeCount
@ -3326,7 +3333,7 @@ func TestConnectionLeak(t *testing.T) {
// Now we have defaultMaxIdleConns busy connections. Open
// a new one, but wait until the busy connections are released
// before returning control to DB.
drv := db.driver.(*fakeDriver)
drv := db.Driver().(*fakeDriver)
drv.waitCh = make(chan struct{}, 1)
drv.waitingCh = make(chan struct{}, 1)
var wg sync.WaitGroup