-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
293 lines (264 loc) · 5.8 KB
/
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
package orm
import (
"github.com/valyala/bytebufferpool"
"orm/internal/errs"
"orm/model"
)
type Builder struct {
core
// sb stringb.Builder // 普通字符串 Builder
// 使用 bytebufferpool 以减少内存分配
// 每次调用 Get 之后不要忘记再调用 Put
buffer *bytebufferpool.ByteBuffer
// 元数据模型
model *model.Model
args []any
// as 别名映射
aliasMap map[string]int
//dialect Dialect
quoter byte
}
func (b *Builder) writeSpace() {
_ = b.buffer.WriteByte(' ')
}
func (b *Builder) writeComma() {
b.writeByte(',')
}
func (b *Builder) writePlaceholder() {
b.writeByte('?')
}
func (b *Builder) writeLeftParenthesis() {
b.writeString("(")
}
func (b *Builder) writeRightParenthesis() {
b.writeString(")")
}
func (b *Builder) quote(name string) {
b.writeByte(b.quoter)
b.writeString(name)
b.writeByte(b.quoter)
}
func (b *Builder) end() {
b.writeString(";")
}
func (b *Builder) writeString(val string) {
_, _ = b.buffer.WriteString(val)
}
func (b *Builder) writeByte(c byte) {
_ = b.buffer.WriteByte(c)
}
func (b *Builder) buildAssignment(a Assignment) error {
fd, ok := b.model.FieldMap[a.column]
if !ok {
return errs.NewErrUnknownField(a.column)
}
b.quote(fd.ColName)
b.writeString(" = ")
return b.buildExpression(a.val, false, false)
}
func (b *Builder) buildPredicates(pres *predicates) error {
ps := pres.ps[0]
for i := 1; i < len(pres.ps); i++ {
ps = ps.And(pres.ps[i])
}
return b.buildExpression(ps, pres.useColsAlias, pres.useAggreAlias)
}
func (b *Builder) addArgs(args ...any) {
if b.args == nil {
b.args = make([]any, 0, 8)
}
b.args = append(b.args, args...)
}
func (b *Builder) buildAs(alias string) error {
if alias != "" {
_, ok := b.aliasMap[alias]
if ok {
return errs.NewErrDuplicateAlias(alias)
}
b.writeString(" AS ")
b.quote(alias)
b.aliasMap[alias] = 1
}
return nil
}
func (b *Builder) buildRaw(r RawExpr) error {
b.writeString(r.raw)
if len(r.args) > 0 {
b.addArgs(r.args...)
}
return nil
}
func (b *Builder) buildValue(v value) error {
b.writePlaceholder()
b.addArgs(v.val)
return nil
}
func (b *Builder) colName(table TableReference, fdName string, useAlias bool) (string, error) {
switch tab := table.(type) {
case nil:
_, ok := b.aliasMap[fdName]
if useAlias && ok {
return fdName, nil
}
fd, ok := b.model.FieldMap[fdName]
if !ok {
return "", errs.NewErrUnknownField(fdName)
}
return fd.ColName, nil
case Table:
m, err := b.r.Get(tab.entity)
if err != nil {
return "", err
}
fd, ok := m.FieldMap[fdName]
if !ok {
return "", errs.NewErrUnknownField(fdName)
}
return fd.ColName, nil
case Subquery:
if len(tab.columns) > 0 {
for _, col := range tab.columns {
if col.selectedAlias() == fdName {
return fdName, nil
}
if col.fieldName() == fdName {
return b.colName(col.target(), fdName, useAlias)
}
return "", errs.NewErrUnknownField(fdName)
}
}
return b.colName(tab.table, fdName, useAlias)
default:
return "", errs.NewErrUnsupportedExpressionType(tab)
}
}
func (b *Builder) buildColumn(val Column, useAlias bool) error {
var alias string
if val.table != nil {
alias = val.table.tableAlias()
}
if alias != "" {
b.quote(alias)
b.writeByte('.')
}
colName, err := b.colName(val.table, val.name, useAlias)
if err != nil {
return err
}
b.quote(colName)
return nil
}
func (b *Builder) buildAggregate(val Aggregate, useAlias bool) error {
b.writeString(val.fn)
b.writeLeftParenthesis()
err := b.buildColumn(
Column{table: val.table, name: val.arg}, useAlias)
if err != nil {
return err
}
b.writeRightParenthesis()
if useAlias {
err := b.buildAs(val.alias)
if err != nil {
return err
}
}
return nil
}
func (b *Builder) buildSubquery(sub Subquery, useAlias bool) error {
q, err := sub.s.Build()
if err != nil {
return err
}
b.writeLeftParenthesis()
b.writeString(q.SQL[:len(q.SQL)-1])
b.writeRightParenthesis()
if len(q.Args) > 0 {
b.addArgs(q.Args...)
}
if useAlias {
if err = b.buildAs(sub.alias); err != nil {
return err
}
}
return nil
}
func (b *Builder) buildBinaryExpr(
exp binaryExpr, colsAlias, aggreAlias bool) error {
err := b.buildSubExpr(
exp.left, colsAlias, aggreAlias)
if err != nil {
return err
}
if exp.op != "" {
b.writeSpace()
b.writeString(exp.op.String())
b.writeSpace()
}
return b.buildSubExpr(
exp.right, colsAlias, aggreAlias)
}
func (b *Builder) buildSubExpr(expr Expression, colsAlias, aggreAlias bool) error {
switch e := expr.(type) {
case MathExpr:
b.writeLeftParenthesis()
err := b.buildBinaryExpr(
binaryExpr(e), colsAlias, aggreAlias)
if err != nil {
return err
}
b.writeRightParenthesis()
case binaryExpr:
b.writeLeftParenthesis()
err := b.buildBinaryExpr(
e, colsAlias, aggreAlias)
if err != nil {
return err
}
b.writeRightParenthesis()
case Predicate:
b.writeLeftParenthesis()
err := b.buildBinaryExpr(
binaryExpr(e), colsAlias, aggreAlias)
if err != nil {
return err
}
b.writeRightParenthesis()
default:
err := b.buildExpression(
e, colsAlias, aggreAlias)
if err != nil {
return err
}
}
return nil
}
func (b *Builder) buildExpression(
e Expression, colsAlias, aggreAlias bool) error {
switch exp := e.(type) {
case nil:
return nil
case Column:
return b.buildColumn(exp, colsAlias)
case Aggregate:
return b.buildAggregate(exp, aggreAlias)
case value:
return b.buildValue(exp)
case RawExpr:
return b.buildRaw(exp)
case MathExpr:
return b.buildBinaryExpr(
binaryExpr(exp), colsAlias, aggreAlias)
case Predicate:
return b.buildBinaryExpr(
binaryExpr(exp), colsAlias, aggreAlias)
case Subquery:
return b.buildSubquery(exp, false)
case SubqueryExpr:
b.writeString(exp.pred)
b.writeSpace()
return b.buildSubquery(exp.s, false)
default:
return errs.NewErrUnsupportedExpressionType(exp)
}
}