Skip to content

Commit 2262ea9

Browse files
committed
PlaylistDAO: add helper playlistExist(), use when appending tracks
1 parent b7b66b4 commit 2262ea9

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/library/dao/playlistdao.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,29 @@ bool PlaylistDAO::removeTracksFromPlaylist(int playlistId, int startIndex) {
427427
return true;
428428
}
429429

430+
bool PlaylistDAO::playlistExists(const int playlistId) const {
431+
ScopedTransaction transaction(m_database);
432+
QSqlQuery query(m_database);
433+
query.prepare(QStringLiteral("SELECT id FROM Playlists WHERE id = :id"));
434+
query.bindValue(":id", playlistId);
435+
436+
if (!query.exec()) {
437+
LOG_FAILED_QUERY(query);
438+
return false;
439+
}
440+
441+
if (query.next()) {
442+
// id is guaranteed to be unique, so we can return here
443+
return true;
444+
}
445+
// not found
446+
return false;
447+
}
448+
430449
bool PlaylistDAO::appendTracksToPlaylist(const QList<TrackId>& trackIds, const int playlistId) {
431450
// qDebug() << "PlaylistDAO::appendTracksToPlaylist"
432451
// << QThread::currentThread() << m_database.connectionName();
452+
DEBUG_ASSERT(playlistExists(playlistId));
433453

434454
// Start the transaction
435455
ScopedTransaction transaction(m_database);

src/library/dao/playlistdao.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class PlaylistDAO : public QObject, public virtual DAO {
5656
int setPlaylistsLocked(const QSet<int>& playlistIds, const bool lock);
5757
// Find out the state of a playlist lock
5858
bool isPlaylistLocked(const int playlistId) const;
59+
// Check if a playlist exists
60+
bool playlistExists(const int playlistId) const;
5961
// Append a list of tracks to a playlist
6062
bool appendTracksToPlaylist(const QList<TrackId>& trackIds, const int playlistId);
6163
// Append a track to a playlist

0 commit comments

Comments
 (0)