Merge pull request #69 from unistack-org/fixes

remove ErrUnsupported and fixes
This commit is contained in:
Василий Толстов 2023-02-18 00:00:42 +03:00 committed by GitHub
commit 5106334858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}
// for postgres user stdlib.GetDefaultDriver() or it fails
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
wrapper.DatabaseHost("localhost"),
wrapper.DatabaseName("mydb"),

View File

@ -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) {

25
conn.go
View File

@ -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}

View File

@ -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
}

View File

@ -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 {