2021-10-27 18:49:33 +03:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
2023-01-06 23:24:39 +03:00
|
|
|
"context"
|
2021-10-27 18:49:33 +03:00
|
|
|
"database/sql/driver"
|
2023-01-06 23:24:39 +03:00
|
|
|
"time"
|
2021-10-27 18:49:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// wrapperDriver defines a wrapper for driver.Driver
|
|
|
|
type wrapperDriver struct {
|
|
|
|
driver driver.Driver
|
|
|
|
opts Options
|
2023-01-16 23:38:29 +03:00
|
|
|
ctx context.Context
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWrapper creates and returns a new SQL driver with passed capabilities
|
|
|
|
func NewWrapper(d driver.Driver, opts ...Option) driver.Driver {
|
2023-01-16 23:38:29 +03:00
|
|
|
return &wrapperDriver{driver: d, opts: NewOptions(opts...), ctx: context.Background()}
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements driver.Driver Open
|
|
|
|
func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
|
2023-01-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2021-10-27 18:49:33 +03:00
|
|
|
c, err := w.driver.Open(name)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
2023-01-16 23:38:29 +03:00
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(w.ctx, "Open", labelUnknown, td, err)...).Log(w.ctx, w.opts.LoggerLevel)
|
2023-01-06 23:24:39 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-06 23:24:39 +03:00
|
|
|
|
2023-01-16 23:38:29 +03:00
|
|
|
return &wrapperConn{conn: c, opts: w.opts, ctx: w.ctx}, nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|