[db] Fix mpd command 'delete' removing to many items

Previously db_queue_delete_bypos iterated over the items and called
delete for each item. The delete also adjusted the pos/shuffle_pos
values for the remaining items. 

A delete of pos = 0 resulted therefor in:
- fetch item with pos = 0
- delete item at pos
- update remaining items to pos = pos - 1
- fetch item with pos = 0 (returns the new item at pos 0)
- delete item at pos
- ...
This commit is contained in:
chme 2017-02-04 07:26:48 +01:00 committed by ejurgensen
parent d293356d8b
commit 9680fa093a

View File

@ -4904,48 +4904,39 @@ db_queue_delete_byitemid(uint32_t item_id)
int int
db_queue_delete_bypos(uint32_t pos, int count) db_queue_delete_bypos(uint32_t pos, int count)
{ {
struct query_params query_params; char *query;
struct db_queue_item queue_item;
int to_pos; int to_pos;
int ret; int ret;
// Find items with the given position
memset(&query_params, 0, sizeof(struct query_params));
to_pos = pos + count;
query_params.filter = sqlite3_mprintf("pos >= %d AND pos < %d", pos, to_pos);
db_transaction_begin(); db_transaction_begin();
ret = queue_enum_start(&query_params); // Remove item with the given item_id
if (ret < 0) to_pos = pos + count;
{ query = sqlite3_mprintf("DELETE FROM queue where pos >= %d AND pos < %d;", pos, to_pos);
return -1; ret = db_query_run(query, 1, 0);
}
while ((ret = queue_enum_fetch(&query_params, &queue_item, 0)) == 0 && (queue_item.id > 0))
{
ret = queue_delete_item(&queue_item);
if (ret < 0)
{
DPRINTF(E_LOG, L_DB, "Failed to delete item with item-id: %d\n", queue_item.id);
break;
}
}
db_query_end(&query_params);
sqlite3_free(query_params.filter);
if (ret < 0) if (ret < 0)
{ {
db_transaction_rollback(); db_transaction_rollback();
return -1;
} }
else
ret = queue_fix_pos(S_POS);
if (ret < 0)
{ {
db_transaction_end(); db_transaction_rollback();
queue_inc_version_and_notify(); return -1;
} }
ret = queue_fix_pos(S_SHUFFLE_POS);
if (ret < 0)
{
db_transaction_rollback();
return -1;
}
db_transaction_end();
queue_inc_version_and_notify();
return ret; return ret;
} }