-
Notifications
You must be signed in to change notification settings - Fork 18
/
unified_querier.go
58 lines (50 loc) · 1.62 KB
/
unified_querier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package db
import (
"context"
"database/sql"
"errors"
"reflect"
)
// unifiedQuerier wraps the Querier subset of methods of the standard sqlx.DB with helpers to return our standard
// errors.
type unifiedQuerier struct {
q Querier
}
func (u unifiedQuerier) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
result, err := u.q.ExecContext(ctx, query, args...)
return result, mapExecErrors(err, result)
}
func (u unifiedQuerier) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
err := u.q.GetContext(ctx, dest, query, args...)
if errors.Is(err, sql.ErrNoRows) {
return ErrNop
}
_, err = mapError(err)
return err
}
func (u unifiedQuerier) NamedGetContext(ctx context.Context, dest interface{}, query string, arg interface{}) error {
err := u.q.NamedGetContext(ctx, dest, query, arg)
if errors.Is(err, sql.ErrNoRows) {
return ErrNop
}
_, err = mapError(err)
return err
}
func (u unifiedQuerier) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
result, err := u.q.NamedExecContext(ctx, query, arg)
return result, mapExecErrors(err, result)
}
func (u unifiedQuerier) SelectContext(ctx context.Context,
dest interface{}, query string, args ...interface{}) error {
if err := u.q.SelectContext(ctx, dest, query, args...); err != nil {
_, err = mapError(err)
return err // This error never represents the no rows condition
}
// SelectContext has asserted dest is a pointer to a slice
value := reflect.ValueOf(dest)
direct := reflect.Indirect(value)
if direct.Len() == 0 {
return ErrNop
}
return nil
}