-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathquery.go
635 lines (564 loc) · 16.2 KB
/
query.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright 2019 Yunion
//
// 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 sqlchemy
import (
"database/sql"
"fmt"
"reflect"
"runtime/debug"
"strings"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/reflectutils"
)
// QueryJoinType is the Join type of SQL query, namely, innerjoin, leftjoin and rightjoin
type QueryJoinType string
const (
// INNERJOIN represents innerjoin
INNERJOIN QueryJoinType = "JOIN"
// LEFTJOIN represents left join
LEFTJOIN QueryJoinType = "LEFT JOIN"
// RIGHTJOIN represents right-join
RIGHTJOIN QueryJoinType = "RIGHT JOIN"
// FULLJOIN QueryJoinType = "FULLJOIN"
)
// sQueryJoin represents the state of a Join Query
type sQueryJoin struct {
jointype QueryJoinType
from IQuerySource
condition ICondition
}
// SQuery is a data structure represents a SQL query in the form of
//
// SELECT ... FROM ... JOIN ... ON ... WHERE ... GROUP BY ... ORDER BY ... HAVING ...
type SQuery struct {
rawSql string
fields []IQueryField
distinct bool
from IQuerySource
joins []sQueryJoin
where ICondition
groupBy []IQueryField
orderBy []sQueryOrder
// having ICondition
limit int
offset int
refFieldMap map[string]IQueryField
snapshot string
db *SDatabase
}
func (tq *SQuery) Copy() *SQuery {
q := &SQuery{
rawSql: tq.rawSql,
fields: []IQueryField{},
refFieldMap: map[string]IQueryField{},
distinct: tq.distinct,
from: tq.from,
joins: []sQueryJoin{},
where: tq.where,
groupBy: []IQueryField{},
orderBy: []sQueryOrder{},
limit: tq.limit,
offset: tq.offset,
snapshot: tq.snapshot,
db: tq.db,
}
for i := range tq.fields {
q.fields = append(q.fields, tq.fields[i])
}
for k := range tq.refFieldMap {
q.refFieldMap[k] = tq.refFieldMap[k]
}
for i := range tq.joins {
q.joins = append(q.joins, tq.joins[i])
}
for i := range tq.groupBy {
q.groupBy = append(q.groupBy, tq.groupBy[i])
}
for i := range tq.orderBy {
q.orderBy = append(q.orderBy, tq.orderBy[i])
}
return q
}
// IsGroupBy returns wether the query contains group by clauses
func (tq *SQuery) IsGroupBy() bool {
return len(tq.groupBy) > 0
}
func (tq *SQuery) hasField(f IQueryField) bool {
if len(tq.fields) == 0 {
return false
}
for i := range tq.fields {
if tq.fields[i].Name() == f.Name() {
return true
}
}
return false
}
// AppendField appends query field to a query
func (tq *SQuery) AppendField(f ...IQueryField) *SQuery {
// log.Debugf("AppendField tq has fields %d", len(tq.fields))
for i := range f {
if !tq.hasField(f[i]) {
if refField, ok := tq.refFieldMap[f[i].Name()]; ok {
tq.fields = append(tq.fields, refField)
delete(tq.refFieldMap, f[i].Name())
} else {
tq.fields = append(tq.fields, f[i])
}
}
}
return tq
}
func (tq *SQuery) addRefField(f IQueryField) *SQuery {
if tq.refFieldMap == nil {
tq.refFieldMap = make(map[string]IQueryField)
}
if !tq.hasField(f) {
if _, ok := tq.refFieldMap[f.Name()]; !ok {
tq.refFieldMap[f.Name()] = f
}
}
return tq
}
func (tq *SQuery) ResetFields() *SQuery {
tq.fields = make([]IQueryField, 0)
tq.refFieldMap = make(map[string]IQueryField)
return tq
}
// Query of SSubQuery generates a new query from a subquery
func (sq *SSubQuery) Query(f ...IQueryField) *SQuery {
return DoQuery(sq, f...)
}
// Query of STable generates a new query from a table
func (tbl *STable) Query(f ...IQueryField) *SQuery {
return DoQuery(tbl, f...)
}
// Query of STableSpec generates a new query from a STableSpec instance
func (ts *STableSpec) Query(f ...IQueryField) *SQuery {
return ts.Instance().Query(f...)
}
// QueryOrderType indicates the query order type, either ASC or DESC
type QueryOrderType string
const (
// SQL_ORDER_ASC represents Ascending order
SQL_ORDER_ASC QueryOrderType = "ASC"
// SQL_ORDER_DESC represents Descending order
SQL_ORDER_DESC QueryOrderType = "DESC"
)
// Equals of QueryOrderType determines whether two order type identical
func (qot QueryOrderType) Equals(orderType string) bool {
if strings.ToUpper(orderType) == string(qot) {
return true
}
return false
}
// internal structure to store state of query order
type sQueryOrder struct {
field IQueryField
order QueryOrderType
}
func (tq *SQuery) _orderBy(order QueryOrderType, fields []IQueryField) *SQuery {
if tq.orderBy == nil {
tq.orderBy = make([]sQueryOrder, 0)
}
for i := range fields {
tq.orderBy = append(tq.orderBy, sQueryOrder{field: fields[i], order: order})
}
return tq
}
// Asc of SQuery does query in ascending order of specified fields
func (tq *SQuery) Asc(fields ...interface{}) *SQuery {
return tq._orderBy(SQL_ORDER_ASC, convertQueryField(tq, fields))
}
// Desc of SQuery does query in descending order of specified fields
func (tq *SQuery) Desc(fields ...interface{}) *SQuery {
return tq._orderBy(SQL_ORDER_DESC, convertQueryField(tq, fields))
}
func convertQueryField(tq IQuery, fields []interface{}) []IQueryField {
nFields := make([]IQueryField, 0)
for _, f := range fields {
switch ff := f.(type) {
case string:
nFields = append(nFields, tq.Field(ff))
case IQueryField:
nFields = append(nFields, ff)
default:
log.Errorf("Invalid query field %s neither string nor IQueryField", f)
}
}
return nFields
}
// GroupBy of SQuery does query group by specified fields
func (tq *SQuery) GroupBy(f ...interface{}) *SQuery {
if tq.groupBy == nil {
tq.groupBy = make([]IQueryField, 0)
}
qfs := convertQueryField(tq, f)
tq.groupBy = append(tq.groupBy, qfs...)
return tq
}
// Limit of SQuery adds limit to a query
func (tq *SQuery) Limit(limit int) *SQuery {
tq.limit = limit
return tq
}
// Offset of SQuery adds offset to a query
func (tq *SQuery) Offset(offset int) *SQuery {
tq.offset = offset
return tq
}
func (tq *SQuery) FieldCount() int {
return len(tq.fields)
}
// QueryFields of SQuery returns fields in SELECT clause of a query
func (tq *SQuery) QueryFields() []IQueryField {
if len(tq.fields) > 0 {
return tq.fields
}
return tq.from.Fields()
}
// String of SQuery implemetation of SQuery for IQuery
func (tq *SQuery) String(fields ...IQueryField) string {
sql := queryString(tq, fields...)
// log.Debugf("Query: %s", sql)
return sql
}
// Join of SQuery joins query with another IQuerySource on specified condition
func (tq *SQuery) Join(from IQuerySource, on ICondition) *SQuery {
return tq._join(from, on, INNERJOIN)
}
// LeftJoin of SQuery left-joins query with another IQuerySource on specified condition
func (tq *SQuery) LeftJoin(from IQuerySource, on ICondition) *SQuery {
return tq._join(from, on, LEFTJOIN)
}
// RightJoin of SQuery right-joins query with another IQuerySource on specified condition
func (tq *SQuery) RightJoin(from IQuerySource, on ICondition) *SQuery {
return tq._join(from, on, RIGHTJOIN)
}
/*func (tq *SQuery) FullJoin(from IQuerySource, on ICondition) *SQuery {
return tq._join(from, on, FULLJOIN)
}*/
func (tq *SQuery) _join(from IQuerySource, on ICondition, joinType QueryJoinType) *SQuery {
if from.database() != tq.db {
panic(fmt.Sprintf("Cannot join across databases %s!=%s", tq.db.name, from.database().name))
}
if tq.joins == nil {
tq.joins = make([]sQueryJoin, 0)
}
qj := sQueryJoin{jointype: joinType, from: from, condition: on}
tq.joins = append(tq.joins, qj)
return tq
}
// Variables implementation of SQuery for IQuery
func (tq *SQuery) Variables() []interface{} {
vars := make([]interface{}, 0)
var fromvars []interface{}
fields := tq.fields
for i := range fields {
fromvars = fields[i].Variables()
vars = append(vars, fromvars...)
}
if tq.from != nil {
fromvars = tq.from.Variables()
vars = append(vars, fromvars...)
}
for _, join := range tq.joins {
fromvars = join.from.Variables()
vars = append(vars, fromvars...)
fromvars = join.condition.Variables()
vars = append(vars, fromvars...)
}
if tq.where != nil {
fromvars = tq.where.Variables()
vars = append(vars, fromvars...)
}
/*if tq.having != nil {
fromvars = tq.having.Variables()
vars = append(vars, fromvars...)
}*/
return vars
}
// Distinct of SQuery indicates a distinct query results
func (tq *SQuery) Distinct() *SQuery {
tq.distinct = true
return tq
}
// SubQuery of SQuery generates a SSubQuery from a Query
func (tq *SQuery) SubQuery() *SSubQuery {
sq := SSubQuery{
query: tq.Copy(),
alias: getTableAliasName(),
referedFields: make(map[string]IQueryField),
}
return &sq
}
func (tq *SQuery) database() *SDatabase {
return tq.db
}
// Row of SQuery returns an instance of sql.Row for native data fetching
func (tq *SQuery) Row() *sql.Row {
sqlstr := tq.String()
vars := tq.Variables()
if DEBUG_SQLCHEMY {
sqlDebug("SQuery.Row", sqlstr, vars)
}
if tq.db == nil {
panic("tq.db")
}
if tq.db.db == nil {
panic("tq.db.db")
}
return tq.db.db.QueryRow(sqlstr, vars...)
}
// Rows of SQuery returns an instance of sql.Rows for native data fetching
func (tq *SQuery) Rows() (*sql.Rows, error) {
sqlstr := tq.String()
vars := tq.Variables()
if DEBUG_SQLCHEMY {
sqlDebug("SQuery.Rows", sqlstr, vars)
}
return tq.db.db.Query(sqlstr, vars...)
}
// Count of SQuery returns the count of a query
// use CountWithError instead
// deprecated
func (tq *SQuery) Count() int {
cnt, _ := tq.CountWithError()
return cnt
}
func (tq *SQuery) CountQuery() *SQuery {
tq2 := *tq
tq2.limit = 0
tq2.offset = 0
cq := &SQuery{
fields: []IQueryField{
COUNT("count"),
},
from: tq2.SubQuery(),
db: tq.database(),
}
return cq
}
// CountWithError of SQuery returns the row count of a query
func (tq *SQuery) CountWithError() (int, error) {
cq := tq.CountQuery()
count := 0
err := cq.Row().Scan(&count)
if err == nil {
return count, nil
}
log.Errorf("SQuery count %s failed: %s", cq.String(), err)
return -1, err
}
// Field implementation of SQuery for IQuery
func (tq *SQuery) Field(name string) IQueryField {
f := tq.findField(name)
if f == nil {
log.Errorf("SQuery %s cannot find Field %s", tq.String(), name)
debug.PrintStack()
}
return f
}
func (tq *SQuery) findField(name string) IQueryField {
for _, f := range tq.fields {
if f.Name() == name {
// switch f.(type) {
// case *SFunctionFieldBase:
// log.Warningf("cannot directly reference a function alias, should use Subquery() to enclose the query")
// }
return f
}
}
if f, ok := tq.refFieldMap[name]; ok {
return f
}
f := tq.from.Field(name)
if f != nil {
return newQueryField(tq.from, name)
}
finds := make([]IQueryField, 0)
for _, join := range tq.joins {
f = join.from.Field(name)
if f != nil {
finds = append(finds, newQueryField(join.from, name))
}
}
if len(finds) == 1 {
return finds[0]
} else if len(finds) > 1 {
log.Errorf("Field %s found duplicated %d, please specifify the field", name, len(finds))
return finds[0]
}
return nil
}
// IRowScanner is an interface for sql data fetching
type IRowScanner interface {
Scan(desc ...interface{}) error
}
func rowScan2StringMap(fields []string, row IRowScanner) (map[string]string, error) {
targets := make([]interface{}, len(fields))
for i := range fields {
var recver interface{}
targets[i] = &recver
}
if err := row.Scan(targets...); err != nil {
return nil, err
}
results := make(map[string]string)
for i, f := range fields {
//log.Debugf("%d %s: %s", i, f, targets[i])
rawValue := reflect.Indirect(reflect.ValueOf(targets[i]))
if rawValue.Interface() == nil {
results[f] = ""
} else {
value := rawValue.Interface()
// log.Infof("%s %s", value, reflect.TypeOf(value))
results[f] = GetStringValue(value)
}
}
return results, nil
}
func (tq *SQuery) rowScan2StringMap(row IRowScanner) (map[string]string, error) {
queryFields := tq.QueryFields()
fields := make([]string, len(queryFields))
for i, f := range queryFields {
fields[i] = f.Name()
}
return rowScan2StringMap(fields, row)
}
// FirstStringMap returns query result of the first row in a stringmap(map[string]string)
func (tq *SQuery) FirstStringMap() (map[string]string, error) {
return tq.rowScan2StringMap(tq.Row())
}
// AllStringMap returns query result of all rows in an array of stringmap(map[string]string)
func (tq *SQuery) AllStringMap() ([]map[string]string, error) {
rows, err := tq.Rows()
if err != nil {
return nil, err
}
defer rows.Close()
results := make([]map[string]string, 0)
for rows.Next() {
result, err := tq.rowScan2StringMap(rows)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}
func mapString2Struct(mapResult map[string]string, destValue reflect.Value) error {
destFields := reflectutils.FetchStructFieldValueSet(destValue)
var err error
for k, v := range mapResult {
if len(v) > 0 {
fieldValue, ok := destFields.GetValue(k)
if ok {
err = setValueBySQLString(fieldValue, v)
if err != nil {
log.Errorf("Set field %q value error %s", k, err)
}
}
}
}
return err
}
func callAfterQuery(val reflect.Value) {
afterQueryFunc := val.MethodByName("AfterQuery")
if afterQueryFunc.IsValid() && !afterQueryFunc.IsNil() {
afterQueryFunc.Call([]reflect.Value{})
}
}
// First return query result of first row and store the result in a data struct
func (tq *SQuery) First(dest interface{}) error {
mapResult, err := tq.FirstStringMap()
if err != nil {
return err
}
destPtrValue := reflect.ValueOf(dest)
if destPtrValue.Kind() != reflect.Ptr {
return errors.Wrap(ErrNeedsPointer, "input must be a pointer")
}
destValue := destPtrValue.Elem()
err = mapString2Struct(mapResult, destValue)
if err != nil {
return err
}
callAfterQuery(destPtrValue)
return nil
}
// All return query results of all rows and store the result in an array of data struct
func (tq *SQuery) All(dest interface{}) error {
arrayType := reflect.TypeOf(dest).Elem()
if arrayType.Kind() != reflect.Array && arrayType.Kind() != reflect.Slice {
return errors.Wrap(ErrNeedsArray, "dest is not an array or slice")
}
elemType := arrayType.Elem()
mapResults, err := tq.AllStringMap()
if err != nil {
return err
}
arrayValue := reflect.ValueOf(dest).Elem()
for _, mapV := range mapResults {
elemPtrValue := reflect.New(elemType)
elemValue := reflect.Indirect(elemPtrValue)
err = mapString2Struct(mapV, elemValue)
if err != nil {
break
}
callAfterQuery(elemPtrValue)
newArray := reflect.Append(arrayValue, elemValue)
arrayValue.Set(newArray)
}
return err
}
// Row2Map is a utility function that fetch stringmap(map[string]string) from a native sql.Row or sql.Rows
func (tq *SQuery) Row2Map(row IRowScanner) (map[string]string, error) {
return tq.rowScan2StringMap(row)
}
// RowMap2Struct is a utility function that fetch struct from a native sql.Row or sql.Rows
func (tq *SQuery) RowMap2Struct(result map[string]string, dest interface{}) error {
destPtrValue := reflect.ValueOf(dest)
if destPtrValue.Kind() != reflect.Ptr {
return errors.Wrap(ErrNeedsPointer, "input must be a pointer")
}
destValue := destPtrValue.Elem()
err := mapString2Struct(result, destValue)
if err != nil {
return err
}
callAfterQuery(destPtrValue)
return nil
}
// Row2Struct is a utility function that fill a struct with the value of a sql.Row or sql.Rows
func (tq *SQuery) Row2Struct(row IRowScanner, dest interface{}) error {
result, err := tq.rowScan2StringMap(row)
if err != nil {
return err
}
return tq.RowMap2Struct(result, dest)
}
// Snapshot of SQuery take a snapshot of the query, so we can tell wether the query is modified later by comparing the SQL with snapshot
func (tq *SQuery) Snapshot() *SQuery {
tq.snapshot = tq.String()
return tq
}
// IsAltered of SQuery indicates whether a query was altered. By comparing with the saved query snapshot, we can tell whether a query is altered
func (tq *SQuery) IsAltered() bool {
if len(tq.snapshot) == 0 {
panic(fmt.Sprintf("Query %s has never been snapshot when IsAltered called", tq.String()))
}
return tq.String() != tq.snapshot
}