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

Attempt to use Cache methods more often

parent e45402a4
...@@ -91,7 +91,7 @@ std::vector<std::shared_ptr<IAlbumTrack>> Album::tracks() const ...@@ -91,7 +91,7 @@ std::vector<std::shared_ptr<IAlbumTrack>> Album::tracks() const
{ {
static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name
+ " WHERE album_id = ?"; + " WHERE album_id = ?";
return sqlite::Tools::fetchAll<AlbumTrack, IAlbumTrack>( m_dbConnection, req, m_id ); return AlbumTrack::fetchAll( m_dbConnection, req, m_id );
} }
AlbumTrackPtr Album::addTrack( const std::string& title, unsigned int trackNb ) AlbumTrackPtr Album::addTrack( const std::string& title, unsigned int trackNb )
...@@ -104,7 +104,7 @@ std::vector<ArtistPtr> Album::artists() const ...@@ -104,7 +104,7 @@ std::vector<ArtistPtr> Album::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art " static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"LEFT JOIN AlbumArtistRelation aar ON aar.id_artist = art.id_artist " "LEFT JOIN AlbumArtistRelation aar ON aar.id_artist = art.id_artist "
"WHERE aar.id_album = ?"; "WHERE aar.id_album = ?";
return sqlite::Tools::fetchAll<Artist, IArtist>( m_dbConnection, req, m_id ); return Artist::fetchAll( m_dbConnection, req, m_id );
} }
bool Album::addArtist( ArtistPtr artist ) bool Album::addArtist( ArtistPtr artist )
......
...@@ -133,12 +133,12 @@ std::vector<ArtistPtr> AlbumTrack::artists() const ...@@ -133,12 +133,12 @@ std::vector<ArtistPtr> AlbumTrack::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art " static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"LEFT JOIN TrackArtistRelation tar ON tar.id_artist = art.id_artist " "LEFT JOIN TrackArtistRelation tar ON tar.id_artist = art.id_artist "
"WHERE tar.id_track = ?"; "WHERE tar.id_track = ?";
return sqlite::Tools::fetchAll<Artist, IArtist>( m_dbConnection, req, m_id ); return Artist::fetchAll( m_dbConnection, req, m_id );
} }
std::vector<FilePtr> AlbumTrack::files() std::vector<FilePtr> AlbumTrack::files()
{ {
static const std::string req = "SELECT * FROM " + policy::FileTable::Name static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE album_track_id = ? "; + " WHERE album_track_id = ? ";
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection, req, m_id ); return File::fetchAll( m_dbConnection, req, m_id );
} }
...@@ -53,7 +53,7 @@ std::vector<AlbumPtr> Artist::albums() const ...@@ -53,7 +53,7 @@ std::vector<AlbumPtr> Artist::albums() const
static const std::string req = "SELECT alb.* FROM " + policy::AlbumTable::Name + " alb " static const std::string req = "SELECT alb.* FROM " + policy::AlbumTable::Name + " alb "
"LEFT JOIN AlbumArtistRelation aar ON aar.id_album = alb.id_album " "LEFT JOIN AlbumArtistRelation aar ON aar.id_album = alb.id_album "
"WHERE aar.id_artist = ?"; "WHERE aar.id_artist = ?";
return sqlite::Tools::fetchAll<Album, IAlbum>( m_dbConnection, req, m_id ); return Album::fetchAll( m_dbConnection, req, m_id );
} }
std::vector<AlbumTrackPtr> Artist::tracks() const std::vector<AlbumTrackPtr> Artist::tracks() const
...@@ -61,7 +61,7 @@ std::vector<AlbumTrackPtr> Artist::tracks() const ...@@ -61,7 +61,7 @@ std::vector<AlbumTrackPtr> Artist::tracks() const
static const std::string req = "SELECT tra.* FROM " + policy::AlbumTrackTable::Name + " tra " static const std::string req = "SELECT tra.* FROM " + policy::AlbumTrackTable::Name + " tra "
"LEFT JOIN TrackArtistRelation tar ON tar.id_track = tra.id_track " "LEFT JOIN TrackArtistRelation tar ON tar.id_track = tra.id_track "
"WHERE tar.id_artist = ?"; "WHERE tar.id_artist = ?";
return sqlite::Tools::fetchAll<AlbumTrack, IAlbumTrack>( m_dbConnection, req, m_id ); return AlbumTrack::fetchAll( m_dbConnection, req, m_id );
} }
bool Artist::createTable( DBConnection dbConnection ) bool Artist::createTable( DBConnection dbConnection )
......
...@@ -67,7 +67,7 @@ AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& co ...@@ -67,7 +67,7 @@ AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& co
{ {
static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
+ " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?"; + " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?";
return sqlite::Tools::fetchOne<AudioTrack>( dbConnection, req, codec, bitrate, sampleRate, nbChannels ); return AudioTrack::fetchOne( dbConnection, req, codec, bitrate, sampleRate, nbChannels );
} }
AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec, AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec,
......
...@@ -128,7 +128,7 @@ std::vector<std::shared_ptr<ILabel> > File::labels() ...@@ -128,7 +128,7 @@ std::vector<std::shared_ptr<ILabel> > File::labels()
static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l " static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label " "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
"WHERE lfr.id_file = ?"; "WHERE lfr.id_file = ?";
return sqlite::Tools::fetchAll<Label, ILabel>( m_dbConnection, req, m_id ); return Label::fetchAll( m_dbConnection, req, m_id );
} }
int File::playCount() const int File::playCount() const
...@@ -180,7 +180,7 @@ std::vector<VideoTrackPtr> File::videoTracks() ...@@ -180,7 +180,7 @@ std::vector<VideoTrackPtr> File::videoTracks()
static const std::string req = "SELECT t.* FROM " + policy::VideoTrackTable::Name + static const std::string req = "SELECT t.* FROM " + policy::VideoTrackTable::Name +
" t LEFT JOIN VideoTrackFileRelation vtfr ON vtfr.id_track = t.id_track" " t LEFT JOIN VideoTrackFileRelation vtfr ON vtfr.id_track = t.id_track"
" WHERE vtfr.id_file = ?"; " WHERE vtfr.id_file = ?";
return sqlite::Tools::fetchAll<VideoTrack, IVideoTrack>( m_dbConnection, req, m_id ); return VideoTrack::fetchAll( m_dbConnection, req, m_id );
} }
bool File::addAudioTrack( const std::string& codec, unsigned int bitrate, bool File::addAudioTrack( const std::string& codec, unsigned int bitrate,
...@@ -203,7 +203,7 @@ std::vector<AudioTrackPtr> File::audioTracks() ...@@ -203,7 +203,7 @@ std::vector<AudioTrackPtr> File::audioTracks()
static const std::string req = "SELECT t.* FROM " + policy::AudioTrackTable::Name + static const std::string req = "SELECT t.* FROM " + policy::AudioTrackTable::Name +
" t LEFT JOIN AudioTrackFileRelation atfr ON atfr.id_track = t.id_track" " t LEFT JOIN AudioTrackFileRelation atfr ON atfr.id_track = t.id_track"
" WHERE atfr.id_file = ?"; " WHERE atfr.id_file = ?";
return sqlite::Tools::fetchAll<AudioTrack, IAudioTrack>( m_dbConnection, req, m_id ); return AudioTrack::fetchAll( m_dbConnection, req, m_id );
} }
const std::string &File::snapshot() const std::string &File::snapshot()
......
...@@ -82,14 +82,14 @@ std::vector<FilePtr> Folder::files() ...@@ -82,14 +82,14 @@ std::vector<FilePtr> Folder::files()
{ {
static const std::string req = "SELECT * FROM " + policy::FileTable::Name + static const std::string req = "SELECT * FROM " + policy::FileTable::Name +
" WHERE folder_id = ?"; " WHERE folder_id = ?";
return sqlite::Tools::fetchAll<File, IFile>( m_dbConection, req, m_id ); return File::fetchAll( m_dbConection, req, m_id );
} }
std::vector<FolderPtr> Folder::folders() std::vector<FolderPtr> Folder::folders()
{ {
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name + static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE id_parent = ?"; " WHERE id_parent = ?";
return sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConection, req, m_id ); return fetchAll( m_dbConection, req, m_id );
} }
FolderPtr Folder::parent() FolderPtr Folder::parent()
...@@ -97,7 +97,7 @@ FolderPtr Folder::parent() ...@@ -97,7 +97,7 @@ FolderPtr Folder::parent()
//FIXME: use path to be able to fetch from cache? //FIXME: use path to be able to fetch from cache?
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name + static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE id_folder = ?"; " WHERE id_folder = ?";
return sqlite::Tools::fetchOne<Folder>( m_dbConection, req, m_parent ); return fetchOne( m_dbConection, req, m_parent );
} }
unsigned int Folder::lastModificationDate() unsigned int Folder::lastModificationDate()
......
...@@ -38,7 +38,7 @@ std::vector<FilePtr> Label::files() ...@@ -38,7 +38,7 @@ std::vector<FilePtr> Label::files()
static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name + " f " static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name + " f "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file " "LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
"WHERE lfr.id_label = ?"; "WHERE lfr.id_label = ?";
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection, req, m_id ); return File::fetchAll( m_dbConnection, req, m_id );
} }
LabelPtr Label::create(DBConnection dbConnection, const std::string& name ) LabelPtr Label::create(DBConnection dbConnection, const std::string& name )
......
...@@ -146,14 +146,14 @@ std::vector<FilePtr> MediaLibrary::audioFiles() ...@@ -146,14 +146,14 @@ std::vector<FilePtr> MediaLibrary::audioFiles()
static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE type = ?"; static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE type = ?";
//FIXME: Replace this with template magic in sqlite's traits //FIXME: Replace this with template magic in sqlite's traits
using type_t = std::underlying_type<IFile::Type>::type; using type_t = std::underlying_type<IFile::Type>::type;
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection.get(), req, static_cast<type_t>( IFile::Type::AudioType ) ); return File::fetchAll( m_dbConnection.get(), req, static_cast<type_t>( IFile::Type::AudioType ) );
} }
std::vector<FilePtr> MediaLibrary::videoFiles() std::vector<FilePtr> MediaLibrary::videoFiles()
{ {
static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE type = ?"; static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE type = ?";
using type_t = std::underlying_type<IFile::Type>::type; using type_t = std::underlying_type<IFile::Type>::type;
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection.get(), req, static_cast<type_t>( IFile::Type::VideoType ) ); return File::fetchAll( m_dbConnection.get(), req, static_cast<type_t>( IFile::Type::VideoType ) );
} }
FilePtr MediaLibrary::file( const std::string& path ) FilePtr MediaLibrary::file( const std::string& path )
...@@ -244,7 +244,7 @@ AlbumPtr MediaLibrary::album(const std::string& title ) ...@@ -244,7 +244,7 @@ AlbumPtr MediaLibrary::album(const std::string& title )
// We can't use Cache helper, since albums are cached by primary keys // We can't use Cache helper, since albums are cached by primary keys
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name + static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE title = ?"; " WHERE title = ?";
return sqlite::Tools::fetchOne<Album>( m_dbConnection.get(), req, title ); return Album::fetchOne( m_dbConnection.get(), req, title );
} }
AlbumPtr MediaLibrary::createAlbum(const std::string& title ) AlbumPtr MediaLibrary::createAlbum(const std::string& title )
...@@ -261,7 +261,7 @@ ShowPtr MediaLibrary::show(const std::string& name) ...@@ -261,7 +261,7 @@ ShowPtr MediaLibrary::show(const std::string& name)
{ {
static const std::string req = "SELECT * FROM " + policy::ShowTable::Name static const std::string req = "SELECT * FROM " + policy::ShowTable::Name
+ " WHERE name = ?"; + " WHERE name = ?";
return sqlite::Tools::fetchOne<Show>( m_dbConnection.get(), req, name ); return Show::fetchOne( m_dbConnection.get(), req, name );
} }
ShowPtr MediaLibrary::createShow(const std::string& name) ShowPtr MediaLibrary::createShow(const std::string& name)
...@@ -273,7 +273,7 @@ MoviePtr MediaLibrary::movie( const std::string& title ) ...@@ -273,7 +273,7 @@ MoviePtr MediaLibrary::movie( const std::string& title )
{ {
static const std::string req = "SELECT * FROM " + policy::MovieTable::Name static const std::string req = "SELECT * FROM " + policy::MovieTable::Name
+ " WHERE title = ?"; + " WHERE title = ?";
return sqlite::Tools::fetchOne<Movie>( m_dbConnection.get(), req, title ); return Movie::fetchOne( m_dbConnection.get(), req, title );
} }
MoviePtr MediaLibrary::createMovie( const std::string& title ) MoviePtr MediaLibrary::createMovie( const std::string& title )
...@@ -285,7 +285,7 @@ ArtistPtr MediaLibrary::artist(const std::string &name) ...@@ -285,7 +285,7 @@ ArtistPtr MediaLibrary::artist(const std::string &name)
{ {
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name
+ " WHERE name = ?"; + " WHERE name = ?";
return sqlite::Tools::fetchOne<Artist>( m_dbConnection.get(), req, name ); return Artist::fetchOne( m_dbConnection.get(), req, name );
} }
ArtistPtr MediaLibrary::createArtist( const std::string& name ) ArtistPtr MediaLibrary::createArtist( const std::string& name )
...@@ -296,7 +296,7 @@ ArtistPtr MediaLibrary::createArtist( const std::string& name ) ...@@ -296,7 +296,7 @@ ArtistPtr MediaLibrary::createArtist( const std::string& name )
std::vector<ArtistPtr> MediaLibrary::artists() const std::vector<ArtistPtr> MediaLibrary::artists() const
{ {
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name; static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name;
return sqlite::Tools::fetchAll<Artist, IArtist>( m_dbConnection.get(), req ); return Artist::fetchAll( m_dbConnection.get(), req );
} }
void MediaLibrary::addMetadataService(std::unique_ptr<IMetadataService> service) void MediaLibrary::addMetadataService(std::unique_ptr<IMetadataService> service)
......
...@@ -109,7 +109,7 @@ std::vector<FilePtr> Movie::files() ...@@ -109,7 +109,7 @@ std::vector<FilePtr> Movie::files()
{ {
static const std::string req = "SELECT * FROM " + policy::FileTable::Name static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE movie_id = ?"; + " WHERE movie_id = ?";
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection, req, m_id ); return File::fetchAll( m_dbConnection, req, m_id );
} }
bool Movie::createTable( DBConnection dbConnection ) bool Movie::createTable( DBConnection dbConnection )
......
...@@ -110,7 +110,7 @@ std::vector<ShowEpisodePtr> Show::episodes() ...@@ -110,7 +110,7 @@ std::vector<ShowEpisodePtr> Show::episodes()
{ {
static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
+ " WHERE show_id = ?"; + " WHERE show_id = ?";
return sqlite::Tools::fetchAll<ShowEpisode, IShowEpisode>( m_dbConnection, req, m_id ); return ShowEpisode::fetchAll( m_dbConnection, req, m_id );
} }
bool Show::destroy() bool Show::destroy()
......
...@@ -123,7 +123,7 @@ std::vector<FilePtr> ShowEpisode::files() ...@@ -123,7 +123,7 @@ std::vector<FilePtr> ShowEpisode::files()
{ {
static const std::string req = "SELECT * FROM " + policy::FileTable::Name static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE show_episode_id = ?"; + " WHERE show_episode_id = ?";
return sqlite::Tools::fetchAll<File, IFile>( m_dbConnection, req, m_id ); return File::fetchAll( m_dbConnection, req, m_id );
} }
bool ShowEpisode::destroy() bool ShowEpisode::destroy()
......
...@@ -77,5 +77,5 @@ VideoTrackPtr VideoTrack::fetch( DBConnection dbConnection, const std::string& c ...@@ -77,5 +77,5 @@ VideoTrackPtr VideoTrack::fetch( DBConnection dbConnection, const std::string& c
{ {
static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name + static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
" WHERE codec = ? AND width = ? AND height = ? AND fps = ?"; " WHERE codec = ? AND width = ? AND height = ? AND fps = ?";
return sqlite::Tools::fetchOne<VideoTrack>( dbConnection, req, codec, width, height, fps ); return VideoTrack::fetchOne( dbConnection, req, codec, width, height, fps );
} }
...@@ -68,14 +68,26 @@ class Cache ...@@ -68,14 +68,26 @@ class Cache
return res; return res;
} }
template <typename... Args>
static std::shared_ptr<IMPL> fetchOne( DBConnection dbConnection, const std::string& req, Args&&... args )
{
return sqlite::Tools::fetchOne<IMPL>( dbConnection, req, std::forward<Args>( args )... );
}
/* /*
* Will fetch all elements from the database & cache them. * Will fetch all elements from the database & cache them.
* *
*/ */
static std::vector<std::shared_ptr<INTF>> fetchAll( DBConnection dbConnectionWeak ) static std::vector<std::shared_ptr<INTF>> fetchAll( DBConnection dbConnection )
{ {
static const std::string req = "SELECT * FROM " + TABLEPOLICY::Name; static const std::string req = "SELECT * FROM " + TABLEPOLICY::Name;
return sqlite::Tools::fetchAll<IMPL, INTF>( dbConnectionWeak, req.c_str() ); return sqlite::Tools::fetchAll<IMPL, INTF>( dbConnection, req.c_str() );
}
template <typename... Args>
static std::vector<std::shared_ptr<INTF>> fetchAll( DBConnection dbConnection, const std::string &req, Args&&... args )
{
return sqlite::Tools::fetchAll<IMPL, INTF>( dbConnection, req, std::forward<Args>( args )... );
} }
static std::shared_ptr<IMPL> load( DBConnection dbConnection, sqlite3_stmt* stmt ) static std::shared_ptr<IMPL> load( DBConnection dbConnection, sqlite3_stmt* stmt )
......
...@@ -58,7 +58,7 @@ void FsDiscoverer::reload() ...@@ -58,7 +58,7 @@ void FsDiscoverer::reload()
//FIXME: This shouldn't be done for "removable"/network files //FIXME: This shouldn't be done for "removable"/network files
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent IS NULL"; + " WHERE id_parent IS NULL";
auto rootFolders = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConn, req ); auto rootFolders = Folder::fetchAll( m_dbConn, req );
for ( const auto f : rootFolders ) for ( const auto f : rootFolders )
{ {
auto folder = m_fsFactory->createDirectory( f->path() ); auto folder = m_fsFactory->createDirectory( f->path() );
...@@ -83,7 +83,7 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFold ...@@ -83,7 +83,7 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFold
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent = ?"; + " WHERE id_parent = ?";
LOG_INFO( "Checking for modifications in ", folder->path() ); LOG_INFO( "Checking for modifications in ", folder->path() );
auto subFoldersInDB = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConn, req, parentFolder->id() ); auto subFoldersInDB = Folder::fetchAll( m_dbConn, req, parentFolder->id() );
for ( const auto& subFolderPath : folder->dirs() ) for ( const auto& subFolderPath : folder->dirs() )
{ {
auto subFolder = m_fsFactory->createDirectory( subFolderPath ); auto subFolder = m_fsFactory->createDirectory( subFolderPath );
...@@ -130,7 +130,7 @@ void FsDiscoverer::checkFiles( fs::IDirectory* folder, FolderPtr parentFolder ) ...@@ -130,7 +130,7 @@ void FsDiscoverer::checkFiles( fs::IDirectory* folder, FolderPtr parentFolder )
LOG_INFO( "Checking file in ", folder->path() ); LOG_INFO( "Checking file in ", folder->path() );
static const std::string req = "SELECT * FROM " + policy::FileTable::Name static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE folder_id = ?"; + " WHERE folder_id = ?";
auto files = sqlite::Tools::fetchAll<File, IFile>( m_dbConn, req, parentFolder->id() ); auto files = File::fetchAll( m_dbConn, req, parentFolder->id() );
for ( const auto& filePath : folder->files() ) for ( const auto& filePath : folder->files() )
{ {
auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IFile>& f) { auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IFile>& f) {
......
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