-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_builder.go
executable file
·450 lines (437 loc) · 11.8 KB
/
query_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
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
package f
import (
"errors"
"fmt"
"github.com/andreyvit/diff"
gmap "github.com/og/x/map"
gtime "github.com/og/x/time"
"log"
"os"
"reflect"
"strings"
"time"
)
type Order struct {
// Field Type 的顺序永远不能换
Field string
Type orderType
}
type Group struct {
Field string
}
type QB struct {
Table string
Select []string
Where []AND
Offset int
Limit int
Order []Order
Group []string
SoftDelete string
Insert Map
Update Map
Count bool
Debug bool
Check []string
}
// QueryBuilder Where
type AND map[string]OP
// FindOr(Find(...), Find(...))
func Or (find ...[]AND) (andList []AND) {
andList = []AND{}
for _, v := range find {
andList = append(andList, v[0])
}
return
}
func Where(v ...interface{}) QB {
return QB{
Where: And(v...),
}
}
// f.And("name","nimo")
// 接收 ...interface{} 作为参数而不是 map[string]interface{} 是因为会存在这种情况
// f.And("age", f.Lt(19), "age", f.Gt(10))
func And(v ...interface{}) []AND {
vLen := len(v)
if vLen%2 !=0 {
panic(errors.New("gofree: f.And(v ...inteface{}) len(v) must be even, may be you forget some."))
}
and := AND{}
for i:=0;i<vLen;i++ {
itemAny := v[i]
var item Filter
var isKey bool
if i%2 == 0 { isKey = true }
if !isKey {
keyAny := v[i-1]
key := keyAny.(string)
_, has := and[key]
_=has
if reflect.TypeOf(itemAny).Name() != "Filter" {
item = Eql(itemAny)
} else {
item = itemAny.(Filter)
}
if has {
and[key] = append(and[key], item)
} else {
and[key] = OP{item}
}
}
}
return []AND{and}
}
func wrapField(field string) string {
return "`" + field + "`"
}
// filter list interface maybe Filter
func (qb QB) GetSelect() (sql string, sqlValues []interface{}) {
return qb.SQL(SQLProps{
Statement: "SELECT",
})
}
func (qb QB) GetUpdate() (sql string, sqlValues []interface{}) {
return qb.SQL(SQLProps{
Statement: "UPDATE",
})
}
func (qb QB) GetInsert() (sql string, sqlValues []interface{}) {
return qb.SQL(SQLProps{
Statement: "INSERT",
})
}
type SQLProps struct {
Statement string `eg:"[]string{\"SELECT\", \"UPDATE\", \"DELETE\", \"INSERT\"}"`
}
func (qb QB) SQL(props SQLProps) (sql string, sqlValues []interface{}){
var sqlList stringQueue
tableName := "`" + qb.Table + "`"
{// Statement
switch props.Statement {
case "SELECT":
sqlList.Push("SELECT")
if qb.Count {
sqlList.Push("count(*)")
} else {
if len(qb.Select) == 0 {
sqlList.Push("*")
} else {
sqlList.Push("`" + strings.Join(qb.Select, "`, `") + "`")
}
}
sqlList.Push("FROM")
sqlList.Push(tableName)
case "UPDATE":
sqlList.Push("UPDATE")
sqlList.Push(tableName)
sqlList.Push("SET")
keys := gmap.UnsafeKeys(qb.Update).String()
if len(keys) == 0 {
panic(errors.New("gofree: update can not be a empty map"))
}
updateValueList := stringQueue{}
for _, key := range keys {
value := qb.Update[key]
updateValueList.Push(wrapField(key) +" = ?")
sqlValues = append(sqlValues, value)
}
sqlList.Push(updateValueList.Join(", "))
case "DELETE":
sqlList.Push("DELETE")
case "INSERT":
sqlList.Push("INSERT INTO")
sqlList.Push(tableName)
keys := gmap.UnsafeKeys(qb.Insert).String()
if len(keys) == 0 {
panic(errors.New("gofree: Insert can not be a empty map"))
}
insertKeyList := stringQueue{}
insertValueList := stringQueue{}
for _, key := range keys {
value := qb.Insert[key]
insertKeyList.Push(wrapField(key))
insertValueList.Push("?")
sqlValues = append(sqlValues, value)
}
sqlList.Push("(" + insertKeyList.Join(", ") + ")")
sqlList.Push("VALUES")
sqlList.Push("(" + insertValueList.Join(", ") + ")")
}
}
{
// Where field operator value
shouldWhere := len(qb.Where) != 0 || qb.SoftDelete != ""
if props.Statement == "INSERT" {
shouldWhere = false
}
if shouldWhere {
sqlList.Push("WHERE")
var WhereList stringQueue
parseWhere(qb.Where, &WhereList, &sqlValues)
switch props.Statement {
case "SELECT", "UPDATE":
if qb.SoftDelete != "" {
WhereList.Push(wrapField(qb.SoftDelete) + " IS NULL")
}
}
sqlList.Push(WhereList.Join(" AND "))
notEmptyStringSqlList := stringQueue{}
for _, item := range sqlList.Value {
if item != "" {
notEmptyStringSqlList.Push(item)
}
}
sqlList = notEmptyStringSqlList
if sqlList.Value[len(sqlList.Value)-1] == "WHERE" {
sqlList.Pop()
}
}
}
{
// group by
if len(qb.Group) != 0 {
sqlList.Push("GROUP BY")
sqlList.Push("`" + strings.Join(qb.Group,"`, `") + "`")
}
}
{
// order by
if len(qb.Order) != 0 {
sqlList.Push("ORDER BY")
orderList := stringQueue{}
for _, orderItem := range qb.Order {
orderType := orderItem.Type
switch orderType {
case ASC:
orderList.Push(wrapField(orderItem.Field) +" ASC")
case DESC:
orderList.Push(wrapField(orderItem.Field)+" DESC")
}
}
sqlList.Push(orderList.Join(", "))
}
}
{
// limit
if qb.Limit != 0 && !qb.Count {
sqlList.Push("LIMIT ?")
sqlValues = append(sqlValues, qb.Limit)
}
}
{
// offset
if qb.Offset != 0 && !qb.Count {
sqlList.Push("OFFSET ?")
sqlValues = append(sqlValues, qb.Offset)
}
}
sql = sqlList.Join(" ")
logDebug(qb.Debug, Map{
"sql": sql,
"values": sqlValues,
})
if len(qb.Check) != 0 {
matched := false
for _, checkSQL := range qb.Check {
if checkSQL == sql {
matched = true
}
}
if !matched {
for _, checkSQL := range qb.Check {
panic("sql check fail:# diff:\r\n" + diff.CharacterDiff(sql, checkSQL) + "\r\n# actual\r\n" + sql + "\r\n# expect:\r\n" + checkSQL)
}
}
}
return
}
func parseAnd (field string, op OP, whereList *stringQueue, sqlValues *[]interface{}) {
for _, filter := range op {
if reflect.ValueOf(filter.Value).IsValid() && reflect.TypeOf(filter.Value).String() == "time.Time" {
panic("gofree: can not use time.Time be sql value, mayby you should time.Format(layout), \r\n` "+ field + ":"+ filter.Value.(time.Time).Format(gtime.LayoutTime) + "`")
}
shouldIgnore := false
var fieldSymbolCondition stringQueue
dict := filter.Dict().Kind
switchValue := filter.Kind
switch switchValue {
case dict.Day:
fieldSymbolCondition.Push(wrapField(field) + " >= ?")
*sqlValues = append(*sqlValues, filter.TimeValue.Format(gtime.LayoutDate) + " 00:00:00")
fieldSymbolCondition.Push("AND")
fieldSymbolCondition.Push(wrapField(field) + " <= ?")
*sqlValues = append(*sqlValues, filter.TimeValue.Format(gtime.LayoutDate) + " 23:59:59")
case dict.Not:
fieldSymbolCondition.Push(wrapField(field), "!=")
fieldSymbolCondition.Push("?")
*sqlValues = append(*sqlValues, filter.Value)
case dict.IsNotNull:
fieldSymbolCondition.Push(wrapField(field), "IS NOT NULL")
case dict.IsNull:
fieldSymbolCondition.Push(wrapField(field), "IS NULL")
case dict.Custom:
var valueList []interface{}
anyValue := reflect.ValueOf(filter.Value)
for i := 0; i < anyValue.Len(); i++ {
valueList = append(valueList, anyValue.Index(i).Interface())
}
*sqlValues = append(*sqlValues, valueList...)
fieldSymbolCondition.Push(wrapField(field), filter.Custom)
case dict.CustomSQL:
var valueList []interface{}
anyValue := reflect.ValueOf(filter.Value)
for i := 0; i < anyValue.Len(); i++ {
valueList = append(valueList, anyValue.Index(i).Interface())
}
*sqlValues = append(*sqlValues, valueList...)
fieldSymbolCondition.Push("(" + filter.CustomSQL + ")")
case dict.In, dict.NotIn:
symbol := ""
switch switchValue {
case dict.In:
symbol = "IN"
case dict.NotIn:
symbol = "NOT IN"
}
fieldSymbolCondition.Push(wrapField(field), symbol)
var valueList []interface{}
var placeholderList stringQueue
anyValue := reflect.ValueOf(filter.Value)
if anyValue.Len() == 0 {
fieldSymbolCondition.Push("(NULL)")
} else {
for i := 0; i < anyValue.Len(); i++ {
valueList = append(valueList, anyValue.Index(i).Interface())
placeholderList.Push("?")
}
*sqlValues = append(*sqlValues, valueList...)
fieldSymbolCondition.Push("(" + placeholderList.Join(", ") + ")")
}
case dict.Like:
var likeValue string
filterValueString := fmt.Sprintf("%s", filter.Value)
switch filter.Like {
case "start":
likeValue = filterValueString+"%"
case "end":
likeValue = "%" + filterValueString
case "have":
likeValue = "%" + filterValueString + "%"
}
fieldSymbolCondition.Push(wrapField(field), "LIKE")
fieldSymbolCondition.Push("?")
*sqlValues = append(*sqlValues, likeValue)
case dict.GofreeIgnore:
shouldIgnore = true
case dict.TimeRange:
timeRange := filter.TimeRange.Range
valueTime := struct {
Start time.Time
End time.Time
} {}
timeRange.Type.Switch(func(_year int) {
valueTime.Start = gtime.FirstMonth(timeRange.Start)
valueTime.End = gtime.LastMonth(timeRange.End)
}, func(_month bool) {
valueTime.Start = gtime.FirstDay(timeRange.Start)
valueTime.End = gtime.LastDay(timeRange.End)
}, func(_day string) {
valueTime.Start = gtime.FirstHour(timeRange.Start)
valueTime.End = gtime.LastHour(timeRange.End)
})
fieldSymbolCondition.Push(wrapField(field) + " >= ?")
*sqlValues = append(*sqlValues, valueTime.Start.Format(filter.TimeRange.SQLValueLayout))
fieldSymbolCondition.Push("AND")
fieldSymbolCondition.Push(wrapField(field) + " <= ?")
*sqlValues = append(*sqlValues, valueTime.End.Format(filter.TimeRange.SQLValueLayout))
case dict.BetweenInt:
fieldSymbolCondition.Push(wrapField(field), "BETWEEN", "?", "AND" , "?")
valueList := []interface{}{
filter.BetweenInt.Begin,
filter.BetweenInt.End,
}
*sqlValues = append(*sqlValues, valueList...)
break
case dict.BetweenFloat:
fieldSymbolCondition.Push(wrapField(field), "BETWEEN", "?", "AND" , "?")
valueList := []interface{}{
filter.BetweenFloat.Begin,
filter.BetweenFloat.End,
}
*sqlValues = append(*sqlValues, valueList...)
break
case dict.BetweenString:
fieldSymbolCondition.Push(wrapField(field), "BETWEEN", "?", "AND" , "?")
valueList := []interface{}{
filter.BetweenString.Begin,
filter.BetweenString.End,
}
*sqlValues = append(*sqlValues, valueList...)
break
default:
if filter.Symbol == "" {
panic(errors.New("f.Filter is empty struct"))
}
fieldSymbolCondition.Push(wrapField(field), filter.Symbol)
fieldSymbolCondition.Push("?")
*sqlValues = append(*sqlValues, filter.Value)
}
if !shouldIgnore {
whereList.Push(fieldSymbolCondition.Join(" "))
}
}
}
func parseWhere (Where []AND, WhereList *stringQueue, sqlValues *[]interface{}) {
var orSqlList stringQueue
for _, and := range Where {
var andList stringQueue
for _, field := range gmap.UnsafeKeys(and).String() {
op := and[field]
parseAnd(field, op, &andList, sqlValues)
}
andString := andList.Join(" AND ")
orSqlList.Push(andString)
}
orSqlString := orSqlList.Join(" ) OR ( ")
if len(orSqlList.Value) > 1 {
orSqlString = "( " + orSqlString + " )"
}
if orSqlString != "" {
WhereList.Push(orSqlString)
}
}
func logDebug(isDebug bool, data Map) {
if !isDebug {
return
}
onlyValueLogger := log.New(os.Stdout,"",log.LUTC)
log.Print("gofree debug: ")
for key, value := range data {
onlyValueLogger.Print(key + ":")
onlyValueLogger.Printf("\t%#+v",value)
}
}
func (qb QB) BindModel(model interface{}) QB {
if qb.Table == "" {
tableName := reflect.ValueOf(model).MethodByName("TableName").Call([]reflect.Value{})[0].String()
qb.Table = tableName
if qb.Table == "" {
panic(errors.New("tableName is empty string"))
}
}
qb.SoftDelete = "deleted_at"
return qb
}
func (qb QB) Paging(page int , perPage int) QB {
if page == 0 {
page = 1
}
if perPage == 0 {
perPage = 10
log.Print("gofree: Paging(page, perPage) alert perPage is 0 ,perPage can't be 0 . gofree will set perPage 10. but you nedd check your code.")
}
qb.Offset = (page - 1) * perPage
qb.Limit = perPage
return qb
}