~mhaulo/mixxx/multilevel_crates_playlists

« back to all changes in this revision

Viewing changes to mixxx/src/library/dao/playlistdao.cpp

  • Committer: Mika Haulo
  • Date: 2011-04-25 19:31:06 UTC
  • Revision ID: mika@haulo.fi-20110425193106-d8uxe431g3u9ccij
Deleting a crate/playlist now deletes all its subcrates/playlists

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
/** Delete a playlist */
112
112
void PlaylistDAO::deletePlaylist(int playlistId)
113
113
{
114
 
    // qDebug() << "PlaylistDAO::deletePlaylist" << QThread::currentThread() << m_database.connectionName();
 
114
    qDebug() << "PlaylistDAO::deletePlaylist()";
115
115
    m_database.transaction();
116
116
 
117
 
    //Get the playlist id for this
118
 
    QSqlQuery query(m_database);
119
 
 
120
 
    //Delete the row in the Playlists table.
121
 
    query.prepare("DELETE FROM Playlists "
122
 
                  "WHERE id= :id");
123
 
    query.bindValue(":id", playlistId);
124
 
    if (!query.exec()) {
125
 
        qDebug() << "deletePlaylist" << query.lastError();
126
 
      m_database.rollback();
127
 
        return;
128
 
    }
129
 
    //query.finish();
130
 
 
131
 
    //Delete the tracks in this playlist from the PlaylistTracks table.
132
 
    query.prepare("DELETE FROM PlaylistTracks "
133
 
                  "WHERE playlist_id = :id");
134
 
    query.bindValue(":id", playlistId);
135
 
    if (!query.exec()) {
136
 
        qDebug() << "deletePlaylist" << query.lastError();
137
 
      m_database.rollback();
138
 
        return;
139
 
    }
140
 
    //query.finish();
141
 
 
142
 
    m_database.commit();
 
117
    if (deleteSubPlaylists(playlistId)) {
 
118
        m_database.commit();
 
119
    }
 
120
    else {
 
121
        m_database.rollback();
 
122
    }
 
123
    
143
124
    //TODO: Crap, we need to shuffle the positions of all the playlists?
144
125
}
145
126
 
146
127
 
 
128
bool PlaylistDAO::deleteSubPlaylists(int parentId) {
 
129
    qDebug() << "deleteSubPlaylists of" << parentId;
 
130
    QList<int> subPlaylists = getPlaylists(parentId).values();
 
131
 
 
132
    
 
133
    foreach(int subPlaylist, subPlaylists) {
 
134
 
 
135
        bool subPlaylistsDeleted = deleteSubPlaylists(subPlaylist);
 
136
        
 
137
        if ( !subPlaylistsDeleted ) {
 
138
            return false;
 
139
        }
 
140
    }
 
141
 
 
142
    QSqlQuery deletePlaylist(m_database);
 
143
    deletePlaylist.prepare("DELETE FROM Playlists WHERE id= :id");
 
144
    deletePlaylist.bindValue(":id", parentId);
 
145
    bool playlistDeleted = deletePlaylist.exec();
 
146
 
 
147
    QSqlQuery deletePlaylistTracks(m_database);
 
148
    deletePlaylistTracks.prepare("DELETE FROM PlaylistTracks WHERE playlist_id = :id");
 
149
    deletePlaylistTracks.bindValue(":id", parentId);
 
150
    bool tracksDeleted = deletePlaylistTracks.exec();
 
151
 
 
152
    return (playlistDeleted && tracksDeleted);
 
153
}
 
154
 
 
155
 
147
156
void PlaylistDAO::renamePlaylist(int playlistId, const QString& newName) {
148
157
    m_database.transaction();
149
158
    QSqlQuery query(m_database);