forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
365 lines (293 loc) · 9.53 KB
/
expr.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
360
361
362
363
364
365
package field
import (
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var _ Expr = new(Field)
// AssignExpr assign expression
type AssignExpr interface {
Expr
AssignExpr() expression
}
// Expr a query expression about field
type Expr interface {
As(alias string) Expr
ColumnName() sql
BuildColumn(*gorm.Statement, ...BuildOpt) sql
Build(*gorm.Statement) sql
BuildWithArgs(*gorm.Statement) (query sql, args []interface{})
RawExpr() expression
// implement Condition
BeCond() interface{}
CondError() error
expression() clause.Expression
}
// OrderExpr order expression
// used in Order()
type OrderExpr interface {
Expr
Desc() Expr
}
type expression interface{}
type sql string
func (e sql) String() string { return string(e) }
type expr struct {
col clause.Column
e clause.Expression
buildOpts []BuildOpt
}
func (e expr) BeCond() interface{} { return e.expression() }
func (expr) CondError() error { return nil }
func (e expr) AssignExpr() expression {
return e.expression()
}
func (e expr) expression() clause.Expression {
if e.e == nil {
return clause.NamedExpr{SQL: "?", Vars: []interface{}{e.col}}
}
return e.e
}
func (e expr) ColumnName() sql { return sql(e.col.Name) }
// BuildOpt build option
type BuildOpt uint
const (
// WithTable build column with table
WithTable BuildOpt = iota
// WithAll build column with table and alias
WithAll
// WithoutQuote build column without quote
WithoutQuote
)
func (e expr) BuildColumn(stmt *gorm.Statement, opts ...BuildOpt) sql {
col := clause.Column{Name: e.col.Name}
for _, opt := range append(e.buildOpts, opts...) {
switch opt {
case WithTable:
col.Table = e.col.Table
case WithAll:
col.Table = e.col.Table
col.Alias = e.col.Alias
case WithoutQuote:
col.Raw = true
}
}
if col.Name == "*" {
if col.Table != "" {
return sql(stmt.Quote(col.Table)) + ".*"
}
return "*"
}
return sql(stmt.Quote(col))
}
func (e expr) Build(stmt *gorm.Statement) sql {
if e.e == nil {
return sql(e.BuildColumn(stmt, WithAll))
}
newStmt := &gorm.Statement{DB: stmt.DB, Table: stmt.Table, Schema: stmt.Schema}
e.e.Build(newStmt)
return sql(newStmt.SQL.String())
}
func (e expr) BuildWithArgs(stmt *gorm.Statement) (sql, []interface{}) {
if e.e == nil {
return sql(e.BuildColumn(stmt, WithAll)), nil
}
newStmt := &gorm.Statement{DB: stmt.DB, Table: stmt.Table, Schema: stmt.Schema}
e.e.Build(newStmt)
return sql(newStmt.SQL.String()), newStmt.Vars
}
func (e expr) RawExpr() expression {
if e.e == nil {
return e.col
}
return e.e
}
func (e expr) setE(expression clause.Expression) expr {
e.e = expression
return e
}
func (e expr) appendBuildOpts(opts ...BuildOpt) expr {
e.buildOpts = append(e.buildOpts, opts...)
return e
}
// ======================== basic function ========================
func (e expr) WithTable(table string) Expr {
e.col.Table = table
return e
}
func (e expr) IsNull() Expr {
return e.setE(clause.Expr{SQL: "? IS NULL", Vars: []interface{}{e.RawExpr()}})
}
func (e expr) IsNotNull() Expr {
return e.setE(clause.Expr{SQL: "? IS NOT NULL", Vars: []interface{}{e.RawExpr()}})
}
func (e expr) Count() Int {
return Int{e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Distinct() Int {
return Int{e.setE(clause.Expr{SQL: "DISTINCT ?", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Length() Int {
return Int{e.setE(clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Max() Float64 {
return Float64{e.setE(clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Min() Float64 {
return Float64{e.setE(clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Avg() Float64 {
return Float64{e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}})}
}
func (e expr) Null() AssignExpr {
return e.setE(clause.Eq{Column: e.col.Name, Value: nil})
}
func (e expr) GroupConcat() Expr {
return e.setE(clause.Expr{SQL: "GROUP_CONCAT(?)", Vars: []interface{}{e.RawExpr()}})
}
// ======================== comparison between columns ========================
func (e expr) EqCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? = ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) NeqCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? <> ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) GtCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? > ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) GteCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? >= ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) LtCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? < ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) LteCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? <= ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}
func (e expr) SetCol(col Expr) AssignExpr {
return e.setE(clause.Eq{Column: e.col.Name, Value: col.RawExpr()})
}
// ======================== keyword ========================
func (e expr) As(alias string) Expr {
if e.e != nil {
return e.setE(clause.Expr{SQL: "? AS ?", Vars: []interface{}{e.e, clause.Column{Name: alias}}})
}
e.col.Alias = alias
return e
}
func (e expr) Desc() Expr {
return e.setE(clause.Expr{SQL: "? DESC", Vars: []interface{}{e.RawExpr()}})
}
// ======================== general experssion ========================
func (e expr) value(value interface{}) AssignExpr {
return e.setE(clause.Eq{Column: e.col.Name, Value: value})
}
func (e expr) between(values []interface{}) expr {
return e.setE(clause.Expr{SQL: "? BETWEEN ? AND ?", Vars: append([]interface{}{e.RawExpr()}, values...)})
}
func (e expr) add(value interface{}) expr {
switch v := value.(type) {
case time.Duration:
return e.setE(clause.Expr{SQL: "DATE_ADD(?, INTERVAL ? MICROSECOND)", Vars: []interface{}{e.RawExpr(), v.Microseconds()}})
default:
return e.setE(clause.Expr{SQL: "?+?", Vars: []interface{}{e.RawExpr(), value}})
}
}
func (e expr) sub(value interface{}) expr {
switch v := value.(type) {
case time.Duration:
return e.setE(clause.Expr{SQL: "DATE_SUB(?, INTERVAL ? MICROSECOND)", Vars: []interface{}{e.RawExpr(), v.Microseconds()}})
default:
return e.setE(clause.Expr{SQL: "?-?", Vars: []interface{}{e.RawExpr(), value}})
}
}
func (e expr) mul(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?*?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)*?", Vars: []interface{}{e.e, value}})
}
func (e expr) div(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?/?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)/?", Vars: []interface{}{e.e, value}})
}
func (e expr) mod(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?%?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)%?", Vars: []interface{}{e.e, value}})
}
func (e expr) floorDiv(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "? DIV ?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?) DIV ?", Vars: []interface{}{e.e, value}})
}
func (e expr) floor() expr {
return e.setE(clause.Expr{SQL: "FLOOR(?)", Vars: []interface{}{e.RawExpr()}})
}
func (e expr) rightShift(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?>>?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)>>?", Vars: []interface{}{e.e, value}})
}
func (e expr) leftShift(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?<<?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)<<?", Vars: []interface{}{e.e, value}})
}
func (e expr) bitXor(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?^?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)^?", Vars: []interface{}{e.e, value}})
}
func (e expr) bitAnd(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?&?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)&?", Vars: []interface{}{e.e, value}})
}
func (e expr) bitOr(value interface{}) expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "?|?", Vars: []interface{}{e.col, value}})
}
return e.setE(clause.Expr{SQL: "(?)|?", Vars: []interface{}{e.e, value}})
}
func (e expr) bitFlip() expr {
if e.isPure() {
return e.setE(clause.Expr{SQL: "~?", Vars: []interface{}{e.col}})
}
return e.setE(clause.Expr{SQL: "~(?)", Vars: []interface{}{e.RawExpr()}})
}
func (e expr) regexp(value interface{}) expr {
return e.setE(clause.Expr{SQL: "? REGEXP ?", Vars: []interface{}{e.RawExpr(), value}})
}
func (e expr) not() expr {
return e.setE(clause.Expr{SQL: "NOT ?", Vars: []interface{}{e.RawExpr()}})
}
func (e expr) is(value interface{}) expr {
return e.setE(clause.Eq{Column: e.RawExpr(), Value: value})
}
func (e expr) and(value interface{}) expr {
return e.setE(clause.Expr{SQL: "? AND ?", Vars: []interface{}{e.RawExpr(), value}})
}
func (e expr) or(value interface{}) expr {
return e.setE(clause.Expr{SQL: "? OR ?", Vars: []interface{}{e.RawExpr(), value}})
}
func (e expr) xor(value interface{}) expr {
return e.setE(clause.Expr{SQL: "? XOR ?", Vars: []interface{}{e.RawExpr(), value}})
}
func (e expr) isPure() bool {
return e.e == nil
}
func (e expr) ifNull(value interface{}) expr {
return e.setE(clause.Expr{SQL: "IFNULL(?,?)", Vars: []interface{}{e.RawExpr(), value}})
}
func (e expr) sum() expr {
return e.setE(clause.Expr{SQL: "SUM(?)", Vars: []interface{}{e.RawExpr()}})
}