From 286525a85c7b41da9340a3af8021c13f0f34f539 Mon Sep 17 00:00:00 2001 From: whatdoineed2do/Ray Date: Sun, 22 Mar 2020 22:01:04 +0000 Subject: [PATCH] [db] inc playcount using filter --- src/db.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ src/db.h | 6 ++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/db.c b/src/db.c index 626f1994..f2e6e3ce 100644 --- a/src/db.c +++ b/src/db.c @@ -2514,10 +2514,10 @@ db_files_get_count(uint32_t *nitems, uint32_t *nstreams, const char *filter) return 0; } -void -db_file_inc_playcount(int id) +static void +db_file_inc_playcount_byfilter(const char *filter) { -#define Q_TMPL "UPDATE files SET play_count = play_count + 1, time_played = %" PRIi64 ", seek = 0 WHERE id = %d;" +#define Q_TMPL "UPDATE files SET play_count = play_count + 1, time_played = %" PRIi64 ", seek = 0 WHERE %s;" /* * Rating calculation is taken from from the beets plugin "mpdstats" (see https://beets.readthedocs.io/en/latest/plugins/mpdstats.html) * and adapted to the forked-daapd rating rage (0 to 100). @@ -2536,14 +2536,16 @@ db_file_inc_playcount(int id) "UPDATE files "\ " SET play_count = play_count + 1, time_played = %" PRIi64 ", seek = 0, "\ " rating = CAST(((play_count + 1.0) / (play_count + skip_count + 2.0) * 100 * 0.75) + ((rating + ((100.0 - rating) / 2.0)) * 0.25) AS INT)" \ - " WHERE id = %d;" + " WHERE %s;" char *query; int ret; + if (db_rating_updates) - query = sqlite3_mprintf(Q_TMPL_WITH_RATING, (int64_t)time(NULL), id); + query = sqlite3_mprintf(Q_TMPL_WITH_RATING, (int64_t)time(NULL), filter); else - query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), id); + query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), filter); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -2558,6 +2560,41 @@ db_file_inc_playcount(int id) #undef Q_TMPL_WITH_RATING } +void +db_file_inc_playcount_byplid(int id, bool only_unplayed) +{ + char *filter; + + filter = sqlite3_mprintf("path IN (SELECT filepath FROM playlistitems WHERE playlistid = %d) %s", + id, only_unplayed ? "AND play_count = 0" : ""); + + db_file_inc_playcount_byfilter(filter); + sqlite3_free(filter); +} + +void +db_file_inc_playcount_bysongalbumid(int64_t id, bool only_unplayed) +{ + char *filter; + + filter = sqlite3_mprintf("songalbumid = %" PRIi64 " %s", + id, only_unplayed ? "AND play_count = 0" : ""); + + db_file_inc_playcount_byfilter(filter); + sqlite3_free(filter); +} + +void +db_file_inc_playcount(int id) +{ + char *filter; + + filter = sqlite3_mprintf("id = %d", id); + + db_file_inc_playcount_byfilter(filter); + sqlite3_free(filter); +} + void db_file_inc_skipcount(int id) { diff --git a/src/db.h b/src/db.h index 740a3fd7..b1f5a1a8 100644 --- a/src/db.h +++ b/src/db.h @@ -580,6 +580,12 @@ db_files_get_count(uint32_t *nitems, uint32_t *nstreams, const char *filter); void db_file_inc_playcount(int id); +void +db_file_inc_playcount_byplid(int id, bool only_unplayed); + +void +db_file_inc_playcount_bysongalbumid(int64_t id, bool only_unplayed); + void db_file_inc_skipcount(int id);