diff --git a/README.md b/README.md index 5aaa3ac..83c7039 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ func Connect(cfg *PostgresConfig) (*sqlx.DB, error) { t.Fatal(err) } + // for postgres user stdlib.GetDefaultDriver() or it fails sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{}, wrapper.DatabaseHost("localhost"), wrapper.DatabaseName("mydb"), diff --git a/common.go b/common.go index b74452d..ce414b4 100644 --- a/common.go +++ b/common.go @@ -6,9 +6,6 @@ import ( "fmt" ) -// ErrUnsupported is an error returned when the underlying driver doesn't provide a given function. -var ErrUnsupported = errors.New("operation unsupported by the underlying driver") - // namedValueToValue converts driver arguments of NamedValue format to Value format. Implemented in the same way as in // database/sql ctxutil.go. func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) { diff --git a/conn.go b/conn.go index 41deb4a..d79e8ae 100644 --- a/conn.go +++ b/conn.go @@ -13,9 +13,11 @@ var _ driver.Conn = &wrapperConn{} // wrapperConn defines a wrapper for driver.Conn type wrapperConn struct { - conn driver.Conn - opts Options - ctx context.Context + d *wrapperDriver + dname string + conn driver.Conn + opts Options + ctx context.Context } // Prepare implements driver.Conn Prepare @@ -243,10 +245,7 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er // nolint:staticcheck execer, ok := w.conn.(driver.Execer) if !ok { - if w.opts.LoggerEnabled { - w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) - } - return nil, ErrUnsupported + return nil, driver.ErrSkip } labels := []string{labelMethod, "Exec", labelQuery, labelUnknown} @@ -341,10 +340,11 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv func (w *wrapperConn) Ping(ctx context.Context) error { conn, ok := w.conn.(driver.Pinger) if !ok { - if w.opts.LoggerEnabled { - w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) + wc, err := w.d.Open(w.dname) + if err != nil { + return err } - return ErrUnsupported + return wc.Close() } var nctx context.Context @@ -389,10 +389,7 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err //nolint:staticcheck conn, ok := w.conn.(driver.Queryer) if !ok { - if w.opts.LoggerEnabled { - w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) - } - return nil, ErrUnsupported + return nil, driver.ErrSkip } labels := []string{labelMethod, "Query", labelQuery, labelUnknown} diff --git a/driver.go b/driver.go index 6b9e4dc..cb60634 100644 --- a/driver.go +++ b/driver.go @@ -32,5 +32,5 @@ func (w *wrapperDriver) Open(name string) (driver.Conn, error) { return nil, err } - return &wrapperConn{conn: c, opts: w.opts, ctx: w.ctx}, nil + return &wrapperConn{d: w, dname: name, conn: c, opts: w.opts, ctx: w.ctx}, nil } diff --git a/options.go b/options.go index c253b3e..eff4053 100644 --- a/options.go +++ b/options.go @@ -78,7 +78,7 @@ func NewOptions(opts ...Option) Options { Tracer: tracer.DefaultTracer, MeterStatsInterval: DefaultMeterStatsInterval, MeterMetricPrefix: DefaultMeterMetricPrefix, - LoggerLevel: logger.ErrorLevel, + LoggerLevel: logger.DebugLevel, LoggerObserver: DefaultLoggerObserver, } for _, o := range opts {