Skip to content

Commit

Permalink
CoverArtDAO - add method to save multi cover arts at once (from qlist)
Browse files Browse the repository at this point in the history
  • Loading branch information
cardinot committed Jul 29, 2014
1 parent 2b579de commit 5fadd65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/library/dao/coverartdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,44 @@ int CoverArtDAO::saveCoverArt(QString coverLocation, QString md5Hash) {
return coverId;
}

QList<int> CoverArtDAO::saveCoverArt(QList<QPair<QString, QString> > covers) {

This comment has been minimized.

Copy link
@kain88-de

kain88-de Jul 31, 2014

Use a QSet to avoid duplicates

if (covers.isEmpty()) {
return QList<int>();
}

// it'll be used to avoid writing a new ID for
// rows which have the same md5 (not changed).
QString selectCoverId = QString("SELECT id FROM cover_art WHERE md5='%1'");

// preparing query to insert multi rows
QString sQuery;
sQuery = QString("INSERT OR REPLACE INTO cover_art ('id', 'location', 'md5') "
"SELECT (%1) AS 'id', '%2' AS 'location', '%3' AS 'md5' ")
.arg(selectCoverId.arg(covers.first().second))
.arg(covers.first().first)
.arg(covers.first().second);
for (int i=1; i<covers.size(); i++) {
sQuery = sQuery % QString("UNION SELECT (%1), '%2', '%3'")
.arg(selectCoverId.arg(covers.at(i).second))
.arg(covers.at(i).first)
.arg(covers.at(i).second);
}
QSqlQuery query(m_database);
if (!query.exec(sQuery)) {
LOG_FAILED_QUERY(query) << "Failed to save multiple covers!";
return QList<int>();
}

// getting the last inserted cover id's
QList<int> coverIds;
for (int index=0; index<covers.size(); index++) {
int coverId = getCoverArtId(covers.at(index).second);
coverIds.append(coverId);
}

return coverIds;
}

void CoverArtDAO::deleteUnusedCoverArts() {
QSqlQuery query(m_database);

Expand Down
1 change: 1 addition & 0 deletions src/library/dao/coverartdao.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CoverArtDAO : public DAO {
void deleteUnusedCoverArts();
int getCoverArtId(QString md5Hash);
int saveCoverArt(QString coverLocation, QString md5Hash);
QList<int> saveCoverArt(QList<QPair<QString, QString> > covers);

struct CoverArtInfo {
int trackId;
Expand Down

0 comments on commit 5fadd65

Please sign in to comment.