2020-01-31 11:35:06 -09:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
. "github.com/Masterminds/squirrel"
|
2020-08-30 09:08:10 -08:00
|
|
|
"github.com/deluan/navidrome/conf"
|
2020-12-13 10:05:48 -09:00
|
|
|
"github.com/deluan/navidrome/utils"
|
2020-01-31 11:35:06 -09:00
|
|
|
)
|
|
|
|
|
|
2020-04-19 18:40:24 -08:00
|
|
|
func getFullText(text ...string) string {
|
2020-12-13 10:05:48 -09:00
|
|
|
fullText := utils.SanitizeStrings(text...)
|
2020-04-19 18:40:24 -08:00
|
|
|
return " " + fullText
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 11:35:06 -09:00
|
|
|
func (r sqlRepository) doSearch(q string, offset, size int, results interface{}, orderBys ...string) error {
|
2020-08-30 09:08:10 -08:00
|
|
|
q = strings.TrimSpace(q)
|
2020-04-19 18:40:24 -08:00
|
|
|
q = strings.TrimSuffix(q, "*")
|
2020-02-09 08:20:34 -09:00
|
|
|
if len(q) < 2 {
|
2020-01-31 11:35:06 -09:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-08-30 09:08:10 -08:00
|
|
|
|
2020-02-24 18:06:12 -09:00
|
|
|
sq := r.newSelectWithAnnotation(r.tableName + ".id").Columns("*")
|
2020-01-31 11:35:06 -09:00
|
|
|
sq = sq.Limit(uint64(size)).Offset(uint64(offset))
|
|
|
|
|
if len(orderBys) > 0 {
|
|
|
|
|
sq = sq.OrderBy(orderBys...)
|
|
|
|
|
}
|
2020-08-30 09:08:10 -08:00
|
|
|
sq = sq.Where(fullTextExpr(q))
|
|
|
|
|
err := r.queryAll(sq, results)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fullTextExpr(value string) Sqlizer {
|
|
|
|
|
var sep string
|
|
|
|
|
if !conf.Server.SearchFullString {
|
|
|
|
|
sep = " "
|
|
|
|
|
}
|
2020-12-13 10:05:48 -09:00
|
|
|
q := utils.SanitizeStrings(value)
|
2020-01-31 11:35:06 -09:00
|
|
|
parts := strings.Split(q, " ")
|
2020-08-30 09:08:10 -08:00
|
|
|
filters := And{}
|
2020-01-31 11:35:06 -09:00
|
|
|
for _, part := range parts {
|
2020-08-30 09:08:10 -08:00
|
|
|
filters = append(filters, Like{"full_text": "%" + sep + part + "%"})
|
2020-01-31 11:35:06 -09:00
|
|
|
}
|
2020-08-30 09:08:10 -08:00
|
|
|
return filters
|
2020-01-31 11:35:06 -09:00
|
|
|
}
|