summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2026-01-24 13:07:04 +0100
committerCarl Hetherington <cth@carlh.net>2026-02-03 21:37:04 +0100
commit79cc9b66c779aa4e0c8a2485653da316bafd8e97 (patch)
treeaeea4caf098ad7a1c939957ff90ed08e5891cbeb /src
parent76a37b29981d77c9bd7f20dc4ecca4a73b10b8f4 (diff)
Add move_entry().
Diffstat (limited to 'src')
-rw-r--r--src/lib/show_playlist_list.cc40
-rw-r--r--src/lib/show_playlist_list.h2
2 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/show_playlist_list.cc b/src/lib/show_playlist_list.cc
index 980a10361..6dfbbad29 100644
--- a/src/lib/show_playlist_list.cc
+++ b/src/lib/show_playlist_list.cc
@@ -277,6 +277,46 @@ ShowPlaylistList::insert_entry(ShowPlaylistID playlist_id, ShowPlaylistEntry con
void
+ShowPlaylistList::move_entry(ShowPlaylistID playlist_id, int old_index, int new_index)
+{
+ SQLiteTransaction transaction(_db);
+
+ if (old_index == new_index) {
+ return;
+ }
+
+ SQLiteStatement find_id(_db, "SELECT id FROM entries WHERE show_playlist=? AND sort_index=?");
+ find_id.bind_int64(1, playlist_id.get());
+ find_id.bind_int64(2, old_index);
+
+ optional<int> moving_id;
+ find_id.execute([&moving_id](SQLiteStatement& statement) {
+ DCPOMATIC_ASSERT(statement.data_count() == 1);
+ moving_id = statement.column_int64(0);
+ });
+ DCPOMATIC_ASSERT(moving_id);
+
+ auto const lower = old_index < new_index ? (old_index + 1) : new_index;
+ auto const upper = old_index < new_index ? new_index : (old_index - 1);
+ auto const direction = old_index < new_index ? "-1" : "+1";
+
+ SQLiteStatement update_others(_db, fmt::format("UPDATE entries SET sort_index=sort_index{} WHERE show_playlist=? AND sort_index>=? AND sort_index<=?", direction));
+ update_others.bind_int64(1, playlist_id.get());
+ update_others.bind_int64(2, lower);
+ update_others.bind_int64(3, upper);
+ update_others.execute();
+
+ SQLiteStatement update(_db, "UPDATE entries SET sort_index=? WHERE show_playlist=? AND id=?");
+ update.bind_int64(1, new_index);
+ update.bind_int64(2, playlist_id.get());
+ update.bind_int64(3, *moving_id);
+ update.execute();
+
+ transaction.commit();
+}
+
+
+void
ShowPlaylistList::update_entry(ShowPlaylistID playlist_id, int index, ShowPlaylistEntry const& entry)
{
SQLiteStatement update_entry(_db, _entries.update("WHERE show_playlist=? AND sort_index=?"));
diff --git a/src/lib/show_playlist_list.h b/src/lib/show_playlist_list.h
index 6ebadfc37..0da94ba99 100644
--- a/src/lib/show_playlist_list.h
+++ b/src/lib/show_playlist_list.h
@@ -80,6 +80,8 @@ public:
void move_entry_up(ShowPlaylistID, int index);
/** Move the given playlist entry one place lower (later) */
void move_entry_down(ShowPlaylistID, int index);
+ /** Move the given playlist entry to the given index */
+ void move_entry(ShowPlaylistID playlist_id, int old_index, int new_index);
void read_legacy(boost::filesystem::path dir);