[Mt-daapd-cvs] mt-daapd/src db-generic.c,1.3,1.4 dbs-sqlite.c,1.5,1.6 dbs-sqlite.h,1.2,1.3 err.c,1.2
Status: Beta
Brought to you by:
andrew40
From: Ron P. <rp...@us...> - 2005-03-20 00:42:57
|
Update of /cvsroot/mt-daapd/mt-daapd/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24849 Modified Files: db-generic.c dbs-sqlite.c dbs-sqlite.h err.c Log Message: auto-upgrade of databases, including auto-creating database Index: dbs-sqlite.h =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/dbs-sqlite.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dbs-sqlite.h 19 Mar 2005 10:02:19 -0000 1.2 --- dbs-sqlite.h 20 Mar 2005 00:42:47 -0000 1.3 *************** *** 27,31 **** extern int db_sqlite_deinit(void); extern int db_sqlite_add(MP3FILE *pmp3); - extern int db_sqlite_update(MP3FILE *pmp3); extern int db_sqlite_enum_start(DBQUERYINFO *pinfo); extern int db_sqlite_enum_size(DBQUERYINFO *pinfo, int *count); --- 27,30 ---- Index: db-generic.c =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/db-generic.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** db-generic.c 19 Mar 2005 10:02:18 -0000 1.3 --- db-generic.c 20 Mar 2005 00:42:47 -0000 1.4 *************** *** 47,51 **** int(*dbs_deinit)(void); int(*dbs_add)(MP3FILE*); - int(*dbs_update)(MP3FILE*); int(*dbs_enum_start)(DBQUERYINFO *); int(*dbs_enum_size)(DBQUERYINFO *, int *); --- 47,50 ---- *************** *** 70,74 **** db_sqlite_deinit, db_sqlite_add, - db_sqlite_update, db_sqlite_enum_start, db_sqlite_enum_size, --- 69,72 ---- Index: dbs-sqlite.c =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/dbs-sqlite.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dbs-sqlite.c 19 Mar 2005 21:56:08 -0000 1.5 --- dbs-sqlite.c 20 Mar 2005 00:42:47 -0000 1.6 *************** *** 53,56 **** --- 53,60 ---- int db_sqlite_get_table(int fatal, char ***resarray, int *rows, int *cols, char *fmt, ...); int db_sqlite_free_table(char **resarray); + 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); #define STR(a) (a) ? (a) : "" *************** *** 105,109 **** loglevel=E_LOG; ! DPRINTF(loglevel,L_DB,"Query: %s Error: %s\n",query,perr); db_sqlite_lock(); sqlite_freemem(query); --- 109,114 ---- loglevel=E_LOG; ! DPRINTF(E_LOG,L_DB,"Query: %s\n",query); ! DPRINTF(loglevel,L_DB,"Error: %s\n",perr); db_sqlite_lock(); sqlite_freemem(query); *************** *** 186,189 **** --- 191,196 ---- int items; + db_sqlite_update_version(db_sqlite_get_version()); + items=db_sqlite_get_count(countSongs); *************** *** 242,245 **** --- 249,254 ---- } + db_sqlite_update_playlists(); + db_sqlite_reload=0; db_sqlite_in_scan=0; *************** *** 353,356 **** --- 362,368 ---- } + if(!db_sqlite_in_scan) + db_sqlite_update_playlists(); + DPRINTF(E_SPAM,L_DB,"Exiting db_sqlite_add\n"); return 0; *************** *** 431,434 **** --- 443,449 ---- } + if(!db_sqlite_in_scan) + db_sqlite_update_playlists(); + return 0; } *************** *** 447,451 **** DPRINTF(E_DBG,L_DB,"Updating playlist counts for %s\n",resarray[cols * index + 1]); if(atoi(resarray[cols * index + 2])) { // is a smart playlist ! db_sqlite_exec(1,"UPDATE playlsits SET items=(SELECT COUNT(*) " "FROM songs WHERE %s) WHERE id=%s",resarray[cols * index + 4], resarray[cols * index]); --- 462,466 ---- DPRINTF(E_DBG,L_DB,"Updating playlist counts for %s\n",resarray[cols * index + 1]); if(atoi(resarray[cols * index + 2])) { // is a smart playlist ! db_sqlite_exec(1,"UPDATE playlists SET items=(SELECT COUNT(*) " "FROM songs WHERE %s) WHERE id=%s",resarray[cols * index + 4], resarray[cols * index]); *************** *** 482,486 **** int rows, cols; int browse=0; ! int results; const char *ptail; --- 497,501 ---- int rows, cols; int browse=0; ! int results=0; const char *ptail; *************** *** 569,602 **** } - /* find out how many hits */ - strcpy(scratch,query_count); - strcat(scratch,query_rest); - if(browse) - strcat(scratch,")"); - - DPRINTF(E_DBG,L_DB,"result count query: %s\n",scratch); ! db_sqlite_lock(); ! err=sqlite_get_table(db_sqlite_songs,scratch,&resarray,&rows,&cols,&perr); ! if(err != SQLITE_OK) { ! db_sqlite_unlock(); ! DPRINTF(E_LOG,L_DB,"Error in results query: %s\n",perr); ! return -1; ! } ! results=atoi(resarray[1]); ! sqlite_free_table(resarray); ! db_sqlite_unlock(); - /* update the playlist counts */ - if(pinfo->query_type == queryTypePlaylistItems) { - sprintf(scratch,"UPDATE playlists SET items=%d WHERE id=%d", - results,pinfo->playlist_id); db_sqlite_lock(); ! sqlite_exec(db_sqlite_songs,scratch,NULL,NULL,&perr); db_sqlite_unlock(); } - - DPRINTF(E_DBG,L_DB,"Number of results: %d\n",results); strcpy(query,query_select); --- 584,613 ---- } ! if(pinfo->index_type == indexTypeLast) { ! /* We don't really care how many items unless we are ! * doing a "last n items" query */ ! strcpy(scratch,query_count); ! strcat(scratch,query_rest); ! if(browse) ! strcat(scratch,")"); ! DPRINTF(E_DBG,L_DB,"result count query: %s\n",scratch); db_sqlite_lock(); ! err=sqlite_get_table(db_sqlite_songs,scratch,&resarray,&rows,&cols,&perr); ! if(err != SQLITE_OK) { ! db_sqlite_unlock(); ! DPRINTF(E_LOG,L_DB,"Error in results query: %s\n",perr); ! return -1; ! } ! ! ! results=atoi(resarray[1]); ! sqlite_free_table(resarray); db_sqlite_unlock(); + + DPRINTF(E_DBG,L_DB,"Number of results: %d\n",results); } strcpy(query,query_select); *************** *** 612,616 **** sprintf(scratch," LIMIT %d",pinfo->index_low); /* unnecessary */ } else { ! sprintf(scratch," LIMIT %d OFFSET %d",pinfo->index_low, results=pinfo->index_low); } break; --- 623,627 ---- sprintf(scratch," LIMIT %d",pinfo->index_low); /* unnecessary */ } else { ! sprintf(scratch," LIMIT %d OFFSET %d",pinfo->index_low, results-pinfo->index_low); } break; *************** *** 1089,1093 **** * \param type either countPlaylists or countSongs (type to count) */ ! extern int db_sqlite_get_count(CountType_t type) { char *table; int rows, cols; --- 1100,1104 ---- * \param type either countPlaylists or countSongs (type to count) */ ! int db_sqlite_get_count(CountType_t type) { char *table; int rows, cols; *************** *** 1115,1116 **** --- 1126,1228 ---- return retval; } + + /** + * get the database version of the currently opened database + */ + int db_sqlite_get_version(void) { + int rows, cols; + char **resarray; + int retval=0; + + db_sqlite_get_table(0,&resarray, &rows, &cols, + "select value from config where term='version'"); + + if(rows != 0) { + retval=atoi(resarray[cols]); + } + + db_sqlite_free_table(resarray); + return retval; + } + + + char *db_sqlite_upgrade_scripts[] = { + "CREATE TABLE songs (\n" + " id INTEGER PRIMARY KEY NOT NULL,\n" + " path VARCHAR(4096) UNIQUE NOT NULL,\n" + " fname VARCHAR(255) NOT NULL,\n" + " title VARCHAR(1024) DEFAULT NULL,\n" + " artist VARCHAR(1024) DEFAULT NULL,\n" + " album VARCHAR(1024) DEFAULT NULL,\n" + " genre VARCHAR(255) DEFAULT NULL,\n" + " comment VARCHAR(4096) DEFAULT NULL,\n" + " type VARCHAR(255) DEFAULT NULL,\n" + " composer VARCHAR(1024) DEFAULT NULL,\n" + " orchestra VARCHAR(1024) DEFAULT NULL,\n" + " conductor VARCHAR(1024) DEFAULT NULL,\n" + " grouping VARCHAR(1024) DEFAULT NULL,\n" + " url VARCHAR(1024) DEFAULT NULL,\n" + " bitrate INTEGER DEFAULT 0,\n" + " samplerate INTEGER DEFAULT 0,\n" + " song_length INTEGER DEFAULT 0,\n" + " file_size INTEGER DEFAULT 0,\n" + " year INTEGER DEFAULT 0,\n" + " track INTEGER DEFAULT 0,\n" + " total_tracks INTEGER DEFAULT 0,\n" + " disc INTEGER DEFAULT 0,\n" + " total_discs INTEGER DEFAULT 0,\n" + " bpm INTEGER DEFAULT 0,\n" + " compilation INTEGER DEFAULT 0,\n" + " rating INTEGER DEFAULT 0,\n" + " play_count INTEGER DEFAULT 0,\n" + " data_kind INTEGER DEFAULT 0,\n" + " item_kind INTEGER DEFAULT 0,\n" + " description INTEGER DEFAULT 0,\n" + " time_added INTEGER DEFAULT 0,\n" + " time_modified INTEGER DEFAULT 0,\n" + " time_played INTEGER DEFAULT 0,\n" + " db_timestamp INTEGER DEFAULT 0,\n" + " disabled INTEGER DEFAULT 0,\n" + " sample_count INTEGER DEFAULT 0,\n" + " force_update INTEGER DEFAULT 0\n" + ");\n" + "CREATE TABLE config (\n" + " term VARCHAR(255) NOT NULL,\n" + " subterm VARCHAR(255) DEFAULT NULL,\n" + " value VARCHAR(1024) NOT NULL\n" + ");\n" + "CREATE TABLE playlists (\n" + " id INTEGER PRIMARY KEY NOT NULL,\n" + " title VARCHAR(255) NOT NULL,\n" + " smart INTEGER NOT NULL,\n" + " items INTEGER NOT NULL,\n" + " query VARCHAR(1024)\n" + ");\n" + "CREATE TABLE playlistitems (\n" + " id INTEGER NOT NULL,\n" + " songid INTEGER NOT NULL\n" + ");\n" + "INSERT INTO config VALUES ('version','','1');\n" + "INSERT INTO playlists VALUES (1,'Library',1,0,'1');\n", /* Version 0 -> Version 1 */ + NULL /* No more versions! */ + }; + + /** + * Upgrade database from an older version of the database to the newest + * + * \param from_version the current version of the database + */ + int db_sqlite_update_version(int from_version) { + + while(db_sqlite_upgrade_scripts[from_version]) { + DPRINTF(E_LOG,L_DB,"Upgrading database from version %d to version %d\n",from_version, + from_version+1); + db_sqlite_exec(1,db_sqlite_upgrade_scripts[from_version]); + from_version++; + } + + return 0; + } + + + Index: err.c =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/err.c,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** err.c 17 Feb 2005 04:24:16 -0000 1.26 --- err.c 20 Mar 2005 00:42:47 -0000 1.27 *************** *** 101,105 **** va_list ap; char timebuf[256]; ! char errbuf[1024]; struct tm tm_now; time_t tt_now; --- 101,105 ---- va_list ap; char timebuf[256]; ! char errbuf[4096]; struct tm tm_now; time_t tt_now; |