go gorm framework Group Condition is invalid

29 Views Asked by At

I'm trying to create my own search query. Name is set as the field name in the request, and AliasName is the field to be queried by the database. When | is displayed in the field to be queried, the current rule is queried by using the Where Or statement

db := core.NewDBAutoConfig(config.GetDevMysql())
easySearch := core.NewEasySearch(db, map[string]interface{}{
        "name": "1111",
        "age":  18,
    })
easySearch.AddRule(&core.SearchRule{Name: "name", AliasName: "a.name|a.sex", SearchType: core.Like})
easySearch.AddRule(&core.SearchRule{Name: "age", AliasName: "a.age", SearchType: core.Equals})

    db = easySearch.Build()

Here is my matching code

if strings.Index(config.ColumnName, ColumnSeparator) > 0 {
    columnNames := strings.Split(config.ColumnName, ColumnSeparator)
    for i := 0; i < len(columnNames); i++ {
        args = append(args, columnNames[i])
        args = append(args, matchValue...)
        if i <= 0 {
            s.db = s.db.Where(matchQuery, args...)
        } else {
            s.db = s.db.Or(matchQuery, args...)
        }
    }
    s.db = s.db.Where(s.db)
} else {
    args = append(args, config.ColumnName)
    args = append(args, matchValue...)
    s.db = s.db.Where(matchQuery, args...)
}

When "|" exists in config.ColumnName, I split the strings as an array and concatenate the Where and Or Conditions, but somehow the result I get is not the Group Conditions structure

Current processing result sql

I want to get the following sql output

SELECT * FROM `yhyh_user` WHERE ('a.name' like %'1111'% OR 'a.sex' like %'1111'%) AND a.age = 18
0

There are 0 best solutions below