From 1ea08e2470544e823bebb7704983eb28b5168ba8 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 23 Jul 2023 13:55:27 +0200 Subject: implement advanced local search --- localBackend.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'localBackend.go') diff --git a/localBackend.go b/localBackend.go index e59bf03..129be35 100644 --- a/localBackend.go +++ b/localBackend.go @@ -129,11 +129,38 @@ func (b *LocalBackend) Play(song *Song, player Player) { player.PlayUri("file://" + song.Uri) } +func genericQuery(pattern string) string { + return fmt.Sprintf("SELECT * FROM songs WHERE Title LIKE '%%%s%%' OR Artist LIKE '%%%s%%' OR Album LIKE '%%%s%%'", pattern, pattern, pattern) +} + +func advancedQuery(patterns map[string]string) string { + query := "Select * FROM songs WHERE" + query_parts := []string{} + for _, comp := range []string{"title", "album", "artist"} { + if pattern, ok := patterns[comp]; ok { + query_parts = append(query_parts, fmt.Sprintf("%s LIKE '%%%s%%'", strings.Title(comp), pattern)) + } + } + + return fmt.Sprintf("%s %s", query, strings.Join(query_parts, " OR ")) +} + func (b *LocalBackend) Search(patterns map[string]string) (results []*Song) { - pattern := strings.ToLower(patterns["pattern"]) + advanced := false + for _, comp := range []string{"title", "album", "artist"} { + if _, ok := patterns[comp]; ok { + advanced = true + break + } + } + + var query string + if advanced { + query = advancedQuery(patterns) + } else { + query = genericQuery(strings.ToLower(patterns["pattern"])) + } - query := fmt.Sprintf("SELECT * FROM songs WHERE Title LIKE '%%%s%%' OR Artist LIKE '%%%s%%'", - pattern, pattern) llog.Debug("Searching in local DB using: %q", query) rows, err := b.db.Query(query) if err != nil { -- cgit v1.2.3