/
options.go
211 lines (181 loc) · 4.68 KB
/
options.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package query
import (
"context"
"github.com/bitmagnet-io/bitmagnet/internal/database/cache"
"github.com/bitmagnet-io/bitmagnet/internal/database/dao"
"github.com/bitmagnet-io/bitmagnet/internal/database/fts"
"github.com/bitmagnet-io/bitmagnet/internal/maps"
"gorm.io/gen/field"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"strings"
)
type Option = func(ctx OptionBuilder) (OptionBuilder, error)
func DefaultOption() Option {
return Options(
Limit(10),
WithAggregationBudget(5_000),
)
}
func Options(o ...Option) Option {
return func(b OptionBuilder) (OptionBuilder, error) {
for _, opt := range o {
if nextCtx, err := opt(b); err != nil {
return b, err
} else {
b = nextCtx
}
}
return b, nil
}
}
type Criteria interface {
Raw(c DbContext) (RawCriteria, error)
}
type TableJoinType int
const (
TableJoinTypeInner TableJoinType = iota
TableJoinTypeLeft
TableJoinTypeRight
)
type TableJoin struct {
Table schema.Tabler
On []field.Expr
Type TableJoinType
Dependencies maps.InsertMap[string, struct{}]
Required bool
}
func Table(name string) Option {
return func(b OptionBuilder) (OptionBuilder, error) {
return b.Table(name), nil
}
}
func Join(fn func(*dao.Query) []TableJoin) Option {
return func(b OptionBuilder) (OptionBuilder, error) {
joins := fn(b.Query())
return b.Join(joins...), nil
}
}
func RequireJoin(names ...string) Option {
return func(b OptionBuilder) (OptionBuilder, error) {
return b.RequireJoin(names...), nil
}
}
func QueryString(str string) Option {
query := fts.AppQueryToTsquery(str)
return func(ctx OptionBuilder) (OptionBuilder, error) {
if len(query) == 0 {
return ctx.Select(clause.Expr{
SQL: "0 AS " + queryStringRankField,
}), nil
}
c, err := GenCriteria(func(ctx DbContext) (Criteria, error) {
return DbCriteria{
Sql: strings.Join([]string{
ctx.TableName() + ".tsv @@ ?::tsquery",
}, " OR "),
Args: []interface{}{
query,
},
}, nil
}).Raw(ctx)
if err != nil {
return ctx, err
}
ctx = ctx.Scope(func(dao SubQuery) error {
dao.UnderlyingDB().Where(c.Query, c.Args...)
return nil
}).RequireJoin(ctx.TableName()).Select(clause.Expr{
SQL: "ts_rank_cd(" + ctx.TableName() + ".tsv, ?::tsquery) AS " + queryStringRankField,
Vars: []interface{}{
query,
},
})
return ctx, nil
}
}
func Select(columns ...clause.Expr) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Select(columns...), nil
}
}
func SelectAll() Option {
return Select(clause.Expr{SQL: "*"})
}
func Group(columns ...clause.Column) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Group(columns...), nil
}
}
func Limit(n uint) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Limit(n), nil
}
}
func Offset(n uint) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Offset(n), nil
}
}
func OrderBy(columns ...clause.OrderByColumn) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.OrderBy(columns...), nil
}
}
const queryStringRankField = "query_string_rank"
func OrderByQueryStringRank() Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.OrderBy(clause.OrderByColumn{
Column: clause.Column{Name: queryStringRankField},
Desc: true,
Reorder: true,
}), nil
}
}
func OrderByColumn(field string, desc bool) Option {
return OrderBy(clause.OrderByColumn{
Column: clause.Column{Name: field},
Desc: desc,
})
}
func WithFacet(facets ...Facet) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Facet(facets...), nil
}
}
func Preload(fn func(query *dao.Query) []field.RelationField) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.Preload(fn(ctx.Query())...), nil
}
}
func WithTotalCount(bl bool) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.WithTotalCount(bl), nil
}
}
func WithHasNextPage(bl bool) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.WithHasNextPage(bl), nil
}
}
func WithAggregationBudget(budget float64) Option {
return func(ctx OptionBuilder) (OptionBuilder, error) {
return ctx.WithAggregationBudget(budget), nil
}
}
func Context(fn func(ctx context.Context) context.Context) Option {
return func(b OptionBuilder) (OptionBuilder, error) {
return b.Context(fn), nil
}
}
func CacheMode(mode cache.Mode) Option {
return Context(func(ctx context.Context) context.Context {
return context.WithValue(ctx, cache.ModeKey, mode)
})
}
func Cached() Option {
return CacheMode(cache.ModeCached)
}
func CacheWarm() Option {
return CacheMode(cache.ModeWarm)
}