void BaseExternalPlaylistModel::setPlaylist(QString playlist_path) { QSqlQuery finder_query(m_database); finder_query.prepare(QString("SELECT id from %1 where name=:name").arg(m_playlistsTable)); finder_query.bindValue(":name", playlist_path); if (!finder_query.exec()) { LOG_FAILED_QUERY(finder_query) << "Error getting id for playlist:" << playlist_path; return; } // TODO(XXX): Why not last-insert id? int playlistId = -1; QSqlRecord finder_query_record = finder_query.record(); while (finder_query.next()) { playlistId = finder_query.value( finder_query_record.indexOf("id")).toInt(); } if (playlistId == -1) { qDebug() << "ERROR: Could not get the playlist ID for playlist:" << playlist_path; return; } QString playlistViewTable = QString("%1_%2").arg(m_playlistTracksTable, QString::number(playlistId)); QStringList columns; columns << "track_id"; columns << "position"; columns << "'' AS " + LIBRARYTABLE_PREVIEW; QSqlQuery query(m_database); FieldEscaper f(m_database); QString queryString = QString( "CREATE TEMPORARY VIEW IF NOT EXISTS %1 AS " "SELECT %2 FROM %3 WHERE playlist_id = %4") .arg(f.escapeString(playlistViewTable), columns.join(","), m_playlistTracksTable, QString::number(playlistId)); query.prepare(queryString); if (!query.exec()) { LOG_FAILED_QUERY(query) << "Error creating temporary view for playlist."; return; } columns[2] = LIBRARYTABLE_PREVIEW; setTable(playlistViewTable, columns[0], columns, m_trackSource); setDefaultSort(fieldIndex(ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION), Qt::AscendingOrder); setSearch(""); }
// reads all playlist entries and executes a SQL statement void RhythmboxFeature::importPlaylist(QXmlStreamReader &xml, QSqlQuery &query_insert_to_playlist_tracks, int playlist_id) { int playlist_position = 1; while (!xml.atEnd()) { //read next XML element xml.readNext(); if (xml.isStartElement() && xml.name() == "location") { QString location = xml.readElementText(); location.remove("file://"); QByteArray strlocbytes = location.toUtf8(); QUrl locationUrl = QUrl::fromEncoded(strlocbytes); location = locationUrl.toLocalFile(); //get the ID of the file in the rhythmbox_library table int track_id = -1; QSqlQuery finder_query(m_database); finder_query.prepare("select id from rhythmbox_library where location=:path"); finder_query.bindValue(":path", location); bool success = finder_query.exec(); if (success) { const int idColumn = finder_query.record().indexOf("id"); while (finder_query.next()) { track_id = finder_query.value(idColumn).toInt(); } } else { qDebug() << "SQL Error in RhythmboxFeature.cpp: line" << __LINE__ << " " << finder_query.lastError(); } query_insert_to_playlist_tracks.bindValue(":playlist_id", playlist_id); query_insert_to_playlist_tracks.bindValue(":track_id", track_id); query_insert_to_playlist_tracks.bindValue(":position", playlist_position++); success = query_insert_to_playlist_tracks.exec(); if (!success) { qDebug() << "SQL Error in RhythmboxFeature.cpp: line" << __LINE__ << " " << query_insert_to_playlist_tracks.lastError() << "trackid" << track_id << "playlis ID " << playlist_id << "-----------------"; } } // Exit the the loop if we reach the closing <playlist> tag if (xml.isEndElement() && xml.name() == "playlist") { break; } } }
void TraktorFeature::parsePlaylistEntries( QXmlStreamReader &xml, QString playlist_path, QSqlQuery query_insert_into_playlist, QSqlQuery query_insert_into_playlisttracks) { // In the database, the name of a playlist is specified by the unique path, // e.g., /someFolderA/someFolderB/playlistA" query_insert_into_playlist.bindValue(":name", playlist_path); if (!query_insert_into_playlist.exec()) { LOG_FAILED_QUERY(query_insert_into_playlist) << "Failed to insert playlist in TraktorTableModel:" << playlist_path; return; } // Get playlist id QSqlQuery id_query(m_database); id_query.prepare("select id from traktor_playlists where name=:path"); id_query.bindValue(":path", playlist_path); if (!id_query.exec()) { LOG_FAILED_QUERY(id_query) << "Could not get inserted playlist id for Traktor playlist::" << playlist_path; return; } //playlist_id = id_query.lastInsertId().toInt(); int playlist_id = -1; while (id_query.next()) { playlist_id = id_query.value(id_query.record().indexOf ("id")).toInt(); } int playlist_position = 1; while (!xml.atEnd() && !m_cancelImport) { //read next XML element xml.readNext(); if (xml.isStartElement()) { if (xml.name() == "PRIMARYKEY") { QXmlStreamAttributes attr = xml.attributes(); QString key = attr.value("KEY").toString(); QString type = attr.value("TYPE").toString(); if (type == "TRACK") { key.replace(QString(":"), QString("")); //TODO: IFDEF #if defined(__WINDOWS__) key.insert(1,":"); #else key.prepend("/Volumes/"); #endif //insert to database int track_id = -1; QSqlQuery finder_query(m_database); finder_query.prepare("select id from traktor_library where location=:path"); finder_query.bindValue(":path", key); if (!finder_query.exec()) { LOG_FAILED_QUERY(finder_query) << "Could not get track id:" << key; continue; } if (finder_query.next()) { track_id = finder_query.value(finder_query.record().indexOf ("id")).toInt(); } query_insert_into_playlisttracks.bindValue(":playlist_id", playlist_id); query_insert_into_playlisttracks.bindValue(":track_id", track_id); query_insert_into_playlisttracks.bindValue(":position", playlist_position++); if (!query_insert_into_playlisttracks.exec()) { LOG_FAILED_QUERY(query_insert_into_playlisttracks) << "trackid" << track_id << " with path " << key << "playlistname; " << playlist_path <<" with ID " << playlist_id; } } } } if (xml.isEndElement()) { //We leave the infinte loop, if twe have the closing "PLAYLIST" tag if (xml.name() == "PLAYLIST") { break; } } } }