mirror of
https://github.com/owntone/owntone-server.git
synced 2025-04-01 10:13:45 -04:00
Add file and playlist deletion (by path) routines
This commit is contained in:
parent
ac96ac6baf
commit
9ff8913dd6
155
src/db.c
155
src/db.c
@ -1296,6 +1296,35 @@ db_file_update(struct media_file_info *mfi)
|
|||||||
#undef Q_TMPL
|
#undef Q_TMPL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
db_file_delete_bypath(char *path)
|
||||||
|
{
|
||||||
|
#define Q_TMPL "DELETE FROM songs WHERE path = '%q';"
|
||||||
|
char *query;
|
||||||
|
char *errmsg;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
query = sqlite3_mprintf(Q_TMPL, path);
|
||||||
|
if (!query)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
|
|
||||||
|
errmsg = NULL;
|
||||||
|
ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
|
||||||
|
if (ret != SQLITE_OK)
|
||||||
|
DPRINTF(E_LOG, L_DB, "Error deleting file: %s\n", errmsg);
|
||||||
|
|
||||||
|
sqlite3_free(errmsg);
|
||||||
|
sqlite3_free(query);
|
||||||
|
|
||||||
|
#undef Q_TMPL
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Playlists */
|
/* Playlists */
|
||||||
int
|
int
|
||||||
@ -1360,6 +1389,56 @@ db_pl_ping(int id)
|
|||||||
#undef Q_TMPL
|
#undef Q_TMPL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
db_pl_id_bypath(char *path, int *id)
|
||||||
|
{
|
||||||
|
#define Q_TMPL "SELECT id FROM playlists WHERE path = '%q';"
|
||||||
|
char *query;
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
query = sqlite3_mprintf(Q_TMPL, path);
|
||||||
|
if (!query)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
|
|
||||||
|
ret = sqlite3_prepare_v2(hdl, query, -1, &stmt, NULL);
|
||||||
|
if (ret != SQLITE_OK)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl));
|
||||||
|
|
||||||
|
sqlite3_free(query);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = sqlite3_step(stmt);
|
||||||
|
if (ret != SQLITE_ROW)
|
||||||
|
{
|
||||||
|
if (ret == SQLITE_DONE)
|
||||||
|
DPRINTF(E_INFO, L_DB, "No results\n");
|
||||||
|
else
|
||||||
|
DPRINTF(E_LOG, L_DB, "Could not step: %s\n", sqlite3_errmsg(hdl));
|
||||||
|
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
sqlite3_free(query);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*id = sqlite3_column_int(stmt, 0);
|
||||||
|
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
sqlite3_free(query);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
#undef Q_TMPL
|
||||||
|
}
|
||||||
|
|
||||||
static struct playlist_info *
|
static struct playlist_info *
|
||||||
db_pl_fetch_byquery(char *query)
|
db_pl_fetch_byquery(char *query)
|
||||||
{
|
{
|
||||||
@ -1700,6 +1779,35 @@ db_pl_add_item(int plid, int mfid)
|
|||||||
#undef QADD_TMPL
|
#undef QADD_TMPL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
db_pl_clear_items(int id)
|
||||||
|
{
|
||||||
|
#define Q_TMPL "DELETE FROM playlistitems WHERE playlistid = %d;"
|
||||||
|
char *query;
|
||||||
|
char *errmsg;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
query = sqlite3_mprintf(Q_TMPL, id);
|
||||||
|
if (!query)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
|
|
||||||
|
errmsg = NULL;
|
||||||
|
ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
|
||||||
|
if (ret != SQLITE_OK)
|
||||||
|
DPRINTF(E_LOG, L_DB, "Error clearing playlist %d items: %s\n", id, errmsg);
|
||||||
|
|
||||||
|
sqlite3_free(errmsg);
|
||||||
|
sqlite3_free(query);
|
||||||
|
|
||||||
|
#undef Q_TMPL
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
db_pl_update(int id)
|
db_pl_update(int id)
|
||||||
{
|
{
|
||||||
@ -1787,47 +1895,48 @@ db_pl_update_all(void)
|
|||||||
void
|
void
|
||||||
db_pl_delete(int id)
|
db_pl_delete(int id)
|
||||||
{
|
{
|
||||||
#define Q1_TMPL "DELETE FROM playlists WHERE id = %d;"
|
#define Q_TMPL "DELETE FROM playlists WHERE id = %d;"
|
||||||
#define Q2_TMPL "DELETE FROM playlistitems WHERE playlistid = %d;"
|
char *query;
|
||||||
char *query[2];
|
|
||||||
char *errmsg;
|
char *errmsg;
|
||||||
int i;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (id == 1)
|
if (id == 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
query[0] = sqlite3_mprintf(Q1_TMPL, id);
|
query = sqlite3_mprintf(Q_TMPL, id);
|
||||||
query[1] = sqlite3_mprintf(Q2_TMPL, id);
|
if (!query)
|
||||||
|
|
||||||
if (!query[0] || !query[1])
|
|
||||||
{
|
{
|
||||||
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||||
|
|
||||||
if (query[0])
|
|
||||||
sqlite3_free(query[0]);
|
|
||||||
if (query[1])
|
|
||||||
sqlite3_free(query[1]);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < (sizeof(query) / sizeof(query[0])); i++)
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
{
|
|
||||||
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query[i]);
|
|
||||||
|
|
||||||
errmsg = NULL;
|
errmsg = NULL;
|
||||||
ret = sqlite3_exec(hdl, query[i], NULL, NULL, &errmsg);
|
ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
|
||||||
if (ret != SQLITE_OK)
|
if (ret != SQLITE_OK)
|
||||||
{
|
DPRINTF(E_LOG, L_DB, "Error deleting playlist %d: %s\n", id, errmsg);
|
||||||
DPRINTF(E_LOG, L_DB, "Query error: %s\n", errmsg);
|
|
||||||
sqlite3_free(errmsg);
|
sqlite3_free(errmsg);
|
||||||
}
|
sqlite3_free(query);
|
||||||
|
|
||||||
sqlite3_free(query[i]);
|
db_pl_clear_items(id);
|
||||||
}
|
|
||||||
|
|
||||||
#undef Q1_TMPL
|
#undef Q_TMPL
|
||||||
#undef Q2_TMPL
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
db_pl_delete_bypath(char *path)
|
||||||
|
{
|
||||||
|
int id;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = db_pl_id_bypath(path, &id);
|
||||||
|
if (ret < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
db_pl_delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
6
src/db.h
6
src/db.h
@ -237,6 +237,9 @@ db_file_add(struct media_file_info *mfi);
|
|||||||
int
|
int
|
||||||
db_file_update(struct media_file_info *mfi);
|
db_file_update(struct media_file_info *mfi);
|
||||||
|
|
||||||
|
void
|
||||||
|
db_file_delete_bypath(char *path);
|
||||||
|
|
||||||
/* Playlists */
|
/* Playlists */
|
||||||
int
|
int
|
||||||
db_pl_get_count(int *count);
|
db_pl_get_count(int *count);
|
||||||
@ -262,6 +265,9 @@ db_pl_update_all(void);
|
|||||||
void
|
void
|
||||||
db_pl_delete(int id);
|
db_pl_delete(int id);
|
||||||
|
|
||||||
|
void
|
||||||
|
db_pl_delete_bypath(char *path);
|
||||||
|
|
||||||
/* Inotify */
|
/* Inotify */
|
||||||
int
|
int
|
||||||
db_watch_clear(void);
|
db_watch_clear(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user