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
|
|
|
)
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
var (
|
|
|
|
// _ driver.DriverContext = (*wrapperDriver)(nil)
|
|
|
|
// _ driver.Connector = (*wrapperDriver)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type conn interface {
|
|
|
|
driver.Pinger
|
|
|
|
driver.Execer
|
|
|
|
driver.ExecerContext
|
|
|
|
driver.Queryer
|
|
|
|
driver.QueryerContext
|
|
|
|
driver.Conn
|
|
|
|
driver.ConnPrepareContext
|
|
|
|
driver.ConnBeginTx
|
|
|
|
}
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
// wrapperDriver defines a wrapper for driver.Driver
|
|
|
|
type wrapperDriver struct {
|
|
|
|
driver driver.Driver
|
|
|
|
opts Options
|
2023-09-01 00:21:08 +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
|
|
|
}
|
|
|
|
|
2023-09-01 00:21:08 +03:00
|
|
|
type wrappedConnector struct {
|
|
|
|
connector driver.Connector
|
|
|
|
name string
|
|
|
|
opts Options
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWrapperConnector(c driver.Connector, opts ...Option) driver.Connector {
|
|
|
|
return &wrappedConnector{connector: c, opts: NewOptions(opts...), ctx: context.Background()}
|
|
|
|
}
|
|
|
|
|
2023-06-10 13:09:25 +03:00
|
|
|
// Connect implements driver.Driver Connect
|
2023-09-01 00:21:08 +03:00
|
|
|
func (w *wrappedConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
|
|
|
return w.connector.Connect(ctx)
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Driver implements driver.Driver Driver
|
2023-09-01 00:21:08 +03:00
|
|
|
func (w *wrappedConnector) Driver() driver.Driver {
|
|
|
|
return w.connector.Driver()
|
2023-06-10 13:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// Connect implements driver.Driver OpenConnector
|
|
|
|
func (w *wrapperDriver) OpenConnector(name string) (driver.Conn, error) {
|
|
|
|
return &wrapperConnector{driver: w.driver, name: name, opts: w.opts}, nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
// Open implements driver.Driver Open
|
|
|
|
func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
|
2023-06-10 13:09:25 +03:00
|
|
|
// ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // Ensure eventual timeout
|
|
|
|
// defer cancel()
|
|
|
|
|
|
|
|
/*
|
|
|
|
connector, err := w.OpenConnector(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return connector.Connect(ctx)
|
|
|
|
*/
|
|
|
|
|
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 {
|
2024-04-11 02:04:15 +03:00
|
|
|
w.opts.Logger.Log(w.ctx, w.opts.LoggerLevel, w.opts.LoggerObserver(w.ctx, "Open", getCallerName(), td, err)...)
|
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-06-10 13:09:25 +03:00
|
|
|
return wrapConn(c, w.opts), nil
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|