-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.go
57 lines (48 loc) · 1.61 KB
/
finder.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
package exql
import (
"context"
"github.com/loilo-inc/exql/v2/query"
)
// Finder is an interface to execute select query and map rows into the destination.
type Finder interface {
Find(q query.Query, destPtrOfStruct any) error
FindContext(ctx context.Context, q query.Query, destPtrOfStruct any) error
FindMany(q query.Query, destSlicePtrOfStruct any) error
FindManyContext(ctx context.Context, q query.Query, destSlicePtrOfStruct any) error
}
type finder struct {
ex Executor
}
// Find implements Finder
func (f *finder) Find(q query.Query, destPtrOfStruct any) error {
return f.FindContext(context.Background(), q, destPtrOfStruct)
}
// FindContext implements Finder
func (f *finder) FindContext(ctx context.Context, q query.Query, destPtrOfStruct any) error {
if stmt, args, err := q.Query(); err != nil {
return err
} else if rows, err := f.ex.QueryContext(ctx, stmt, args...); err != nil {
return err
} else if err := MapRow(rows, destPtrOfStruct); err != nil {
return err
}
return nil
}
// FindMany implements Finder
func (f *finder) FindMany(q query.Query, destSlicePtrOfStruct any) error {
return f.FindManyContext(context.Background(), q, destSlicePtrOfStruct)
}
// FindManyContext implements Finder
func (f *finder) FindManyContext(ctx context.Context, q query.Query, destSlicePtrOfStruct any) error {
if stmt, args, err := q.Query(); err != nil {
return err
} else if rows, err := f.ex.QueryContext(ctx, stmt, args...); err != nil {
return err
} else if err := MapRows(rows, destSlicePtrOfStruct); err != nil {
return err
}
return nil
}
func newFinder(ex Executor) *finder {
return &finder{ex: ex}
}