mirror of
https://github.com/owntone/owntone-server.git
synced 2024-12-29 00:23:23 -05:00
[db] Refactor fetching query result into generic function
Additionally changes the return value in case the end of the result set is reached.
This commit is contained in:
parent
d7086cab00
commit
a65ee4462e
@ -1766,7 +1766,7 @@ process_items(struct artwork_ctx *ctx, int item_mode)
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&ctx->qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&ctx->qp, &dbmfi)) == 0)
|
||||
{
|
||||
// Save the first songalbumid, might need it for process_group() if this search doesn't give anything
|
||||
if (!ctx->persistentid)
|
||||
@ -1859,7 +1859,7 @@ process_group(struct artwork_ctx *ctx)
|
||||
goto invalid_group;
|
||||
}
|
||||
|
||||
is_valid = (db_query_fetch_file(&ctx->qp, &dbmfi) == 0 && dbmfi.id && strcmp(dbmfi.album, CFG_NAME_UNKNOWN_ALBUM) != 0 && strcmp(dbmfi.album_artist, CFG_NAME_UNKNOWN_ARTIST) != 0);
|
||||
is_valid = (db_query_fetch_file(&ctx->qp, &dbmfi) == 0 && strcmp(dbmfi.album, CFG_NAME_UNKNOWN_ALBUM) != 0 && strcmp(dbmfi.album_artist, CFG_NAME_UNKNOWN_ARTIST) != 0);
|
||||
db_query_end(&ctx->qp);
|
||||
if (!is_valid)
|
||||
{
|
||||
|
134
src/db.c
134
src/db.c
@ -2372,34 +2372,25 @@ db_query_run(char *query, int free, short update_events)
|
||||
return ((ret != SQLITE_OK) ? -1 : 0);
|
||||
}
|
||||
|
||||
int
|
||||
db_query_fetch_file(struct query_params *qp, struct db_media_file_info *dbmfi)
|
||||
static int
|
||||
db_query_fetch(void *item, struct query_params *qp, const ssize_t cols_map[], int size)
|
||||
{
|
||||
int ncols;
|
||||
char **strcol;
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
memset(dbmfi, 0, sizeof(struct db_media_file_info));
|
||||
|
||||
if (!qp->stmt)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Query not started!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((qp->type != Q_ITEMS) && (qp->type != Q_PLITEMS) && (qp->type != Q_GROUP_ITEMS))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Not an items, playlist or group items query!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = db_blocking_step(qp->stmt);
|
||||
if (ret == SQLITE_DONE)
|
||||
{
|
||||
DPRINTF(E_DBG, L_DB, "End of query results\n");
|
||||
dbmfi->id = NULL;
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
else if (ret != SQLITE_ROW)
|
||||
{
|
||||
@ -2410,15 +2401,15 @@ db_query_fetch_file(struct query_params *qp, struct db_media_file_info *dbmfi)
|
||||
ncols = sqlite3_column_count(qp->stmt);
|
||||
|
||||
// We allow more cols in db than in map because the db may be a future schema
|
||||
if (ncols < ARRAY_SIZE(dbmfi_cols_map))
|
||||
if (ncols < size)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "BUG: database has fewer columns (%d) than dbmfi column map (%u)\n", ncols, ARRAY_SIZE(dbmfi_cols_map));
|
||||
DPRINTF(E_LOG, L_DB, "BUG: database has fewer columns (%d) than column map (%u)\n", ncols, size);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(dbmfi_cols_map); i++)
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
strcol = (char **) ((char *)dbmfi + dbmfi_cols_map[i]);
|
||||
strcol = (char **) ((char *)item + cols_map[i]);
|
||||
|
||||
*strcol = (char *)sqlite3_column_text(qp->stmt, i);
|
||||
}
|
||||
@ -2426,6 +2417,26 @@ db_query_fetch_file(struct query_params *qp, struct db_media_file_info *dbmfi)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
db_query_fetch_file(struct query_params *qp, struct db_media_file_info *dbmfi)
|
||||
{
|
||||
int ret;
|
||||
|
||||
memset(dbmfi, 0, sizeof(struct db_media_file_info));
|
||||
|
||||
if ((qp->type != Q_ITEMS) && (qp->type != Q_PLITEMS) && (qp->type != Q_GROUP_ITEMS))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Not an items, playlist or group items query!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = db_query_fetch(dbmfi, qp, dbmfi_cols_map, ARRAY_SIZE(dbmfi_cols_map));
|
||||
if (ret < 0) {
|
||||
DPRINTF(E_LOG, L_DB, "Failed to fetch db_media_file_info\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
db_query_fetch_pl(struct query_params *qp, struct db_playlist_info *dbpli)
|
||||
{
|
||||
@ -2496,107 +2507,41 @@ db_query_fetch_pl(struct query_params *qp, struct db_playlist_info *dbpli)
|
||||
int
|
||||
db_query_fetch_group(struct query_params *qp, struct db_group_info *dbgri)
|
||||
{
|
||||
int ncols;
|
||||
char **strcol;
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
memset(dbgri, 0, sizeof(struct db_group_info));
|
||||
|
||||
if (!qp->stmt)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Query not started!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((qp->type != Q_GROUP_ALBUMS) && (qp->type != Q_GROUP_ARTISTS))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Not a groups query!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = db_blocking_step(qp->stmt);
|
||||
if (ret == SQLITE_DONE)
|
||||
{
|
||||
DPRINTF(E_DBG, L_DB, "End of query results\n");
|
||||
return 1;
|
||||
}
|
||||
else if (ret != SQLITE_ROW)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Could not step: %s\n", sqlite3_errmsg(hdl));
|
||||
return -1;
|
||||
}
|
||||
|
||||
ncols = sqlite3_column_count(qp->stmt);
|
||||
|
||||
// We allow more cols in db than in map because the db may be a future schema
|
||||
if (ncols < ARRAY_SIZE(dbgri_cols_map))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "BUG: database has fewer columns (%d) than dbgri column map (%u)\n", ncols, ARRAY_SIZE(dbgri_cols_map));
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(dbgri_cols_map); i++)
|
||||
{
|
||||
strcol = (char **) ((char *)dbgri + dbgri_cols_map[i]);
|
||||
|
||||
*strcol = (char *)sqlite3_column_text(qp->stmt, i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
ret = db_query_fetch(dbgri, qp, dbgri_cols_map, ARRAY_SIZE(dbgri_cols_map));
|
||||
if (ret < 0) {
|
||||
DPRINTF(E_LOG, L_DB, "Failed to fetch db_group_info\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
db_query_fetch_browse(struct query_params *qp, struct db_browse_info *dbbi)
|
||||
{
|
||||
int ncols;
|
||||
char **strcol;
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
memset(dbbi, 0, sizeof(struct db_browse_info));
|
||||
|
||||
if (!qp->stmt)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Query not started!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(qp->type & Q_F_BROWSE))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Not a browse query!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = db_blocking_step(qp->stmt);
|
||||
if (ret == SQLITE_DONE)
|
||||
{
|
||||
DPRINTF(E_DBG, L_DB, "End of query results\n");
|
||||
return 1;
|
||||
}
|
||||
else if (ret != SQLITE_ROW)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Could not step: %s\n", sqlite3_errmsg(hdl));
|
||||
return -1;
|
||||
}
|
||||
|
||||
ncols = sqlite3_column_count(qp->stmt);
|
||||
|
||||
// We allow more cols in db than in map because the db may be a future schema
|
||||
if (ncols < ARRAY_SIZE(dbbi_cols_map))
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "BUG: database has fewer columns (%d) than dbbi column map (%u)\n", ncols, ARRAY_SIZE(dbbi_cols_map));
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(dbbi_cols_map); i++)
|
||||
{
|
||||
strcol = (char **) ((char *)dbbi + dbbi_cols_map[i]);
|
||||
|
||||
*strcol = (char *)sqlite3_column_text(qp->stmt, i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
ret = db_query_fetch(dbbi, qp, dbbi_cols_map, ARRAY_SIZE(dbbi_cols_map));
|
||||
if (ret < 0) {
|
||||
DPRINTF(E_LOG, L_DB, "Failed to fetch db_browse_info\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
@ -5174,7 +5119,7 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
|
||||
goto end_transaction;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = queue_item_add_from_file(&dbmfi, pos, queue_count, queue_version);
|
||||
|
||||
@ -5195,6 +5140,9 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
|
||||
queue_count++;
|
||||
}
|
||||
|
||||
if (ret > 0)
|
||||
ret = 0;
|
||||
|
||||
db_query_end(qp);
|
||||
|
||||
if (ret < 0)
|
||||
|
@ -1271,7 +1271,7 @@ daap_reply_songlist_generic(struct httpd_request *hreq, int playlist)
|
||||
|
||||
nsongs = 0;
|
||||
last_codectype = NULL;
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
nsongs++;
|
||||
|
||||
|
@ -253,7 +253,7 @@ find_first_song_id(const char *query)
|
||||
goto no_query_start;
|
||||
}
|
||||
|
||||
if (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
if ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = safe_atoi32(dbmfi.id, &id);
|
||||
if (ret < 0)
|
||||
@ -264,7 +264,6 @@ find_first_song_id(const char *query)
|
||||
}
|
||||
|
||||
DPRINTF(E_DBG, L_DACP, "Found index song (id %d)\n", id);
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -280,7 +279,7 @@ find_first_song_id(const char *query)
|
||||
if (qp.filter)
|
||||
free(qp.filter);
|
||||
|
||||
if (ret == 1)
|
||||
if (id > 0)
|
||||
return id;
|
||||
else
|
||||
return -1;
|
||||
|
@ -458,7 +458,7 @@ fetch_tracks(struct query_params *query_params, json_object *items, int *total)
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
||||
while (((ret = db_query_fetch_file(query_params, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(query_params, &dbmfi)) == 0)
|
||||
{
|
||||
item = track_to_json(&dbmfi);
|
||||
if (!item)
|
||||
@ -3322,8 +3322,7 @@ jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq)
|
||||
ret = db_query_fetch_file(&query_params, &dbmfi);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
||||
if (dbmfi.id == 0)
|
||||
else if (ret == 1)
|
||||
{
|
||||
DPRINTF(E_LOG, L_WEB, "Track with id '%s' not found.\n", track_id);
|
||||
ret = -1;
|
||||
|
@ -588,7 +588,7 @@ rsp_reply_playlist(struct httpd_request *hreq)
|
||||
mxmlNewTextf(node, 0, "%d", qp.results);
|
||||
|
||||
/* Items block (all items) */
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
headers = evhttp_request_get_input_headers(hreq->req);
|
||||
|
||||
|
@ -982,7 +982,7 @@ pipelist_create(void)
|
||||
return NULL;
|
||||
|
||||
head = NULL;
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = safe_atoi32(dbmfi.id, &id);
|
||||
if (ret < 0)
|
||||
|
@ -1909,7 +1909,7 @@ playlist_add_files(FILE *fp, int pl_id, const char *virtual_path)
|
||||
|
||||
if (qp.results > 0)
|
||||
{
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
if ((safe_atou32(dbmfi.data_kind, &data_kind) < 0)
|
||||
|| (data_kind == DATA_KIND_PIPE))
|
||||
|
14
src/mpd.c
14
src/mpd.c
@ -2317,7 +2317,7 @@ mpd_command_listplaylist(struct evbuffer *evbuf, int argc, char **argv, char **e
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
evbuffer_add_printf(evbuf,
|
||||
"file: %s\n",
|
||||
@ -2380,7 +2380,7 @@ mpd_command_listplaylistinfo(struct evbuffer *evbuf, int argc, char **argv, char
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = mpd_add_db_media_file_info(evbuf, &dbmfi);
|
||||
if (ret < 0)
|
||||
@ -2672,7 +2672,7 @@ mpd_command_find(struct evbuffer *evbuf, int argc, char **argv, char **errmsg, s
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = mpd_add_db_media_file_info(evbuf, &dbmfi);
|
||||
if (ret < 0)
|
||||
@ -2777,7 +2777,7 @@ mpd_command_list(struct evbuffer *evbuf, int argc, char **argv, char **errmsg, s
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
strval = (char **) ((char *)&dbmfi + tagtype->mfi_offset);
|
||||
|
||||
@ -2917,7 +2917,7 @@ mpd_add_directory(struct evbuffer *evbuf, int directory_id, int listall, int lis
|
||||
*errmsg = safe_asprintf("Could not start query");
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
if (listinfo)
|
||||
{
|
||||
@ -3140,7 +3140,7 @@ mpd_command_search(struct evbuffer *evbuf, int argc, char **argv, char **errmsg,
|
||||
return ACK_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = mpd_add_db_media_file_info(evbuf, &dbmfi);
|
||||
if (ret < 0)
|
||||
@ -3392,7 +3392,7 @@ mpd_sticker_find(struct evbuffer *evbuf, int argc, char **argv, char **errmsg, c
|
||||
return ret;
|
||||
}
|
||||
|
||||
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
||||
while ((ret = db_query_fetch_file(&qp, &dbmfi)) == 0)
|
||||
{
|
||||
ret = safe_atou32(dbmfi.rating, &rating);
|
||||
if (ret < 0)
|
||||
|
Loading…
Reference in New Issue
Block a user