Use ILIKE helper function
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
kolaente 2021-07-30 23:49:38 +02:00
parent 549a87856e
commit bdf34e50aa
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
13 changed files with 46 additions and 102 deletions

View File

@ -17,8 +17,8 @@
package db
import (
"code.vikunja.io/api/pkg/config"
"xorm.io/builder"
"xorm.io/xorm/schemas"
)
// ILIKE returns an ILIKE query on postgres and a LIKE query on all other platforms.
@ -27,7 +27,7 @@ import (
// ILIKE is preferred over LOWER(text) LIKE for performance reasons.
// See https://stackoverflow.com/q/7005302/10924593
func ILIKE(column, search string) builder.Cond {
if config.DatabaseType.GetString() == "postgres" {
if Type() == schemas.POSTGRES {
return builder.Expr("`"+column+"` ILIKE ?", "%"+search+"%")
}

View File

@ -21,7 +21,8 @@ import (
"strings"
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
@ -202,11 +203,7 @@ func getLabelsByTaskIDs(s *xorm.Session, opts *LabelByTaskIDsOptions) (ls []*lab
if len(ids) > 0 {
cond = builder.And(cond, builder.In("labels.id", ids))
} else {
if config.DatabaseType.GetString() == "postgres" {
cond = builder.And(cond, builder.Expr("labels.title ILIKE ?", "%"+opts.Search+"%"))
} else {
cond = builder.And(cond, &builder.Like{"labels.title", "%" + opts.Search + "%"})
}
cond = builder.And(cond, db.ILIKE("labels.title", opts.Search))
}
limit, start := getLimitFromPageIndex(opts.Page, opts.PerPage)

View File

@ -20,7 +20,8 @@ import (
"errors"
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/api/pkg/utils"
@ -207,22 +208,13 @@ func (share *LinkSharing) ReadAll(s *xorm.Session, a web.Auth, search string, pa
limit, start := getLimitFromPageIndex(page, perPage)
var shares []*LinkSharing
var where builder.Cond
where = builder.Or(
builder.Like{"hash", "%" + search + "%"},
builder.Like{"name", "%" + search + "%"},
)
if config.DatabaseType.GetString() == "postgres" {
where = builder.Or(
builder.Expr("hash ILIKE ?", "%"+search+"%"),
builder.Expr("name ILIKE ?", "%"+search+"%"),
)
}
query := s.
Where(builder.And(
builder.Eq{"list_id": share.ListID},
where,
builder.Or(
db.ILIKE("hash", search),
db.ILIKE("name", search),
),
))
if limit > 0 {

View File

@ -21,6 +21,8 @@ import (
"strings"
"time"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/files"
@ -403,14 +405,9 @@ func getRawListsForUser(s *xorm.Session, opts *listOptions) (lists []*List, resu
}
}
filterCond = db.ILIKE("l.title", opts.search)
if len(ids) > 0 {
filterCond = builder.In("l.id", ids)
} else {
if config.DatabaseType.GetString() == "postgres" {
filterCond = builder.Expr("l.title ILIKE ?", "%"+opts.search+"%")
} else {
filterCond = &builder.Like{"l.title", "%" + opts.search + "%"}
}
}
// Gets all Lists where the user is either owner or in a team which has access to the list

View File

@ -19,11 +19,11 @@ package models
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/web"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -196,16 +196,11 @@ func (tl *TeamList) ReadAll(s *xorm.Session, a web.Auth, search string, page int
// Get the teams
all := []*TeamWithRight{}
var where builder.Cond
where = builder.Like{"teams.name", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("teams.name ILIKE ?", "%"+search+"%")
}
query := s.
Table("teams").
Join("INNER", "team_lists", "team_id = teams.id").
Where("team_lists.list_id = ?", tl.ListID).
Where(where)
Where(db.ILIKE("teams.name", search))
if limit > 0 {
query = query.Limit(limit, start)
}

View File

@ -17,9 +17,9 @@
package models
import (
"code.vikunja.io/api/pkg/config"
"time"
"xorm.io/builder"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
@ -203,15 +203,10 @@ func (lu *ListUser) ReadAll(s *xorm.Session, a web.Auth, search string, page int
// Get all users
all := []*UserWithRight{}
var where builder.Cond
where = builder.Like{"users.username", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("users.username ILIKE ?", "%"+search+"%")
}
query := s.
Join("INNER", "users_lists", "user_id = users.id").
Where("users_lists.list_id = ?", lu.ListID).
Where(where)
Where(db.ILIKE("users.username", search))
if limit > 0 {
query = query.Limit(limit, start)
}

View File

@ -22,7 +22,8 @@ import (
"strings"
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
@ -201,10 +202,7 @@ func makeNamespaceSlice(namespaces map[int64]*NamespaceWithLists, userMap map[in
}
func getNamespaceFilterCond(search string) (filterCond builder.Cond) {
filterCond = &builder.Like{"namespaces.title", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
filterCond = builder.Expr("namespaces.title ILIKE ?", "%"+search+"%")
}
filterCond = db.ILIKE("namespaces.title", search)
if search == "" {
return

View File

@ -19,11 +19,11 @@ package models
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/web"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -180,19 +180,12 @@ func (tn *TeamNamespace) ReadAll(s *xorm.Session, a web.Auth, search string, pag
// Get the teams
all := []*TeamWithRight{}
limit, start := getLimitFromPageIndex(page, perPage)
var where builder.Cond
where = builder.Like{"teams.name", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("teams.name ILIKE ?", "%"+search+"%")
}
query := s.
Table("teams").
Join("INNER", "team_namespaces", "team_id = teams.id").
Where("team_namespaces.namespace_id = ?", tn.NamespaceID).
Where(where)
Where(db.ILIKE("teams.name", search))
if limit > 0 {
query = query.Limit(limit, start)
}

View File

@ -19,12 +19,12 @@ package models
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
user2 "code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -187,17 +187,11 @@ func (nu *NamespaceUser) ReadAll(s *xorm.Session, a web.Auth, search string, pag
// Get all users
all := []*UserWithRight{}
limit, start := getLimitFromPageIndex(page, perPage)
var where builder.Cond
where = builder.Like{"users.username", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("users.username ILIKE ?", "%"+search+"%")
}
query := s.
Join("INNER", "users_namespaces", "user_id = users.id").
Where("users_namespaces.namespace_id = ?", nu.NamespaceID).
Where(where)
Where(db.ILIKE("users.username", search))
if limit > 0 {
query = query.Limit(limit, start)
}

View File

@ -17,8 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/config"
"time"
"code.vikunja.io/api/pkg/db"
"xorm.io/builder"
"code.vikunja.io/api/pkg/events"
@ -269,17 +271,14 @@ func (la *TaskAssginee) ReadAll(s *xorm.Session, a web.Auth, search string, page
return nil, 0, 0, ErrGenericForbidden{}
}
limit, start := getLimitFromPageIndex(page, perPage)
var taskAssignees []*user.User
var where builder.Cond
where = builder.Like{"users.username", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("users.username ILIKE ?", "%"+search+"%")
}
query := s.Table("task_assignees").
Select("users.*").
Join("INNER", "users", "task_assignees.user_id = users.id").
Where(builder.And(builder.Eq{"task_id": la.TaskID}, where))
Where(builder.And(
builder.Eq{"task_id": la.TaskID},
db.ILIKE("users.username", search),
))
if limit > 0 {
query = query.Limit(limit, start)
}

View File

@ -19,7 +19,8 @@ package models
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
@ -215,15 +216,12 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa
}
limit, start := getLimitFromPageIndex(page, perPage)
comments := []*TaskComment{}
var where builder.Cond
where = builder.Like{"comment", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("comment ILIKE ?", "%"+search+"%")
}
query := s.
Where(builder.And(builder.Eq{"task_id": tc.TaskID}, where)).
Where(builder.And(
builder.Eq{"task_id": tc.TaskID},
db.ILIKE("comment", search),
)).
Join("LEFT", "users", "users.id = task_comments.author_id")
if limit > 0 {
query = query.Limit(limit, start)

View File

@ -369,16 +369,7 @@ func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskO
var where builder.Cond
if opts.search != "" {
// Postgres' is case sensitive by default.
// To work around this, we're using ILIKE as opposed to normal LIKE statements.
// ILIKE is preferred over LOWER(text) LIKE for performance reasons.
// See https://stackoverflow.com/q/7005302/10924593
// Seems okay to use that now, we may need to find a better solution overall in the future.
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("title ILIKE ?", "%"+opts.search+"%")
} else {
where = &builder.Like{"title", "%" + opts.search + "%"}
}
where = db.ILIKE("title", opts.search)
searchIndex := getTaskIndexFromSearchString(opts.search)
if searchIndex > 0 {

View File

@ -19,7 +19,8 @@ package models
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
@ -210,18 +211,12 @@ func (t *Team) ReadAll(s *xorm.Session, a web.Auth, search string, page int, per
}
limit, start := getLimitFromPageIndex(page, perPage)
all := []*Team{}
var where builder.Cond
where = builder.Like{"teams.name", "%" + search + "%"}
if config.DatabaseType.GetString() == "postgres" {
where = builder.Expr("teams.name ILIKE ?", "%"+search+"%")
}
query := s.Select("teams.*").
Table("teams").
Join("INNER", "team_members", "team_members.team_id = teams.id").
Where("team_members.user_id = ?", a.GetID()).
Where(where)
Where(db.ILIKE("teams.name", search))
if limit > 0 {
query = query.Limit(limit, start)
}