Skip to content

Commit bd90e40

Browse files
authored
Merge pull request #297 from stephenafamo/cache-hooks
Make bob.Cache() require an Executor to run any query hooks.
2 parents 1d6c532 + b0b9fc2 commit bd90e40

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7777
The same functionality can be achieved by using `modelSlice.Delete()` or creating an `Delete` query using `table.Delete()`.
7878
- `BeforeInsertHooks` now only takes a single `ModelSetter` at a time.
7979
This is because it is not possible to know before executing the queries exactly how many setters are being used since additional rows can be inserted by applying another setter as a mod.
80+
- `bob.Cache()` now requires an `Executor`. This is used to run any query hooks.
8081

8182
### Removed
8283

cached.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,43 @@ import (
66
"io"
77
)
88

9-
func Cache(ctx context.Context, q Query) (BaseQuery[*cached], error) {
10-
return CacheN(ctx, q, 1)
9+
func Cache(ctx context.Context, exec Executor, q Query) (BaseQuery[*cached], error) {
10+
return CacheN(ctx, exec, q, 1)
1111
}
1212

13-
func CacheN(ctx context.Context, q Query, start int) (BaseQuery[*cached], error) {
13+
func CacheN(ctx context.Context, exec Executor, q Query, start int) (BaseQuery[*cached], error) {
14+
var err error
15+
16+
if h, ok := q.(HookableQuery); ok {
17+
ctx, err = h.RunHooks(ctx, exec)
18+
if err != nil {
19+
return BaseQuery[*cached]{}, err
20+
}
21+
}
22+
1423
query, args, err := BuildN(ctx, q, start)
1524
if err != nil {
1625
return BaseQuery[*cached]{}, err
1726
}
1827

19-
return BaseQuery[*cached]{
28+
cached := BaseQuery[*cached]{
29+
QueryType: q.Type(),
2030
Expression: &cached{
2131
query: []byte(query),
2232
args: args,
2333
start: start,
2434
},
25-
}, nil
35+
}
36+
37+
if l, ok := q.(Loadable); ok {
38+
cached.Expression.SetLoaders(l.GetLoaders()...)
39+
}
40+
41+
if m, ok := q.(MapperModder); ok {
42+
cached.Expression.SetMapperMods(m.GetMapperMods()...)
43+
}
44+
45+
return cached, nil
2646
}
2747

2848
type WrongStartError struct {
@@ -38,6 +58,7 @@ type cached struct {
3858
query []byte
3959
args []any
4060
start int
61+
Load
4162
}
4263

4364
// WriteSQL implements Expression.

query.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func (q BaseQuery[E]) BuildN(ctx context.Context, start int) (string, []any, err
136136
}
137137

138138
// Convinient function to cache a query
139-
func (q BaseQuery[E]) Cache(ctx context.Context) (BaseQuery[*cached], error) {
140-
return CacheN(ctx, q, 1)
139+
func (q BaseQuery[E]) Cache(ctx context.Context, exec Executor) (BaseQuery[*cached], error) {
140+
return CacheN(ctx, exec, q, 1)
141141
}
142142

143143
// Convinient function to cache a query from a point
144-
func (q BaseQuery[E]) CacheN(ctx context.Context, start int) (BaseQuery[*cached], error) {
145-
return CacheN(ctx, q, start)
144+
func (q BaseQuery[E]) CacheN(ctx context.Context, exec Executor, start int) (BaseQuery[*cached], error) {
145+
return CacheN(ctx, exec, q, start)
146146
}

query_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ var (
55
_ Loadable = BaseQuery[Expression]{}
66
_ MapperModder = BaseQuery[Expression]{}
77
_ HookableQuery = BaseQuery[Expression]{}
8+
9+
_ Expression = &cached{}
10+
_ Loadable = &cached{}
11+
_ MapperModder = &cached{}
812
)

0 commit comments

Comments
 (0)