-
Notifications
You must be signed in to change notification settings - Fork 45
/
query.go
217 lines (175 loc) · 5.44 KB
/
query.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
212
213
214
215
216
217
package bob
import (
"context"
"database/sql"
"io"
"github.com/qdm12/reprint"
"github.com/stephenafamo/scan"
)
// To pervent unnecessary allocations
const (
openPar = "("
closePar = ")"
commaSpace = ", "
)
type QueryType int
const (
QueryTypeUnknown QueryType = iota
QueryTypeSelect
QueryTypeInsert
QueryTypeUpdate
QueryTypeDelete
)
type Query interface {
// It should satisfy the Expression interface so that it can be used
// in places such as a sub-select
// However, it is allowed for a query to use its own dialect and not
// the dialect given to it
Expression
// start is the index of the args, usually 1.
// it is present to allow re-indexing in cases of a subquery
// The method returns the value of any args placed
WriteQuery(ctx context.Context, w io.Writer, start int) (args []any, err error)
// Type returns the query type
Type() QueryType
}
// BaseQuery wraps common functionality such as cloning, applying new mods and
// the actual query interface implementation
type BaseQuery[E Expression] struct {
Expression E
Dialect Dialect
QueryType QueryType
}
func (b BaseQuery[E]) Clone() BaseQuery[E] {
if c, ok := any(b.Expression).(interface{ Clone() E }); ok {
return BaseQuery[E]{
Expression: c.Clone(),
Dialect: b.Dialect,
}
}
return BaseQuery[E]{
Expression: reprint.This(b.Expression).(E),
Dialect: b.Dialect,
}
}
func (b BaseQuery[E]) Type() QueryType {
return b.QueryType
}
func (b BaseQuery[E]) Exec(ctx context.Context, exec Executor) (sql.Result, error) {
return Exec(ctx, exec, b)
}
func (b BaseQuery[E]) RunHooks(ctx context.Context, exec Executor) (context.Context, error) {
if l, ok := any(b.Expression).(HookableQuery); ok {
return l.RunHooks(ctx, exec)
}
return ctx, nil
}
func (b BaseQuery[E]) GetLoaders() []Loader {
if l, ok := any(b.Expression).(Loadable); ok {
return l.GetLoaders()
}
return nil
}
func (b BaseQuery[E]) GetMapperMods() []scan.MapperMod {
if l, ok := any(b.Expression).(MapperModder); ok {
return l.GetMapperMods()
}
return nil
}
func (b BaseQuery[E]) Apply(mods ...Mod[E]) {
for _, mod := range mods {
mod.Apply(b.Expression)
}
}
func (b BaseQuery[E]) WriteQuery(ctx context.Context, w io.Writer, start int) ([]any, error) {
// If it a query, just call its WriteQuery method
if e, ok := any(b.Expression).(interface {
WriteQuery(context.Context, io.Writer, int) ([]any, error)
}); ok {
return e.WriteQuery(ctx, w, start)
}
return b.Expression.WriteSQL(ctx, w, b.Dialect, start)
}
// Satisfies the Expression interface, but uses its own dialect instead
// of the dialect passed to it
func (b BaseQuery[E]) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
// If it a query, don't wrap it in parentheses
// it may already do this on its own and we don't want to double wrap
if e, ok := any(b.Expression).(Query); ok {
return e.WriteSQL(ctx, w, d, start)
}
w.Write([]byte(openPar))
args, err := b.Expression.WriteSQL(ctx, w, b.Dialect, start)
w.Write([]byte(closePar))
return args, err
}
// MustBuild builds the query and panics on error
// useful for initializing queries that need to be reused
func (q BaseQuery[E]) MustBuild(ctx context.Context) (string, []any) {
return MustBuildN(ctx, q, 1)
}
// MustBuildN builds the query and panics on error
// start numbers the arguments from a different point
func (q BaseQuery[E]) MustBuildN(ctx context.Context, start int) (string, []any) {
return MustBuildN(ctx, q, start)
}
// Convinient function to build query from start
func (q BaseQuery[E]) Build(ctx context.Context) (string, []any, error) {
return BuildN(ctx, q, 1)
}
// Convinient function to build query from a point
func (q BaseQuery[E]) BuildN(ctx context.Context, start int) (string, []any, error) {
return BuildN(ctx, q, start)
}
// Convinient function to cache a query
func (q BaseQuery[E]) Cache(ctx context.Context, exec Executor) (BaseQuery[*cached], error) {
return CacheN(ctx, exec, q, 1)
}
// Convinient function to cache a query from a point
func (q BaseQuery[E]) CacheN(ctx context.Context, exec Executor, start int) (BaseQuery[*cached], error) {
return CacheN(ctx, exec, q, start)
}
func BindNamed[Arg any](ctx context.Context, q Query, args Arg) BoundQuery[Arg] {
return BoundQuery[Arg]{Query: q, namedArgs: args}
}
type BoundQuery[Arg any] struct {
Query
namedArgs Arg
}
func (b BoundQuery[Arg]) WriteQuery(ctx context.Context, w io.Writer, start int) ([]any, error) {
args, err := b.Query.WriteQuery(ctx, w, start)
if err != nil {
return nil, err
}
return bindArgs(args, b.namedArgs)
}
// Satisfies the Expression interface, but uses its own dialect instead
// of the dialect passed to it
func (b BoundQuery[E]) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
args, err := b.Query.WriteSQL(ctx, w, d, start)
if err != nil {
return nil, err
}
return bindArgs(args, b.namedArgs)
}
func (b BoundQuery[E]) Exec(ctx context.Context, exec Executor) (sql.Result, error) {
return Exec(ctx, exec, b)
}
func (b BoundQuery[E]) RunHooks(ctx context.Context, exec Executor) (context.Context, error) {
if l, ok := any(b.Query).(HookableQuery); ok {
return l.RunHooks(ctx, exec)
}
return ctx, nil
}
func (b BoundQuery[E]) GetLoaders() []Loader {
if l, ok := any(b.Query).(Loadable); ok {
return l.GetLoaders()
}
return nil
}
func (b BoundQuery[E]) GetMapperMods() []scan.MapperMod {
if l, ok := any(b.Query).(MapperModder); ok {
return l.GetMapperMods()
}
return nil
}