Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the track order during drag and drop Lp1829601 #2237

Merged
merged 13 commits into from
Oct 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use a temp table to keep sorting unchanged in TrackDAO::getTrackIds()
  • Loading branch information
daschuer committed Aug 12, 2019
commit 0e9b7272de191aed85cb7ea8aae2b539860a7a13
53 changes: 43 additions & 10 deletions src/library/dao/trackdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,45 @@ QList<TrackId> TrackDAO::getTrackIds(const QList<QFileInfo>& files) {
QList<TrackId> trackIds;
trackIds.reserve(files.size());

// Create a temporary database of the paths of all the imported tracks.
QSqlQuery query(m_database);
{
QStringList pathList;
pathList.reserve(files.size());
for (const auto& file: files) {
pathList << file.absoluteFilePath();
}
query.prepare(QString("SELECT library.id FROM library INNER JOIN "
"track_locations ON library.location = track_locations.id "
"WHERE track_locations.location in (%1)").arg(
SqlStringFormatter::formatList(m_database, pathList)));
query.prepare(
"CREATE TEMP TABLE playlist_import "
"(location varchar (512))");
if (!query.exec()) {
LOG_FAILED_QUERY(query);
return trackIds;
}

QStringList pathList;
pathList.reserve(files.size());
for (const auto& file: files) {
pathList << "(" + SqlStringFormatter::format(m_database, file.absoluteFilePath()) + ")";
}

// Add all the track paths temporary to this database.
query.prepare(
"INSERT INTO playlist_import (location) "
"VALUES " + pathList.join(','));
if (!query.exec()) {
LOG_FAILED_QUERY(query);
}

query.prepare(
"SELECT library.id FROM playlist_import "
"INNER JOIN track_locations ON playlist_import.location = track_locations.location "
"INNER JOIN library ON library.location = track_locations.id "
// the order by clause enforces the native sorting which is used anyway
// hopefully optimized away. TODO() verify.
"ORDER BY playlist_import.ROWID");

// Old syntax for a shorter but less readable query. TODO() check performance gain
// query.prepare(
// "SELECT library.id FROM playlist_import, "
// "track_locations, library WHERE library.location = track_locations.id "
// "AND playlist_import.location = track_locations.location");
// "ORDER BY playlist_import.ROWID");

if (query.exec()) {
const int idColumn = query.record().indexOf("id");
while (query.next()) {
Expand All @@ -164,6 +191,12 @@ QList<TrackId> TrackDAO::getTrackIds(const QList<QFileInfo>& files) {
LOG_FAILED_QUERY(query);
}

// Drop the temporary playlist-import table.
query.prepare("DROP TABLE IF EXISTS playlist_import");
if (!query.exec()) {
LOG_FAILED_QUERY(query);
}

return trackIds;
}

Expand Down