From 47101ab87f60d10d71cf3bd7ab9753c9d5aec543 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 17 Feb 2023 23:54:50 +0300 Subject: [PATCH] remove ErrUnsupported and fixes Signed-off-by: Vasiliy Tolstov --- README.md | 1 + common.go | 3 --- conn.go | 25 +++++++++++-------------- driver.go | 2 +- options.go | 2 +- 5 files changed, 14 insertions(+), 19 deletions(-) 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 {