Commit 9bc488b0 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Use a QueryParameter object instead of individual sort & desc parameters

parent b5a23750
...@@ -44,13 +44,13 @@ public: ...@@ -44,13 +44,13 @@ public:
/** /**
* @brief tracks fetches album tracks from the database * @brief tracks fetches album tracks from the database
*/ */
virtual Query<IMedia> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> tracks( const QueryParameters* params = nullptr ) const = 0;
/** /**
* @brief tracks fetches album tracks, filtered by genre * @brief tracks fetches album tracks, filtered by genre
* @param genre A musical genre. Only tracks of this genre will be returned * @param genre A musical genre. Only tracks of this genre will be returned
* @return * @return
*/ */
virtual Query<IMedia> tracks( GenrePtr genre, SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> tracks( GenrePtr genre, const QueryParameters* params = nullptr ) const = 0;
/** /**
* @brief albumArtist Returns the album main artist (generally tagged as album-artist) * @brief albumArtist Returns the album main artist (generally tagged as album-artist)
*/ */
......
...@@ -42,8 +42,8 @@ public: ...@@ -42,8 +42,8 @@ public:
* This will return all albums by this artist, and all album the artist * This will return all albums by this artist, and all album the artist
* appeared on, even if they are not the main artist (or AlbumArtist) * appeared on, even if they are not the main artist (or AlbumArtist)
*/ */
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> media( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> media( const QueryParameters* params = nullptr ) const = 0;
virtual const std::string& artworkMrl() const = 0; virtual const std::string& artworkMrl() const = 0;
virtual const std::string& musicBrainzId() const = 0; virtual const std::string& musicBrainzId() const = 0;
/** /**
......
...@@ -34,9 +34,9 @@ public: ...@@ -34,9 +34,9 @@ public:
virtual int64_t id() const = 0; virtual int64_t id() const = 0;
virtual const std::string& name() const = 0; virtual const std::string& name() const = 0;
virtual uint32_t nbTracks() const = 0; virtual uint32_t nbTracks() const = 0;
virtual Query<IArtist> artists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IArtist> artists( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> tracks( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
}; };
} }
...@@ -74,6 +74,13 @@ enum class SortingCriteria ...@@ -74,6 +74,13 @@ enum class SortingCriteria
TrackNumber, TrackNumber,
}; };
struct QueryParameters
{
SortingCriteria sort;
bool desc;
};
enum class InitializeResult enum class InitializeResult
{ {
//< Everything worked out fine //< Everything worked out fine
...@@ -252,10 +259,10 @@ class IMediaLibrary ...@@ -252,10 +259,10 @@ class IMediaLibrary
virtual MediaPtr media( int64_t mediaId ) const = 0; virtual MediaPtr media( int64_t mediaId ) const = 0;
virtual MediaPtr media( const std::string& mrl ) const = 0; virtual MediaPtr media( const std::string& mrl ) const = 0;
virtual MediaPtr addMedia( const std::string& mrl ) = 0; virtual MediaPtr addMedia( const std::string& mrl ) = 0;
virtual Query<IMedia> audioFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> audioFiles( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> videoFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IMedia> videoFiles( const QueryParameters* params = nullptr ) const = 0;
virtual AlbumPtr album( int64_t id ) const = 0; virtual AlbumPtr album( int64_t id ) const = 0;
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
virtual ShowPtr show( const std::string& name ) const = 0; virtual ShowPtr show( const std::string& name ) const = 0;
virtual MoviePtr movie( const std::string& title ) const = 0; virtual MoviePtr movie( const std::string& title ) const = 0;
virtual ArtistPtr artist( int64_t id ) const = 0; virtual ArtistPtr artist( int64_t id ) const = 0;
...@@ -270,20 +277,19 @@ class IMediaLibrary ...@@ -270,20 +277,19 @@ class IMediaLibrary
* @param desc If true, the provided sorting criteria will be reversed. * @param desc If true, the provided sorting criteria will be reversed.
*/ */
virtual Query<IArtist> artists( bool includeAll, virtual Query<IArtist> artists( bool includeAll,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0;
/** /**
* @brief genres Return the list of music genres * @brief genres Return the list of music genres
* @param sort A sorting criteria. So far, this is ignored, and artists are sorted by lexial order * @param sort A sorting criteria. So far, this is ignored, and artists are sorted by lexial order
* @param desc If true, the provided sorting criteria will be reversed. * @param desc If true, the provided sorting criteria will be reversed.
*/ */
virtual Query<IGenre> genres( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; virtual Query<IGenre> genres( const QueryParameters* params = nullptr ) const = 0;
virtual GenrePtr genre( int64_t id ) const = 0; virtual GenrePtr genre( int64_t id ) const = 0;
/*** /***
* Playlists * Playlists
*/ */
virtual PlaylistPtr createPlaylist( const std::string& name ) = 0; virtual PlaylistPtr createPlaylist( const std::string& name ) = 0;
virtual Query<IPlaylist> playlists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) = 0; virtual Query<IPlaylist> playlists( const QueryParameters* params = nullptr ) = 0;
virtual PlaylistPtr playlist( int64_t id ) const = 0; virtual PlaylistPtr playlist( int64_t id ) const = 0;
virtual bool deletePlaylist( int64_t playlistId ) = 0; virtual bool deletePlaylist( int64_t playlistId ) = 0;
...@@ -307,21 +313,17 @@ class IMediaLibrary ...@@ -307,21 +313,17 @@ class IMediaLibrary
* Search * Search
*/ */
virtual MediaSearchAggregate searchMedia( const std::string& pattern, virtual MediaSearchAggregate searchMedia( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0;
virtual Query<IPlaylist> searchPlaylists( const std::string& name, virtual Query<IPlaylist> searchPlaylists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0;
virtual Query<IAlbum> searchAlbums( const std::string& pattern, virtual Query<IAlbum> searchAlbums( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0; virtual Query<IGenre> searchGenre( const std::string& genre,
virtual Query<IGenre> searchGenre( const std::string& genre, SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0; const QueryParameters* params = nullptr ) const = 0;
virtual Query<IArtist> searchArtists( const std::string& name, virtual Query<IArtist> searchArtists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0;
virtual SearchAggregate search( const std::string& pattern, virtual SearchAggregate search( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default, const QueryParameters* params = nullptr ) const = 0;
bool desc = false ) const = 0;
/** /**
* @brief discover Launch a discovery on the provided entry point. * @brief discover Launch a discovery on the provided entry point.
......
...@@ -190,9 +190,11 @@ bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin orig ...@@ -190,9 +190,11 @@ bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin orig
return true; return true;
} }
std::string Album::orderTracksBy( SortingCriteria sort, bool desc ) std::string Album::orderTracksBy( const QueryParameters* params = nullptr )
{ {
std::string req = " ORDER BY "; std::string req = " ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort ) switch ( sort )
{ {
case SortingCriteria::Alpha: case SortingCriteria::Alpha:
...@@ -217,9 +219,11 @@ std::string Album::orderTracksBy( SortingCriteria sort, bool desc ) ...@@ -217,9 +219,11 @@ std::string Album::orderTracksBy( SortingCriteria sort, bool desc )
return req; return req;
} }
std::string Album::orderBy( SortingCriteria sort, bool desc ) std::string Album::orderBy( const QueryParameters* params )
{ {
std::string req = " ORDER BY "; std::string req = " ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort ) switch ( sort )
{ {
case SortingCriteria::ReleaseDate: case SortingCriteria::ReleaseDate:
...@@ -247,18 +251,18 @@ std::string Album::orderBy( SortingCriteria sort, bool desc ) ...@@ -247,18 +251,18 @@ std::string Album::orderBy( SortingCriteria sort, bool desc )
return req; return req;
} }
Query<IMedia> Album::tracks( SortingCriteria sort, bool desc ) const Query<IMedia> Album::tracks( const QueryParameters* params ) const
{ {
// This doesn't return the cached version, because it would be fairly complicated, if not impossible or // This doesn't return the cached version, because it would be fairly complicated, if not impossible or
// counter productive, to maintain a cache that respects all orderings. // counter productive, to maintain a cache that respects all orderings.
std::string req = "FROM " + policy::MediaTable::Name + " med " std::string req = "FROM " + policy::MediaTable::Name + " med "
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media " " INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present != 0"; " WHERE att.album_id = ? AND med.is_present != 0";
req += orderTracksBy( sort, desc ); 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 ), m_id );
} }
Query<IMedia> Album::tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const Query<IMedia> Album::tracks( GenrePtr genre, const QueryParameters* params ) const
{ {
if ( genre == nullptr ) if ( genre == nullptr )
return {}; return {};
...@@ -266,7 +270,7 @@ Query<IMedia> Album::tracks( GenrePtr genre, SortingCriteria sort, bool desc ) c ...@@ -266,7 +270,7 @@ Query<IMedia> Album::tracks( GenrePtr genre, SortingCriteria sort, bool desc ) c
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media " " INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present != 0" " WHERE att.album_id = ? AND med.is_present != 0"
" AND genre_id = ?"; " AND genre_id = ?";
req += orderTracksBy( sort, desc ); 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 ), m_id, genre->id() );
} }
...@@ -274,7 +278,7 @@ std::vector<MediaPtr> Album::cachedTracks() const ...@@ -274,7 +278,7 @@ std::vector<MediaPtr> Album::cachedTracks() const
{ {
auto lock = m_tracks.lock(); auto lock = m_tracks.lock();
if ( m_tracks.isCached() == false ) if ( m_tracks.isCached() == false )
m_tracks = tracks( SortingCriteria::Default, false )->all(); m_tracks = tracks( nullptr )->all();
return m_tracks.get(); return m_tracks.get();
} }
...@@ -493,18 +497,18 @@ std::shared_ptr<Album> Album::createUnknownAlbum( MediaLibraryPtr ml, const Arti ...@@ -493,18 +497,18 @@ std::shared_ptr<Album> Album::createUnknownAlbum( MediaLibraryPtr ml, const Arti
} }
Query<IAlbum> Album::search( MediaLibraryPtr ml, const std::string& pattern, Query<IAlbum> Album::search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc ) const QueryParameters* params )
{ {
std::string req = "FROM " + policy::AlbumTable::Name + " alb " std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"WHERE id_album IN " "WHERE id_album IN "
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " + "(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')" policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND is_present != 0"; "AND is_present != 0";
req += orderBy( sort, desc ); req += orderBy( params );
return make_query<Album, IAlbum>( ml, "*", std::move( req ), pattern ); return make_query<Album, IAlbum>( ml, "*", std::move( req ), pattern );
} }
Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc ) Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params )
{ {
std::string req = "FROM " + policy::AlbumTable::Name + " alb " std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att " "INNER JOIN " + policy::AlbumTrackTable::Name + " att "
...@@ -512,6 +516,8 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCr ...@@ -512,6 +516,8 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCr
"WHERE (att.artist_id = ? OR alb.artist_id = ?) " "WHERE (att.artist_id = ? OR alb.artist_id = ?) "
"AND att.is_present != 0 " "AND att.is_present != 0 "
"GROUP BY att.album_id ORDER BY "; "GROUP BY att.album_id ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort ) switch ( sort )
{ {
case SortingCriteria::Alpha: case SortingCriteria::Alpha:
...@@ -533,17 +539,19 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCr ...@@ -533,17 +539,19 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCr
return make_query<Album, IAlbum>( ml, "*", req, artistId, artistId ); return make_query<Album, IAlbum>( ml, "*", req, artistId, artistId );
} }
Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc) Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params )
{ {
std::string req = "FROM " + policy::AlbumTable::Name + " alb " std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = alb.id_album " "INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = alb.id_album "
"WHERE att.genre_id = ? GROUP BY att.album_id"; "WHERE att.genre_id = ? GROUP BY att.album_id";
req += orderBy( sort, desc ); req += orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), genreId ); return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), genreId );
} }
Query<IAlbum> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc ) Query<IAlbum> Album::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{ {
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
if ( sort == SortingCriteria::Artist ) if ( sort == SortingCriteria::Artist )
{ {
std::string req = "FROM " + policy::AlbumTable::Name + " alb " std::string req = "FROM " + policy::AlbumTable::Name + " alb "
...@@ -570,7 +578,7 @@ Query<IAlbum> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort, bool des ...@@ -570,7 +578,7 @@ Query<IAlbum> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort, bool des
} }
std::string req = "FROM " + policy::AlbumTable::Name + " alb " std::string req = "FROM " + policy::AlbumTable::Name + " alb "
" WHERE is_present != 0"; " WHERE is_present != 0";
req += orderBy( sort, desc ); req += orderBy( params );
return make_query<Album, IAlbum>( ml, "*", std::move( req ) ); return make_query<Album, IAlbum>( ml, "*", std::move( req ) );
} }
......
...@@ -76,8 +76,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable> ...@@ -76,8 +76,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual const std::string& artworkMrl() const override; virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail(); std::shared_ptr<Thumbnail> thumbnail();
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin ); bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
virtual Query<IMedia> tracks( SortingCriteria sort, bool desc ) const override; virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
virtual Query<IMedia> tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const override; virtual Query<IMedia> tracks( GenrePtr genre, const QueryParameters* params ) const override;
/// ///
/// \brief cachedTracks Returns a cached list of tracks /// \brief cachedTracks Returns a cached list of tracks
/// This has no warranty of ordering, validity, or anything else. /// This has no warranty of ordering, validity, or anything else.
...@@ -110,14 +110,14 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable> ...@@ -110,14 +110,14 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
/// \return /// \return
/// ///
static Query<IAlbum> search( MediaLibraryPtr ml, const std::string& pattern, static Query<IAlbum> search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc ); const QueryParameters* params );
static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc ); static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params );
static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc ); static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params );
static Query<IAlbum> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc ); static Query<IAlbum> listAll( MediaLibraryPtr ml, const QueryParameters* params );
private: private:
static std::string orderTracksBy( SortingCriteria sort, bool desc ); static std::string orderTracksBy( const QueryParameters* params );
static std::string orderBy( SortingCriteria sort, bool desc ); static std::string orderBy( const QueryParameters* params );
protected: protected:
MediaLibraryPtr m_ml; MediaLibraryPtr m_ml;
int64_t m_id; int64_t m_id;
......
...@@ -149,11 +149,13 @@ AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId ) ...@@ -149,11 +149,13 @@ AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
return fetch( ml, req, mediaId ); return fetch( ml, req, mediaId );
} }
Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc ) Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params )
{ {
std::string req = "FROM " + policy::MediaTable::Name + " m" std::string req = "FROM " + policy::MediaTable::Name + " m"
" INNER JOIN " + policy::AlbumTrackTable::Name + " t ON m.id_media = t.media_id" " 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 ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort ) switch ( sort )
{ {
case SortingCriteria::Duration: case SortingCriteria::Duration:
......
...@@ -74,7 +74,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy ...@@ -74,7 +74,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
unsigned int discNumber, int64_t artistId, int64_t genreId, unsigned int discNumber, int64_t artistId, int64_t genreId,
int64_t duration ); int64_t duration );
static AlbumTrackPtr fromMedia( MediaLibraryPtr ml, int64_t mediaId ); static AlbumTrackPtr fromMedia( MediaLibraryPtr ml, int64_t mediaId );
static Query<IMedia> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc ); static Query<IMedia> fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params );
private: private:
MediaLibraryPtr m_ml; MediaLibraryPtr m_ml;
......
...@@ -88,15 +88,17 @@ bool Artist::setShortBio(const std::string& shortBio) ...@@ -88,15 +88,17 @@ bool Artist::setShortBio(const std::string& shortBio)
return true; return true;
} }
Query<IAlbum> Artist::albums( SortingCriteria sort, bool desc ) const Query<IAlbum> Artist::albums( const QueryParameters* params ) const
{ {
return Album::fromArtist( m_ml, m_id, sort, desc ); return Album::fromArtist( m_ml, m_id, params );
} }
Query<IMedia> Artist::media( SortingCriteria sort, bool desc ) const Query<IMedia> Artist::media( const QueryParameters* params ) const
{ {
std::string req = "FROM " + policy::MediaTable::Name + " med "; std::string req = "FROM " + policy::MediaTable::Name + " med ";
SortingCriteria sort = params != nullptr ? params->sort : SortingCriteria::Default;
bool desc = params != nullptr ? params->desc : false;
// Various artist is a special artist that doesn't have tracks per-se. // Various artist is a special artist that doesn't have tracks per-se.
// Rather, it's a virtual artist for albums with many artist but no declared // Rather, it's a virtual artist for albums with many artist but no declared
// album artist. When listing its tracks, we need to list those by albums // album artist. When listing its tracks, we need to list those by albums
...@@ -402,17 +404,17 @@ std::shared_ptr<Artist> Artist::create( MediaLibraryPtr ml, const std::string& n ...@@ -402,17 +404,17 @@ std::shared_ptr<Artist> Artist::create( MediaLibraryPtr ml, const std::string& n
} }
Query<IArtist> Artist::search( MediaLibraryPtr ml, const std::string& name, Query<IArtist> Artist::search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc ) const QueryParameters* params )
{ {
std::string req = "FROM " + policy::ArtistTable::Name + " WHERE id_artist IN " std::string req = "FROM " + policy::ArtistTable::Name + " WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')" "(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')"
"AND is_present != 0"; "AND is_present != 0";
req += sortRequest( sort, desc ); req += sortRequest( params );
return make_query<Artist, IArtist>( ml, "*", std::move( req ), name ); return make_query<Artist, IArtist>( ml, "*", std::move( req ), name );
} }
Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll, Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
SortingCriteria sort, bool desc) const QueryParameters* params )
{ {
std::string req = "FROM " + policy::ArtistTable::Name + " WHERE "; std::string req = "FROM " + policy::ArtistTable::Name + " WHERE ";
if ( includeAll == true ) if ( includeAll == true )
...@@ -421,19 +423,14 @@ Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll, ...@@ -421,19 +423,14 @@ Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
req += "nb_albums > 0"; req += "nb_albums > 0";
req += " AND is_present != 0"; req += " AND is_present != 0";
req += sortRequest( sort, desc ); req += sortRequest( params );
return make_query<Artist, IArtist>( ml, "*", std::move( req ) ); return make_query<Artist, IArtist>( ml, "*", std::move( req ) );
} }
std::string Artist::sortRequest( SortingCriteria sort, bool desc ) std::string Artist::sortRequest( const QueryParameters* params )
{ {
std::string req = " ORDER BY "; std::string req = " ORDER BY name";
switch ( sort ) if ( params != nullptr && params->desc == true )
{
default:
req += "name";
}
if ( desc == true )
req += " DESC"; req += " DESC";
return req; return req;
} }
......
...@@ -55,8 +55,8 @@ public: ...@@ -55,8 +55,8 @@ public:
virtual const std::string& name() const override; virtual const std::string& name() const override;
virtual const std::string& shortBio() const override; virtual const std::string& shortBio() const override;
bool setShortBio( const std::string& shortBio ); bool setShortBio( const std::string& shortBio );
virtual Query<IAlbum> albums( SortingCriteria sort, bool desc ) const override; virtual Query<IAlbum> albums( const QueryParameters* params ) const override;
virtual Query<IMedia> media(SortingCriteria sort, bool desc) const override; virtual Query<IMedia> media( const QueryParameters* params ) const override;
bool addMedia( Media& media ); bool addMedia( Media& media );
virtual const std::string& artworkMrl() const override; virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail(); std::shared_ptr<Thumbnail> thumbnail();
...@@ -74,12 +74,12 @@ public: ...@@ -74,12 +74,12 @@ public:
static bool createDefaultArtists( sqlite::Connection* dbConnection ); static bool createDefaultArtists( sqlite::Connection* dbConnection );
static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name ); static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name );
static Query<IArtist> search( MediaLibraryPtr ml, const std::string& name, static Query<IArtist> search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc ); const QueryParameters* params );
static Query<IArtist> listAll( MediaLibraryPtr ml, bool includeAll, static Query<IArtist> listAll( MediaLibraryPtr ml, bool includeAll,
SortingCriteria sort, bool desc ); const QueryParameters* params );
private: private:
static std::string sortRequest( SortingCriteria sort, bool desc ); static std::string sortRequest( const QueryParameters* params );
private: private:
MediaLibraryPtr m_ml; MediaLibraryPtr m_ml;
......
...@@ -77,25 +77,25 @@ void Genre::updateCachedNbTracks( int increment ) ...@@ -77,25 +77,25 @@ void Genre::updateCachedNbTracks( int increment )
m_nbTracks += increment; m_nbTracks += increment;
} }
Query<IArtist> Genre::artists( SortingCriteria, bool desc ) const Query<IArtist> Genre::artists( const QueryParameters* params ) const
{ {
std::string req = "FROM " + policy::ArtistTable::Name + " a " std::string req = "FROM " + policy::ArtistTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist " "INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
"WHERE att.genre_id = ? GROUP BY att.artist_id" "WHERE att.genre_id = ? GROUP BY att.artist_id"
" ORDER BY a.name"; " ORDER BY a.name";
if ( desc == true ) if ( params != nullptr && params->desc == true )
req += " DESC"; req += " DESC";
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ), m_id ); return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ), m_id );
} }
Query<IMedia> Genre::tracks( SortingCriteria sort, bool desc ) const Query<IMedia> Genre::tracks( const QueryParameters* params ) const
{ {
return AlbumTrack::fromGenre( m_ml, m_id, sort, desc ); return AlbumTrack::fromGenre( m_ml, m_id, params );
} }
Query<IAlbum> Genre::albums( SortingCriteria sort, bool desc ) const Query<IAlbum> Genre::albums( const QueryParameters* params ) const
{ {
return Album::fromGenre( m_ml, m_id, sort, desc ); return Album::fromGenre( m_ml, m_id, params );
} }
void Genre::createTable( sqlite::Connection* dbConn ) void Genre::createTable( sqlite::Connection* dbConn )
...@@ -171,19 +171,20 @@ std::shared_ptr<Genre> Genre::fromName( MediaLibraryPtr ml, const std::string& n ...@@ -171,19 +171,20 @@ std::shared_ptr<Genre> Genre::fromName( MediaLibraryPtr ml, const std::string& n
return fetch( ml, req, name ); return fetch( ml, req, name );
} }
Query<IGenre> Genre::search( MediaLibraryPtr ml, const std::string& name, SortingCriteria sort, bool desc ) Query<IGenre> Genre::search( MediaLibraryPtr ml, const std::string& name,
const QueryParameters* params )
{ {
std::string req = "FROM " + policy::GenreTable::Name + " WHERE id_genre IN " 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 '*' || ? || '*') ORDER BY name";
if ( desc == true ) if ( params != nullptr && params->desc == true )
req += " DESC"; req += " DESC";
return make_query<Genre, IGenre>( ml, "*", req, name ); return make_query<Genre, IGenre>( ml, "*", req, name );
} }
Query<IGenre> Genre::listAll( MediaLibraryPtr ml, SortingCriteria, bool desc ) 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 + " ORDER BY name";
if ( desc == true ) if ( params != nullptr && params->desc == true )
req += " DESC"; req += " DESC";
return make_query<Genre, IGenre>( ml, "*", std::move( req ) ); return make_query<Genre, IGenre>( ml, "*", std::move( req ) );
} }
......
...@@ -50,16 +50,16 @@ public: ...@@ -50,16 +50,16 @@ public:
virtual const std::string& name() const override; virtual const std::string& name() const override;
virtual uint32_t nbTracks() const override; virtual uint32_t nbTracks() const override;
void updateCachedNbTracks( int increment ); void updateCachedNbTracks( int increment );
virtual Query<IArtist> artists( SortingCriteria sort, bool desc ) const override; virtual Query<IArtist> artists( const QueryParameters* params ) const override;
virtual Query<IMedia> tracks(SortingCriteria sort, bool desc) const override; virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
virtual Query<IAlbum> albums( SortingCriteria sort, bool desc ) const override; virtual Query<IAlbum> albums( const QueryParameters* params ) const override;
static void createTable( sqlite::Connection* dbConn ); static void createTable( sqlite::Connection* dbConn );
static void createTriggers( sqlite::Connection* dbConn ); static void createTriggers( sqlite::Connection* dbConn );
static std::shared_ptr<Genre> create( MediaLibraryPtr ml, const std::string& name ); static std::shared_ptr<Genre> create( MediaLibraryPtr ml, const std::string& name );
static std::shared_ptr<Genre> fromName( MediaLibraryPtr ml, const std::string& name ); static std::shared_ptr<Genre> fromName( MediaLibraryPtr ml, const std::string& name );
static Query<IGenre> search( MediaLibraryPtr ml, const std::string& name, SortingCriteria sort, bool desc ); static Query<IGenre> search( MediaLibraryPtr ml, const std::string& name, const QueryParameters* params );
static Query<IGenre> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc ); static Query<IGenre> listAll( MediaLibraryPtr ml,