filter icon indicating copy to clipboard operation
filter copied to clipboard

need help with filter

Open manh7890 opened this issue 1 year ago • 1 comments

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

manh7890 avatar Aug 21 '24 04:08 manh7890

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%")

System-Glitch avatar Aug 21 '24 08:08 System-Glitch