Skip to content

Commit

Permalink
Only refresh music DB if changes were made
Browse files Browse the repository at this point in the history
  • Loading branch information
cnsldv committed May 15, 2017
1 parent cac7d92 commit 0a533d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ int main(void)
int key = 0;
int idx;
int done = 0;
int madd = 0, mrem = 0;

SceAppUtilInitParam init_param;
SceAppUtilBootParam boot_param;
Expand Down Expand Up @@ -102,10 +103,13 @@ int main(void)
printf("Managing music, please wait\n");
idx = 0;
while (music_dirs[idx]) {
add_music(music_dirs[idx]);
madd += add_music(music_dirs[idx]);
idx++;
}
clean_music();
mrem = clean_music();
if (madd > 0 || mrem > 0) {
refresh_music_db();
}
psvDebugScreenSetFgColor(COLOR_GREEN);
printf("Done.\n");
psvDebugScreenSetFgColor(COLOR_WHITE);
Expand Down
40 changes: 28 additions & 12 deletions src/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static int add_music_int(sqlite3 *db, const char *dir, int added)
return added;
}

void add_music(const char *dir)
int add_music(const char *dir)
{
int added;
sqlite3 *db;
Expand All @@ -242,21 +242,13 @@ void add_music(const char *dir)
sqlite3_exec(db, "BEGIN", 0, 0, 0);
added = add_music_int(db, dir, 0);
printf("Added %d tracks\n", added);

sqlite3_stmt *stmt;
const char *sql = refresh_db_sql;
ret = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("Failed to execute %s, error %s\n", sql, sqlite3_errmsg(db));
goto fail;
}
sqlite3_step(stmt);
sqlite3_exec(db, "COMMIT", 0, 0, 0);
fail:
sqlite3_exec(db, "COMMIT", 0, 0, 0);
sqlite3_close(db);
return added;
}

void clean_music(void)
int clean_music(void)
{
int removed = 0;
sqlite3 *db;
Expand Down Expand Up @@ -297,6 +289,7 @@ void clean_music(void)
sqlite3_close(db);

printf("Removed %d tracks\n", removed);
return removed;
}

void empty_music(void)
Expand Down Expand Up @@ -324,3 +317,26 @@ void empty_music(void)
sqlite3_close(db);
}

void refresh_music_db(void)
{
sqlite3 *db;
int ret = sqlite3_open(MUSIC_DB, &db);
if (ret) {
printf("Failed to open the database: %s\n", sqlite3_errmsg(db));
goto fail;
}

sqlite3_stmt *stmt;
const char *sql = refresh_db_sql;
ret = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("Failed to execute %s, error %s\n", sql, sqlite3_errmsg(db));
goto fail;
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);

fail:
sqlite3_close(db);
}

5 changes: 3 additions & 2 deletions src/music.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#ifndef _MUSIC_H_
#define _MUSIC_H_

void add_music(const char *dir);
void clean_music(void);
int add_music(const char *dir);
int clean_music(void);
void empty_music(void);
void refresh_music_db(void);

#endif

0 comments on commit 0a533d9

Please sign in to comment.