-
Notifications
You must be signed in to change notification settings - Fork 45
/
cached.go
75 lines (60 loc) · 1.4 KB
/
cached.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package bob
import (
"context"
"fmt"
"io"
)
func Cache(ctx context.Context, exec Executor, q Query) (BaseQuery[*cached], error) {
return CacheN(ctx, exec, q, 1)
}
func CacheN(ctx context.Context, exec Executor, q Query, start int) (BaseQuery[*cached], error) {
var err error
if h, ok := q.(HookableQuery); ok {
ctx, err = h.RunHooks(ctx, exec)
if err != nil {
return BaseQuery[*cached]{}, err
}
}
query, args, err := BuildN(ctx, q, start)
if err != nil {
return BaseQuery[*cached]{}, err
}
cached := BaseQuery[*cached]{
QueryType: q.Type(),
Expression: &cached{
query: []byte(query),
args: args,
start: start,
},
}
if l, ok := q.(Loadable); ok {
cached.Expression.SetLoaders(l.GetLoaders()...)
}
if m, ok := q.(MapperModder); ok {
cached.Expression.SetMapperMods(m.GetMapperMods()...)
}
return cached, nil
}
type WrongStartError struct {
Expected int
Got int
}
func (e WrongStartError) Error() string {
return fmt.Sprintf("expected to start at %d, started at %d", e.Expected, e.Got)
}
type cached struct {
query []byte
args []any
start int
Load
}
// WriteSQL implements Expression.
func (c *cached) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
if start != c.start {
return nil, WrongStartError{Expected: c.start, Got: start}
}
if _, err := w.Write(c.query); err != nil {
return nil, err
}
return c.args, nil
}