Commit 7c02ae77 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

sqlite: Query: Provide order & group by as a separate parameter

This will help fix COUNT request results
parent 89c29403
......@@ -258,8 +258,8 @@ Query<IMedia> Album::tracks( const QueryParameters* params ) const
std::string req = "FROM " + policy::MediaTable::Name + " med "
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present != 0";
req += orderTracksBy( params );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ),
orderTracksBy( params ), m_id );
}
Query<IMedia> Album::tracks( GenrePtr genre, const QueryParameters* params ) const
......@@ -270,8 +270,8 @@ Query<IMedia> Album::tracks( GenrePtr genre, const QueryParameters* params ) con
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present != 0"
" AND genre_id = ?";
req += orderTracksBy( params );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id, genre->id() );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ),
orderTracksBy( params ), m_id, genre->id() );
}
std::vector<MediaPtr> Album::cachedTracks() const
......@@ -364,10 +364,12 @@ Query<IArtist> Album::artists( const QueryParameters* params ) const
{
std::string req = "FROM " + policy::ArtistTable::Name + " art "
"INNER JOIN AlbumArtistRelation aar ON aar.artist_id = art.id_artist "
"WHERE aar.album_id = ? ORDER BY art.name";
"WHERE aar.album_id = ?";
std::string orderBy = "ORDER BY art.name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Artist, IArtist>( m_ml, "art.*", std::move( req ), m_id );
orderBy += " DESC";
return make_query<Artist, IArtist>( m_ml, "art.*", std::move( req ),
std::move( orderBy ), m_id );
}
bool Album::addArtist( std::shared_ptr<Artist> artist )
......@@ -510,8 +512,8 @@ Query<IAlbum> Album::search( MediaLibraryPtr ml, const std::string& pattern,
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND is_present != 0";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "*", std::move( req ), pattern );
return make_query<Album, IAlbum>( ml, "*", std::move( req ),
orderBy( params ), pattern );
}
Query<IAlbum> Album::searchFromArtist( MediaLibraryPtr ml, const std::string& pattern,
......@@ -523,8 +525,8 @@ Query<IAlbum> Album::searchFromArtist( MediaLibraryPtr ml, const std::string& pa
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND is_present != 0 "
"AND artist_id = ?";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "*", std::move( req ), pattern, artistId );
return make_query<Album, IAlbum>( ml, "*", std::move( req ),
orderBy( params ), pattern, artistId );
}
Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params )
......@@ -533,29 +535,30 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, const Que
"INNER JOIN " + policy::AlbumTrackTable::Name + " att "
"ON att.album_id = alb.id_album "
"WHERE (att.artist_id = ? OR alb.artist_id = ?) "
"AND att.is_present != 0 "
"GROUP BY att.album_id ORDER BY ";
"AND att.is_present != 0 ";
std::string groupAndOrder = "GROUP BY att.album_id ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort )
{
case SortingCriteria::Alpha:
req += "title";
groupAndOrder += "title";
if ( desc == true )
req += " DESC";
groupAndOrder += " DESC";
break;
default:
// When listing albums of an artist, default order is by descending year (with album title
// discrimination in case 2+ albums went out the same year)
// This leads to DESC being used for "non-desc" case
if ( desc == true )
req += "release_year, title";
groupAndOrder += "release_year, title";
else
req += "release_year DESC, title";
groupAndOrder += "release_year DESC, title";
break;
}
return make_query<Album, IAlbum>( ml, "*", req, artistId, artistId );
return make_query<Album, IAlbum>( ml, "*", req, std::move( groupAndOrder ),
artistId, artistId );
}
Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params )
......@@ -563,8 +566,8 @@ Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, const Query
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = alb.id_album "
"WHERE att.genre_id = ? GROUP BY att.album_id";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), genreId );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
orderBy( params ), genreId );
}
Query<IAlbum> Album::searchFromGenre( MediaLibraryPtr ml, const std::string& pattern,
......@@ -576,8 +579,8 @@ Query<IAlbum> Album::searchFromGenre( MediaLibraryPtr ml, const std::string& pat
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND att.genre_id = ? GROUP BY att.album_id";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), pattern, genreId );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
orderBy( params ), pattern, genreId );
}
Query<IAlbum> Album::listAll( MediaLibraryPtr ml, const QueryParameters* params )
......@@ -588,30 +591,32 @@ Query<IAlbum> Album::listAll( MediaLibraryPtr ml, const QueryParameters* params
{
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::ArtistTable::Name + " art ON alb.artist_id = art.id_artist "
"WHERE alb.is_present != 0 "
"ORDER BY art.name ";
"WHERE alb.is_present != 0 ";
std::string orderBy = "ORDER BY art.name ";
if ( desc == true )
req += "DESC ";
req += ", alb.title";
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ) );
orderBy += "DESC ";
orderBy += ", alb.title";
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
std::move( orderBy ) );
}
if ( sort == SortingCriteria::PlayCount )
{
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::AlbumTrackTable::Name + " t ON alb.id_album = t.album_id "
"INNER JOIN " + policy::MediaTable::Name + " m ON t.media_id = m.id_media "
"WHERE alb.is_present != 0 "
"GROUP BY id_album "
"WHERE alb.is_present != 0 ";
std::string groupBy = "GROUP BY id_album "
"ORDER BY SUM(m.play_count) ";
if ( desc == false )
req += "DESC "; // Most played first by default
req += ", alb.title";
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ) );
groupBy += "DESC "; // Most played first by default
groupBy += ", alb.title";
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
std::move( groupBy ) );
}
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
" WHERE is_present != 0";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "*", std::move( req ) );
return make_query<Album, IAlbum>( ml, "*", std::move( req ),
orderBy( params ) );
}
}
......@@ -159,34 +159,36 @@ Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, const
{
std::string req = "FROM " + policy::MediaTable::Name + " m"
" INNER JOIN " + policy::AlbumTrackTable::Name + " t ON m.id_media = t.media_id"
" WHERE t.genre_id = ? AND m.is_present = 1 ORDER BY ";
" WHERE t.genre_id = ? AND m.is_present = 1";
std::string orderBy = "ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort )
{
case SortingCriteria::Duration:
req += "m.duration";
orderBy += "m.duration";
break;
case SortingCriteria::InsertionDate:
req += "m.insertion_date";
orderBy += "m.insertion_date";
break;
case SortingCriteria::ReleaseDate:
req += "m.release_date";
orderBy += "m.release_date";
break;
case SortingCriteria::Alpha:
req += "m.title";
orderBy += "m.title";
break;
default:
if ( desc == true )
req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
orderBy += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
else
req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
orderBy += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
break;
}
if ( desc == true )
req += " DESC";
return make_query<Media, IMedia>( ml, "m.*", std::move( req ), genreId );
orderBy += " DESC";
return make_query<Media, IMedia>( ml, "m.*", std::move( req ),
std::move( orderBy ), genreId );
}
GenrePtr AlbumTrack::genre()
......
......@@ -123,32 +123,34 @@ Query<IMedia> Artist::tracks( const QueryParameters* params ) const
"WHERE alb.artist_id = ? ";
}
req += "AND med.is_present != 0 ORDER BY ";
req += "AND med.is_present != 0";
std::string orderBy = "ORDER BY ";
switch ( sort )
{
case SortingCriteria::Duration:
req += "med.duration";
orderBy += "med.duration";
break;
case SortingCriteria::InsertionDate:
req += "med.insertion_date";
orderBy += "med.insertion_date";
break;
case SortingCriteria::ReleaseDate:
req += "med.release_date";
orderBy += "med.release_date";
break;
case SortingCriteria::Album:
if ( desc == true )
req += "atr.album_id DESC, atr.disc_number DESC, atr.track_number";
orderBy += "atr.album_id DESC, atr.disc_number DESC, atr.track_number";
else
req += "atr.album_id, atr.disc_number, atr.track_number";
orderBy += "atr.album_id, atr.disc_number, atr.track_number";
break;
default:
req += "med.title";
orderBy += "med.title";
break;
}
if ( desc == true )
req += " DESC";
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id );
orderBy += " DESC";
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ),
std::move( orderBy ), m_id );
}
Query<IMedia> Artist::searchTracks( const std::string& pattern, const QueryParameters* params ) const
......@@ -420,8 +422,8 @@ Query<IArtist> Artist::search( MediaLibraryPtr ml, const std::string& name,
std::string req = "FROM " + policy::ArtistTable::Name + " WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')"
"AND is_present != 0";
req += sortRequest( params );
return make_query<Artist, IArtist>( ml, "*", std::move( req ), name );
return make_query<Artist, IArtist>( ml, "*", std::move( req ),
sortRequest( params ), name );
}
Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
......@@ -434,8 +436,8 @@ Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
req += "nb_albums > 0";
req += " AND is_present != 0";
req += sortRequest( params );
return make_query<Artist, IArtist>( ml, "*", std::move( req ) );
return make_query<Artist, IArtist>( ml, "*", std::move( req ),
sortRequest( params ) );
}
Query<IArtist> Artist::searchByGenre( MediaLibraryPtr ml, const std::string& pattern,
......@@ -445,12 +447,14 @@ Query<IArtist> Artist::searchByGenre( MediaLibraryPtr ml, const std::string& pat
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
"WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')"
"AND att.genre_id = ? "
"GROUP BY att.artist_id "
"ORDER BY a.name";
"AND att.genre_id = ? ";
std::string groupBy = "GROUP BY att.artist_id "
"ORDER BY a.name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Artist, IArtist>( ml, "a.*", std::move( req ), pattern, genreId );
groupBy += " DESC";
return make_query<Artist, IArtist>( ml, "a.*", std::move( req ),
std::move( groupBy ), pattern, genreId );
}
std::string Artist::sortRequest( const QueryParameters* params )
......
......@@ -82,11 +82,12 @@ Query<IArtist> Genre::artists( const QueryParameters* params ) const
{
std::string req = "FROM " + policy::ArtistTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
"WHERE att.genre_id = ? GROUP BY att.artist_id"
" ORDER BY a.name";
"WHERE att.genre_id = ? GROUP BY att.artist_id";
std::string orderBy = "ORDER BY a.name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ), m_id );
orderBy += " DESC";
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ),
std::move( orderBy ), m_id );
}
Query<IArtist> Genre::searchArtists( const std::string& pattern,
......@@ -193,18 +194,22 @@ Query<IGenre> Genre::search( MediaLibraryPtr ml, const std::string& name,
const QueryParameters* params )
{
std::string req = "FROM " + policy::GenreTable::Name + " WHERE id_genre IN "
"(SELECT rowid FROM " + policy::GenreTable::Name + "Fts WHERE name MATCH '*' || ? || '*') ORDER BY name";
"(SELECT rowid FROM " + policy::GenreTable::Name + "Fts "
"WHERE name MATCH '*' || ? || '*')";
std::string orderBy = "ORDER BY name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Genre, IGenre>( ml, "*", req, name );
orderBy += " DESC";
return make_query<Genre, IGenre>( ml, "*", req, std::move( orderBy ), name );
}
Query<IGenre> Genre::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
std::string req = "FROM " + policy::GenreTable::Name + " ORDER BY name";
std::string req = "FROM " + policy::GenreTable::Name;
std::string orderBy = " ORDER BY name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Genre, IGenre>( ml, "*", std::move( req ) );
orderBy += " DESC";
return make_query<Genre, IGenre>( ml, "*", std::move( req ),
std::move( orderBy ) );
}
}
......@@ -69,7 +69,7 @@ Query<IMedia> Label::media()
static const std::string req = "FROM " + policy::MediaTable::Name + " f "
"INNER JOIN LabelFileRelation lfr ON lfr.media_id = f.id_media "
"WHERE lfr.label_id = ?";
return make_query<Media, IMedia>( m_ml, "f.*", req, m_id );
return make_query<Media, IMedia>( m_ml, "f.*", req, "", m_id );
}
LabelPtr Label::create( MediaLibraryPtr ml, const std::string& name )
......
......@@ -171,7 +171,7 @@ Query<ILabel> Media::labels() const
static const std::string req = "FROM " + policy::LabelTable::Name + " l "
"INNER JOIN LabelFileRelation lfr ON lfr.label_id = l.id_label "
"WHERE lfr.media_id = ?";
return make_query<Label, ILabel>( m_ml, "l.*", req, m_id );
return make_query<Label, ILabel>( m_ml, "l.*", req, "", m_id );
}
int Media::playCount() const
......@@ -264,7 +264,7 @@ Query<IVideoTrack> Media::videoTracks() const
{
static const std::string req = "FROM " + policy::VideoTrackTable::Name +
" WHERE media_id = ?";
return make_query<VideoTrack, IVideoTrack>( m_ml, "*", req, m_id );
return make_query<VideoTrack, IVideoTrack>( m_ml, "*", req, "", m_id );
}
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
......@@ -278,7 +278,7 @@ Query<IAudioTrack> Media::audioTracks() const
{
static const std::string req = "FROM " + policy::AudioTrackTable::Name +
" WHERE media_id = ?";
return make_query<AudioTrack, IAudioTrack>( m_ml, "*", req, m_id );
return make_query<AudioTrack, IAudioTrack>( m_ml, "*", req, "", m_id );
}
const std::string& Media::thumbnail() const
......@@ -502,9 +502,8 @@ Query<IMedia> Media::listAll( MediaLibraryPtr ml, IMedia::Type type,
" AND f.type = ?"
" AND f.is_present != 0";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ),
type, IFile::Type::Main );
sortRequest( params ), type, IFile::Type::Main );
}
int64_t Media::id() const
......@@ -674,8 +673,8 @@ Query<IMedia> Media::search( MediaLibraryPtr ml, const std::string& title,
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.type != ? AND m.type != ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, title, File::Type::Main,
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
title, File::Type::Main,
Media::Type::External, Media::Type::Stream );
}
......@@ -690,8 +689,8 @@ Query<IMedia> Media::search( MediaLibraryPtr ml, const std::string& title,
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.type = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, title, File::Type::Main, type );
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
title, File::Type::Main, type );
}
Query<IMedia> Media::searchAlbumTracks(MediaLibraryPtr ml, const std::string& pattern, int64_t albumId, const QueryParameters* params)
......@@ -706,8 +705,8 @@ Query<IMedia> Media::searchAlbumTracks(MediaLibraryPtr ml, const std::string& pa
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, albumId,
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
pattern, albumId,
File::Type::Main, Media::SubType::AlbumTrack );
}
......@@ -723,8 +722,8 @@ Query<IMedia> Media::searchArtistTracks(MediaLibraryPtr ml, const std::string& p
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, artistId,
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
pattern, artistId,
File::Type::Main, Media::SubType::AlbumTrack );
}
......@@ -740,8 +739,8 @@ Query<IMedia> Media::searchGenreTracks(MediaLibraryPtr ml, const std::string& pa
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, genreId,
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
pattern, genreId,
File::Type::Main, Media::SubType::AlbumTrack );
}
......@@ -758,8 +757,8 @@ Query<IMedia> Media::searchShowEpisodes(MediaLibraryPtr ml, const std::string& p
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, showId,
return make_query<Media, IMedia>( ml, "m.*", req, sortRequest( params ),
pattern, showId,
File::Type::Main, Media::SubType::ShowEpisode );
}
......@@ -772,26 +771,28 @@ Query<IMedia> Media::searchInPlaylist( MediaLibraryPtr ml, const std::string& pa
"WHERE pmr.playlist_id = ? AND m.is_present != 0 AND "
"m.id_media IN (SELECT rowid FROM " + policy::MediaTable::Name + "Fts "
"WHERE " + policy::MediaTable::Name + "Fts MATCH '*' || ? || '*')";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ), playlistId, pattern );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ),
sortRequest( params ), playlistId, pattern );
}
Query<IMedia> Media::fetchHistory( MediaLibraryPtr ml )
{
static const std::string req = "FROM " + policy::MediaTable::Name +
" WHERE last_played_date IS NOT NULL"
" AND type != ?"
" ORDER BY last_played_date DESC";
return make_query<Media, IMedia>( ml, "*", req, IMedia::Type::Stream );
" AND type != ?";
return make_query<Media, IMedia>( ml, "*", req,
"ORDER BY last_played_date DESC",
IMedia::Type::Stream );
}
Query<IMedia> Media::fetchStreamHistory(MediaLibraryPtr ml)
{
static const std::string req = "FROM " + policy::MediaTable::Name +
" WHERE last_played_date IS NOT NULL"
" AND type = ?"
" ORDER BY last_played_date DESC";
return make_query<Media, IMedia>( ml, "*", req, IMedia::Type::Stream );
" AND type = ?";
return make_query<Media, IMedia>( ml, "*", req,
"ORDER BY last_played_date DESC",
IMedia::Type::Stream );
}
void Media::clearHistory( MediaLibraryPtr ml )
......
......@@ -1313,7 +1313,7 @@ Query<IFolder> MediaLibrary::entryPoints() const
{
static const std::string req = "FROM " + policy::FolderTable::Name + " WHERE parent_id IS NULL"
" AND is_blacklisted = 0";
return make_query<Folder, IFolder>( this, "*", req );
return make_query<Folder, IFolder>( this, "*", req, "" );
}
FolderPtr MediaLibrary::folder( const std::string& mrl ) const
......
......@@ -114,10 +114,10 @@ Query<IMedia> Playlist::media() const
{
static const std::string req = "FROM " + policy::MediaTable::Name + " m "
"LEFT JOIN PlaylistMediaRelation pmr ON pmr.media_id = m.id_media "
"WHERE pmr.playlist_id = ? AND m.is_present != 0 "
"ORDER BY pmr.position";
"WHERE pmr.playlist_id = ? AND m.is_present != 0 ";
curateNullMediaID();
return make_query<Media, IMedia>( m_ml, "m.*", req, m_id );
return make_query<Media, IMedia>( m_ml, "m.*", req, "ORDER BY pmr.position",
m_id );
}
Query<IMedia> Playlist::searchMedia( const std::string& pattern,
......@@ -296,15 +296,14 @@ Query<IPlaylist> Playlist::search( MediaLibraryPtr ml, const std::string& name,
{
std::string req = "FROM " + policy::PlaylistTable::Name + " WHERE id_playlist IN "
"(SELECT rowid FROM " + policy::PlaylistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')";
req += sortRequest( params );
return make_query<Playlist, IPlaylist>( ml, "*", req, name );
return make_query<Playlist, IPlaylist>( ml, "*", req, sortRequest( params ),
name );
}
Query<IPlaylist> Playlist::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
std::string req = "FROM " + policy::PlaylistTable::Name;
req += sortRequest( params );
return make_query<Playlist, IPlaylist>( ml, "*", req );
return make_query<Playlist, IPlaylist>( ml, "*", req, sortRequest( params ) );
}
void Playlist::clearExternalPlaylistContent(MediaLibraryPtr ml)
......
......@@ -141,25 +141,26 @@ Query<IMedia> Show::episodes( const QueryParameters* params ) const
{
std::string req = "FROM " + policy::MediaTable::Name + " med "
" INNER JOIN " + policy::ShowEpisodeTable::Name + " ep ON ep.media_id = med.id_media "
+ " WHERE ep.show_id = ? ORDER BY ";
+ " WHERE ep.show_id = ?";
std::string orderBy = " ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort )
{
case SortingCriteria::Alpha:
req += "med.name";
orderBy += "med.name";
if ( desc == true )
req += " DESC";
orderBy += " DESC";
break;
default:
if ( desc == true )
req += "ep.season_number DESC, ep.episode_number DESC";
orderBy += "ep.season_number DESC, ep.episode_number DESC";
else
req += "ep.season_number, ep.episode_number";
orderBy += "ep.season_number, ep.episode_number";
break;
}
return make_query<Media, IMedia>( m_ml, "*", req, m_id );
return make_query<Media, IMedia>( m_ml, "*", req, std::move( orderBy), m_id );
}
Query<IMedia> Show::searchEpisodes( const std::string& pattern,
......@@ -226,8 +227,7 @@ std::shared_ptr<Show> Show::create( MediaLibraryPtr ml, const std::string& name
Query<IShow> Show::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
std::string req = "FROM " + policy::ShowTable::Name;
req += orderBy( params );
return make_query<Show, IShow>( ml, "*", std::move( req ) );
return make_query<Show, IShow>( ml, "*", std::move( req ), orderBy( params ) );
}
std::string Show::orderBy( const QueryParameters* params )
......@@ -257,8 +257,7 @@ Query<IShow> Show::search( MediaLibraryPtr ml, const std::string& pattern,
std::string req = "FROM " + policy::ShowTable::Name + " WHERE id_show IN"
"(SELECT rowid FROM " + policy::ShowTable::Name + "Fts WHERE " +
policy::ShowTable::Name + "Fts MATCH '*' || ? || '*')";
req += orderBy( params );
return make_query<Show, IShow>( ml, "*", req, pattern );
return make_query<Show, IShow>( ml, "*", req, orderBy( params ), pattern );
}
}
......@@ -41,10 +41,11 @@ public:
template <typename... Params>
SqliteQuery( MediaLibraryPtr ml, std::string field, std::string base,
Params&&... params )
std::string groupAndOrderBy, Params&&... params )
: m_ml( ml )
, m_field( std::move( field ) )
, m_base( std::move( base ) )
, m_groupAndOrderBy( std::move( groupAndOrderBy ) )
, m_params( std::forward<Params>( params )... )
, m_count( 0 )
, m_hasCount( false )
......@@ -76,13 +77,15 @@ public:
{
if ( nbItems == 0 && offset == 0 )
return all();
const std::string req = "SELECT " + m_field + " " + m_base + " LIMIT ? OFFSET ?";
const std::string req = "SELECT " + m_field + " " + m_base + " " +
m_groupAndOrderBy + " LIMIT ? OFFSET ?";
return Impl::template fetchAll<Intf>( m_ml, req, m_params, nbItems, offset );
}
virtual Result all() override
{
const std::string req = "SELECT " + m_field + " " + m_base;
const std::string req = "SELECT " + m_field + " " + m_base + " " +
m_groupAndOrderBy;
return Impl::template fetchAll<Intf>( m_ml, req, m_params );
}
......@@ -90,6 +93,7 @@ private:
MediaLibraryPtr m_ml;
std::string m_field;
std::string m_base;
std::string m_groupAndOrderBy;
std::tuple<typename std::decay<RequestParams>::type...> m_params;
size_t m_count;
......@@ -98,11 +102,12 @@ private:
template <typename Impl, typename Intf = Impl, typename... Args>
Query<Intf> make_query( MediaLibraryPtr ml, std::string field, std::string base,
Args&&... args )
std::string orderAndGroupBy, Args&&... args )
{
return std::unique_ptr<IQuery<Intf>>(
new SqliteQuery<Impl, Intf, Args...>( ml, std::move( field ),
std::move( base ),
std::move( orderAndGroupBy ),
std::forward<Args>( args )... )
);
}
......
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