/
query_handle.go
162 lines (145 loc) · 4.11 KB
/
query_handle.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
package queryutil
import (
"fmt"
"strings"
"github.com/PolarPanda611/trinitygo/util"
"github.com/jinzhu/gorm"
)
var (
_spiltValue = "__"
_filterCondition = []string{"like", "ilike", "in", "notin", "start", "end", "lt", "lte", "gt", "gte", "isnull", "isempty"}
)
// QueryDecoder query decoder
type QueryDecoder interface {
ConditionSQL() string
ValueSQL() interface{}
}
// NewDecoder new query decoder
func NewDecoder(queryName string, queryValue string) QueryDecoder {
decoder := &filterQuery{
QueryName: queryName,
QueryValue: queryValue,
}
decoder.decode()
decoder.decodeFilterAndValue()
decoder.decodeNestSQL()
return decoder
}
type filterQuery struct {
QueryName string
QueryValue string
// processing
assosiationParam []string
queryParam string
condition string
value string
//output
conditionSQL string
valueSQL interface{}
}
func (f *filterQuery) decode() {
params := strings.Split(f.QueryName, _spiltValue)
paramsLen := len(params)
if paramsLen == 1 {
f.queryParam = params[paramsLen-1]
}
if paramsLen >= 2 {
if util.StringInSlice(params[paramsLen-1], _filterCondition) {
f.condition = params[paramsLen-1]
f.queryParam = params[paramsLen-2]
if paramsLen >= 3 {
f.assosiationParam = params[:paramsLen-2]
}
} else {
f.queryParam = params[paramsLen-1]
f.assosiationParam = params[:paramsLen-1]
}
}
f.queryParam = gorm.ToColumnName(f.queryParam)
newAssosiationParam := make([]string, len(f.assosiationParam))
for i, v := range f.assosiationParam {
newAssosiationParam[i] = gorm.ToTableName(v)
}
f.assosiationParam = newAssosiationParam
}
// decodeFilterAndValue get query sql
func (f *filterQuery) decodeFilterAndValue() {
switch f.condition {
case "like", "ilike":
f.conditionSQL = fmt.Sprintf(" %v %v ? ", f.queryParam, f.condition)
f.valueSQL = fmt.Sprintf("%v%v%v", "%", f.QueryValue, "%")
break
case "in":
f.conditionSQL = fmt.Sprintf(" %v in (?) ", f.queryParam)
f.valueSQL = strings.Split(f.QueryValue, ",")
break
case "notin":
f.conditionSQL = fmt.Sprintf(" %v not in (?) ", f.queryParam)
f.valueSQL = strings.Split(f.QueryValue, ",")
break
case "start":
f.conditionSQL = fmt.Sprintf(" %v >= ? ", f.queryParam)
f.valueSQL = fmt.Sprintf("%v%v", f.QueryValue, " 00:00:00")
break
case "end":
f.conditionSQL = fmt.Sprintf(" %v <= ? ", f.queryParam)
f.valueSQL = fmt.Sprintf("%v%v", f.QueryValue, " 23:59:59")
break
case "isnull":
f.conditionSQL = fmt.Sprintf(" %v is not null ", f.queryParam)
f.valueSQL = nil
if f.QueryValue == "true" {
f.conditionSQL = fmt.Sprintf(" %v is null ", f.queryParam)
}
break
case "lt":
f.conditionSQL = fmt.Sprintf(" %v < ? ", f.queryParam)
f.valueSQL = f.QueryValue
break
case "lte":
f.conditionSQL = fmt.Sprintf(" %v <= ? ", f.queryParam)
f.valueSQL = f.QueryValue
break
case "gt":
f.conditionSQL = fmt.Sprintf(" %v > ? ", f.queryParam)
f.valueSQL = f.QueryValue
break
case "gte":
f.conditionSQL = fmt.Sprintf(" %v >= ? ", f.queryParam)
f.valueSQL = f.QueryValue
break
case "isempty":
f.conditionSQL = fmt.Sprintf(" (COALESCE(\"%v\"::varchar ,'') != '' ) ", f.queryParam)
f.valueSQL = nil
if f.QueryValue == "true" {
f.conditionSQL = fmt.Sprintf(" (COALESCE(\"%v\"::varchar ,'') = '' ) ", f.queryParam)
f.valueSQL = nil
}
break
default:
f.conditionSQL = fmt.Sprintf(" %v = ? ", f.queryParam)
f.valueSQL = f.QueryValue
}
// break
return
}
// decodeNestSQL get query
func (f *filterQuery) decodeNestSQL() {
assosiationParamLen := len(f.assosiationParam)
for range f.assosiationParam {
lastIndex := assosiationParamLen - 1
lastParam := f.assosiationParam[lastIndex]
f.conditionSQL = fmt.Sprintf(" %v_id in ( select id from %v where %v ) ", lastParam, gorm.DefaultTableNameHandler(nil, lastParam), f.conditionSQL)
assosiationParamLen = assosiationParamLen - 1
}
f.conditionSQL = util.DeleteExtraSpace(f.conditionSQL)
return
}
// ConditionSQL get condition sql
func (f *filterQuery) ConditionSQL() string {
return f.conditionSQL
}
// ValueSQL get value sql
func (f *filterQuery) ValueSQL() interface{} {
return f.valueSQL
}