crazy fixes for sqlite3

This commit is contained in:
Ron Pedde 2006-03-06 07:48:53 +00:00
parent 11650fbf76
commit f037a53452
3 changed files with 71 additions and 29 deletions

View File

@ -71,10 +71,10 @@ static char db_sqlite3_path[PATH_MAX + 1];
/* Forwards */ /* Forwards */
void db_sqlite3_lock(void); void _db_sqlite3_lock(void);
void db_sqlite3_unlock(void); void _db_sqlite3_unlock(void);
extern char *db_sqlite3_initial1; extern char *_db_sqlite3_initial1;
extern char *db_sqlite3_initial2; extern char *_db_sqlite3_initial2;
int db_sqlite3_enum_begin_helper(char **pe); int db_sqlite3_enum_begin_helper(char **pe);
@ -82,7 +82,7 @@ int db_sqlite3_enum_begin_helper(char **pe);
/** /**
* lock the db_mutex * lock the db_mutex
*/ */
void db_sqlite3_lock(void) { void _db_sqlite3_lock(void) {
int err; int err;
if((err=pthread_mutex_lock(&db_sqlite3_mutex))) { if((err=pthread_mutex_lock(&db_sqlite3_mutex))) {
@ -93,7 +93,7 @@ void db_sqlite3_lock(void) {
/** /**
* unlock the db_mutex * unlock the db_mutex
*/ */
void db_sqlite3_unlock(void) { void _db_sqlite3_unlock(void) {
int err; int err;
if((err=pthread_mutex_unlock(&db_sqlite3_mutex))) { if((err=pthread_mutex_unlock(&db_sqlite3_mutex))) {
@ -116,6 +116,19 @@ void db_sqlite3_vmfree(char *query) {
} }
int _db_sqlite3_reopen(char **pe) {
if(sqlite3_open(db_sqlite3_path,&db_sqlite3_songs) != SQLITE_OK) {
db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs));
DPRINTF(E_LOG,L_DB,"db_sqlite3_open: %s (%s)\n",*pe,db_sqlite3_path);
return DB_E_SQL_ERROR;
}
return DB_E_SUCCESS;
}
void _db_sqlite3_reclose() {
sqlite3_close(db_sqlite3_songs);
}
/** /**
* open a sqlite3 database * open a sqlite3 database
* *
@ -130,22 +143,26 @@ int db_sqlite3_open(char **pe, char *dsn) {
snprintf(db_sqlite3_path,sizeof(db_sqlite3_path),"%s/songs3.db",dsn); snprintf(db_sqlite3_path,sizeof(db_sqlite3_path),"%s/songs3.db",dsn);
db_sqlite3_lock(); _db_sqlite3_lock();
if(sqlite3_open(db_sqlite3_path,&db_sqlite3_songs) != SQLITE_OK) { if(sqlite3_open(db_sqlite3_path,&db_sqlite3_songs) != SQLITE_OK) {
db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs)); db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs));
DPRINTF(E_LOG,L_DB,"db_sqlite3_open: %s (%s)\n",*pe, DPRINTF(E_LOG,L_DB,"db_sqlite3_open: %s (%s)\n",*pe,
db_sqlite3_path); db_sqlite3_path);
db_sqlite3_unlock(); _db_sqlite3_unlock();
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
} }
sqlite3_busy_timeout(db_sqlite3_songs,30000); /* 30 seconds */ sqlite3_busy_timeout(db_sqlite3_songs,30000); /* 30 seconds */
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
err = db_sql_fetch_int(pe,&ver,"select value from config where " err = db_sql_fetch_int(pe,&ver,"select value from config where "
"term='version'"); "term='version'");
if(err != DB_E_SUCCESS) { if(err != DB_E_SUCCESS) {
if(pe) { free(*pe); } if(pe) {
DPRINTF(E_DBG,L_DB,"error getting version: %d, %s\n",err, pe);
free(*pe);
}
/* we'll catch this on the init */ /* we'll catch this on the init */
DPRINTF(E_LOG,L_DB,"Can't get db version. New database?\n"); DPRINTF(E_LOG,L_DB,"Can't get db version. New database?\n");
} else if(ver != DB_SQLITE3_VERSION) { } else if(ver != DB_SQLITE3_VERSION) {
@ -163,9 +180,9 @@ int db_sqlite3_open(char **pe, char *dsn) {
* close the database * close the database
*/ */
int db_sqlite3_close(void) { int db_sqlite3_close(void) {
db_sqlite3_lock(); _db_sqlite3_lock();
sqlite3_close(db_sqlite3_songs); // sqlite3_close(db_sqlite3_songs);
db_sqlite3_unlock(); _db_sqlite3_unlock();
return DB_E_SUCCESS; return DB_E_SUCCESS;
} }
@ -185,7 +202,12 @@ int db_sqlite3_exec(char **pe, int loglevel, char *fmt, ...) {
int err; int err;
char *perr; char *perr;
db_sqlite3_lock(); _db_sqlite3_lock();
if((err=_db_sqlite3_reopen(pe)) != DB_E_SUCCESS) {
_db_sqlite3_unlock();
return err;
}
va_start(ap,fmt); va_start(ap,fmt);
query=sqlite3_vmprintf(fmt,ap); query=sqlite3_vmprintf(fmt,ap);
@ -206,7 +228,8 @@ int db_sqlite3_exec(char **pe, int loglevel, char *fmt, ...) {
} }
sqlite3_free(query); sqlite3_free(query);
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
if(err != SQLITE_OK) if(err != SQLITE_OK)
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
@ -218,8 +241,15 @@ int db_sqlite3_exec(char **pe, int loglevel, char *fmt, ...) {
*/ */
int db_sqlite3_enum_begin(char **pe, char *fmt, ...) { int db_sqlite3_enum_begin(char **pe, char *fmt, ...) {
va_list ap; va_list ap;
int err;
_db_sqlite3_lock();
if((err=_db_sqlite3_reopen(pe)) != DB_E_SUCCESS) {
_db_sqlite3_unlock();
return err;
}
db_sqlite3_lock();
va_start(ap, fmt); va_start(ap, fmt);
db_sqlite3_enum_query = sqlite3_vmprintf(fmt,ap); db_sqlite3_enum_query = sqlite3_vmprintf(fmt,ap);
va_end(ap); va_end(ap);
@ -234,14 +264,14 @@ int db_sqlite3_enum_begin_helper(char **pe) {
if(!db_sqlite3_enum_query) if(!db_sqlite3_enum_query)
*((int*)NULL) = 1; *((int*)NULL) = 1;
DPRINTF(E_DBG,L_DB,"Executing: %s\n",db_sqlite3_enum_query); DPRINTF(E_DBG,L_DB,"Executing: %s\n",db_sqlite3_enum_query);
err=sqlite3_prepare(db_sqlite3_songs,db_sqlite3_enum_query,0, err=sqlite3_prepare(db_sqlite3_songs,db_sqlite3_enum_query,0,
&db_sqlite3_stmt,&ptail); &db_sqlite3_stmt,&ptail);
if(err != SQLITE_OK) { if(err != SQLITE_OK) {
db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs)); db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs));
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
sqlite3_free(db_sqlite3_enum_query); sqlite3_free(db_sqlite3_enum_query);
db_sqlite3_enum_query=NULL; db_sqlite3_enum_query=NULL;
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
@ -315,7 +345,7 @@ int db_sqlite3_enum_fetch(char **pe, SQL_ROW *pr) {
db_sqlite3_row = NULL; db_sqlite3_row = NULL;
db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs)); db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs));
sqlite3_finalize(db_sqlite3_stmt); DPRINTF(E_SPAM,L_DB,"sql error: %d\n",err);
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
} }
@ -338,11 +368,13 @@ int db_sqlite3_enum_end(char **pe) {
err = sqlite3_finalize(db_sqlite3_stmt); err = sqlite3_finalize(db_sqlite3_stmt);
if(err != SQLITE_OK) { if(err != SQLITE_OK) {
db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs)); db_get_error(pe,DB_E_SQL_ERROR,sqlite3_errmsg(db_sqlite3_songs));
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
} }
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
return DB_E_SUCCESS; return DB_E_SUCCESS;
} }
@ -375,8 +407,8 @@ int db_sqlite3_event(int event_type) {
db_sqlite3_exec(NULL,E_DBG,"vacuum"); db_sqlite3_exec(NULL,E_DBG,"vacuum");
db_sqlite3_exec(NULL,E_DBG,db_sqlite3_initial1); db_sqlite3_exec(NULL,E_DBG,_db_sqlite3_initial1);
db_sqlite3_exec(NULL,E_DBG,db_sqlite3_initial2); db_sqlite3_exec(NULL,E_DBG,_db_sqlite3_initial2);
db_sqlite3_reload=1; db_sqlite3_reload=1;
break; break;
@ -445,16 +477,18 @@ int db_sqlite3_event(int event_type) {
int db_sqlite3_insert_id(void) { int db_sqlite3_insert_id(void) {
int result; int result;
db_sqlite3_lock(); _db_sqlite3_lock();
_db_sqlite3_reopen(NULL);
result = (int)sqlite3_last_insert_rowid(db_sqlite3_songs); result = (int)sqlite3_last_insert_rowid(db_sqlite3_songs);
db_sqlite3_unlock(); _db_sqlite3_reclose();
_db_sqlite3_unlock();
return result; return result;
} }
char *db_sqlite3_initial1 = char *_db_sqlite3_initial1 =
"create table songs (\n" "create table songs (\n"
" id INTEGER PRIMARY KEY NOT NULL,\n" " id INTEGER PRIMARY KEY NOT NULL,\n"
" path VARCHAR(4096) UNIQUE NOT NULL,\n" " path VARCHAR(4096) UNIQUE NOT NULL,\n"
@ -510,7 +544,7 @@ char *db_sqlite3_initial1 =
");\n" ");\n"
"insert into config values ('version','','9');\n"; "insert into config values ('version','','9');\n";
char *db_sqlite3_initial2 = char *_db_sqlite3_initial2 =
"create table playlists (\n" "create table playlists (\n"
" id INTEGER PRIMARY KEY NOT NULL,\n" " id INTEGER PRIMARY KEY NOT NULL,\n"
" title VARCHAR(255) NOT NULL,\n" " title VARCHAR(255) NOT NULL,\n"

View File

@ -144,11 +144,13 @@ int db_sql_fetch_row(char **pe, SQL_ROW *row, char *fmt, ...) {
db_sql_vmfree_fn(query); db_sql_vmfree_fn(query);
if(err != DB_E_SUCCESS) { if(err != DB_E_SUCCESS) {
DPRINTF(E_SPAM,L_DB,"Error: enum_begin failed: %s\n",*pe);
return err; return err;
} }
err=db_sql_enum_fetch_fn(pe, row); err=db_sql_enum_fetch_fn(pe, row);
if(err != DB_E_SUCCESS) { if(err != DB_E_SUCCESS) {
DPRINTF(E_SPAM,L_DB,"Error: enum_fetch failed: %s\n",*pe);
db_sql_enum_end_fn(NULL); db_sql_enum_end_fn(NULL);
return err; return err;
} }
@ -176,8 +178,10 @@ int db_sql_fetch_int(char **pe, int *result, char *fmt, ...) {
err = db_sql_fetch_row(pe, &row, "%s", query); err = db_sql_fetch_row(pe, &row, "%s", query);
db_sql_vmfree_fn(query); db_sql_vmfree_fn(query);
if(err != DB_E_SUCCESS) if(err != DB_E_SUCCESS) {
DPRINTF(E_SPAM,L_DB,"fetch_row failed in fetch_int: %s\n",*pe);
return err; return err;
}
*result = atoi(row[0]); *result = atoi(row[0]);
db_sql_dispose_row(); db_sql_dispose_row();

View File

@ -393,7 +393,11 @@ int main(int argc, char *argv[]) {
end_time=(int) time(NULL); end_time=(int) time(NULL);
db_get_song_count(NULL,&song_count); db_get_song_count(&perr,&song_count);
if(perr) {
DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr);
}
DPRINTF(E_LOG,L_MAIN,"Scanned %d songs in %d seconds\n",song_count, DPRINTF(E_LOG,L_MAIN,"Scanned %d songs in %d seconds\n",song_count,
end_time-start_time); end_time-start_time);