Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement database/sql/driver.DriverContext #900

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
return Open(name)
}

// OpenConnector sets up a new Connector, ready to create connections. name is
// a connection string. Most users should only use it through database/sql
// package from the standard library.
//
// Implements `database/sql/driver.DriverContext`.
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
return NewConnector(name)
}

func init() {
sql.Register("postgres", &Driver{})
}
Expand Down
21 changes: 21 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ func TestNewConnector_Driver(t *testing.T) {
}
txn.Rollback()
}

func TestNewConnector_DriverContext(t *testing.T) {
name := ""
d := &Driver{}
c, err := d.OpenConnector(name)
if err != nil {
t.Fatal(err)
}
db, err := c.Connect(context.Background())
if err != nil {
t.Fatal(err)
}
defer db.Close()
// database/sql might not call our Open at all unless we do something with
// the connection
txn, err := db.(driver.ConnBeginTx).BeginTx(context.Background(), driver.TxOptions{})
if err != nil {
t.Fatal(err)
}
txn.Rollback()
}