forked from ecodeclub/eorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharding_select.go
373 lines (340 loc) · 7.95 KB
/
sharding_select.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
366
367
368
369
370
371
372
373
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eorm
import (
"context"
"sync"
"github.com/ecodeclub/eorm/internal/merger/batchmerger"
"github.com/ecodeclub/eorm/internal/sharding"
"github.com/ecodeclub/eorm/internal/errs"
"github.com/valyala/bytebufferpool"
)
type ShardingSelector[T any] struct {
shardingSelectorBuilder
table *T
db Session
lock sync.Mutex
}
func NewShardingSelector[T any](db Session) *ShardingSelector[T] {
b := shardingSelectorBuilder{}
b.core = db.getCore()
b.buffer = bytebufferpool.Get()
return &ShardingSelector[T]{
shardingSelectorBuilder: b,
db: db,
}
}
func (s *ShardingSelector[T]) Build(ctx context.Context) ([]sharding.Query, error) {
var err error
if s.meta == nil {
s.meta, err = s.metaRegistry.Get(new(T))
if err != nil {
return nil, err
}
}
shardingRes, err := s.findDst(ctx, s.where...)
if err != nil {
return nil, err
}
res := make([]sharding.Query, 0, len(shardingRes.Dsts))
defer bytebufferpool.Put(s.buffer)
for _, dst := range shardingRes.Dsts {
q, err := s.buildQuery(dst.DB, dst.Table, dst.Name)
if err != nil {
return nil, err
}
res = append(res, q)
s.args = nil
s.buffer.Reset()
}
return res, nil
}
func (s *ShardingSelector[T]) buildQuery(db, tbl, ds string) (sharding.Query, error) {
var err error
s.writeString("SELECT ")
if len(s.columns) == 0 {
if err = s.buildAllColumns(); err != nil {
return sharding.EmptyQuery, err
}
} else {
err = s.buildSelectedList()
if err != nil {
return sharding.EmptyQuery, err
}
}
s.writeString(" FROM ")
s.quote(db)
s.writeByte('.')
s.quote(tbl)
if len(s.where) > 0 {
s.writeString(" WHERE ")
p := s.where[0]
for i := 1; i < len(s.where); i++ {
p = p.And(s.where[i])
}
if err = s.buildExpr(p); err != nil {
return sharding.EmptyQuery, err
}
}
// group by
if len(s.groupBy) > 0 {
err = s.buildGroupBy()
if err != nil {
return sharding.EmptyQuery, err
}
}
// order by
if len(s.orderBy) > 0 {
err = s.buildOrderBy()
if err != nil {
return sharding.EmptyQuery, err
}
}
// having
if len(s.having) > 0 {
s.writeString(" HAVING ")
p := s.having[0]
for i := 1; i < len(s.having); i++ {
p = p.And(s.having[i])
}
if err = s.buildExpr(p); err != nil {
return sharding.EmptyQuery, err
}
}
if s.offset > 0 {
s.writeString(" OFFSET ")
s.parameter(s.offset)
}
if s.limit > 0 {
s.writeString(" LIMIT ")
s.parameter(s.limit)
}
s.end()
return sharding.Query{SQL: s.buffer.String(), Args: s.args, Datasource: ds, DB: db}, nil
}
func (s *ShardingSelector[T]) buildAllColumns() error {
for i, cMeta := range s.meta.Columns {
_ = s.buildColumns(i, cMeta.FieldName)
}
return nil
}
func (s *ShardingSelector[T]) buildSelectedList() error {
for i, selectable := range s.columns {
if i > 0 {
s.comma()
}
switch expr := selectable.(type) {
case Column:
err := s.builder.buildColumn(expr)
if err != nil {
return errs.NewInvalidFieldError(expr.name)
}
case columns:
for j, c := range expr.cs {
err := s.buildColumns(j, c)
if err != nil {
return err
}
}
case Aggregate:
if err := s.selectAggregate(expr); err != nil {
return err
}
case RawExpr:
s.buildRawExpr(expr)
}
}
return nil
}
func (s *ShardingSelector[T]) selectAggregate(aggregate Aggregate) error {
s.writeString(aggregate.fn)
s.writeByte('(')
if aggregate.distinct {
s.writeString("DISTINCT ")
}
cMeta, ok := s.meta.FieldMap[aggregate.arg]
if !ok {
return errs.NewInvalidFieldError(aggregate.arg)
}
if aggregate.table != nil {
if alias := aggregate.table.getAlias(); alias != "" {
s.quote(alias)
s.point()
}
}
s.quote(cMeta.ColumnName)
s.writeByte(')')
if aggregate.alias != "" {
s.writeString(" AS ")
s.quote(aggregate.alias)
}
return nil
}
func (s *ShardingSelector[T]) buildColumns(index int, name string) error {
if index > 0 {
s.comma()
}
cMeta, ok := s.meta.FieldMap[name]
if !ok {
return errs.NewInvalidFieldError(name)
}
s.quote(cMeta.ColumnName)
return nil
}
func (s *ShardingSelector[T]) buildExpr(expr Expr) error {
switch exp := expr.(type) {
case nil:
case Column:
exp.alias = ""
_ = s.buildColumn(exp)
case valueExpr:
s.parameter(exp.val)
case RawExpr:
s.buildRawExpr(exp)
case Predicate:
if err := s.buildBinaryExpr(binaryExpr(exp)); err != nil {
return err
}
default:
return errs.NewErrUnsupportedExpressionType()
}
return nil
}
func (s *ShardingSelector[T]) buildOrderBy() error {
s.writeString(" ORDER BY ")
for i, ob := range s.orderBy {
if i > 0 {
s.comma()
}
for _, c := range ob.fields {
cMeta, ok := s.meta.FieldMap[c]
if !ok {
return errs.NewInvalidFieldError(c)
}
s.quote(cMeta.ColumnName)
}
s.space()
s.writeString(ob.order)
}
return nil
}
func (s *ShardingSelector[T]) buildGroupBy() error {
s.writeString(" GROUP BY ")
for i, gb := range s.groupBy {
cMeta, ok := s.meta.FieldMap[gb]
if !ok {
return errs.NewInvalidFieldError(gb)
}
if i > 0 {
s.comma()
}
s.quote(cMeta.ColumnName)
}
return nil
}
func (s *ShardingSelector[T]) Get(ctx context.Context) (*T, error) {
qs, err := s.Limit(1).Build(ctx)
if err != nil {
return nil, err
}
if len(qs) == 0 {
return nil, errs.ErrNotGenShardingQuery
}
// TODO 要确保前面的改写 SQL 只能生成一个 SQL
if len(qs) > 1 {
return nil, errs.ErrOnlyResultOneQuery
}
q := qs[0]
// TODO 利用 ctx 传递 DB name
row, err := s.db.queryContext(ctx, q)
if err != nil {
return nil, err
}
if !row.Next() {
return nil, ErrNoRows
}
tp := new(T)
val := s.valCreator.NewPrimitiveValue(tp, s.meta)
if err = val.SetColumns(row); err != nil {
return nil, err
}
return tp, nil
}
func (s *ShardingSelector[T]) GetMulti(ctx context.Context) ([]*T, error) {
qs, err := s.Build(ctx)
if err != nil {
return nil, err
}
mgr := batchmerger.NewMerger()
rowsList, err := s.db.queryMulti(ctx, qs)
if err != nil {
return nil, err
}
rows, err := mgr.Merge(ctx, rowsList.AsSlice())
if err != nil {
return nil, err
}
defer rows.Close()
var res []*T
for rows.Next() {
tp := new(T)
val := s.valCreator.NewPrimitiveValue(tp, s.meta)
if err = val.SetColumns(rows); err != nil {
return nil, err
}
res = append(res, tp)
}
return res, nil
}
// Select 指定查询的列。
// 列可以是物理列,也可以是聚合函数,或者 RawExpr
func (s *ShardingSelector[T]) Select(columns ...Selectable) *ShardingSelector[T] {
s.columns = columns
return s
}
// From specifies the table which must be pointer of structure
func (s *ShardingSelector[T]) From(tbl *T) *ShardingSelector[T] {
s.table = tbl
return s
}
// Where accepts predicates
func (s *ShardingSelector[T]) Where(predicates ...Predicate) *ShardingSelector[T] {
s.where = predicates
return s
}
// Having accepts predicates
func (s *ShardingSelector[T]) Having(predicates ...Predicate) *ShardingSelector[T] {
s.having = predicates
return s
}
// GroupBy means "GROUP BY"
func (s *ShardingSelector[T]) GroupBy(columns ...string) *ShardingSelector[T] {
s.groupBy = columns
return s
}
// OrderBy means "ORDER BY"
func (s *ShardingSelector[T]) OrderBy(orderBys ...OrderBy) *ShardingSelector[T] {
s.orderBy = orderBys
return s
}
// Limit limits the size of result set
func (s *ShardingSelector[T]) Limit(limit int) *ShardingSelector[T] {
s.limit = limit
return s
}
// Offset was used by "LIMIT"
func (s *ShardingSelector[T]) Offset(offset int) *ShardingSelector[T] {
s.offset = offset
return s
}