remove ErrUnsupported and fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-02-17 23:54:50 +03:00
parent 38a14743de
commit 47101ab87f
5 changed files with 14 additions and 19 deletions

View File

@ -50,6 +50,7 @@ func Connect(cfg *PostgresConfig) (*sqlx.DB, error) {
t.Fatal(err) t.Fatal(err)
} }
// for postgres user stdlib.GetDefaultDriver() or it fails
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{}, sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
wrapper.DatabaseHost("localhost"), wrapper.DatabaseHost("localhost"),
wrapper.DatabaseName("mydb"), wrapper.DatabaseName("mydb"),

View File

@ -6,9 +6,6 @@ import (
"fmt" "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 // namedValueToValue converts driver arguments of NamedValue format to Value format. Implemented in the same way as in
// database/sql ctxutil.go. // database/sql ctxutil.go.
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) { func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {

25
conn.go
View File

@ -13,9 +13,11 @@ var _ driver.Conn = &wrapperConn{}
// wrapperConn defines a wrapper for driver.Conn // wrapperConn defines a wrapper for driver.Conn
type wrapperConn struct { type wrapperConn struct {
conn driver.Conn d *wrapperDriver
opts Options dname string
ctx context.Context conn driver.Conn
opts Options
ctx context.Context
} }
// Prepare implements driver.Conn Prepare // Prepare implements driver.Conn Prepare
@ -243,10 +245,7 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
// nolint:staticcheck // nolint:staticcheck
execer, ok := w.conn.(driver.Execer) execer, ok := w.conn.(driver.Execer)
if !ok { if !ok {
if w.opts.LoggerEnabled { return nil, driver.ErrSkip
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Exec", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
} }
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown} 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 { func (w *wrapperConn) Ping(ctx context.Context) error {
conn, ok := w.conn.(driver.Pinger) conn, ok := w.conn.(driver.Pinger)
if !ok { if !ok {
if w.opts.LoggerEnabled { wc, err := w.d.Open(w.dname)
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Ping", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel) if err != nil {
return err
} }
return ErrUnsupported return wc.Close()
} }
var nctx context.Context var nctx context.Context
@ -389,10 +389,7 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
//nolint:staticcheck //nolint:staticcheck
conn, ok := w.conn.(driver.Queryer) conn, ok := w.conn.(driver.Queryer)
if !ok { if !ok {
if w.opts.LoggerEnabled { return nil, driver.ErrSkip
w.opts.Logger.Fields(w.opts.LoggerObserver(ctx, "Query", labelUnknown, 0, ErrUnsupported)...).Log(ctx, w.opts.LoggerLevel)
}
return nil, ErrUnsupported
} }
labels := []string{labelMethod, "Query", labelQuery, labelUnknown} labels := []string{labelMethod, "Query", labelQuery, labelUnknown}

View File

@ -32,5 +32,5 @@ func (w *wrapperDriver) Open(name string) (driver.Conn, error) {
return nil, err 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
} }

View File

@ -78,7 +78,7 @@ func NewOptions(opts ...Option) Options {
Tracer: tracer.DefaultTracer, Tracer: tracer.DefaultTracer,
MeterStatsInterval: DefaultMeterStatsInterval, MeterStatsInterval: DefaultMeterStatsInterval,
MeterMetricPrefix: DefaultMeterMetricPrefix, MeterMetricPrefix: DefaultMeterMetricPrefix,
LoggerLevel: logger.ErrorLevel, LoggerLevel: logger.DebugLevel,
LoggerObserver: DefaultLoggerObserver, LoggerObserver: DefaultLoggerObserver,
} }
for _, o := range opts { for _, o := range opts {