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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement database/sql/driver.DriverContext
In order to fully instrument SQL operations (including new conns)
by wrapping this driver with something like
`github.com/luna-duclos/instrumentedsql`, this driver needs to
implement `database/sql/driver.DriverContext`.

Now it does.
  • Loading branch information
dcormier committed Mar 22, 2020
commit 8230504bc7bac2aaba481c2e658e3fe8b975beec
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()
}