-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
152 lines (145 loc) · 5.45 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
package search
import (
"fmt"
"reflect"
"strings"
)
const (
// FromQueryTag tag标记
FromQueryTag = "search"
// Mysql 数据库标识
Mysql = "mysql"
// Postgres 数据库标识
Postgres = "postgres"
)
// ResolveSearchQuery 解析
/**
* exact / iexact 等于
* contains / icontains 包含
* gt / gte 大于 / 大于等于
* lt / lte 小于 / 小于等于
* startswith / istartswith 以…起始
* endswith / iendswith 以…结束
* in
* isnull
* order 排序 e.g. order[key]=desc order[key]=asc
*/
func ResolveSearchQuery(driver string, q interface{}, condition Condition) {
qType := reflect.TypeOf(q)
qValue := reflect.ValueOf(q)
var tag string
var ok bool
var t *resolveSearchTag
//var sep = "`"
if driver == Postgres {
//sep = "\""
}
for i := 0; i < qType.NumField(); i++ {
tag, ok = "", false
tag, ok = qType.Field(i).Tag.Lookup(FromQueryTag)
if !ok {
//递归调用
ResolveSearchQuery(driver, qValue.Field(i).Interface(), condition)
continue
}
switch tag {
case "-":
continue
}
t = makeTag(tag)
if qValue.Field(i).IsZero() {
continue
}
//解析 Postgres `语法不支持,单独适配
if driver == Postgres {
pgSql(driver, t, condition, qValue, i)
} else {
otherSql(driver, t, condition, qValue, i)
}
}
}
func pgSql(driver string, t *resolveSearchTag, condition Condition, qValue reflect.Value, i int) {
switch t.Type {
case "left":
//左关联
join := condition.SetJoinOn(t.Type, fmt.Sprintf(
"left join %s on %s.%s = %s.%s", t.Join, t.Join, t.On[0], t.Table, t.On[1],
))
ResolveSearchQuery(driver, qValue.Field(i).Interface(), join)
case "exact", "iexact":
condition.SetWhere(fmt.Sprintf("%s.%s = ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "icontains":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "contains":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "gt":
condition.SetWhere(fmt.Sprintf("%s.%s > ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "gte":
condition.SetWhere(fmt.Sprintf("%s.%s >= ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "lt":
condition.SetWhere(fmt.Sprintf("%s.%s < ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "lte":
condition.SetWhere(fmt.Sprintf("%s.%s <= ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "istartswith":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", t.Table, t.Column), []interface{}{qValue.Field(i).String() + "%"})
case "startswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", t.Table, t.Column), []interface{}{qValue.Field(i).String() + "%"})
case "iendswith":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String()})
case "endswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String()})
case "in":
condition.SetWhere(fmt.Sprintf("%s.%s in (?)", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "isnull":
if !(qValue.Field(i).IsZero() && qValue.Field(i).IsNil()) {
condition.SetWhere(fmt.Sprintf("%s.%s isnull", t.Table, t.Column), make([]interface{}, 0))
}
case "order":
switch strings.ToLower(qValue.Field(i).String()) {
case "desc", "asc":
condition.SetOrder(fmt.Sprintf("%s.%s %s", t.Table, t.Column, qValue.Field(i).String()))
}
}
}
func otherSql(driver string, t *resolveSearchTag, condition Condition, qValue reflect.Value, i int) {
switch t.Type {
case "left":
//左关联
join := condition.SetJoinOn(t.Type, fmt.Sprintf(
"left join `%s` on `%s`.`%s` = `%s`.`%s`",
t.Join,
t.Join,
t.On[0],
t.Table,
t.On[1],
))
ResolveSearchQuery(driver, qValue.Field(i).Interface(), join)
case "exact", "iexact":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` = ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "contains", "icontains":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "gt":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` > ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "gte":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` >= ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "lt":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` < ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "lte":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` <= ?", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "startswith", "istartswith":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", t.Table, t.Column), []interface{}{qValue.Field(i).String() + "%"})
case "endswith", "iendswith":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", t.Table, t.Column), []interface{}{"%" + qValue.Field(i).String()})
case "in":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` in (?)", t.Table, t.Column), []interface{}{qValue.Field(i).Interface()})
case "isnull":
if !(qValue.Field(i).IsZero() && qValue.Field(i).IsNil()) {
condition.SetWhere(fmt.Sprintf("`%s`.`%s` isnull", t.Table, t.Column), make([]interface{}, 0))
}
case "order":
switch strings.ToLower(qValue.Field(i).String()) {
case "desc", "asc":
condition.SetOrder(fmt.Sprintf("`%s`.`%s` %s", t.Table, t.Column, qValue.Field(i).String()))
}
}
}