From 1061bc874260970505f6a941bcb5c45c1c1c9702 Mon Sep 17 00:00:00 2001 From: Wessie Date: Sat, 25 May 2024 00:44:00 +0100 Subject: [PATCH] storage/mariadb: implement fave lookup by hash_link mechanism --- storage/mariadb/track.go | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/storage/mariadb/track.go b/storage/mariadb/track.go index d096f84d..aa2f965c 100644 --- a/storage/mariadb/track.go +++ b/storage/mariadb/track.go @@ -292,10 +292,10 @@ func (ss SongStorage) FavoriteCount(song radio.Song) (int64, error) { handle, deferFn := ss.handle.span(op) defer deferFn() - var query = `SELECT count(*) FROM efave WHERE isong=?;` + var query = `SELECT count(*) FROM efave JOIN esong ON esong.id = efave.isong WHERE esong.hash_link=?;` var faveCount int64 - err := sqlx.Get(handle, &faveCount, query, song.ID) + err := sqlx.Get(handle, &faveCount, query, song.HashLink) if err != nil { return 0, errors.E(op, err) } @@ -352,27 +352,6 @@ func (ss SongStorage) UpdateHashLink(old, new radio.SongHash) error { } var songFavoritesOfQuery = expand(` -SELECT - {songColumns}, - {maybeTrackColumns}, - {lastplayedSelect}, - NOW() AS synctime -FROM - enick -JOIN - efave ON efave.inick = enick.id -JOIN - esong ON esong.id = efave.isong - (esong.hash == esong.hash_link) -LEFT JOIN - tracks ON tracks.hash = esong.hash -WHERE - enick.nick = ? -ORDER BY efave.id ASC -LIMIT ? OFFSET ?; -`) - -var songFavoritesOfQuery2 = expand(` SELECT {songColumns}, {maybeTrackColumns}, @@ -381,10 +360,10 @@ SELECT FROM esong LEFT JOIN - tracks ON tracks.hash = esong.hash_link + tracks ON tracks.hash = esong.hash JOIN - (SELECT - esong.hash + (SELECT DISTINCT + esong.hash_link FROM enick JOIN @@ -392,10 +371,10 @@ JOIN JOIN esong ON esong.id = efave.isong WHERE - enick.nick = ? - AND - esong.hash == esong.hash_link) AS truth - ON esong.hash_link = truth.hash; + enick.nick = ?) AS truth + ON esong.hash = truth.hash_link +ORDER BY efave.id ASC +LIMIT ? OFFSET ?; `) var songFavoritesOfDatabaseOnlyQuery = expand(`