Another tweak for postgres sigh

This commit is contained in:
Jamie Curnow 2023-05-31 15:11:25 +10:00
parent 4b4c7baea4
commit 63845377d9
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
2 changed files with 16 additions and 3 deletions

View File

@ -25,3 +25,14 @@ func QuoteTableName(tbl string) string {
return fmt.Sprintf("`%s`", tbl)
}
}
// GetCaseInsensitiveLike returns a different operator based on
// the db driver
func GetCaseInsensitiveLike() string {
switch strings.ToLower(config.Configuration.DB.Driver) {
case config.DatabasePostgres:
return "ILIKE"
default:
return "LIKE"
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"npm/internal/database"
"npm/internal/model"
"gorm.io/gorm"
@ -37,6 +38,7 @@ func ScopeOrderBy(pageInfo *model.PageInfo, defaultSort model.Sort) func(db *gor
func ScopeFilters(filters []model.Filter, filterMap map[string]filterMapValue) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
like := database.GetCaseInsensitiveLike()
for _, f := range filters {
// Lookup this filter field from the name map
if _, ok := filterMap[f.Field]; ok {
@ -69,11 +71,11 @@ func ScopeFilters(filters []model.Filter, filterMap map[string]filterMapValue) f
// LIKE modifiers:
case "contains":
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), `%`+f.Value[0]+`%`)
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), `%`+f.Value[0]+`%`)
case "starts":
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), f.Value[0]+`%`)
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), f.Value[0]+`%`)
case "ends":
db.Where(fmt.Sprintf("%s LIKE ?", f.Field), `%`+f.Value[0])
db.Where(fmt.Sprintf("%s %s ?", f.Field, like), `%`+f.Value[0])
// Array parameter modifiers:
case "in":