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

Folders: Split fetchAll in 2 separate methods

This fetchAll method was:
- hidding the DatabaseHelper method
- 2 fully separate cases, which belong in their own respective methods

Plus there was already an instance method for fetching sub folders
parent c4538412
......@@ -241,7 +241,9 @@ std::vector<std::shared_ptr<File>> Folder::files()
std::vector<std::shared_ptr<Folder>> Folder::folders()
{
return fetchAll( m_ml, m_id );
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE parent_id = ? AND is_blacklisted = 0 AND is_present = 1";
return DatabaseHelpers::fetchAll<Folder>( m_ml, req, m_id );
}
std::shared_ptr<Folder> Folder::parent()
......@@ -259,20 +261,11 @@ bool Folder::isPresent() const
return m_isPresent;
}
std::vector<std::shared_ptr<Folder>> Folder::fetchAll( MediaLibraryPtr ml, int64_t parentFolderId )
std::vector<std::shared_ptr<Folder>> Folder::fetchRootFolders( MediaLibraryPtr ml )
{
if ( parentFolderId == 0 )
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE parent_id IS NULL AND is_blacklisted = 0 AND is_present = 1";
return DatabaseHelpers::fetchAll<Folder>( ml, req );
}
else
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE parent_id = ? AND is_blacklisted = 0 AND is_present = 1";
return DatabaseHelpers::fetchAll<Folder>( ml, req, parentFolderId );
}
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE parent_id IS NULL AND is_blacklisted = 0 AND is_present = 1";
return DatabaseHelpers::fetchAll<Folder>( ml, req );
}
}
......@@ -63,7 +63,7 @@ public:
static bool createTable( DBConnection connection );
static std::shared_ptr<Folder> create( MediaLibraryPtr ml, const std::string& path, int64_t parentId, Device& device, fs::IDevice& deviceFs );
static bool blacklist( MediaLibraryPtr ml, const std::string& fullPath );
static std::vector<std::shared_ptr<Folder>> fetchAll( MediaLibraryPtr ml, int64_t parentFolderId );
static std::vector<std::shared_ptr<Folder>> fetchRootFolders( MediaLibraryPtr ml );
static std::shared_ptr<Folder> fromPath(MediaLibraryPtr ml, const std::string& fullPath );
static std::shared_ptr<Folder> blacklistedFolder(MediaLibraryPtr ml, const std::string& fullPath );
......
......@@ -90,7 +90,7 @@ bool FsDiscoverer::reload()
LOG_ERROR( "Refusing to reloading files with no storage device" );
return false;
}
auto rootFolders = Folder::fetchAll( m_ml, 0 );
auto rootFolders = Folder::fetchRootFolders( m_ml );
for ( const auto& f : rootFolders )
{
auto folder = m_fsFactory->createDirectory( f->path() );
......@@ -170,7 +170,7 @@ void FsDiscoverer::checkFolder( fs::IDirectory& currentFolderFs, Folder& current
// Don't try to fetch any potential sub folders if the folder was freshly added
std::vector<std::shared_ptr<Folder>> subFoldersInDB;
if ( newFolder == false )
subFoldersInDB = Folder::fetchAll( m_ml, currentFolder.id() );
subFoldersInDB = currentFolder.folders();
for ( const auto& subFolder : currentFolderFs.dirs() )
{
auto it = std::find_if( begin( subFoldersInDB ), end( subFoldersInDB ), [&subFolder](const std::shared_ptr<Folder>& 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