go-goyave / filter

Dynamic query params filters for Goyave
MIT License
16 stars 4 forks source link

need help with filter #11

Open manh7890 opened 3 weeks ago

manh7890 commented 3 weeks ago

i see in the filter you warp all the filter and the or to a combination is there a way i could write a query like SELECT * FROM "users" WHERE ("users"."username" LIKE '%ass%' AND "users"."username" LIKE '%John%' AND "users"."username" LIKE '%Doe%') OR "users"."username" LIKE '%a%' OR "users"."username" LIKE '%n%' LIMIT 10 with two or more 'or' i want to select some name and get all the name that match one of my filter

System-Glitch commented 3 weeks ago

All possible combinations are described in the readme here.

If both "filter" and "or" are present, then they are interpreted as a combination of two AND groups compared with each other using OR:

?filter=age||$eq||50&filter=name||$cont||Jack&or=name||$cont||John&or=name||$cont||Doe

WHERE ((age = 50 AND name LIKE "%Jack%") OR (name LIKE "%John%" AND name LIKE "%Doe%"))

I don't think you can generate a SQL query like the one you want using this library.

Just to make sure I understand the goal of your SQL query: you want to search for a user that has a username containing multiple search criteria? (Contains "John" AND contains "Doe")

You may modify the "$cont" operator to take multiple criteria into account instead of only one:

filter.Operators["$cont"] = &filter.Operator{
    Function: func(tx *gorm.DB, f *filter.Filter, column string, dataType filter.DataType) *gorm.DB {
        if dataType != filter.DataTypeText && dataType != filter.DataTypeEnum {
            return f.Where(tx, "FALSE")
        }
        groupCondition := tx.Session(&gorm.Session{NewDB: true})
        for _, a := range f.Args {
            query := castEnumAsText(column, dataType) + " LIKE ?"
            value := "%" + sqlutil.EscapeLike(a) + "%"
            groupCondition = f.Where(groupCondition, query, value)
        }
        return tx.Where(groupCondition)
    },
    RequiredArguments: 1,
}

And that would be used like so:

?filter=username||$cont||John,Doe  -> WHERE (username LIKE "%John%" AND username LIKE "%Doe%")