Skip to content

Commit

Permalink
Implement database/sql/driver.DriverContext
Browse files Browse the repository at this point in the history
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 Sep 27, 2019
1 parent 931b5ae commit 9dc9624
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
return Open(name)
}

// OpenConnector sets up a new Connector, ready to create connections.
//
// 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()
}

0 comments on commit 9dc9624

Please sign in to comment.