Skip to content

Commit

Permalink
Replace ilike with LOWER(?) LIKE LOWER(?).
Browse files Browse the repository at this point in the history
Its slightly more performant, and slightly prettier
  • Loading branch information
NodudeWasTaken committed Nov 2, 2024
1 parent 3f1da4d commit 393ad59
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 31 deletions.
12 changes: 6 additions & 6 deletions pkg/sqlite/criterion_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func stringCriterionHandler(c *models.StringCriterionInput, column string) crite
case models.CriterionModifierExcludes:
f.whereClauses = append(f.whereClauses, getStringSearchClause([]string{column}, c.Value, true))
case models.CriterionModifierEquals:
f.addWhere(column+" "+getDBLike()+" ?", c.Value)
f.addWhere("LOWER("+column+") LIKE LOWER(?)", c.Value)
case models.CriterionModifierNotEquals:
f.addWhere(column+" NOT "+getDBLike()+" ?", c.Value)
f.addWhere("LOWER("+column+") NOT LIKE LOWER(?)", c.Value)
case models.CriterionModifierMatchesRegex:
if _, err := regexp.Compile(c.Value); err != nil {
f.setError(err)
Expand Down Expand Up @@ -86,9 +86,9 @@ func uuidCriterionHandler(c *models.StringCriterionInput, column string) criteri
case models.CriterionModifierExcludes:
f.whereClauses = append(f.whereClauses, getStringSearchClause([]string{columnCast}, c.Value, true))
case models.CriterionModifierEquals:
f.addWhere(columnCast+" "+getDBLike()+" ?", c.Value)
f.addWhere("LOWER("+columnCast+") LIKE LOWER(?)", c.Value)
case models.CriterionModifierNotEquals:
f.addWhere(columnCast+" NOT "+getDBLike()+" ?", c.Value)
f.addWhere("LOWER("+columnCast+") NOT LIKE LOWER(?)", c.Value)
case models.CriterionModifierMatchesRegex:
if _, err := regexp.Compile(c.Value); err != nil {
f.setError(err)
Expand Down Expand Up @@ -191,7 +191,7 @@ func getPathSearchClause(pathColumn, basenameColumn, p string, addWildcards, not
}

filepathColumn := fmt.Sprintf("%s || '%s' || %s", pathColumn, string(filepath.Separator), basenameColumn)
ret := makeClause(fmt.Sprintf("%s "+getDBLike()+" ?", filepathColumn), p)
ret := makeClause(fmt.Sprintf("LOWER(%s) LIKE LOWER(?)", filepathColumn), p)

if not {
ret = ret.not()
Expand Down Expand Up @@ -589,7 +589,7 @@ func (m *stringListCriterionHandlerBuilder) handler(criterion *models.StringCrit
// excludes all of the provided values
// need to use actual join table name for this
// <primaryTable>.id NOT IN (select <joinTable>.<primaryFK> from <joinTable> where <joinTable>.<foreignFK> in <values>)
whereClause := utils.StrFormat("{primaryTable}.id NOT IN (SELECT {joinTable}.{primaryFK} from {joinTable} where {joinTable}.{stringColumn} "+getDBLike()+" ?)",
whereClause := utils.StrFormat("{primaryTable}.id NOT IN (SELECT {joinTable}.{primaryFK} from {joinTable} where LOWER({joinTable}.{stringColumn}) LIKE LOWER(?))",
utils.StrFormatMap{
"primaryTable": m.primaryTable,
"joinTable": m.joinTable,
Expand Down
9 changes: 0 additions & 9 deletions pkg/sqlite/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,6 @@ func getDBMinFunc() string {
}
}

func getDBLike() string {
switch dbWrapper.dbType {
case PostgresBackend:
return "ILIKE"
default:
return "LIKE"
}
}

func getDBGroupConcat(columnName string) string {
switch dbWrapper.dbType {
case PostgresBackend:
Expand Down
12 changes: 6 additions & 6 deletions pkg/sqlite/filter_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func TestStringCriterionHandlerIncludes(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("(%[1]s "+getDBLike()+" ? OR %[1]s "+getDBLike()+" ?)", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("(LOWER(%[1]s) LIKE LOWER(?) OR LOWER(%[1]s) LIKE LOWER(?))", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 2)
assert.Equal("%two%", f.whereClauses[0].args[0])
assert.Equal("%words%", f.whereClauses[0].args[1])
Expand All @@ -483,7 +483,7 @@ func TestStringCriterionHandlerIncludes(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("(%[1]s "+getDBLike()+" ?)", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("(LOWER(%[1]s) LIKE LOWER(?))", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 1)
assert.Equal("%two words%", f.whereClauses[0].args[0])
}
Expand All @@ -502,7 +502,7 @@ func TestStringCriterionHandlerExcludes(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("(%[1]s NOT "+getDBLike()+" ? AND %[1]s NOT "+getDBLike()+" ?)", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("(LOWER(%[1]s) NOT LIKE LOWER(?) AND LOWER(%[1]s) NOT LIKE LOWER(?))", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 2)
assert.Equal("%two%", f.whereClauses[0].args[0])
assert.Equal("%words%", f.whereClauses[0].args[1])
Expand All @@ -514,7 +514,7 @@ func TestStringCriterionHandlerExcludes(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("(%[1]s NOT "+getDBLike()+" ?)", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("(LOWER(%[1]s) NOT LIKE LOWER(?))", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 1)
assert.Equal("%two words%", f.whereClauses[0].args[0])
}
Expand All @@ -532,7 +532,7 @@ func TestStringCriterionHandlerEquals(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("%[1]s "+getDBLike()+" ?", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("LOWER(%[1]s) LIKE LOWER(?)", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 1)
assert.Equal(value1, f.whereClauses[0].args[0])
}
Expand All @@ -550,7 +550,7 @@ func TestStringCriterionHandlerNotEquals(t *testing.T) {
}, column))

assert.Len(f.whereClauses, 1)
assert.Equal(fmt.Sprintf("%[1]s NOT "+getDBLike()+" ?", column), f.whereClauses[0].sql)
assert.Equal(fmt.Sprintf("LOWER(%[1]s) NOT LIKE LOWER(?)", column), f.whereClauses[0].sql)
assert.Len(f.whereClauses[0].args, 1)
assert.Equal(value1, f.whereClauses[0].args[0])
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/gallery_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ func (qb *galleryFilterHandler) pathCriterionHandler(c *models.StringCriterionIn
case models.CriterionModifierEquals:
addWildcards = false
clause := getPathSearchClause(pathColumn, basenameColumn, c.Value, addWildcards, not)
clause2 := makeClause(folderPathColumn+" "+getDBLike()+" ?", c.Value)
clause2 := makeClause("LOWER("+folderPathColumn+") LIKE LOWER(?)", c.Value)
f.whereClauses = append(f.whereClauses, orClauses(clause, clause2))
case models.CriterionModifierNotEquals:
addWildcards = false
not = true
clause := getPathSearchClause(pathColumn, basenameColumn, c.Value, addWildcards, not)
clause2 := makeClause(folderPathColumn+" NOT "+getDBLike()+" ?", c.Value)
clause2 := makeClause("LOWER("+folderPathColumn+") NOT LIKE LOWER(?)", c.Value)
f.whereClauses = append(f.whereClauses, orClauses(clause, clause2))
case models.CriterionModifierMatchesRegex:
if _, err := regexp.Compile(c.Value); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (qb *queryBuilder) parseQueryString(columns []string, q string) {
var clauses []string

for _, column := range columns {
clauses = append(clauses, column+" "+getDBLike()+" ?")
clauses = append(clauses, "LOWER("+column+") LIKE LOWER(?)")
qb.addArg(like(t))
}

Expand All @@ -199,7 +199,7 @@ func (qb *queryBuilder) parseQueryString(columns []string, q string) {

for _, t := range specs.MustNot {
for _, column := range columns {
qb.addWhere(coalesce(column) + " NOT " + getDBLike() + " ?")
qb.addWhere("LOWER(" + coalesce(column) + ") NOT LIKE LOWER(?)")
qb.addArg(like(t))
}
}
Expand All @@ -209,7 +209,7 @@ func (qb *queryBuilder) parseQueryString(columns []string, q string) {

for _, column := range columns {
for _, v := range set {
clauses = append(clauses, column+" "+getDBLike()+" ?")
clauses = append(clauses, "LOWER("+column+") LIKE LOWER(?)")
qb.addArg(like(v))
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/scene_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (qb *SceneMarkerStore) CountByTagID(ctx context.Context, tagID int) (int, e
func (qb *SceneMarkerStore) GetMarkerStrings(ctx context.Context, q *string, sort *string) ([]*models.MarkerStringsResultType, error) {
query := "SELECT count(*) as `count`, scene_markers.id as id, scene_markers.title as title FROM scene_markers"
if q != nil {
query += " WHERE title " + getDBLike() + " '%" + *q + "%'"
query += " WHERE LOWER(title) LIKE LOWER('%" + *q + "%')"
}
query += " GROUP BY title"
if sort != nil && *sort == "count" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ func getStringSearchClause(columns []string, q string, not bool) sqlClause {
// Search for any word
for _, word := range queryWords {
for _, column := range columns {
likeClauses = append(likeClauses, column+notStr+" "+getDBLike()+" ?")
likeClauses = append(likeClauses, "LOWER("+column+")"+notStr+" LIKE LOWER(?)")
args = append(args, "%"+word+"%")
}
}
} else {
// Search the exact query
for _, column := range columns {
likeClauses = append(likeClauses, column+notStr+" "+getDBLike()+" ?")
likeClauses = append(likeClauses, "LOWER("+column+")"+notStr+" LIKE LOWER(?)")
args = append(args, "%"+trimmedQuery+"%")
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ func (qb *TagStore) QueryForAutoTag(ctx context.Context, words []string) ([]*mod

for _, w := range words {
ww := w + "%"
whereClauses = append(whereClauses, "tags.name "+getDBLike()+" ?")
whereClauses = append(whereClauses, "LOWER(tags.name) LIKE LOWER(?)")
args = append(args, ww)

// include aliases
whereClauses = append(whereClauses, "tag_aliases.alias "+getDBLike()+" ?")
whereClauses = append(whereClauses, "LOWER(tag_aliases.alias) LIKE LOWER(?)")
args = append(args, ww)
}

Expand Down

0 comments on commit 393ad59

Please sign in to comment.