Skip to content

Commit 6384537

Browse files
committed
Another tweak for postgres sigh
1 parent 4b4c7ba commit 6384537

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

backend/internal/database/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ func QuoteTableName(tbl string) string {
2525
return fmt.Sprintf("`%s`", tbl)
2626
}
2727
}
28+
29+
// GetCaseInsensitiveLike returns a different operator based on
30+
// the db driver
31+
func GetCaseInsensitiveLike() string {
32+
switch strings.ToLower(config.Configuration.DB.Driver) {
33+
case config.DatabasePostgres:
34+
return "ILIKE"
35+
default:
36+
return "LIKE"
37+
}
38+
}

backend/internal/entity/scopes.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"npm/internal/database"
78
"npm/internal/model"
89

910
"gorm.io/gorm"
@@ -37,6 +38,7 @@ func ScopeOrderBy(pageInfo *model.PageInfo, defaultSort model.Sort) func(db *gor
3738

3839
func ScopeFilters(filters []model.Filter, filterMap map[string]filterMapValue) func(db *gorm.DB) *gorm.DB {
3940
return func(db *gorm.DB) *gorm.DB {
41+
like := database.GetCaseInsensitiveLike()
4042
for _, f := range filters {
4143
// Lookup this filter field from the name map
4244
if _, ok := filterMap[f.Field]; ok {
@@ -69,11 +71,11 @@ func ScopeFilters(filters []model.Filter, filterMap map[string]filterMapValue) f
6971

7072
// LIKE modifiers:
7173
case "contains":
72-
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), `%`+f.Value[0]+`%`)
74+
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), `%`+f.Value[0]+`%`)
7375
case "starts":
74-
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), f.Value[0]+`%`)
76+
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), f.Value[0]+`%`)
7577
case "ends":
76-
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), `%`+f.Value[0])
78+
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), `%`+f.Value[0])
7779

7880
// Array parameter modifiers:
7981
case "in":

0 commit comments

Comments
 (0)