Commit 8e1967d7 authored by Alexander Stippich's avatar Alexander Stippich
Browse files

implement loading tracks from genre

parent 1691274e
......@@ -47,6 +47,7 @@ public:
mSelectTrackQuery(mTracksDatabase), mSelectAlbumIdFromTitleQuery(mTracksDatabase),
mInsertAlbumQuery(mTracksDatabase), mSelectTrackIdFromTitleAlbumIdArtistQuery(mTracksDatabase),
mInsertTrackQuery(mTracksDatabase), mSelectTracksFromArtist(mTracksDatabase),
mSelectTracksFromGenre(mTracksDatabase),
mSelectTrackFromIdQuery(mTracksDatabase), mSelectRadioFromIdQuery(mTracksDatabase),
mSelectCountAlbumsForArtistQuery(mTracksDatabase),
mSelectTrackIdFromTitleArtistAlbumTrackDiscNumberQuery(mTracksDatabase),
......@@ -109,6 +110,8 @@ public:
QSqlQuery mSelectTracksFromArtist;
QSqlQuery mSelectTracksFromGenre;
QSqlQuery mSelectTrackFromIdQuery;
QSqlQuery mSelectRadioFromIdQuery;
......@@ -723,6 +726,24 @@ DataTypes::ListTrackDataType DatabaseInterface::tracksDataFromAuthor(const QStri
return allTracks;
}
DataTypes::ListTrackDataType DatabaseInterface::tracksDataFromGenre(const QString &genre)
{
auto allTracks = DataTypes::ListTrackDataType{};
auto transactionResult = startTransaction();
if (!transactionResult) {
return allTracks;
}
allTracks = internalTracksFromGenre(genre);
transactionResult = finishTransaction();
if (!transactionResult) {
return allTracks;
}
return allTracks;
}
DataTypes::TrackDataType DatabaseInterface::trackDataFromDatabaseId(qulonglong id)
{
auto result = DataTypes::TrackDataType();
......@@ -5695,6 +5716,135 @@ void DatabaseInterface::initRequest()
}
}
{
auto selectTracksFromGenreQueryText = QStringLiteral("SELECT "
"tracks.`ID`, "
"tracks.`Title`, "
"album.`ID`, "
"tracks.`ArtistName`, "
"( "
"SELECT "
"COUNT(DISTINCT tracksFromAlbum1.`ArtistName`) "
"FROM "
"`Tracks` tracksFromAlbum1 "
"WHERE "
"tracksFromAlbum1.`AlbumTitle` = album.`Title` AND "
"(tracksFromAlbum1.`AlbumArtistName` = album.`ArtistName` OR "
"(tracksFromAlbum1.`AlbumArtistName` IS NULL AND "
"album.`ArtistName` IS NULL "
") "
") AND "
"tracksFromAlbum1.`AlbumPath` = album.`AlbumPath` "
") AS ArtistsCount, "
"( "
"SELECT "
"GROUP_CONCAT(tracksFromAlbum2.`ArtistName`) "
"FROM "
"`Tracks` tracksFromAlbum2 "
"WHERE "
"tracksFromAlbum2.`AlbumTitle` = album.`Title` AND "
"(tracksFromAlbum2.`AlbumArtistName` = album.`ArtistName` OR "
"(tracksFromAlbum2.`AlbumArtistName` IS NULL AND "
"album.`ArtistName` IS NULL "
") "
") AND "
"tracksFromAlbum2.`AlbumPath` = album.`AlbumPath` "
") AS AllArtists, "
"tracks.`AlbumArtistName`, "
"tracksMapping.`FileName`, "
"tracksMapping.`FileModifiedTime`, "
"tracks.`TrackNumber`, "
"tracks.`DiscNumber`, "
"tracks.`Duration`, "
"tracks.`AlbumTitle`, "
"tracks.`Rating`, "
"album.`CoverFileName`, "
"("
"SELECT "
"COUNT(DISTINCT tracks2.DiscNumber) <= 1 "
"FROM "
"`Tracks` tracks2 "
"WHERE "
"tracks2.`AlbumTitle` = album.`Title` AND "
"(tracks2.`AlbumArtistName` = album.`ArtistName` OR "
"(tracks2.`AlbumArtistName` IS NULL AND "
"album.`ArtistName` IS NULL"
")"
") AND "
"tracks2.`AlbumPath` = album.`AlbumPath` "
") as `IsSingleDiscAlbum`, "
"trackGenre.`Name`, "
"trackComposer.`Name`, "
"trackLyricist.`Name`, "
"tracks.`Comment`, "
"tracks.`Year`, "
"tracks.`Channels`, "
"tracks.`BitRate`, "
"tracks.`SampleRate`, "
"tracks.`HasEmbeddedCover`, "
"tracksMapping.`ImportDate`, "
"tracksMapping.`FirstPlayDate`, "
"tracksMapping.`LastPlayDate`, "
"tracksMapping.`PlayCounter`, "
"tracksMapping.`PlayCounter` / (strftime('%s', 'now') - tracksMapping.`FirstPlayDate`) as PlayFrequency, "
"( "
"SELECT tracksCover.`FileName` "
"FROM "
"`Tracks` tracksCover "
"WHERE "
"tracksCover.`HasEmbeddedCover` = 1 AND "
"tracksCover.`AlbumTitle` = album.`Title` AND "
"(tracksCover.`AlbumArtistName` = album.`ArtistName` OR "
"(tracksCover.`AlbumArtistName` IS NULL AND "
"album.`ArtistName` IS NULL "
") "
") AND "
"tracksCover.`AlbumPath` = album.`AlbumPath` "
") as EmbeddedCover "
"FROM "
"`Tracks` tracks, "
"`TracksData` tracksMapping "
"LEFT JOIN "
"`Albums` album "
"ON "
"tracks.`AlbumTitle` = album.`Title` AND "
"(tracks.`AlbumArtistName` = album.`ArtistName` OR tracks.`AlbumArtistName` IS NULL ) AND "
"tracks.`AlbumPath` = album.`AlbumPath` "
"LEFT JOIN `Composer` trackComposer ON trackComposer.`Name` = tracks.`Composer` "
"LEFT JOIN `Lyricist` trackLyricist ON trackLyricist.`Name` = tracks.`Lyricist` "
"LEFT JOIN `Genre` trackGenre ON trackGenre.`Name` = tracks.`Genre` "
"WHERE "
"tracks.`Genre` = :genre AND "
"tracksMapping.`FileName` = tracks.`FileName` AND "
"tracks.`Priority` = ("
" SELECT "
" MIN(`Priority`) "
" FROM "
" `Tracks` tracks2 "
" WHERE "
" tracks.`Title` = tracks2.`Title` AND "
" (tracks.`ArtistName` IS NULL OR tracks.`ArtistName` = tracks2.`ArtistName`) AND "
" (tracks.`AlbumTitle` IS NULL OR tracks.`AlbumTitle` = tracks2.`AlbumTitle`) AND "
" (tracks.`AlbumArtistName` IS NULL OR tracks.`AlbumArtistName` = tracks2.`AlbumArtistName`) AND "
" (tracks.`AlbumPath` IS NULL OR tracks.`AlbumPath` = tracks2.`AlbumPath`)"
")"
"ORDER BY "
"album.`Title` ASC, "
"tracks.`DiscNumber` ASC, "
"tracks.`TrackNumber` ASC, "
"tracks.`Title` ASC"
"");
auto result = prepareQuery(d->mSelectTracksFromGenre, selectTracksFromGenreQueryText);
if (!result) {
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::initRequest" << d->mSelectTracksFromGenre.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::initRequest" << d->mSelectTracksFromGenre.lastError();
Q_EMIT databaseError();
}
}
{
auto selectAlbumIdsFromArtistQueryText = QStringLiteral("SELECT "
"album.`ID` "
......@@ -7477,6 +7627,36 @@ DataTypes::ListTrackDataType DatabaseInterface::internalTracksFromAuthor(const Q
return allTracks;
}
DataTypes::ListTrackDataType DatabaseInterface::internalTracksFromGenre(const QString &genre)
{
auto allTracks = DataTypes::ListTrackDataType{};
d->mSelectTracksFromGenre.bindValue(QStringLiteral(":genre"), genre);
auto result = execQuery(d->mSelectTracksFromGenre);
if (!result || !d->mSelectTracksFromGenre.isSelect() || !d->mSelectTracksFromGenre.isActive()) {
Q_EMIT databaseError();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::tracksFromGenre" << d->mSelectTracksFromGenre.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::tracksFromGenre" << d->mSelectTracksFromGenre.boundValues();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::tracksFromGenre" << d->mSelectTracksFromGenre.lastError();
return allTracks;
}
while (d->mSelectTracksFromGenre.next()) {
const auto &currentRecord = d->mSelectTracksFromGenre.record();
allTracks.push_back(buildTrackDataFromDatabaseRecord(currentRecord));
}
d->mSelectTracksFromGenre.finish();
return allTracks;
}
QList<qulonglong> DatabaseInterface::internalAlbumIdsFromAuthor(const QString &ArtistName)
{
auto allAlbumIds = QList<qulonglong>();
......
......@@ -82,6 +82,8 @@ public:
DataTypes::ListTrackDataType tracksDataFromAuthor(const QString &artistName);
DataTypes::ListTrackDataType tracksDataFromGenre(const QString &genre);
DataTypes::TrackDataType trackDataFromDatabaseId(qulonglong id);
DataTypes::TrackDataType trackDataFromDatabaseIdAndUrl(qulonglong id, const QUrl &trackUrl);
......@@ -192,6 +194,8 @@ private:
DataTypes::ListTrackDataType internalTracksFromAuthor(const QString &artistName);
DataTypes::ListTrackDataType internalTracksFromGenre(const QString &genre);
QList<qulonglong> internalAlbumIdsFromAuthor(const QString &artistName);
void initDatabase();
......
......@@ -229,9 +229,11 @@ void TracksListener::newEntryInList(qulonglong newDatabaseId,
case ElisaUtils::Album:
newAlbumInList(newDatabaseId, entryTitle);
break;
case ElisaUtils::Genre:
newGenreInList(newDatabaseId, entryTitle);
break;
case ElisaUtils::Lyricist:
case ElisaUtils::Composer:
case ElisaUtils::Genre:
case ElisaUtils::Unknown:
break;
}
......@@ -301,4 +303,19 @@ void TracksListener::newArtistInList(qulonglong newDatabaseId, const QString &ar
Q_EMIT tracksListAdded(newDatabaseId, artist, ElisaUtils::Artist, newTracks);
}
void TracksListener::newGenreInList(qulonglong newDatabaseId, const QString &entryTitle)
{
auto newTracks = d->mDatabase->tracksDataFromGenre(entryTitle);
if (newTracks.isEmpty()) {
return;
}
for (const auto &oneTrack : newTracks) {
d->mTracksByIdSet.insert(oneTrack.databaseId());
}
Q_EMIT tracksListAdded(newDatabaseId, entryTitle, ElisaUtils::Genre, newTracks);
}
#include "moc_trackslistener.cpp"
......@@ -78,6 +78,8 @@ private:
void newArtistInList(qulonglong newDatabaseId, const QString &artist);
void newGenreInList(qulonglong newDatabaseId, const QString &entryTitle);
void newAlbumInList(qulonglong newDatabaseId,
const QString &entryTitle);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment