Merge pull request #310 from chme/dbcleanup

[db] Small code improvements (avoid hard coded values in query strings)
This commit is contained in:
ejurgensen 2016-11-12 22:58:54 +01:00 committed by GitHub
commit fb808bd36a
2 changed files with 43 additions and 32 deletions

View File

@ -316,6 +316,9 @@ db_pl_fetch_byid(int id);
static enum group_type static enum group_type
db_group_type_bypersistentid(int64_t persistentid); db_group_type_bypersistentid(int64_t persistentid);
static int
db_query_run(char *query, int free, int cache_update);
char * char *
db_escape_string(const char *str) db_escape_string(const char *str)
@ -713,55 +716,47 @@ db_hook_post_scan(void)
void void
db_purge_cruft(time_t ref) db_purge_cruft(time_t ref)
{ {
char *errmsg; #define Q_TMPL "DELETE FROM directories WHERE id >= %d AND db_timestamp < %" PRIi64 ";"
int i; int i;
int ret; int ret;
char *queries[4] = { NULL, NULL, NULL, NULL }; char *query;
char *queries_tmpl[4] = char *queries_tmpl[3] =
{ {
"DELETE FROM playlistitems WHERE playlistid IN (SELECT id FROM playlists p WHERE p.type <> %d AND p.db_timestamp < %" PRIi64 ");", "DELETE FROM playlistitems WHERE playlistid IN (SELECT id FROM playlists p WHERE p.type <> %d AND p.db_timestamp < %" PRIi64 ");",
"DELETE FROM playlists WHERE type <> %d AND db_timestamp < %" PRIi64 ";", "DELETE FROM playlists WHERE type <> %d AND db_timestamp < %" PRIi64 ";",
"DELETE FROM files WHERE -1 <> %d AND db_timestamp < %" PRIi64 ";", "DELETE FROM files WHERE -1 <> %d AND db_timestamp < %" PRIi64 ";",
"DELETE FROM directories WHERE id > 4 AND -1 <> %d AND db_timestamp < %" PRIi64 ";"
}; };
if (sizeof(queries) != sizeof(queries_tmpl))
{
DPRINTF(E_LOG, L_DB, "db_purge_cruft(): queries out of sync with queries_tmpl\n");
return;
}
for (i = 0; i < (sizeof(queries_tmpl) / sizeof(queries_tmpl[0])); i++) for (i = 0; i < (sizeof(queries_tmpl) / sizeof(queries_tmpl[0])); i++)
{ {
queries[i] = sqlite3_mprintf(queries_tmpl[i], PL_SPECIAL, (int64_t)ref); query = sqlite3_mprintf(queries_tmpl[i], PL_SPECIAL, (int64_t)ref);
if (!queries[i]) if (!query)
{ {
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
goto purge_fail; return;
} }
}
for (i = 0; i < (sizeof(queries) / sizeof(queries[0])); i++) DPRINTF(E_DBG, L_DB, "Running purge query '%s'\n", query);
{
DPRINTF(E_DBG, L_DB, "Running purge query '%s'\n", queries[i]);
ret = db_exec(queries[i], &errmsg); ret = db_query_run(query, 1, 0);
if (ret != SQLITE_OK) if (ret == 0)
{
DPRINTF(E_LOG, L_DB, "Purge query %d error: %s\n", i, errmsg);
sqlite3_free(errmsg);
}
else
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl)); DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
} }
purge_fail: query = sqlite3_mprintf(Q_TMPL, DIR_MAX, (int64_t)ref);
for (i = 0; i < (sizeof(queries) / sizeof(queries[0])); i++) if (!query)
{ {
sqlite3_free(queries[i]); DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
return;
} }
DPRINTF(E_DBG, L_DB, "Running purge query '%s'\n", query);
ret = db_query_run(query, 1, 0);
if (ret == 0)
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
#undef Q_TMPL
} }
void void
@ -3894,14 +3889,15 @@ db_pairing_fetch_byguid(struct pairing_info *pi)
void void
db_spotify_purge(void) db_spotify_purge(void)
{ {
char *queries[5] = #define Q_TMPL "UPDATE directories SET disabled = %" PRIi64 " WHERE virtual_path = '/spotify:';"
char *queries[4] =
{ {
"DELETE FROM files WHERE path LIKE 'spotify:%%';", "DELETE FROM files WHERE path LIKE 'spotify:%%';",
"DELETE FROM playlistitems WHERE filepath LIKE 'spotify:%%';", "DELETE FROM playlistitems WHERE filepath LIKE 'spotify:%%';",
"DELETE FROM playlists WHERE path LIKE 'spotify:%%';", "DELETE FROM playlists WHERE path LIKE 'spotify:%%';",
"DELETE FROM directories WHERE virtual_path LIKE '/spotify:/%%';", "DELETE FROM directories WHERE virtual_path LIKE '/spotify:/%%';",
"UPDATE directories SET disabled = 4294967296 WHERE virtual_path = '/spotify:';",
}; };
char *query;
int i; int i;
int ret; int ret;
@ -3912,6 +3908,20 @@ db_spotify_purge(void)
if (ret == 0) if (ret == 0)
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl)); DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
} }
// Disable the spotify directory by setting 'disabled' to INOTIFY_FAKE_COOKIE value
query = sqlite3_mprintf(Q_TMPL, INOTIFY_FAKE_COOKIE);
if (!query)
{
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
return;
}
ret = db_query_run(query, 1, 1);
if (ret == 0)
DPRINTF(E_DBG, L_DB, "Disabled spotify directory\n");
#undef Q_TMPL
} }
/* Spotify */ /* Spotify */

View File

@ -189,7 +189,7 @@ struct media_file_info {
#define mfi_offsetof(field) offsetof(struct media_file_info, field) #define mfi_offsetof(field) offsetof(struct media_file_info, field)
/* PL_SPECIAL value must be in sync with type value in Q_PL* in db.c */ /* PL_SPECIAL value must be in sync with type value in Q_PL* in db_init.c */
enum pl_type { enum pl_type {
PL_SPECIAL = 0, PL_SPECIAL = 0,
PL_FOLDER = 1, PL_FOLDER = 1,
@ -350,12 +350,13 @@ struct filecount_info {
uint32_t length; uint32_t length;
}; };
/* Directory ids must be in sync with the ids in Q_DIR* in db.c */ /* Directory ids must be in sync with the ids in Q_DIR* in db_init.c */
enum directory_ids { enum directory_ids {
DIR_ROOT = 1, DIR_ROOT = 1,
DIR_FILE = 2, DIR_FILE = 2,
DIR_HTTP = 3, DIR_HTTP = 3,
DIR_SPOTIFY = 4, DIR_SPOTIFY = 4,
DIR_MAX
}; };
struct directory_info { struct directory_info {