-
Notifications
You must be signed in to change notification settings - Fork 0
/
exprs.go
309 lines (258 loc) · 5.51 KB
/
exprs.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
package geq
import "fmt"
type Expr interface {
Selection
Eq(v any) AnonExpr
Neq(v any) AnonExpr
Gt(v any) AnonExpr
Gte(v any) AnonExpr
Lt(v any) AnonExpr
Lte(v any) AnonExpr
Add(v any) AnonExpr
Sbt(v any) AnonExpr
Mlt(v any) AnonExpr
Dvd(v any) AnonExpr
LikePrefix(v any) AnonExpr
LikeSuffix(v any) AnonExpr
LikePartial(v any) AnonExpr
InAny(vals ...any) AnonExpr
IsNull() AnonExpr
IsNotNull() AnonExpr
And(e Expr) AnonExpr
Or(e Expr) AnonExpr
Asc() Orderer
Desc() Orderer
getPrecedence() int
appendExpr(w *queryWriter, c *QueryConfig)
}
// expression precedence
const (
_ int = iota
prcdOr
prcdAnd
prcdLowExpr
prcdEqual
prcdLessGreater
prcdSum
prcdProduct
prcdValue
)
type AnonExpr interface {
Expr
As(name string) Aliased
}
type Aliased interface {
getExpr() Expr
getAlias() string
}
type aliased struct {
expr Expr
alias string
}
func (a *aliased) getExpr() Expr { return a.expr }
func (a *aliased) getAlias() string { return a.alias }
type AnyColumn interface {
Expr
getColumnName() string
}
type Column[F any] struct {
ops
tableName string
columnName string
}
func (c *Column[F]) getPrecedence() int {
return prcdValue
}
func NewColumn[F any](tableName, columnName string) *Column[F] {
return implOps(&Column[F]{tableName: tableName, columnName: columnName})
}
func (c *Column[F]) getColumnName() string {
return c.columnName
}
func (c *Column[F]) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Printf("%s.%s", cfg.dialect.Ident(c.tableName), cfg.dialect.Ident(c.columnName))
}
func (c *Column[F]) In(values []F) Expr {
anyVals := make([]any, 0, len(values))
for _, v := range values {
anyVals = append(anyVals, v)
}
return implOps(&inExpr{operand: c, values: anyVals})
}
func (c *Column[F]) Set(value F) ValuePair {
return ValuePair{column: c, value: toExpr(value)}
}
func toExpr(v any) Expr {
if v == nil {
return implOps(&nullExpr{})
}
switch val := v.(type) {
case Expr:
return val
case *aliased:
return val.expr
default:
return implOps(&litExpr{val: val})
}
}
type nullExpr struct {
ops
}
func (e *nullExpr) getPrecedence() int {
return prcdValue
}
func (e *nullExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Write("NULL")
}
type litExpr struct {
ops
val any
}
func (e *litExpr) getPrecedence() int {
return prcdValue
}
func (e *litExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Write(cfg.dialect.Placeholder("", w.Args), e.val)
}
type infixExpr struct {
ops
op string
left Expr
right Expr
precedence int
}
func (e *infixExpr) getPrecedence() int {
return e.precedence
}
func (e *infixExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
e.left.appendExpr(w, cfg)
w.Printf(" %s ", e.op)
// Wrap the right side expression by parentheses if necessary.
// a.Mlt(b).Add(c) //=> a * b + c
// a.Mlt(b.Add(c)) //=> a * (b + c)
// On the other hand, the left side expression does not be wrapped.
// a.Add(b).Mlt(c) //=> a + b * c
// Because the code and the generated expression match, making it less likely to cause confusion.
if e.getPrecedence() > e.right.getPrecedence() {
e.right = Parens(e.right)
}
e.right.appendExpr(w, cfg)
}
type suffixExpr struct {
ops
op string
val Expr
precedence int
}
func (e *suffixExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
e.val.appendExpr(w, cfg)
w.Write(" ")
w.Write(e.op)
}
func (e *suffixExpr) getPrecedence() int {
return e.precedence
}
type concatExpr struct {
ops
vals []Expr
}
func newConcatExpr(vals ...any) *concatExpr {
exprs := make([]Expr, 0, len(vals))
for _, v := range vals {
exprs = append(exprs, toExpr(v))
}
return implOps(&concatExpr{vals: exprs})
}
func (e *concatExpr) getPrecedence() int {
return prcdValue
}
func (e *concatExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
stype := cfg.dialect.StrConcatType()
switch stype {
case StrConcatStandard:
for i, v := range e.vals {
if i > 0 {
w.Write(" || ")
}
v.appendExpr(w, cfg)
}
case StrConcatFunc:
fe := &FuncExpr{name: "CONCAT", args: e.vals}
fe.appendExpr(w, cfg)
default:
panic(fmt.Sprintf("unknown string concat type: %v", stype))
}
}
type inExpr struct {
ops
operand Expr
values []any
}
func (e *inExpr) getPrecedence() int {
return prcdLowExpr
}
func (e *inExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
e.operand.appendExpr(w, cfg)
w.Write(" IN (")
for i, v := range e.values {
if i > 0 {
w.Write(", ")
}
toExpr(v).appendExpr(w, cfg)
}
w.Write(")")
}
type FuncExpr struct {
ops
distinct bool
name string
args []Expr
}
func (e *FuncExpr) getPrecedence() int {
return prcdValue
}
func (e *FuncExpr) Distinct() *FuncExpr {
return implOps(&FuncExpr{name: e.name, args: e.args, distinct: true})
}
func (e *FuncExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Write(e.name)
w.Write("(")
if e.distinct {
w.Write("DISTINCT ")
}
for i, arg := range e.args {
if i > 0 {
w.Write(", ")
}
arg.appendExpr(w, cfg)
}
w.Write(")")
}
type parensExpr struct {
ops
expr Expr
}
func (e *parensExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Write("(")
e.expr.appendExpr(w, cfg)
w.Write(")")
}
func (e *parensExpr) getPrecedence() int {
return prcdValue
}
type RawExpr struct {
ops
sql string
}
func newRawExpr(sql string) *RawExpr {
return implOps(&RawExpr{sql: sql})
}
func (e *RawExpr) getPrecedence() int {
return prcdValue
}
func (e *RawExpr) appendExpr(w *queryWriter, cfg *QueryConfig) {
w.Write(e.sql)
}
func (e *RawExpr) appendTable(w *queryWriter, cfg *QueryConfig) {
w.Write(e.sql)
}