[db/mpd] Simplify getting file count from db

This commit is contained in:
chme 2017-08-12 16:47:44 +02:00
parent c45a85d143
commit 0b2ee5a9eb
3 changed files with 30 additions and 28 deletions

View File

@ -1685,6 +1685,31 @@ db_query_fetch_count(struct query_params *qp, struct filecount_info *fci)
return 0;
}
int
db_filecount_get(struct filecount_info *fci, struct query_params *qp)
{
int ret;
ret = db_query_start(qp);
if (ret < 0)
{
db_query_end(qp);
return -1;
}
ret = db_query_fetch_count(qp, fci);
if (ret < 0)
{
db_query_end(qp);
return -1;
}
db_query_end(qp);
return 0;
}
int
db_query_fetch_string(struct query_params *qp, char **string)
{

View File

@ -562,6 +562,9 @@ db_file_enable_bycookie(uint32_t cookie, char *path);
int
db_file_update_directoryid(char *path, int dir_id);
int
db_filecount_get(struct filecount_info *fci, struct query_params *qp);
/* Playlists */
int
db_pl_get_count(void);

View File

@ -807,26 +807,13 @@ mpd_command_stats(struct evbuffer *evbuf, int argc, char **argv, char **errmsg)
memset(&qp, 0, sizeof(struct query_params));
qp.type = Q_COUNT_ITEMS;
ret = db_query_start(&qp);
ret = db_filecount_get(&fci, &qp);
if (ret < 0)
{
db_query_end(&qp);
*errmsg = safe_asprintf("Could not start query");
return ACK_ERROR_UNKNOWN;
}
ret = db_query_fetch_count(&qp, &fci);
if (ret < 0)
{
db_query_end(&qp);
*errmsg = safe_asprintf("Could not fetch query count");
return ACK_ERROR_UNKNOWN;
}
db_query_end(&qp);
artists = db_files_get_artist_count();
albums = db_files_get_album_count();
@ -2457,31 +2444,18 @@ mpd_command_count(struct evbuffer *evbuf, int argc, char **argv, char **errmsg)
}
memset(&qp, 0, sizeof(struct query_params));
qp.type = Q_COUNT_ITEMS;
mpd_get_query_params_find(argc - 1, argv + 1, &qp);
ret = db_query_start(&qp);
ret = db_filecount_get(&fci, &qp);
if (ret < 0)
{
db_query_end(&qp);
sqlite3_free(qp.filter);
*errmsg = safe_asprintf("Could not start query");
return ACK_ERROR_UNKNOWN;
}
ret = db_query_fetch_count(&qp, &fci);
if (ret < 0)
{
db_query_end(&qp);
sqlite3_free(qp.filter);
*errmsg = safe_asprintf("Could not fetch query count");
return ACK_ERROR_UNKNOWN;
}
evbuffer_add_printf(evbuf,
"songs: %d\n"
"playtime: %" PRIu64 "\n",