integrate smart playlist parsing into the database

This commit is contained in:
Ron Pedde 2005-10-23 07:33:24 +00:00
parent 1d0ecad346
commit 8d3d31d119
3 changed files with 846 additions and 805 deletions

View File

@ -36,7 +36,7 @@ mt_daapd_SOURCES = main.c daapd.h rend.h uici.c uici.h webserver.c \
mp3-scanner.h mp3-scanner.c rend-unix.h strcasestr.c strcasestr.h \
strsep.c dynamic-art.c dynamic-art.h query.c query.h ssc.c ssc.h \
db-generic.c db-generic.h dispatch.c dispatch.h \
rxml.c rxml.h redblack.c redblack.h scan-mp3.c \
rxml.c rxml.h redblack.c redblack.h scan-mp3.c smart-parser.c \
scan-xml.c scan-wma.c scan-aac.c scan-aac.h scan-wav.c scan-url.c \
$(PRENDSRC) $(ORENDSRC) $(HRENDSRC) $(OGGVORBISSRC) $(FLACSRC) \
$(SQLITEDB)

View File

@ -40,6 +40,7 @@
#include "dbs-sqlite.h"
#include "restart.h"
#include "ssc.h"
#include "smart-parser.h"
/* Globals */
static sqlite *db_sqlite_songs; /**< Database that holds the mp3 info */
@ -63,11 +64,40 @@ int db_sqlite_update(MP3FILE *pmp3);
int db_sqlite_update_version(int from_version);
int db_sqlite_get_version(void);
int db_sqlite_update_playlists(void);
char *db_sqlite_parse_smart(char *phrase);
#define STR(a) (a) ? (a) : ""
#define ISSTR(a) ((a) && strlen((a)))
#define MAYBEFREE(a) { if((a)) free((a)); };
/**
* get the sql where clause for a smart playlist spec. This
* where clause must be freed by the caller
*
* @param phrase playlist spec to be converted
* @returns sql where clause if successful, NULL otherwise
*/
char *db_sqlite_parse_smart(char *phrase) {
PARSETREE pt;
char *result = NULL;
if(strcmp(phrase,"1") == 0)
return strdup("1");
pt=sp_init();
if(!pt)
return NULL;
if(!sp_parse(pt,phrase)) {
DPRINTF(E_LOG,L_DB,"Error parsing smart playlist: %s",sp_get_error(pt));
} else {
result = sp_sql_clause(pt);
}
sp_dispose(pt);
return result;
}
/**
* lock the db_mutex
*/
@ -694,15 +724,20 @@ int db_sqlite_update(MP3FILE *pmp3) {
int db_sqlite_update_playlists(void) {
char **resarray;
int rows, cols, index;
char *where_clause;
db_sqlite_get_table(E_FATAL,&resarray, &rows, &cols, "SELECT * FROM playlists");
for(index=1;index <= rows; index ++) {
DPRINTF(E_DBG,L_DB,"Updating playlist counts for %s\n",resarray[cols * index + 1]);
if(atoi(resarray[cols * index + 2]) == 1) { // is a smart playlist
where_clause=db_sqlite_parse_smart(resarray[cols * index + 4]);
if(where_clause) {
db_sqlite_exec(E_FATAL,"UPDATE playlists SET items=(SELECT COUNT(*) "
"FROM songs WHERE %s) WHERE id=%s",resarray[cols * index + 4],
"FROM songs WHERE %s) WHERE id=%s",where_clause,
resarray[cols * index]);
free(where_clause);
}
} else {
db_sqlite_exec(E_FATAL,"UPDATE playlists SET items=(SELECT COUNT(*) "
"FROM playlistitems WHERE playlistid=%s) WHERE id=%s",
@ -727,6 +762,7 @@ int db_sqlite_enum_start(DBQUERYINFO *pinfo) {
char query_select[255];
char query_count[255];
char query_rest[4096];
char *where_clause;
int is_smart;
int have_clause=0;
@ -769,9 +805,14 @@ int db_sqlite_enum_start(DBQUERYINFO *pinfo) {
is_smart=(atoi(resarray[2]) == 1);
have_clause=1;
if(is_smart) {
where_clause=db_sqlite_parse_smart(resarray[3]);
if(!where_clause) {
return -1;
}
sprintf(query_select,"SELECT * FROM songs ");
sprintf(query_count,"SELECT COUNT(id) FROM songs ");
sprintf(query_rest,"WHERE (%s)",resarray[3]);
sprintf(query_rest,"WHERE (%s)",where_clause);
free(where_clause);
} else {
sprintf(query_select,"SELECT * FROM songs,playlistitems ");
sprintf(query_count,"SELECT COUNT(id) FROM songs ");

View File

@ -609,7 +609,7 @@ SP_NODE *sp_parse_oexpr(PARSETREE tree) {
expr=pnew;
}
sp_enter_exit(tree,"sp_parse_oexptr",0,expr);
sp_enter_exit(tree,"sp_parse_oexpr",0,expr);
return expr;
}