...
 
Commits (9)
......@@ -37,6 +37,7 @@ libmedialibrary_la_HEADERS = \
include/medialibrary/Types.h \
include/medialibrary/IDeviceLister.h \
include/medialibrary/IFolder.h \
include/medialibrary/IQuery.h \
$(NULL)
libmedialibrary_la_SOURCES = \
......@@ -123,6 +124,7 @@ noinst_HEADERS = \
src/database/DatabaseHelpers.h \
src/database/SqliteConnection.h \
src/database/SqliteErrors.h \
src/database/SqliteQuery.h \
src/database/SqliteTools.h \
src/database/SqliteTraits.h \
src/database/SqliteTransaction.h \
......
......@@ -22,7 +22,6 @@
#pragma once
#include <vector>
#include <string>
#include "IMediaLibrary.h"
......@@ -45,23 +44,24 @@ public:
/**
* @brief tracks fetches album tracks from the database
*/
virtual std::vector<MediaPtr> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
/**
* @brief tracks fetches album tracks, filtered by genre
* @param genre A musical genre. Only tracks of this genre will be returned
* @return
*/
virtual std::vector<MediaPtr> tracks( GenrePtr genre, SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> tracks( GenrePtr genre, SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
/**
* @brief albumArtist Returns the album main artist (generally tagged as album-artist)
*/
virtual ArtistPtr albumArtist() const = 0;
/**
* @brief artists Returns a vector of all additional artists appearing on the album.
* @brief artists Returns a Query object representing all additional
* artists appearing on the album.
* Artists are sorted by name.
* @param desc
*/
virtual std::vector<ArtistPtr> artists( bool desc ) const = 0;
virtual Query<IArtist> artists( bool desc ) const = 0;
/**
* @brief nbTracks Returns the amount of track in this album.
* The value is cached, and doesn't require fetching anything.
......
......@@ -23,7 +23,6 @@
#pragma once
#include <string>
#include <vector>
#include "IMediaLibrary.h"
......@@ -43,8 +42,8 @@ public:
* 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)
*/
virtual std::vector<AlbumPtr> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual std::vector<MediaPtr> media( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> media( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& musicBrainzId() const = 0;
/**
......
......@@ -24,8 +24,6 @@
#include "IMediaLibrary.h"
#include <vector>
namespace medialibrary
{
......@@ -36,9 +34,9 @@ public:
virtual int64_t id() const = 0;
virtual const std::string& name() const = 0;
virtual uint32_t nbTracks() const = 0;
virtual std::vector<ArtistPtr> artists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual std::vector<MediaPtr> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual std::vector<AlbumPtr> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IArtist> artists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> tracks( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
};
}
......@@ -38,7 +38,7 @@ class ILabel
virtual int64_t id() const = 0;
virtual const std::string& name() const = 0;
virtual std::vector<MediaPtr> files() = 0;
virtual Query<IMedia> media() = 0;
};
}
......
......@@ -117,9 +117,9 @@ class IMedia
virtual bool addLabel( LabelPtr label ) = 0;
virtual bool removeLabel( LabelPtr label ) = 0;
virtual MoviePtr movie() const = 0;
virtual std::vector<LabelPtr> labels() = 0;
virtual std::vector<VideoTrackPtr> videoTracks() = 0;
virtual std::vector<AudioTrackPtr> audioTracks() = 0;
virtual Query<ILabel> labels() = 0;
virtual Query<IVideoTrack> videoTracks() = 0;
virtual Query<IAudioTrack> audioTracks() = 0;
///
/// \brief thumbnail Returns the path of a thumbnail for this media
/// \return A path, relative to the thumbnailPath configured when initializing
......
......@@ -28,6 +28,7 @@
#include "medialibrary/ILogger.h"
#include "Types.h"
#include "IQuery.h"
namespace medialibrary
{
......@@ -37,19 +38,19 @@ static constexpr auto VariousArtistID = 2u;
struct MediaSearchAggregate
{
std::vector<MediaPtr> episodes;
std::vector<MediaPtr> movies;
std::vector<MediaPtr> others;
std::vector<MediaPtr> tracks;
Query<IMedia> episodes;
Query<IMedia> movies;
Query<IMedia> others;
Query<IMedia> tracks;
};
struct SearchAggregate
{
std::vector<AlbumPtr> albums;
std::vector<ArtistPtr> artists;
std::vector<GenrePtr> genres;
Query<IAlbum> albums;
Query<IArtist> artists;
Query<IGenre> genres;
MediaSearchAggregate media;
std::vector<PlaylistPtr> playlists;
Query<IPlaylist> playlists;
};
enum class SortingCriteria
......@@ -251,10 +252,10 @@ class IMediaLibrary
virtual MediaPtr media( int64_t mediaId ) const = 0;
virtual MediaPtr media( const std::string& mrl ) const = 0;
virtual MediaPtr addMedia( const std::string& mrl ) = 0;
virtual std::vector<MediaPtr> audioFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual std::vector<MediaPtr> videoFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> audioFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IMedia> videoFiles( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual AlbumPtr album( int64_t id ) const = 0;
virtual std::vector<AlbumPtr> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IAlbum> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual ShowPtr show( const std::string& name ) const = 0;
virtual MoviePtr movie( const std::string& title ) const = 0;
virtual ArtistPtr artist( int64_t id ) const = 0;
......@@ -268,7 +269,7 @@ class IMediaLibrary
* @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.
*/
virtual std::vector<ArtistPtr> artists( bool includeAll,
virtual Query<IArtist> artists( bool includeAll,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
/**
......@@ -276,13 +277,13 @@ class IMediaLibrary
* @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.
*/
virtual std::vector<GenrePtr> genres( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual Query<IGenre> genres( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual GenrePtr genre( int64_t id ) const = 0;
/***
* Playlists
*/
virtual PlaylistPtr createPlaylist( const std::string& name ) = 0;
virtual std::vector<PlaylistPtr> playlists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) = 0;
virtual Query<IPlaylist> playlists( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) = 0;
virtual PlaylistPtr playlist( int64_t id ) const = 0;
virtual bool deletePlaylist( int64_t playlistId ) = 0;
......@@ -290,8 +291,8 @@ class IMediaLibrary
* History
*/
virtual bool addToStreamHistory( MediaPtr media ) = 0;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const = 0;
virtual std::vector<MediaPtr> lastMediaPlayed() const = 0;
virtual Query<IHistoryEntry> lastStreamsPlayed() const = 0;
virtual Query<IMedia> lastMediaPlayed() const = 0;
/**
* @brief clearHistory will clear both streams history & media history.
* @return true in case of success, false otherwise. The database will stay untouched in case
......@@ -308,14 +309,14 @@ class IMediaLibrary
virtual MediaSearchAggregate searchMedia( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name,
virtual Query<IPlaylist> searchPlaylists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern,
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const = 0;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
virtual Query<IGenre> searchGenre( const std::string& genre ) const = 0;
virtual Query<IArtist> searchArtists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual SearchAggregate search( const std::string& pattern,
......@@ -331,7 +332,7 @@ class IMediaLibrary
*/
virtual void discover( const std::string& entryPoint ) = 0;
virtual void setDiscoverNetworkEnabled( bool enable ) = 0;
virtual std::vector<FolderPtr> entryPoints() const = 0;
virtual Query<IFolder> entryPoints() const = 0;
virtual FolderPtr folder( const std::string& mrl ) const = 0;
virtual void removeEntryPoint( const std::string& entryPoint ) = 0;
/**
......
......@@ -37,7 +37,7 @@ class IMovie
virtual const std::string& shortSummary() const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& imdbId() const = 0;
virtual std::vector<MediaPtr> files() = 0;
virtual Query<IMedia> media() = 0;
};
}
......
......@@ -22,9 +22,8 @@
#pragma once
#include <vector>
#include "Types.h"
#include "IQuery.h"
namespace medialibrary
{
......@@ -38,7 +37,7 @@ public:
virtual bool setName( const std::string& name ) = 0;
virtual unsigned int creationDate() const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual std::vector<MediaPtr> media() const = 0;
virtual Query<IMedia> media() const = 0;
///
/// \brief append Appends a media to a playlist
/// The media will be the last element of a subsequent call to media()
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#pragma once
#include <vector>
#include <memory>
namespace medialibrary
{
template <typename T>
class IQuery
{
public:
using Result = std::vector<std::shared_ptr<T>>;
virtual ~IQuery() = default;
virtual size_t count() = 0;
virtual Result items( uint32_t nbItems, uint32_t offset ) = 0;
virtual Result all() = 0;
};
template <typename T>
using Query = std::unique_ptr<IQuery<T>>;
}
......@@ -38,7 +38,7 @@ class IShow
virtual const std::string& shortSummary() const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& tvdbId() = 0;
virtual std::vector<ShowEpisodePtr> episodes() = 0;
virtual Query<IShowEpisode> episodes() = 0;
};
}
......
......@@ -41,7 +41,7 @@ class IShowEpisode
virtual const std::string& shortSummary() const = 0;
virtual const std::string& tvdbId() const = 0;
virtual std::shared_ptr<IShow> show() = 0;
virtual std::vector<MediaPtr> files() = 0;
virtual Query<IMedia> media() = 0;
};
}
......
......@@ -34,6 +34,7 @@
#include "Thumbnail.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -246,34 +247,34 @@ std::string Album::orderBy( SortingCriteria sort, bool desc )
return req;
}
std::vector<MediaPtr> Album::tracks( SortingCriteria sort, bool desc ) const
Query<IMedia> Album::tracks( SortingCriteria sort, bool desc ) const
{
// 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.
std::string req = "SELECT med.* 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 "
" WHERE att.album_id = ? AND med.is_present != 0";
req += orderTracksBy( sort, desc );
return Media::fetchAll<IMedia>( m_ml, req, m_id );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id );
}
std::vector<MediaPtr> Album::tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const
Query<IMedia> Album::tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const
{
if ( genre == nullptr )
return {};
std::string req = "SELECT med.* 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 "
" WHERE att.album_id = ? AND med.is_present != 0"
" AND genre_id = ?";
req += orderTracksBy( sort, desc );
return Media::fetchAll<IMedia>( m_ml, req, m_id, genre->id() );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id, genre->id() );
}
std::vector<MediaPtr> Album::cachedTracks() const
{
auto lock = m_tracks.lock();
if ( m_tracks.isCached() == false )
m_tracks = tracks( SortingCriteria::Default, false );
m_tracks = tracks( SortingCriteria::Default, false )->all();
return m_tracks.get();
}
......@@ -349,14 +350,14 @@ bool Album::setAlbumArtist( std::shared_ptr<Artist> artist )
return true;
}
std::vector<ArtistPtr> Album::artists( bool desc ) const
Query<IArtist> Album::artists( bool desc ) const
{
std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
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";
if ( desc == true )
req += " DESC";
return Artist::fetchAll<IArtist>( m_ml, req, m_id );
return make_query<Artist, IArtist>( m_ml, "art.*", std::move( req ), m_id );
}
bool Album::addArtist( std::shared_ptr<Artist> artist )
......@@ -491,21 +492,21 @@ std::shared_ptr<Album> Album::createUnknownAlbum( MediaLibraryPtr ml, const Arti
return album;
}
std::vector<AlbumPtr> Album::search( MediaLibraryPtr ml, const std::string& pattern,
Query<IAlbum> Album::search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc )
{
std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"WHERE id_album IN "
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND is_present != 0";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req, pattern );
return make_query<Album, IAlbum>( ml, "*", std::move( req ), pattern );
}
std::vector<AlbumPtr> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc )
Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc )
{
std::string req = "SELECT * 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 "
"WHERE (att.artist_id = ? OR alb.artist_id = ?) "
......@@ -529,34 +530,34 @@ std::vector<AlbumPtr> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, S
break;
}
return fetchAll<IAlbum>( ml, req, artistId, artistId );
return make_query<Album, IAlbum>( ml, "*", req, artistId, artistId );
}
std::vector<AlbumPtr> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc)
Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc)
{
std::string req = "SELECT alb.* 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 "
"WHERE att.genre_id = ? GROUP BY att.album_id";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req, genreId );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), genreId );
}
std::vector<AlbumPtr> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc )
Query<IAlbum> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc )
{
if ( sort == SortingCriteria::Artist )
{
std::string req = "SELECT alb.* FROM " + policy::AlbumTable::Name + " alb "
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 ";
if ( desc == true )
req += "DESC ";
req += ", alb.title";
return fetchAll<IAlbum>( ml, req );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ) );
}
if ( sort == SortingCriteria::PlayCount )
{
std::string req = "SELECT alb.* FROM " + policy::AlbumTable::Name + " alb "
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 "
......@@ -565,12 +566,12 @@ std::vector<AlbumPtr> Album::listAll( MediaLibraryPtr ml, SortingCriteria sort,
if ( desc == false )
req += "DESC "; // Most played first by default
req += ", alb.title";
return fetchAll<IAlbum>( ml, req );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ) );
}
std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
" WHERE is_present != 0";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req );
return make_query<Album, IAlbum>( ml, "*", std::move( req ) );
}
}
......@@ -76,8 +76,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail();
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
virtual std::vector<MediaPtr> tracks( SortingCriteria sort, bool desc ) const override;
virtual std::vector<MediaPtr> tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const override;
virtual Query<IMedia> tracks( SortingCriteria sort, bool desc ) const override;
virtual Query<IMedia> tracks( GenrePtr genre, SortingCriteria sort, bool desc ) const override;
///
/// \brief cachedTracks Returns a cached list of tracks
/// This has no warranty of ordering, validity, or anything else.
......@@ -96,7 +96,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual ArtistPtr albumArtist() const override;
bool setAlbumArtist( std::shared_ptr<Artist> artist );
virtual std::vector<ArtistPtr> artists(bool desc) const override;
virtual Query<IArtist> artists(bool desc) const override;
bool addArtist( std::shared_ptr<Artist> artist );
bool removeArtist( Artist* artist );
......@@ -109,11 +109,11 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
/// \param pattern A pattern representing the title, or the name of the main artist
/// \return
///
static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern,
static Query<IAlbum> search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc );
static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
static Query<IAlbum> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
private:
static std::string orderTracksBy( SortingCriteria sort, bool desc );
......
......@@ -30,6 +30,7 @@
#include "Media.h"
#include "Genre.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
#include "logging/Logger.h"
namespace medialibrary
......@@ -148,9 +149,9 @@ AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
return fetch( ml, req, mediaId );
}
std::vector<MediaPtr> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc )
Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc )
{
std::string req = "SELECT m.* 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"
" WHERE t.genre_id = ? AND m.is_present = 1 ORDER BY ";
switch ( sort )
......@@ -177,7 +178,7 @@ std::vector<MediaPtr> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId
if ( desc == true )
req += " DESC";
return Media::fetchAll<IMedia>( ml, req, genreId );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ), genreId );
}
GenrePtr AlbumTrack::genre()
......
......@@ -74,8 +74,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
unsigned int discNumber, int64_t artistId, int64_t genreId,
int64_t duration );
static AlbumTrackPtr fromMedia( MediaLibraryPtr ml, int64_t mediaId );
static std::vector<MediaPtr> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
static std::vector<MediaPtr> search( sqlite::Connection* dbConn, const std::string& title );
static Query<IMedia> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
private:
MediaLibraryPtr m_ml;
......
......@@ -30,6 +30,7 @@
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -87,14 +88,14 @@ bool Artist::setShortBio(const std::string& shortBio)
return true;
}
std::vector<AlbumPtr> Artist::albums( SortingCriteria sort, bool desc ) const
Query<IAlbum> Artist::albums( SortingCriteria sort, bool desc ) const
{
return Album::fromArtist( m_ml, m_id, sort, desc );
}
std::vector<MediaPtr> Artist::media( SortingCriteria sort, bool desc ) const
Query<IMedia> Artist::media( SortingCriteria sort, bool desc ) const
{
std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med ";
std::string req = "FROM " + policy::MediaTable::Name + " med ";
// 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
......@@ -139,7 +140,7 @@ std::vector<MediaPtr> Artist::media( SortingCriteria sort, bool desc ) const
if ( desc == true )
req += " DESC";
return Media::fetchAll<IMedia>( m_ml, req, m_id );
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id );
}
bool Artist::addMedia( Media& media )
......@@ -400,20 +401,20 @@ std::shared_ptr<Artist> Artist::create( MediaLibraryPtr ml, const std::string& n
return artist;
}
std::vector<ArtistPtr> Artist::search( MediaLibraryPtr ml, const std::string& name,
Query<IArtist> Artist::search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc )
{
std::string req = "SELECT * 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 '*' || ? || '*')"
"AND is_present != 0";
req += sortRequest( sort, desc );
return fetchAll<IArtist>( ml, req, name );
return make_query<Artist, IArtist>( ml, "*", std::move( req ), name );
}
std::vector<ArtistPtr> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
SortingCriteria sort, bool desc)
{
std::string req = "SELECT * FROM " + policy::ArtistTable::Name + " WHERE ";
std::string req = "FROM " + policy::ArtistTable::Name + " WHERE ";
if ( includeAll == true )
req += "( nb_albums > 0 OR nb_tracks > 0 )";
else
......@@ -421,7 +422,7 @@ std::vector<ArtistPtr> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
req += " AND is_present != 0";
req += sortRequest( sort, desc );
return fetchAll<IArtist>( ml, req );
return make_query<Artist, IArtist>( ml, "*", std::move( req ) );
}
std::string Artist::sortRequest( SortingCriteria sort, bool desc )
......
......@@ -55,8 +55,8 @@ public:
virtual const std::string& name() const override;
virtual const std::string& shortBio() const override;
bool setShortBio( const std::string& shortBio );
virtual std::vector<AlbumPtr> albums( SortingCriteria sort, bool desc ) const override;
virtual std::vector<MediaPtr> media(SortingCriteria sort, bool desc) const override;
virtual Query<IAlbum> albums( SortingCriteria sort, bool desc ) const override;
virtual Query<IMedia> media(SortingCriteria sort, bool desc) const override;
bool addMedia( Media& media );
virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail();
......@@ -73,9 +73,9 @@ public:
static void createTriggers( sqlite::Connection* dbConnection, uint32_t dbModelVersion );
static bool createDefaultArtists( sqlite::Connection* dbConnection );
static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name );
static std::vector<ArtistPtr> search( MediaLibraryPtr ml, const std::string& name,
static Query<IArtist> search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc );
static std::vector<ArtistPtr> listAll( MediaLibraryPtr ml, bool includeAll,
static Query<IArtist> listAll( MediaLibraryPtr ml, bool includeAll,
SortingCriteria sort, bool desc );
private:
......
......@@ -29,6 +29,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -76,23 +77,23 @@ void Genre::updateCachedNbTracks( int increment )
m_nbTracks += increment;
}
std::vector<ArtistPtr> Genre::artists( SortingCriteria, bool desc ) const
Query<IArtist> Genre::artists( SortingCriteria, bool desc ) const
{
std::string req = "SELECT a.* 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 "
"WHERE att.genre_id = ? GROUP BY att.artist_id"
" ORDER BY a.name";
if ( desc == true )
req += " DESC";
return Artist::fetchAll<IArtist>( m_ml, req, m_id );
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ), m_id );
}
std::vector<MediaPtr> Genre::tracks( SortingCriteria sort, bool desc ) const
Query<IMedia> Genre::tracks( SortingCriteria sort, bool desc ) const
{
return AlbumTrack::fromGenre( m_ml, m_id, sort, desc );
}
std::vector<AlbumPtr> Genre::albums( SortingCriteria sort, bool desc ) const
Query<IAlbum> Genre::albums( SortingCriteria sort, bool desc ) const
{
return Album::fromGenre( m_ml, m_id, sort, desc );
}
......@@ -170,19 +171,19 @@ std::shared_ptr<Genre> Genre::fromName( MediaLibraryPtr ml, const std::string& n
return fetch( ml, req, name );
}
std::vector<GenrePtr> Genre::search( MediaLibraryPtr ml, const std::string& name )
Query<IGenre> Genre::search( MediaLibraryPtr ml, const std::string& name )
{
static const std::string req = "SELECT * FROM " + policy::GenreTable::Name + " WHERE id_genre IN "
static const std::string req = "FROM " + policy::GenreTable::Name + " WHERE id_genre IN "
"(SELECT rowid FROM " + policy::GenreTable::Name + "Fts WHERE name MATCH '*' || ? || '*')";
return fetchAll<IGenre>( ml, req, name );
return make_query<Genre, IGenre>( ml, "*", req, name );
}
std::vector<GenrePtr> Genre::listAll( MediaLibraryPtr ml, SortingCriteria, bool desc )
Query<IGenre> Genre::listAll( MediaLibraryPtr ml, SortingCriteria, bool desc )
{
std::string req = "SELECT * FROM " + policy::GenreTable::Name + " ORDER BY name";
std::string req = "FROM " + policy::GenreTable::Name + " ORDER BY name";
if ( desc == true )
req += " DESC";
return fetchAll<IGenre>( ml, req );
return make_query<Genre, IGenre>( ml, "*", std::move( req ) );
}
}
......@@ -50,16 +50,16 @@ public:
virtual const std::string& name() const override;
virtual uint32_t nbTracks() const override;
void updateCachedNbTracks( int increment );
virtual std::vector<ArtistPtr> artists( SortingCriteria sort, bool desc ) const override;
virtual std::vector<MediaPtr> tracks(SortingCriteria sort, bool desc) const override;
virtual std::vector<AlbumPtr> albums( SortingCriteria sort, bool desc ) const override;
virtual Query<IArtist> artists( SortingCriteria sort, bool desc ) const override;
virtual Query<IMedia> tracks(SortingCriteria sort, bool desc) const override;
virtual Query<IAlbum> albums( SortingCriteria sort, bool desc ) const override;
static void createTable( 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> fromName( MediaLibraryPtr ml, const std::string& name );
static std::vector<GenrePtr> search( MediaLibraryPtr ml, const std::string& name );
static std::vector<GenrePtr> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
static Query<IGenre> search( MediaLibraryPtr ml, const std::string& name );
static Query<IGenre> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
private:
MediaLibraryPtr m_ml;
......
......@@ -28,6 +28,7 @@
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -81,12 +82,12 @@ bool History::insert( sqlite::Connection* dbConn, int64_t mediaId )
return sqlite::Tools::executeInsert( dbConn, req, mediaId ) != 0;
}
std::vector<HistoryPtr> History::fetch( MediaLibraryPtr ml )
Query<IHistoryEntry> History::fetch( MediaLibraryPtr ml )
{
static const std::string req = "SELECT f.*, h.insertion_date FROM " + policy::MediaTable::Name + " f "
static const std::string req = "FROM " + policy::MediaTable::Name + " f "
"INNER JOIN " + policy::HistoryTable::Name + " h ON h.id_media = f.id_media "
"ORDER BY h.insertion_date DESC";
return fetchAll<IHistoryEntry>( ml, req );
return make_query<History, IHistoryEntry>( ml, "f.*, h.insertion_date", req );
}
void History::clearStreams( MediaLibraryPtr ml )
......
......@@ -52,7 +52,7 @@ public:
static void createTable( sqlite::Connection* dbConnection );
static void createTriggers( sqlite::Connection* dbConnection );
static bool insert( sqlite::Connection* dbConn, int64_t mediaId );
static std::vector<HistoryPtr> fetch( MediaLibraryPtr ml );
static Query<IHistoryEntry> fetch( MediaLibraryPtr ml );
static void clearStreams( MediaLibraryPtr ml );
virtual MediaPtr media() const override;
......
......@@ -31,6 +31,7 @@
#include "Label.h"
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -63,12 +64,12 @@ const std::string& Label::name() const
return m_name;
}
std::vector<MediaPtr> Label::files()
Query<IMedia> Label::media()
{
static const std::string req = "SELECT f.* FROM " + policy::MediaTable::Name + " f "
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 Media::fetchAll<IMedia>( m_ml, req, m_id );
return make_query<Media, IMedia>( m_ml, "f.*", req, m_id );
}
LabelPtr Label::create( MediaLibraryPtr ml, const std::string& name )
......
......@@ -54,7 +54,7 @@ class Label : public ILabel, public DatabaseHelpers<Label, policy::LabelTable>
public:
virtual int64_t id() const override;
virtual const std::string& name() const override;
virtual std::vector<MediaPtr> files() override;
virtual Query<IMedia> media() override;
static LabelPtr create( MediaLibraryPtr ml, const std::string& name );
static void createTable( sqlite::Connection* dbConnection );
......
......@@ -43,6 +43,7 @@
#include "ShowEpisode.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
#include "VideoTrack.h"
#include "filesystem/IFile.h"
#include "filesystem/IDirectory.h"
......@@ -161,12 +162,12 @@ void Media::setShowEpisode( ShowEpisodePtr episode )
m_changed = true;
}
std::vector<LabelPtr> Media::labels()
Query<ILabel> Media::labels()
{
static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
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 Label::fetchAll<ILabel>( m_ml, req, m_id );
return make_query<Label, ILabel>( m_ml, "l.*", req, m_id );
}
int Media::playCount() const
......@@ -240,11 +241,11 @@ bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned
return VideoTrack::create( m_ml, codec, width, height, fps, m_id, language, description ) != nullptr;
}
std::vector<VideoTrackPtr> Media::videoTracks()
Query<IVideoTrack> Media::videoTracks()
{
static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
static const std::string req = "FROM " + policy::VideoTrackTable::Name +
" WHERE media_id = ?";
return VideoTrack::fetchAll<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,
......@@ -254,11 +255,11 @@ bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
return AudioTrack::create( m_ml, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
}
std::vector<AudioTrackPtr> Media::audioTracks()
Query<IAudioTrack> Media::audioTracks()
{
static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
static const std::string req = "FROM " + policy::AudioTrackTable::Name +
" WHERE media_id = ?";
return AudioTrack::fetchAll<IAudioTrack>( m_ml, req, m_id );
return make_query<AudioTrack, IAudioTrack>( m_ml, "*", req, m_id );
}
const std::string& Media::thumbnail()
......@@ -461,10 +462,10 @@ void Media::removeFile( File& file )
}));
}
std::string Media::sortRequest( SortingCriteria sort, bool desc )
{
std::string req = " ORDER BY ";
switch ( sort )
{
case SortingCriteria::Duration:
......@@ -498,16 +499,18 @@ std::string Media::sortRequest( SortingCriteria sort, bool desc )
return req;
}
std::vector<MediaPtr> Media::listAll( MediaLibraryPtr ml, IMedia::Type type,
Query<IMedia> Media::listAll( MediaLibraryPtr ml, IMedia::Type type,
SortingCriteria sort, bool desc )
{
std::string req = "SELECT m.* FROM " + policy::MediaTable::Name + " m INNER JOIN "
std::string req = "FROM " + policy::MediaTable::Name + " m INNER JOIN "
+ policy::FileTable::Name + " f ON m.id_media = f.media_id"
" WHERE m.type = ?"
" AND f.type = ?"
" AND f.is_present != 0";
req += sortRequest( sort, desc );
return fetchAll<IMedia>( ml, req, type, File::Type::Main );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ),
type, IFile::Type::Main );
}
int64_t Media::id() const
......@@ -571,7 +574,9 @@ void Media::createTable( sqlite::Connection* connection )
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MediaTable::Name + "("
"id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
"type INTEGER,"
"subtype INTEGER,"
"subtype INTEGER NOT NULL DEFAULT " +
std::to_string( static_cast<typename std::underlying_type<IMedia::SubType>::type>(
IMedia::SubType::Unknown ) ) + ","
"duration INTEGER DEFAULT -1,"
"play_count UNSIGNED INTEGER,"
"last_played_date UNSIGNED INTEGER,"
......@@ -709,25 +714,26 @@ bool Media::removeLabel( LabelPtr label )
}
}
std::vector<MediaPtr> Media::search( MediaLibraryPtr ml, const std::string& title,
SortingCriteria sort, bool desc )
Query<IMedia> Media::search( MediaLibraryPtr ml, const std::string& title,
Media::SubType subType, SortingCriteria sort, bool desc )
{
std::string req = "SELECT m.* FROM " + policy::MediaTable::Name + " m "
std::string req = "FROM " + policy::MediaTable::Name + " m "
" INNER JOIN " + policy::FileTable::Name + " f ON m.id_media = f.media_id"
" WHERE"
" m.id_media IN (SELECT rowid FROM " + policy::MediaTable::Name + "Fts"
" WHERE " + policy::MediaTable::Name + "Fts MATCH '*' || ? || '*')"
" AND f.is_present = 1"
" AND f.type = ?";
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( sort, desc );
return Media::fetchAll<IMedia>( ml, req, title, File::Type::Main );
return make_query<Media, IMedia>( ml, "m.*", req, title, File::Type::Main, subType );
}
std::vector<MediaPtr> Media::fetchHistory( MediaLibraryPtr ml )
Query<IMedia> Media::fetchHistory( MediaLibraryPtr ml )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE last_played_date IS NOT NULL"
static const std::string req = "FROM " + policy::MediaTable::Name + " WHERE last_played_date IS NOT NULL"
" ORDER BY last_played_date DESC LIMIT 100";
return fetchAll<IMedia>( ml, req );
return make_query<Media, IMedia>( ml, "*", req );
}
void Media::clearHistory( MediaLibraryPtr ml )
......
......@@ -109,7 +109,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
void setShowEpisode( ShowEpisodePtr episode );
virtual bool addLabel( LabelPtr label ) override;
virtual bool removeLabel( LabelPtr label ) override;
virtual std::vector<LabelPtr> labels() override;
virtual Query<ILabel> labels() override;
virtual int playCount() const override;
virtual bool increasePlayCount() override;
virtual bool isFavorite() const override;
......@@ -119,10 +119,10 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
void setMovie( MoviePtr movie );
bool addVideoTrack( const std::string& codec, unsigned int width, unsigned int height,
float fps, const std::string& language, const std::string& description );
virtual std::vector<VideoTrackPtr> videoTracks() override;
virtual Query<IVideoTrack> videoTracks() override;
bool addAudioTrack( const std::string& codec, unsigned int bitrate, unsigned int sampleRate,
unsigned int nbChannels, const std::string& language, const std::string& desc );
virtual std::vector<AudioTrackPtr> audioTracks() override;
virtual Query<IAudioTrack> audioTracks() override;
virtual const std::string& thumbnail() override;
virtual bool isThumbnailGenerated() const override;
virtual bool setThumbnail( const std::string &thumbnail ) override;
......@@ -142,10 +142,13 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
virtual FilePtr addExternalMrl( const std::string& mrl, IFile::Type type ) override;
void removeFile( File& file );
static std::vector<MediaPtr> listAll(MediaLibraryPtr ml, Type type , SortingCriteria sort, bool desc);
static std::vector<MediaPtr> search( MediaLibraryPtr ml, const std::string& title,
SortingCriteria sort, bool desc );
static std::vector<MediaPtr> fetchHistory( MediaLibraryPtr ml );
static Query<IMedia> listAll(MediaLibraryPtr ml, Type type, SortingCriteria sort, bool desc);
static Query<IMedia> search( MediaLibraryPtr ml, const std::string& title,
Media::SubType subType, SortingCriteria sort,
bool desc );
static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
static void clearHistory( MediaLibraryPtr ml );
private:
......
......@@ -54,6 +54,7 @@
#include "Thumbnail.h"
#include "database/SqliteTools.h"
#include "database/SqliteConnection.h"
#include "database/SqliteQuery.h"
#include "parser/Task.h"
#include "utils/Filename.h"
#include "utils/Url.h"
......@@ -412,12 +413,12 @@ MediaPtr MediaLibrary::addMedia( const std::string& mrl )
}
}
std::vector<MediaPtr> MediaLibrary::audioFiles( SortingCriteria sort, bool desc ) const
Query<IMedia> MediaLibrary::audioFiles( SortingCriteria sort, bool desc ) const
{
return Media::listAll( this, IMedia::Type::Audio, sort, desc );
}
std::vector<MediaPtr> MediaLibrary::videoFiles( SortingCriteria sort, bool desc ) const
Query<IMedia> MediaLibrary::videoFiles( SortingCriteria sort, bool desc ) const
{
return Media::listAll( this, IMedia::Type::Video, sort, desc );
}
......@@ -497,12 +498,12 @@ std::shared_ptr<Album> MediaLibrary::createAlbum( const std::string& title, int6
return Album::create( this, title, thumbnailId );
}
std::vector<AlbumPtr> MediaLibrary::albums( SortingCriteria sort, bool desc ) const
Query<IAlbum> MediaLibrary::albums( SortingCriteria sort, bool desc ) const
{
return Album::listAll( this, sort, desc );
}
std::vector<GenrePtr> MediaLibrary::genres( SortingCriteria sort, bool desc ) const
Query<IGenre> MediaLibrary::genres( SortingCriteria sort, bool desc ) const
{
return Genre::listAll( this, sort, desc );
}
......@@ -564,9 +565,8 @@ std::shared_ptr<Artist> MediaLibrary::createArtist( const std::string& name )
}
}
std::vector<ArtistPtr> MediaLibrary::artists( bool includeAll,
SortingCriteria sort,
bool desc ) const
Query<IArtist> MediaLibrary::artists( bool includeAll, SortingCriteria sort,
bool desc ) const
{
return Artist::listAll( this, includeAll, sort, desc );
}
......@@ -584,7 +584,7 @@ PlaylistPtr MediaLibrary::createPlaylist( const std::string& name )
}
}
std::vector<PlaylistPtr> MediaLibrary::playlists( SortingCriteria sort, bool desc )
Query<IPlaylist> MediaLibrary::playlists( SortingCriteria sort, bool desc )
{
return Playlist::listAll( this, sort, desc );
}
......@@ -620,12 +620,12 @@ bool MediaLibrary::addToStreamHistory( MediaPtr media )
}
}
std::vector<HistoryPtr> MediaLibrary::lastStreamsPlayed() const
Query<IHistoryEntry> MediaLibrary::lastStreamsPlayed() const
{
return History::fetch( this );
}
std::vector<MediaPtr> MediaLibrary::lastMediaPlayed() const
Query<IMedia> MediaLibrary::lastMediaPlayed() const
{
return Media::fetchHistory( this );
}
......@@ -654,30 +654,19 @@ MediaSearchAggregate MediaLibrary::searchMedia( const std::string& title,
{
if ( validateSearchPattern( title ) == false )
return {};
auto tmp = Media::search( this, title, sort, desc );
MediaSearchAggregate res;
for ( auto& m : tmp )
{
switch ( m->subType() )
{
case IMedia::SubType::AlbumTrack:
res.tracks.emplace_back( std::move( m ) );
break;
case IMedia::SubType::Movie:
res.movies.emplace_back( std::move( m ) );
break;
case IMedia::SubType::ShowEpisode:
res.episodes.emplace_back( std::move( m ) );
break;
default:
res.others.emplace_back( std::move( m ) );
break;
}
}
res.episodes = Media::search( this, title, IMedia::SubType::ShowEpisode,
sort, desc );
res.movies = Media::search( this, title, IMedia::SubType::Movie,
sort, desc );
res.others = Media::search( this, title, IMedia::SubType::Unknown,
sort, desc );
res.tracks = Media::search( this, title, IMedia::SubType::AlbumTrack,
sort, desc );
return res;
}
std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name,
Query<IPlaylist> MediaLibrary::searchPlaylists( const std::string& name,
SortingCriteria sort,
bool desc ) const
{
......@@ -686,7 +675,7 @@ std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name,
return Playlist::search( this, name, sort, desc );
}
std::vector<AlbumPtr> MediaLibrary::searchAlbums( const std::string& pattern,
Query<IAlbum> MediaLibrary::searchAlbums( const std::string& pattern,
SortingCriteria sort, bool desc ) const
{
if ( validateSearchPattern( pattern ) == false )
......@@ -694,14 +683,14 @@ std::vector<AlbumPtr> MediaLibrary::searchAlbums( const std::string& pattern,
return Album::search( this, pattern, sort, desc );
}
std::vector<GenrePtr> MediaLibrary::searchGenre( const std::string& genre ) const
Query<IGenre> MediaLibrary::searchGenre( const std::string& genre ) const
{
if ( validateSearchPattern( genre ) == false )
return {};
return Genre::search( this, genre );
}
std::vector<ArtistPtr> MediaLibrary::searchArtists( const std::string& name,
Query<IArtist> MediaLibrary::searchArtists( const std::string& name,
SortingCriteria sort,
bool desc ) const
{
......@@ -1222,11 +1211,11 @@ void MediaLibrary::setDiscoverNetworkEnabled( bool enabled )
}
}
std::vector<FolderPtr> MediaLibrary::entryPoints() const
Query<IFolder> MediaLibrary::entryPoints() const
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE parent_id IS NULL"
static const std::string req = "FROM " + policy::FolderTable::Name + " WHERE parent_id IS NULL"
" AND is_blacklisted = 0";
return Folder::fetchAll<IFolder>( this, req );
return make_query<Folder, IFolder>( this, "*", req );
}
FolderPtr MediaLibrary::folder( const std::string& mrl ) const
......
......@@ -72,8 +72,8 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual MediaPtr media( int64_t mediaId ) const override;
virtual MediaPtr media( const std::string& path ) const override;
virtual MediaPtr addMedia( const std::string& mrl ) override;
virtual std::vector<MediaPtr> audioFiles( SortingCriteria sort, bool desc) const override;
virtual std::vector<MediaPtr> videoFiles( SortingCriteria sort, bool desc) const override;
virtual Query<IMedia> audioFiles( SortingCriteria sort, bool desc) const override;
virtual Query<IMedia> videoFiles( SortingCriteria sort, bool desc) const override;
virtual void addDiscoveredFile( std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
......@@ -87,9 +87,9 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual AlbumPtr album( int64_t id ) const override;
std::shared_ptr<Album> createAlbum( const std::string& title, int64_t thumbnailId );
virtual std::vector<AlbumPtr> albums(SortingCriteria sort, bool desc) const override;
virtual Query<IAlbum> albums(SortingCriteria sort, bool desc) const override;
virtual std::vector<GenrePtr> genres( SortingCriteria sort, bool desc ) const override;
virtual Query<IGenre> genres( SortingCriteria sort, bool desc ) const override;
virtual GenrePtr genre( int64_t id ) const override;
virtual ShowPtr show( const std::string& name ) const override;
......@@ -101,30 +101,30 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual ArtistPtr artist( int64_t id ) const override;
ArtistPtr artist( const std::string& name );
std::shared_ptr<Artist> createArtist( const std::string& name );
virtual std::vector<ArtistPtr> artists( bool includeAll,
virtual Query<IArtist> artists( bool includeAll,
SortingCriteria sort,
bool desc ) const override;
virtual PlaylistPtr createPlaylist( const std::string& name ) override;
virtual std::vector<PlaylistPtr> playlists( SortingCriteria sort, bool desc ) override;
virtual Query<IPlaylist> playlists( SortingCriteria sort, bool desc ) override;
virtual PlaylistPtr playlist( int64_t id ) const override;
virtual bool deletePlaylist( int64_t playlistId ) override;
virtual bool addToStreamHistory( MediaPtr media ) override;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const override;
virtual std::vector<MediaPtr> lastMediaPlayed() const override;
virtual Query<IHistoryEntry> lastStreamsPlayed() const override;
virtual Query<IMedia> lastMediaPlayed() const override;
virtual bool clearHistory() override;
virtual MediaSearchAggregate searchMedia( const std::string& title,
SortingCriteria sort, bool desc ) const override;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name,
virtual Query<IPlaylist> searchPlaylists( const std::string& name,
SortingCriteria sort,
bool desc ) const override;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern,
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
SortingCriteria sort,
bool desc ) const override;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const override;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
virtual Query<IGenre> searchGenre( const std::string& genre ) const override;
virtual Query<IArtist> searchArtists( const std::string& name,
SortingCriteria sort,
bool desc ) const override;
virtual SearchAggregate search( const std::string& pattern,
......@@ -132,7 +132,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual void discover( const std::string& entryPoint ) override;
virtual void setDiscoverNetworkEnabled( bool enabled ) override;
virtual std::vector<FolderPtr> entryPoints() const override;
virtual Query<IFolder> entryPoints() const override;
virtual FolderPtr folder( const std::string& mrl ) const override;
virtual void removeEntryPoint( const std::string& entryPoint ) override;
virtual void banFolder( const std::string& path ) override;
......
......@@ -27,6 +27,7 @@
#include "Movie.h"
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -109,11 +110,11 @@ bool Movie::setImdbId( const std::string& imdbId )
return true;
}
std::vector<MediaPtr> Movie::files()
Query<IMedia> Movie::media()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
static const std::string req = "FROM " + policy::MediaTable::Name
+ " WHERE movie_id = ?";
return Media::fetchAll<IMedia>( m_ml, req, m_id );
return make_query<Media, IMedia>( m_ml, "*", req, m_id );
}
void Movie::createTable( sqlite::Connection* dbConnection )
......
......@@ -56,7 +56,7 @@ class Movie : public IMovie, public DatabaseHelpers<Movie, policy::MovieTable>
bool setArtworkMrl(const std::string& artworkMrl);
virtual const std::string& imdbId() const override;
bool setImdbId(const std::string& imdbId);
virtual std::vector<MediaPtr> files() override;
virtual Query<IMedia> media() override;
static void createTable( sqlite::Connection* dbConnection );
static std::shared_ptr<Movie> create( MediaLibraryPtr ml, int64_t mediaId, const std::string& title );
......
......@@ -28,6 +28,8 @@
#include "Media.h"
#include "database/SqliteQuery.h"
namespace medialibrary
{
......@@ -116,13 +118,13 @@ const std::string& Playlist::artworkMrl() const
return m_artworkMrl;
}
std::vector<MediaPtr> Playlist::media() const
Query<IMedia> Playlist::media() const
{
static const std::string req = "SELECT m.* FROM " + policy::MediaTable::Name + " m "
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";
return Media::fetchAll<IMedia>( m_ml, req, m_id );
return make_query<Media, IMedia>( m_ml, "m.*", req, m_id );
}
bool Playlist::append( int64_t mediaId )
......@@ -260,20 +262,20 @@ void Playlist::createTriggers( sqlite::Connection* dbConn )
sqlite::Tools::executeRequest( dbConn, vtriggerDelete );
}
std::vector<PlaylistPtr> Playlist::search( MediaLibraryPtr ml, const std::string& name,
Query<IPlaylist> Playlist::search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc )
{
std::string req = "SELECT * FROM " + policy::PlaylistTable::Name + " WHERE id_playlist IN "
std::string req = "FROM " + policy::PlaylistTable::Name + " WHERE id_playlist IN "
"(SELECT rowid FROM " + policy::PlaylistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')";
req += sortRequest( sort, desc );
return fetchAll<IPlaylist>(