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
{
static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name
+ " 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 )
......@@ -104,7 +104,7 @@ std::vector<ArtistPtr> Album::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"LEFT JOIN AlbumArtistRelation aar ON aar.id_artist = art.id_artist "
"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 )
......
......@@ -133,12 +133,12 @@ std::vector<ArtistPtr> AlbumTrack::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"LEFT JOIN TrackArtistRelation tar ON tar.id_artist = art.id_artist "
"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()
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " 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
static const std::string req = "SELECT alb.* FROM " + policy::AlbumTable::Name + " alb "
"LEFT JOIN AlbumArtistRelation aar ON aar.id_album = alb.id_album "
"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
......@@ -61,7 +61,7 @@ std::vector<AlbumTrackPtr> Artist::tracks() const
static const std::string req = "SELECT tra.* FROM " + policy::AlbumTrackTable::Name + " tra "
"LEFT JOIN TrackArtistRelation tar ON tar.id_track = tra.id_track "
"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 )
......
......@@ -67,7 +67,7 @@ AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& co
{
static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
+ " 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,
......
......@@ -128,7 +128,7 @@ std::vector<std::shared_ptr<ILabel> > File::labels()
static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
"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
......@@ -180,7 +180,7 @@ std::vector<VideoTrackPtr> File::videoTracks()
static const std::string req = "SELECT t.* FROM " + policy::VideoTrackTable::Name +
" t LEFT JOIN VideoTrackFileRelation vtfr ON vtfr.id_track = t.id_track"
" 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,
......@@ -203,7 +203,7 @@ std::vector<AudioTrackPtr> File::audioTracks()
static const std::string req = "SELECT t.* FROM " + policy::AudioTrackTable::Name +
" t LEFT JOIN AudioTrackFileRelation atfr ON atfr.id_track = t.id_track"
" 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()
......
......@@ -82,14 +82,14 @@ std::vector<FilePtr> Folder::files()
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name +
" 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()
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE id_parent = ?";
return sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConection, req, m_id );
return fetchAll( m_dbConection, req, m_id );
}
FolderPtr Folder::parent()
......@@ -97,7 +97,7 @@ FolderPtr Folder::parent()
//FIXME: use path to be able to fetch from cache?
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE id_folder = ?";
return sqlite::Tools::fetchOne<Folder>( m_dbConection, req, m_parent );
return fetchOne( m_dbConection, req, m_parent );
}
unsigned int Folder::lastModificationDate()
......
......@@ -38,7 +38,7 @@ std::vector<FilePtr> Label::files()
static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name + " f "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
"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 )
......
......@@ -146,14 +146,14 @@ std::vector<FilePtr> MediaLibrary::audioFiles()
static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE type = ?";
//FIXME: Replace this with template magic in sqlite's traits
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()
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name + " WHERE 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 )
......@@ -244,7 +244,7 @@ AlbumPtr MediaLibrary::album(const std::string& title )
// We can't use Cache helper, since albums are cached by primary keys
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" 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 )
......@@ -261,7 +261,7 @@ ShowPtr MediaLibrary::show(const std::string& name)
{
static const std::string req = "SELECT * FROM " + policy::ShowTable::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)
......@@ -273,7 +273,7 @@ MoviePtr MediaLibrary::movie( const std::string& title )
{
static const std::string req = "SELECT * FROM " + policy::MovieTable::Name
+ " 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 )
......@@ -285,7 +285,7 @@ ArtistPtr MediaLibrary::artist(const std::string &name)
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::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 )
......@@ -296,7 +296,7 @@ ArtistPtr MediaLibrary::createArtist( const std::string& name )
std::vector<ArtistPtr> MediaLibrary::artists() const
{
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)
......
......@@ -109,7 +109,7 @@ std::vector<FilePtr> Movie::files()
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " 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 )
......
......@@ -110,7 +110,7 @@ std::vector<ShowEpisodePtr> Show::episodes()
{
static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
+ " 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()
......
......@@ -123,7 +123,7 @@ std::vector<FilePtr> ShowEpisode::files()
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " 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()
......
......@@ -77,5 +77,5 @@ VideoTrackPtr VideoTrack::fetch( DBConnection dbConnection, const std::string& c
{
static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
" 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
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.
*
*/
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;
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 )
......
......@@ -58,7 +58,7 @@ void FsDiscoverer::reload()
//FIXME: This shouldn't be done for "removable"/network files
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " 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 )
{
auto folder = m_fsFactory->createDirectory( f->path() );
......@@ -83,7 +83,7 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFold
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent = ?";
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() )
{
auto subFolder = m_fsFactory->createDirectory( subFolderPath );
......@@ -130,7 +130,7 @@ void FsDiscoverer::checkFiles( fs::IDirectory* folder, FolderPtr parentFolder )
LOG_INFO( "Checking file in ", folder->path() );
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " 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() )
{
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