-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathselect_builder.go
359 lines (311 loc) · 9.67 KB
/
select_builder.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package dbr
import (
"context"
"database/sql"
"reflect"
"time"
)
// SelectBuilder build "SELECT" stmt
type SelectBuilder interface {
Builder
EventReceiver
loader
typesLoader
As(alias string) Builder
Comment(text string) SelectBuilder
Distinct() SelectBuilder
ForUpdate() SelectBuilder
From(table interface{}) SelectBuilder
FullJoin(table, on interface{}) SelectBuilder
GroupBy(col ...string) SelectBuilder
Having(query interface{}, value ...interface{}) SelectBuilder
InTimezone(loc *time.Location) SelectBuilder
Join(table, on interface{}) SelectBuilder
LeftJoin(table, on interface{}) SelectBuilder
Limit(n uint64) SelectBuilder
Offset(n uint64) SelectBuilder
OrderAsc(col string) SelectBuilder
OrderBy(col string) SelectBuilder
OrderDesc(col string) SelectBuilder
OrderDir(col string, isAsc bool) SelectBuilder
Paginate(page, perPage uint64) SelectBuilder
Prewhere(query interface{}, value ...interface{}) SelectBuilder
RightJoin(table, on interface{}) SelectBuilder
SkipLocked() SelectBuilder
Where(query interface{}, value ...interface{}) SelectBuilder
GetRows() (*sql.Rows, error)
GetRowsContext(context.Context) (*sql.Rows, error)
}
type selectBuilder struct {
runner
EventReceiver
Dialect Dialect
selectStmt *selectStmt
timezone *time.Location
ctx context.Context
}
func prepareSelect(a []string) []interface{} {
b := make([]interface{}, len(a))
for i := range a {
b[i] = a[i]
}
return b
}
// Select creates a SelectBuilder
func (sess *Session) Select(column ...string) SelectBuilder {
return &selectBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
selectStmt: createSelectStmt(prepareSelect(column)),
ctx: sess.ctx,
}
}
// Select creates a SelectBuilder
func (tx *Tx) Select(column ...string) SelectBuilder {
return &selectBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
selectStmt: createSelectStmt(prepareSelect(column)),
ctx: tx.ctx,
}
}
// SelectBySql creates a SelectBuilder from raw query
func (sess *Session) SelectBySql(query string, value ...interface{}) SelectBuilder {
return &selectBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
selectStmt: createSelectStmtBySQL(query, value),
ctx: sess.ctx,
}
}
// SelectBySql creates a SelectBuilder from raw query
func (tx *Tx) SelectBySql(query string, value ...interface{}) SelectBuilder {
return &selectBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
selectStmt: createSelectStmtBySQL(query, value),
ctx: tx.ctx,
}
}
func (b *selectBuilder) changeTimezone(value reflect.Value) {
v, t := extractOriginal(value)
switch t {
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
b.changeTimezone(v.Index(i))
}
case reflect.Map:
// TODO: add timezone changing for map keys
for _, k := range v.MapKeys() {
b.changeTimezone(v.MapIndex(k))
}
case reflect.Struct:
if v.Type() == reflect.TypeOf(time.Time{}) {
v.Set(reflect.ValueOf(v.Interface().(time.Time).In(b.timezone)))
return
}
for i := 0; i < v.NumField(); i++ {
b.changeTimezone(v.Field(i))
}
}
}
func (b *selectBuilder) Build(d Dialect, buf Buffer) error {
return b.selectStmt.Build(d, buf)
}
// Load loads any value from query result with background context
func (b *selectBuilder) Load(value interface{}) (int, error) {
return b.LoadContext(b.ctx, value)
}
// LoadContext loads any value from query result
func (b *selectBuilder) LoadContext(ctx context.Context, value interface{}) (int, error) {
c, err := query(ctx, b.runner, b.EventReceiver, b, b.Dialect, value)
if err == nil && b.timezone != nil {
b.changeTimezone(reflect.ValueOf(value))
}
return c, err
}
// LoadStruct loads struct from query result with background context, returns ErrNotFound if there is no result
func (b *selectBuilder) LoadStruct(value interface{}) error {
return b.LoadStructContext(b.ctx, value)
}
// LoadStructContext loads struct from query result, returns ErrNotFound if there is no result
func (b *selectBuilder) LoadStructContext(ctx context.Context, value interface{}) error {
count, err := query(ctx, b.runner, b.EventReceiver, b, b.Dialect, value)
if err != nil {
return err
}
if count == 0 {
return ErrNotFound
}
if b.timezone != nil {
b.changeTimezone(reflect.ValueOf(value))
}
return nil
}
// LoadStructs loads structures from query result with background context
func (b *selectBuilder) LoadStructs(value interface{}) (int, error) {
return b.LoadStructsContext(b.ctx, value)
}
// LoadStructsContext loads structures from query result
func (b *selectBuilder) LoadStructsContext(ctx context.Context, value interface{}) (int, error) {
c, err := query(ctx, b.runner, b.EventReceiver, b, b.Dialect, value)
if err == nil && b.timezone != nil {
b.changeTimezone(reflect.ValueOf(value))
}
return c, err
}
// GetRows returns sql.Rows from query result.
func (b *selectBuilder) GetRows() (*sql.Rows, error) {
return b.GetRowsContext(b.ctx)
}
// GetRowsContext returns sql.Rows from query result.
func (b *selectBuilder) GetRowsContext(ctx context.Context) (*sql.Rows, error) {
rows, _, err := queryRows(ctx, b.runner, b.EventReceiver, b, b.Dialect)
return rows, err
}
// LoadValue loads any value from query result with background context, returns ErrNotFound if there is no result
func (b *selectBuilder) LoadValue(value interface{}) error {
return b.LoadValueContext(b.ctx, value)
}
// LoadValueContext loads any value from query result, returns ErrNotFound if there is no result
func (b *selectBuilder) LoadValueContext(ctx context.Context, value interface{}) error {
count, err := query(ctx, b.runner, b.EventReceiver, b, b.Dialect, value)
if err != nil {
return err
}
if count == 0 {
return ErrNotFound
}
if b.timezone != nil {
b.changeTimezone(reflect.ValueOf(value))
}
return nil
}
// LoadValues loads any values from query result with background context
func (b *selectBuilder) LoadValues(value interface{}) (int, error) {
return b.LoadValuesContext(b.ctx, value)
}
// LoadValuesContext loads any values from query result
func (b *selectBuilder) LoadValuesContext(ctx context.Context, value interface{}) (int, error) {
c, err := query(ctx, b.runner, b.EventReceiver, b, b.Dialect, value)
if err == nil && b.timezone != nil {
b.changeTimezone(reflect.ValueOf(value))
}
return c, err
}
// Join joins table on condition
func (b *selectBuilder) Join(table, on interface{}) SelectBuilder {
b.selectStmt.Join(table, on)
return b
}
// LeftJoin joins table on condition via LEFT JOIN
func (b *selectBuilder) LeftJoin(table, on interface{}) SelectBuilder {
b.selectStmt.LeftJoin(table, on)
return b
}
// RightJoin joins table on condition via RIGHT JOIN
func (b *selectBuilder) RightJoin(table, on interface{}) SelectBuilder {
b.selectStmt.RightJoin(table, on)
return b
}
// FullJoin joins table on condition via FULL JOIN
func (b *selectBuilder) FullJoin(table, on interface{}) SelectBuilder {
b.selectStmt.FullJoin(table, on)
return b
}
// Distinct adds `DISTINCT`
func (b *selectBuilder) Distinct() SelectBuilder {
b.selectStmt.Distinct()
return b
}
// From specifies table
func (b *selectBuilder) From(table interface{}) SelectBuilder {
b.selectStmt.From(table)
return b
}
// GroupBy specifies columns for grouping
func (b *selectBuilder) GroupBy(col ...string) SelectBuilder {
b.selectStmt.GroupBy(col...)
return b
}
// Having adds a having condition
func (b *selectBuilder) Having(query interface{}, value ...interface{}) SelectBuilder {
b.selectStmt.Having(query, value...)
return b
}
// Limit adds LIMIT
func (b *selectBuilder) Limit(n uint64) SelectBuilder {
b.selectStmt.Limit(n)
return b
}
// Offset adds OFFSET, works only if LIMIT is set
func (b *selectBuilder) Offset(n uint64) SelectBuilder {
b.selectStmt.Offset(n)
return b
}
// OrderDir specifies columns for ordering in direction
func (b *selectBuilder) OrderDir(col string, isAsc bool) SelectBuilder {
if isAsc {
b.selectStmt.OrderAsc(col)
} else {
b.selectStmt.OrderDesc(col)
}
return b
}
// Paginate adds LIMIT and OFFSET
func (b *selectBuilder) Paginate(page, perPage uint64) SelectBuilder {
b.Limit(perPage)
b.Offset((page - 1) * perPage)
return b
}
// OrderBy specifies column for ordering
func (b *selectBuilder) OrderBy(col string) SelectBuilder {
b.selectStmt.Order = append(b.selectStmt.Order, Expr(col))
return b
}
// Where adds a where condition
func (b *selectBuilder) Prewhere(query interface{}, value ...interface{}) SelectBuilder {
b.selectStmt.Prewhere(query, value...)
return b
}
// Where adds a where condition
func (b *selectBuilder) Where(query interface{}, value ...interface{}) SelectBuilder {
b.selectStmt.Where(query, value...)
return b
}
// ForUpdate adds lock via FOR UPDATE
func (b *selectBuilder) ForUpdate() SelectBuilder {
b.selectStmt.ForUpdate()
return b
}
// SkipLocked skips locked rows via SKIP LOCKED
func (b *selectBuilder) SkipLocked() SelectBuilder {
b.selectStmt.SkipLocked()
return b
}
// InTimezone all time.Time fields in the result will be returned with the specified location.
func (b *selectBuilder) InTimezone(loc *time.Location) SelectBuilder {
b.timezone = loc
return b
}
func (b *selectBuilder) OrderAsc(col string) SelectBuilder {
b.selectStmt.OrderAsc(col)
return b
}
func (b *selectBuilder) OrderDesc(col string) SelectBuilder {
b.selectStmt.OrderDesc(col)
return b
}
// As creates alias for select statement
func (b *selectBuilder) As(alias string) Builder {
return b.selectStmt.As(alias)
}
// Comment adds a comment at the beginning of the query
func (b *selectBuilder) Comment(text string) SelectBuilder {
b.selectStmt.AddComment(text)
return b
}