return id from db_add

This commit is contained in:
Ron Pedde 2006-01-10 23:57:43 +00:00
parent 2e00839a36
commit 01180d60f1
10 changed files with 60 additions and 16 deletions

View File

@ -46,7 +46,7 @@ typedef struct tag_db_functions {
int(*dbs_open)(char **, char *);
int(*dbs_init)(int);
int(*dbs_deinit)(void);
int(*dbs_add)(char **, MP3FILE*);
int(*dbs_add)(char **, MP3FILE*, int*);
int(*dbs_add_playlist)(char **, char *, int, char *,char *, int, int *);
int(*dbs_add_playlist_item)(char **, int, int);
int(*dbs_delete_playlist)(char **, int);
@ -508,13 +508,13 @@ int db_scanning(void) {
/**
* add (or update) a file
*/
int db_add(char **pe, MP3FILE *pmp3) {
int db_add(char **pe, MP3FILE *pmp3, int *id) {
int retval;
db_writelock();
db_utf8_validate(pmp3);
db_trim_strings(pmp3);
retval=db_current->dbs_add(pe,pmp3);
retval=db_current->dbs_add(pe,pmp3,id);
db_revision_no++;
db_unlock();

View File

@ -141,7 +141,7 @@ extern int db_deinit(void);
extern int db_revision(void);
extern int db_add(char **pe, MP3FILE *pmp3);
extern int db_add(char **pe, MP3FILE *pmp3, int *id);
extern int db_enum_start(char **pe, DBQUERYINFO *pinfo);
extern int db_enum_size(char **pe, DBQUERYINFO *pinfo, int *count, int *total_size);

View File

@ -385,6 +385,17 @@ int db_sqlite2_event(int event_type) {
return DB_E_SUCCESS;
}
/**
* get the id of the last auto_update inserted item
*
* @returns autoupdate value
*/
int db_sqlite2_insert_id(void) {
return sqlite_last_insert_rowid(db_sqlite2_songs);
}
char *db_sqlite2_initial =
"create table songs (\n"
" id INTEGER PRIMARY KEY NOT NULL,\n"

View File

@ -38,5 +38,7 @@ extern int db_sqlite2_enum_restart(char **pe);
extern int db_sqlite2_event(int event_type);
extern int db_sqlite2_insert_id(void);
#endif /* _DB_SQL_SQLITE2_ */

View File

@ -406,6 +406,18 @@ int db_sqlite3_event(int event_type) {
return DB_E_SUCCESS;
}
/**
* get the id of the last auto_update inserted item
*
* @returns autoupdate value
*/
int db_sqlite3_insert_id(void) {
return (int)sqlite3_last_insert_rowid(db_sqlite3_songs);
}
char *db_sqlite3_initial =
"create table songs (\n"
" id INTEGER PRIMARY KEY NOT NULL,\n"

View File

@ -37,6 +37,7 @@ extern int db_sqlite3_enum_end(char **pe);
extern int db_sqlite3_enum_restart(char **pe);
extern int db_sqlite3_event(int event_type);
extern int db_sqlite3_insert_id(void);
#endif /* _DB_SQL_SQLITE3_ */

View File

@ -60,7 +60,7 @@ static int db_sql_need_dispose=0;
int db_sql_get_size(DBQUERYINFO *pinfo, char **valarray);
int db_sql_build_dmap(DBQUERYINFO *pinfo, char **valarray, unsigned char *presult, int len);
void db_sql_build_mp3file(char **valarray, MP3FILE *pmp3);
int db_sql_update(char **pe, MP3FILE *pmp3);
int db_sql_update(char **pe, MP3FILE *pmp3, int *id);
int db_sql_update_playlists(char **pe);
char *db_sql_parse_smart(char *phrase);
@ -83,7 +83,7 @@ int (*db_sql_enum_fetch_fn)(char **pe, SQL_ROW *pr) = NULL;
int (*db_sql_enum_end_fn)(char **pe) = NULL;
int (*db_sql_enum_restart_fn)(char **pe);
int (*db_sql_event_fn)(int event_type);
int (*db_sql_insert_id_fn)(void);
#ifdef HAVE_LIBSQLITE
int db_sql_open_sqlite2(char **pe, char *parameters) {
@ -98,6 +98,7 @@ int db_sql_open_sqlite2(char **pe, char *parameters) {
db_sql_enum_end_fn = db_sqlite2_enum_end;
db_sql_enum_restart_fn = db_sqlite2_enum_restart;
db_sql_event_fn = db_sqlite2_event;
db_sql_insert_id_fn = db_sqlite2_insert_id;
return db_sql_open(pe,parameters);
}
@ -116,6 +117,7 @@ int db_sql_open_sqlite3(char **pe, char *parameters) {
db_sql_enum_end_fn = db_sqlite3_enum_end;
db_sql_enum_restart_fn = db_sqlite3_enum_restart;
db_sql_event_fn = db_sqlite3_event;
db_sql_insert_id_fn = db_sqlite3_insert_id;
return db_sql_open(pe,parameters);
}
@ -591,9 +593,10 @@ int db_sql_add_playlist_item(char **pe, int playlistid, int songid) {
*
* @param pmp3 mp3 file to add
*/
int db_sql_add(char **pe, MP3FILE *pmp3) {
int db_sql_add(char **pe, MP3FILE *pmp3, int *id) {
int err;
int count;
int insertid;
DPRINTF(E_SPAM,L_DB,"Entering db_sql_add\n");
@ -612,7 +615,7 @@ int db_sql_add(char **pe, MP3FILE *pmp3) {
"path='%q'",pmp3->path);
if((err == DB_E_SUCCESS) && (count == 1)) { /* we should update */
return db_sql_update(pe,pmp3);
return db_sql_update(pe,pmp3,id);
}
}
@ -699,14 +702,18 @@ int db_sql_add(char **pe, MP3FILE *pmp3) {
if(err != DB_E_SUCCESS)
DPRINTF(E_FATAL,L_DB,"Error inserting file %s in database\n",pmp3->fname);
insertid = db_sql_insert_id_fn();
if((db_sql_in_scan)&&(!db_sql_reload)) {
/* FIXME: this is sqlite-specific */
db_sql_exec_fn(NULL,E_FATAL,"insert into updated values (last_insert_rowid())");
db_sql_exec_fn(NULL,E_FATAL,"insert into updated values (%d)",
insertid);
}
if((!db_sql_in_scan) && (!db_sql_in_playlist_scan))
db_sql_update_playlists(NULL);
if(id)
*id = insertid;
DPRINTF(E_SPAM,L_DB,"Exiting db_sql_add\n");
return DB_E_SUCCESS;
}
@ -716,7 +723,7 @@ int db_sql_add(char **pe, MP3FILE *pmp3) {
*
* @param pmp3 mp3 file to update
*/
int db_sql_update(char **pe, MP3FILE *pmp3) {
int db_sql_update(char **pe, MP3FILE *pmp3, int *id) {
int err;
if(!pmp3->time_modified)
@ -788,10 +795,21 @@ int db_sql_update(char **pe, MP3FILE *pmp3) {
if(err != DB_E_SUCCESS)
DPRINTF(E_FATAL,L_DB,"Error updating file: %s\n",pmp3->fname);
if(id) { /* we need the insert/update id */
err=db_sql_fetch_int(pe,id,"select id from songs where path='%q'",pmp3->path);
if(err != DB_E_SUCCESS)
return err;
}
if((db_sql_in_scan) && (!db_sql_reload)) {
db_sql_exec_fn(NULL,E_FATAL,"INSERT INTO updated (id) select id from songs where path='%q'",
if(id) {
db_sql_exec_fn(NULL,E_FATAL,"insert into updated (id) values (%d)",*id);
} else {
db_sql_exec_fn(NULL,E_FATAL,"insert into updated (id) "
"select id from songs where path='%q'",
pmp3->path);
}
}
if((!db_sql_in_scan) && (!db_sql_in_playlist_scan))
db_sql_update_playlists(NULL);

View File

@ -34,7 +34,7 @@ extern int db_sql_open_sqlite3(char **pe, char *parameters);
extern int db_sql_open(char **pe, char *parameters);
extern int db_sql_init(int reload);
extern int db_sql_deinit(void);
extern int db_sql_add(char **pe, MP3FILE *pmp3);
extern int db_sql_add(char **pe, MP3FILE *pmp3, int *id);
extern int db_sql_enum_start(char **pe, DBQUERYINFO *pinfo);
extern int db_sql_enum_size(char **pe, DBQUERYINFO *pinfo, int *count, int *total_size);
extern int db_sql_enum_fetch(char **pe, DBQUERYINFO *pinfo, int *size, unsigned char **pdmap);

View File

@ -548,7 +548,7 @@ void scan_music_file(char *path, struct dirent *pde,
mp3file.compilation = 1;
/* FIXME: error handling */
db_add(NULL,&mp3file);
db_add(NULL,&mp3file,NULL);
} else {
DPRINTF(E_WARN,L_SCAN,"Skipping %s - scan failed\n",mp3file.path);
}

View File

@ -661,7 +661,7 @@ int scan_xml_tracks_section(int action, char *info) {
/* must add to the red-black tree */
scan_xml_add_lookup(current_track_id,pmp3->id);
db_add(NULL,pmp3);
db_add(NULL,pmp3,NULL);
db_dispose_item(pmp3);
memset((void*)&mp3,0,sizeof(MP3FILE));