revert to 1.11

This commit is contained in:
Ron Pedde 2006-03-06 08:32:43 +00:00
parent f79756769a
commit d53e2f83e4

View File

@ -1,6 +1,6 @@
/* /*
* $Id$ * $Id$
* sqlite3-specific db implementation * sqlite2-specific db implementation
* *
* Copyright (C) 2005 Ron Pedde (ron@pedde.com) * Copyright (C) 2005 Ron Pedde (ron@pedde.com)
* *
@ -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,19 +116,6 @@ 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
* *
@ -143,26 +130,22 @@ 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_reclose(); db_sqlite3_unlock();
_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) { if(pe) { free(*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) {
@ -180,9 +163,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;
} }
@ -202,12 +185,7 @@ 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);
@ -228,8 +206,7 @@ int db_sqlite3_exec(char **pe, int loglevel, char *fmt, ...) {
} }
sqlite3_free(query); sqlite3_free(query);
_db_sqlite3_reclose(); db_sqlite3_unlock();
_db_sqlite3_unlock();
if(err != SQLITE_OK) if(err != SQLITE_OK)
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
@ -241,15 +218,8 @@ 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);
@ -264,14 +234,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_reclose(); db_sqlite3_unlock();
_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;
@ -345,7 +315,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));
DPRINTF(E_SPAM,L_DB,"sql error: %d\n",err); sqlite3_finalize(db_sqlite3_stmt);
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
} }
@ -368,13 +338,11 @@ 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_reclose(); db_sqlite3_unlock();
_db_sqlite3_unlock();
return DB_E_SQL_ERROR; return DB_E_SQL_ERROR;
} }
_db_sqlite3_reclose(); db_sqlite3_unlock();
_db_sqlite3_unlock();
return DB_E_SUCCESS; return DB_E_SUCCESS;
} }
@ -407,15 +375,15 @@ 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;
case DB_SQL_EVENT_SONGSCANSTART: case DB_SQL_EVENT_SONGSCANSTART:
if(db_sqlite3_reload) { if(db_sqlite3_reload) {
db_sqlite3_exec(NULL,E_FATAL,"pragma synchronous = off"); db_sqlite3_exec(NULL,E_FATAL,"pragma synchronous = off");
// db_sqlite3_exec(NULL,E_FATAL,"begin transaction"); db_sqlite3_exec(NULL,E_FATAL,"begin transaction");
} else { } else {
db_sqlite3_exec(NULL,E_DBG,"drop table updated"); db_sqlite3_exec(NULL,E_DBG,"drop table updated");
db_sqlite3_exec(NULL,E_FATAL,"create temp table updated (id int)"); db_sqlite3_exec(NULL,E_FATAL,"create temp table updated (id int)");
@ -426,7 +394,7 @@ int db_sqlite3_event(int event_type) {
case DB_SQL_EVENT_SONGSCANEND: case DB_SQL_EVENT_SONGSCANEND:
if(db_sqlite3_reload) { if(db_sqlite3_reload) {
// db_sqlite3_exec(NULL,E_FATAL,"commit transaction"); db_sqlite3_exec(NULL,E_FATAL,"commit transaction");
db_sqlite3_exec(NULL,E_FATAL,"create index idx_path on songs(path)"); db_sqlite3_exec(NULL,E_FATAL,"create index idx_path on songs(path)");
db_sqlite3_exec(NULL,E_DBG,"delete from config where term='rescan'"); db_sqlite3_exec(NULL,E_DBG,"delete from config where term='rescan'");
} else { } else {
@ -437,13 +405,13 @@ int db_sqlite3_event(int event_type) {
break; break;
case DB_SQL_EVENT_PLSCANSTART: case DB_SQL_EVENT_PLSCANSTART:
// if(db_sqlite3_reload) if(db_sqlite3_reload)
// db_sqlite3_exec(NULL,E_FATAL,"begin transaction"); db_sqlite3_exec(NULL,E_FATAL,"begin transaction");
break; break;
case DB_SQL_EVENT_PLSCANEND: case DB_SQL_EVENT_PLSCANEND:
if(db_sqlite3_reload) { if(db_sqlite3_reload) {
// db_sqlite3_exec(NULL,E_FATAL,"end transaction"); db_sqlite3_exec(NULL,E_FATAL,"end transaction");
db_sqlite3_exec(NULL,E_FATAL,"pragma synchronous=normal"); db_sqlite3_exec(NULL,E_FATAL,"pragma synchronous=normal");
db_sqlite3_exec(NULL,E_FATAL,"create index idx_songid on playlistitems(songid)"); db_sqlite3_exec(NULL,E_FATAL,"create index idx_songid on playlistitems(songid)");
db_sqlite3_exec(NULL,E_FATAL,"create index idx_playlistid on playlistitems(playlistid)"); db_sqlite3_exec(NULL,E_FATAL,"create index idx_playlistid on playlistitems(playlistid)");
@ -477,18 +445,16 @@ 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_reclose(); db_sqlite3_unlock();
_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"
@ -544,7 +510,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"