From dc95a045625cc32bc979f518e66907e47994dc22 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sat, 24 Apr 2010 10:20:26 +0200 Subject: [PATCH] Add optional sorting clause to Q_ITEMS --- src/db.c | 24 ++++++++++++++++++++---- src/db.h | 7 +++++++ src/httpd_daap.c | 2 ++ src/httpd_rsp.c | 2 ++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/db.c b/src/db.c index 94db9ec5..efda780a 100644 --- a/src/db.c +++ b/src/db.c @@ -618,6 +618,7 @@ db_build_query_items(struct query_params *qp, char **q) char *query; char *count; char *idx; + char *sort; int ret; if (qp->filter) @@ -643,14 +644,29 @@ db_build_query_items(struct query_params *qp, char **q) if (ret < 0) return -1; + switch (qp->sort) + { + case S_NONE: + sort = ""; + break; + + case S_NAME: + sort = "ORDER BY title ASC"; + break; + + case S_ALBUM: + sort = "ORDER BY track ASC, album ASC"; + break; + } + if (idx && qp->filter) - query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 AND %s %s;", qp->filter, idx); + query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 AND %s %s %s;", qp->filter, sort, idx); else if (idx) - query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 %s;", idx); + query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 %s %s;", sort, idx); else if (qp->filter) - query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 AND %s;", qp->filter); + query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 AND %s %s;", qp->filter, sort); else - query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0;"); + query = sqlite3_mprintf("SELECT * FROM files WHERE disabled = 0 %s;", sort); if (!query) { diff --git a/src/db.h b/src/db.h index d1be19b3..119fec6a 100644 --- a/src/db.h +++ b/src/db.h @@ -16,6 +16,12 @@ enum index_type { I_SUB }; +enum sort_type { + S_NONE = 0, + S_NAME, + S_ALBUM, +}; + #define Q_F_BROWSE (1 << 15) enum query_type { @@ -35,6 +41,7 @@ struct query_params { /* Query parameters, filled in by caller */ enum query_type type; enum index_type idx_type; + enum sort_type sort; int id; int offset; int limit; diff --git a/src/httpd_daap.c b/src/httpd_daap.c index 7df409f4..3690f78b 100644 --- a/src/httpd_daap.c +++ b/src/httpd_daap.c @@ -891,6 +891,8 @@ get_query_params(struct evkeyvalq *query, struct query_params *qp) qp->idx_type = I_SUB; + qp->sort = S_NONE; + param = evhttp_find_header(query, "query"); if (!param) param = evhttp_find_header(query, "filter"); diff --git a/src/httpd_rsp.c b/src/httpd_rsp.c index a9a1cbcf..f50ae3fe 100644 --- a/src/httpd_rsp.c +++ b/src/httpd_rsp.c @@ -202,6 +202,8 @@ get_query_params(struct evhttp_request *req, struct evkeyvalq *query, struct que else qp->idx_type = I_NONE; + qp->sort = S_NONE; + param = evhttp_find_header(query, "query"); if (param) {