diff options
author | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2008-09-05 00:59:36 +0200 |
---|---|---|
committer | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2008-09-05 00:59:36 +0200 |
commit | e3603a8c0e11bfc302450a1be85c513fac9aac69 (patch) | |
tree | bab8183b5c1b0f0ea1829fb5ba6ab27dec735910 /src/mfs_subr.c | |
parent | 69b306cc7294701632f0d6fca0f5e8e3bbd328c8 (diff) |
- Added mfs_cleanup_db, to remove inactive paths
Diffstat (limited to 'src/mfs_subr.c')
-rw-r--r-- | src/mfs_subr.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 17a2126..ac33eca 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -137,6 +137,74 @@ mfs_insert_path(const char *path, sqlite3 *handle) } /* + * Clean up the database: + * + * - Remove disabled paths + * - Remove songs in disabled paths + * - Remove unused artists and genres + */ +void +mfs_cleanup_db(sqlite3 *handle) +{ + DEBUG("cleaning up db\n"); + /* sqlite doesn't support deleting from multiple tables at the same time :-( */ + sqlite3_stmt *st, *st_; + int res, res_; + const char *path; + + + /* Removed songs from inactive paths */ + res = sqlite3_prepare_v2(handle, "SELECT path FROM path WHERE " + "active = 0", -1, &st, NULL); + if (res != SQLITE_OK) { + DEBUG("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + return; + } + res = sqlite3_step(st); + + while (res == SQLITE_ROW) { + path = (const char*)sqlite3_column_text(st, 0); + + res_ = sqlite3_prepare_v2(handle, "DELETE FROM song WHERE " + "filepath LIKE (?||'%')", -1, &st_, NULL); + if (res_ != SQLITE_OK) { + DEBUG("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + return; + } + + DEBUG("removing songs from '%s'\n", path); + sqlite3_bind_text(st_, 1, path, -1, SQLITE_TRANSIENT); + res_ = sqlite3_step(st_); + if (res_ != SQLITE_DONE) { + DEBUG("Error deleting '%s' from database: %s\n", + path, sqlite3_errmsg(handle)); + } + sqlite3_finalize(st_); + + res = sqlite3_step(st); + } + sqlite3_finalize(st); + + /* Remove the actual paths */ + res = sqlite3_prepare_v2(handle, "DELETE FROM path WHERE " + "active = 0", -1, &st, NULL); + if (res != SQLITE_OK) { + DEBUG("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + return; + } + res = sqlite3_step(st); + if (res != SQLITE_DONE) { + DEBUG("Error deleting inactive paths from db: %s\n", + sqlite3_errmsg(handle)); + return; + } + sqlite3_finalize(st); +} + +/* * Reload the configuration from $HOME/.mfsrc * */ @@ -189,6 +257,8 @@ mfs_reload_config() } } + mfs_cleanup_db(handle); + free (mfsrc); sqlite3_close(handle); |