Skip to content

Commit 51502e0

Browse files
committed
Add OpenConnector to Driver to satisfy DriverContext interface
sql.Open will use driver's OpenConnector instead of its Open method. This will enable TCP keepalive support. Note, testDriver was added for TestRuntimeParameters since it expects old fashioned Driver interface (without OpenConnector).
1 parent d9d4371 commit 51502e0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

conn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
4848
return Open(name)
4949
}
5050

51+
// OpenConnector parses the name in the same format that Driver.Open
52+
// parses the name parameter.
53+
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
54+
return NewConnector(name)
55+
}
56+
5157
func init() {
5258
sql.Register("postgres", &Driver{})
5359
}

conn_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ import (
1414
"time"
1515
)
1616

17+
// testDriver is the Postgres database driver that doesn't implement DriverContext interface.
18+
type testDriver struct{}
19+
20+
func (d *testDriver) Open(name string) (driver.Conn, error) {
21+
return Open(name)
22+
}
23+
24+
func init() {
25+
sql.Register("postgres-test", &testDriver{})
26+
}
27+
1728
type Fatalistic interface {
1829
Fatal(args ...interface{})
1930
}
@@ -48,7 +59,7 @@ func testConninfo(conninfo string) string {
4859
}
4960

5061
func openTestConnConninfo(conninfo string) (*sql.DB, error) {
51-
return sql.Open("postgres", testConninfo(conninfo))
62+
return sql.Open("postgres-test", testConninfo(conninfo))
5263
}
5364

5465
func openTestConn(t Fatalistic) *sql.DB {

0 commit comments

Comments
 (0)