diff --git a/builder/builder.go b/builder/builder.go index 0dc447b..942a8ed 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -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) diff --git a/builder/builder_test.go b/builder/builder_test.go index 54ae426..fdefb37 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -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"}, }, }, }, @@ -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, }, },