21
21
errHavingUnsupportedOperator = errors .New (`[builder] "_having" contains unsupported operator` )
22
22
errLockModeValueType = errors .New (`[builder] the value of "_lockMode" must be of string type` )
23
23
errNotAllowedLockMode = errors .New (`[builder] the value of "_lockMode" is not allowed` )
24
+ errUpdateLimitType = errors .New (`[builder] the value of "_limit" in update query must be one of int,uint,int64,uint64` )
24
25
25
26
errWhereInterfaceSliceType = `[builder] the value of "xxx %s" must be of []interface{} type`
26
27
errEmptySliceCondition = `[builder] the value of "%s" must contain at least one element`
28
+
29
+ defaultIgnoreKeys = map [string ]struct {}{
30
+ "_orderby" : struct {}{},
31
+ "_groupby" : struct {}{},
32
+ "_having" : struct {}{},
33
+ "_limit" : struct {}{},
34
+ "_lockMode" : struct {}{},
35
+ }
27
36
)
28
37
29
38
type whereMapSet struct {
@@ -59,37 +68,31 @@ func BuildSelect(table string, where map[string]interface{}, selectField []strin
59
68
var groupBy string
60
69
var having map [string ]interface {}
61
70
var lockMode string
62
- copiedWhere := copyWhere (where )
63
- if val , ok := copiedWhere ["_orderby" ]; ok {
71
+ if val , ok := where ["_orderby" ]; ok {
64
72
s , ok := val .(string )
65
73
if ! ok {
66
74
err = errOrderByValueType
67
75
return
68
76
}
69
77
orderBy = strings .TrimSpace (s )
70
- delete (copiedWhere , "_orderby" )
71
78
}
72
- if val , ok := copiedWhere ["_groupby" ]; ok {
79
+ if val , ok := where ["_groupby" ]; ok {
73
80
s , ok := val .(string )
74
81
if ! ok {
75
82
err = errGroupByValueType
76
83
return
77
84
}
78
85
groupBy = strings .TrimSpace (s )
79
- delete (copiedWhere , "_groupby" )
80
86
if "" != groupBy {
81
- if h , ok := copiedWhere ["_having" ]; ok {
87
+ if h , ok := where ["_having" ]; ok {
82
88
having , err = resolveHaving (h )
83
89
if nil != err {
84
90
return
85
91
}
86
92
}
87
93
}
88
94
}
89
- if _ , ok := copiedWhere ["_having" ]; ok {
90
- delete (copiedWhere , "_having" )
91
- }
92
- if val , ok := copiedWhere ["_limit" ]; ok {
95
+ if val , ok := where ["_limit" ]; ok {
93
96
arr , ok := val .([]uint )
94
97
if ! ok {
95
98
err = errLimitValueType
@@ -108,9 +111,8 @@ func BuildSelect(table string, where map[string]interface{}, selectField []strin
108
111
begin : begin ,
109
112
step : step ,
110
113
}
111
- delete (copiedWhere , "_limit" )
112
114
}
113
- if val , ok := copiedWhere ["_lockMode" ]; ok {
115
+ if val , ok := where ["_lockMode" ]; ok {
114
116
s , ok := val .(string )
115
117
if ! ok {
116
118
err = errLockModeValueType
@@ -121,14 +123,13 @@ func BuildSelect(table string, where map[string]interface{}, selectField []strin
121
123
err = errNotAllowedLockMode
122
124
return
123
125
}
124
- delete (copiedWhere , "_lockMode" )
125
126
}
126
- conditions , err := getWhereConditions (copiedWhere )
127
+ conditions , err := getWhereConditions (where , defaultIgnoreKeys )
127
128
if nil != err {
128
129
return
129
130
}
130
131
if having != nil {
131
- havingCondition , err1 := getWhereConditions (having )
132
+ havingCondition , err1 := getWhereConditions (having , defaultIgnoreKeys )
132
133
if nil != err1 {
133
134
err = err1
134
135
return
@@ -169,16 +170,31 @@ func resolveHaving(having interface{}) (map[string]interface{}, error) {
169
170
170
171
// BuildUpdate work as its name says
171
172
func BuildUpdate (table string , where map [string ]interface {}, update map [string ]interface {}) (string , []interface {}, error ) {
172
- conditions , err := getWhereConditions (where )
173
+ var limit uint
174
+ if v , ok := where ["_limit" ]; ok {
175
+ switch val := v .(type ) {
176
+ case int :
177
+ limit = uint (val )
178
+ case uint :
179
+ limit = val
180
+ case int64 :
181
+ limit = uint (val )
182
+ case uint64 :
183
+ limit = uint (val )
184
+ default :
185
+ return "" , nil , errUpdateLimitType
186
+ }
187
+ }
188
+ conditions , err := getWhereConditions (where , defaultIgnoreKeys )
173
189
if nil != err {
174
190
return "" , nil , err
175
191
}
176
- return buildUpdate (table , update , conditions ... )
192
+ return buildUpdate (table , update , limit , conditions ... )
177
193
}
178
194
179
195
// BuildDelete work as its name says
180
196
func BuildDelete (table string , where map [string ]interface {}) (string , []interface {}, error ) {
181
- conditions , err := getWhereConditions (where )
197
+ conditions , err := getWhereConditions (where , defaultIgnoreKeys )
182
198
if nil != err {
183
199
return "" , nil , err
184
200
}
@@ -209,7 +225,7 @@ func isStringInSlice(str string, arr []string) bool {
209
225
return false
210
226
}
211
227
212
- func getWhereConditions (where map [string ]interface {}) ([]Comparable , error ) {
228
+ func getWhereConditions (where map [string ]interface {}, ignoreKeys map [ string ] struct {} ) ([]Comparable , error ) {
213
229
if len (where ) == 0 {
214
230
return nil , nil
215
231
}
@@ -218,6 +234,9 @@ func getWhereConditions(where map[string]interface{}) ([]Comparable, error) {
218
234
var field , operator string
219
235
var err error
220
236
for key , val := range where {
237
+ if _ , ok := ignoreKeys [key ]; ok {
238
+ continue
239
+ }
221
240
if key == "_or" {
222
241
var (
223
242
orWheres []map [string ]interface {}
@@ -231,7 +250,7 @@ func getWhereConditions(where map[string]interface{}) ([]Comparable, error) {
231
250
if orWhere == nil {
232
251
continue
233
252
}
234
- orNestWhere , err := getWhereConditions (orWhere )
253
+ orNestWhere , err := getWhereConditions (orWhere , ignoreKeys )
235
254
if nil != err {
236
255
return nil , err
237
256
}
0 commit comments