Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,35 @@ func getWhereConditions(where map[string]interface{}, ignoreKeys map[string]stru
}
if key == "_or" {
var (
orWheres []map[string]interface{}
orWhereComparable []Comparable
ok bool
orWheres []map[string]interface{}
_orWheres [][]map[string]interface{}
ok bool
)

if orWheres, ok = val.([]map[string]interface{}); !ok {
return nil, errOrValueType
}
for _, orWhere := range orWheres {
if orWhere == nil {
continue
if _orWheres, ok = val.([][]map[string]interface{}); !ok {
return nil, errOrValueType
}
orNestWhere, err := getWhereConditions(orWhere, ignoreKeys)
if nil != err {
return nil, err
} else {
_orWheres = [][]map[string]interface{}{orWheres}
}

var multiOrWhereComparable []Comparable
for _, orWheres := range _orWheres {
var orWhereComparable []Comparable
for _, orWhere := range orWheres {
if orWhere == nil {
continue
}
orNestWhere, err := getWhereConditions(orWhere, ignoreKeys)
if nil != err {
return nil, err
}
orWhereComparable = append(orWhereComparable, NestWhere(orNestWhere))
}
orWhereComparable = append(orWhereComparable, NestWhere(orNestWhere))
multiOrWhereComparable = append(multiOrWhereComparable, OrWhere(orWhereComparable))
}
comparables = append(comparables, OrWhere(orWhereComparable))
comparables = append(comparables, multiOrWhereComparable...)
continue
}
field, operator, err = splitKey(key, val)
Expand Down
14 changes: 7 additions & 7 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,14 @@ func Test_BuildUpdate(t *testing.T) {
"foo": "bar",
"age >=": 23,
"sex in": []interface{}{"male", "female"},
"_or": []map[string]interface{}{
"_or": [][]map[string]interface{}{
{
"x1": 11,
"x2 >=": 45,
{"x1": 11, "x2 >=": 45},
{"x3": "234", "x4 <>": "tx2"},
},
{
"x3": "234",
"x4 <>": "tx2",
{"id": "11"},
{"act_id": "11"},
},
},
},
Expand All @@ -444,8 +444,8 @@ func Test_BuildUpdate(t *testing.T) {
},
},
out: outStruct{
cond: "UPDATE tb SET district=?,score=? WHERE (((x1=? AND x2>=?) OR (x3=? AND x4!=?)) AND foo=? AND sex IN (?,?) AND age>=?)",
vals: []interface{}{"010", 50, 11, 45, "234", "tx2", "bar", "male", "female", 23},
cond: "UPDATE tb SET district=?,score=? WHERE (((x1=? AND x2>=?) OR (x3=? AND x4!=?)) AND ((id=?) OR (act_id=?)) AND foo=? AND sex IN (?,?) AND age>=?)",
vals: []interface{}{"010", 50, 11, 45, "234", "tx2", "11", "11", "bar", "male", "female", 23},
err: nil,
},
},
Expand Down