Skip to content
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
1 change: 1 addition & 0 deletions conn_go110.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.10
// +build go1.10

package sqlmw
Expand Down
1 change: 1 addition & 0 deletions conn_go115.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.15
// +build go1.15

package sqlmw
Expand Down
1 change: 1 addition & 0 deletions conn_go19.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.9
// +build go1.9

package sqlmw
Expand Down
7 changes: 7 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.10
// +build go1.10

package sqlmw
Expand All @@ -16,6 +17,12 @@ var (
_ driver.Connector = wrappedConnector{}
)

// WrapConnector returns the supplied driver.Connector wrapped in a new object that has all of its calls intercepted by
// the Interceptor in the supplied driver.
func WrapConnector(driverRef *wrappedDriver, connector driver.Connector) wrappedConnector {
return wrappedConnector{driverRef: driverRef, parent: connector}
}

func (c wrappedConnector) Connect(ctx context.Context) (conn driver.Conn, err error) {
conn, err = c.driverRef.intr.ConnectorConnect(ctx, c.parent)
if err != nil {
Expand Down
14 changes: 10 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ var (
_ driver.Driver = wrappedDriver{}
)

// WrapDriver will wrap the passed SQL driver and return a new sql driver that uses it and also logs and traces calls using the passed logger and tracer
// The returned driver will still have to be registered with the sql package before it can be used.
// WrapDriver returns the supplied driver.Driver wrapped in a new object that has all of its calls intercepted by the
// supplied Interceptor object.
//
// Important note: Seeing as the context passed into the various instrumentation calls this package calls,
// Any call without a context passed will not be intercepted. Please be sure to use the ___Context() and BeginTx()
// function calls added in Go 1.8 instead of the older calls which do not accept a context.
func WrapDriver(driver driver.Driver, intr Interceptor) wrappedDriver {
return wrappedDriver{parent: driver, intr: intr}
}

// Driver returns the supplied driver.Driver with a new object that has all of its calls intercepted by the supplied
// Interceptor object.
// WrapDriver returns the supplied driver.Driver wrapped in a new object that has all of its calls intercepted by the
// supplied Interceptor object.
//
// Important note: Seeing as the context passed into the various instrumentation calls this package calls,
// Any call without a context passed will not be intercepted. Please be sure to use the ___Context() and BeginTx()
Expand Down
9 changes: 3 additions & 6 deletions driver_go110.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.10
// +build go1.10

package sqlmw
Expand All @@ -9,15 +10,11 @@ var _ driver.DriverContext = wrappedDriver{}
func (d wrappedDriver) OpenConnector(name string) (driver.Connector, error) {
driver, ok := d.parent.(driver.DriverContext)
if !ok {
return wrappedConnector{
parent: dsnConnector{dsn: name, driver: d.parent},
driverRef: &d,
}, nil
return WrapConnector(&d, dsnConnector{dsn: name, driver: d.parent}), nil
}
conn, err := driver.OpenConnector(name)
if err != nil {
return nil, err
}

return wrappedConnector{parent: conn, driverRef: &d}, nil
return WrapConnector(&d, conn), nil
}