Skip to content

Add ILIKE operator to filter types for filtered result becomes case-insensitive. #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
21 changes: 11 additions & 10 deletions plugins/admin/modules/parameter/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ const (
)

var operators = map[string]string{
"like": "like",
"gr": ">",
"gq": ">=",
"eq": "=",
"ne": "!=",
"le": "<",
"lq": "<=",
"free": "free",
"ilike": "ilike",
"like": "like",
"gr": ">",
"gq": ">=",
"eq": "=",
"ne": "!=",
"le": "<",
"lq": "<=",
"free": "free",
}

var keys = []string{Page, PageSize, Sort, Columns, Prefix, Pjax, form.NoAnimationKey}
Expand Down Expand Up @@ -419,7 +420,7 @@ func (param Parameters) Statement(wheres, table, delimiter, delimiter2 string, w
} else {
wheres += keys[0] + "." + modules.FilterField(keys[1], delimiter, delimiter2) + " " + op + " ? and "
}
if op == "like" && !strings.Contains(val, "%") {
if strings.Contains(op, "like") && !strings.Contains(val, "%") {
whereArgs = append(whereArgs, "%"+val+"%")
} else {
for _, v := range value {
Expand All @@ -437,7 +438,7 @@ func (param Parameters) Statement(wheres, table, delimiter, delimiter2 string, w
} else {
wheres += modules.Delimiter(delimiter, delimiter2, table) + "." + modules.FilterField(key, delimiter, delimiter2) + " " + op + " ? and "
}
if op == "like" && !strings.Contains(value[0], "%") {
if strings.Contains(op, "like") && !strings.Contains(value[0], "%") {
whereArgs = append(whereArgs, "%"+filterProcess(key, value[0], keyIndexSuffix)+"%")
} else {
for _, v := range value {
Expand Down
9 changes: 7 additions & 2 deletions template/types/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "html/template"
type FilterOperator string

const (
FilterOperatorILike FilterOperator = "ilike"
FilterOperatorLike FilterOperator = "like"
FilterOperatorGreater FilterOperator = ">"
FilterOperatorGreaterOrEqual FilterOperator = ">="
Expand All @@ -17,6 +18,8 @@ const (

func GetOperatorFromValue(value string) FilterOperator {
switch value {
case "ilike":
return FilterOperatorILike
case "like":
return FilterOperatorLike
case "gr":
Expand All @@ -40,6 +43,8 @@ func GetOperatorFromValue(value string) FilterOperator {

func (o FilterOperator) Value() string {
switch o {
case FilterOperatorILike:
return "ilike"
case FilterOperatorLike:
return "like"
case FilterOperatorGreater:
Expand All @@ -66,7 +71,7 @@ func (o FilterOperator) String() string {
}

func (o FilterOperator) Label() template.HTML {
if o == FilterOperatorLike {
if o == FilterOperatorLike || o == FilterOperatorILike {
return ""
}
return template.HTML(o)
Expand All @@ -78,7 +83,7 @@ func (o FilterOperator) AddOrNot() bool {

func (o FilterOperator) Valid() bool {
switch o {
case FilterOperatorLike, FilterOperatorGreater, FilterOperatorGreaterOrEqual,
case FilterOperatorILike, FilterOperatorLike, FilterOperatorGreater, FilterOperatorGreaterOrEqual,
FilterOperatorLess, FilterOperatorLessOrEqual, FilterOperatorFree:
return true
default:
Expand Down