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

Folder: Add parenting relationship

parent bbd053c3
......@@ -12,4 +12,5 @@ public:
virtual const std::string& path() = 0;
// This will only returns the files in this immediate folder
virtual std::vector<FilePtr> files() = 0;
virtual FolderPtr parent() = 0;
};
......@@ -26,29 +26,43 @@ Folder::Folder( DBConnection dbConnection, sqlite3_stmt* stmt )
{
m_id = Traits<unsigned int>::Load( stmt, 0 );
m_path = Traits<std::string>::Load( stmt, 1 );
m_parent = Traits<unsigned int>::Load( stmt, 2 );
}
Folder::Folder( const std::string& path )
Folder::Folder( const std::string& path, unsigned int parent )
: m_path( path )
, m_parent( parent )
{
}
bool Folder::createTable(DBConnection connection)
{
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name + "("
+ "id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
"path TEXT UNIQUE ON CONFLICT FAIL"
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name +
"("
"id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
"path TEXT UNIQUE ON CONFLICT FAIL,"
"id_parent UNSIGNED INTEGER,"
"FOREIGN KEY (id_parent) REFERENCES " + policy::FolderTable::Name +
"(id_folder) ON DELETE CASCADE"
")";
return SqliteTools::executeRequest( connection, req );
}
FolderPtr Folder::create(DBConnection connection, const std::string& path)
FolderPtr Folder::create(DBConnection connection, const std::string& path, unsigned int parent )
{
auto self = std::make_shared<Folder>( path );
auto self = std::make_shared<Folder>( path, parent );
static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
"(path) VALUES(?)";
if ( _Cache::insert( connection, self, req, path ) == false )
return nullptr;
"(path, id_parent) VALUES(?, ?)";
if ( parent == 0 )
{
if ( _Cache::insert( connection, self, req, path, nullptr ) == false )
return nullptr;
}
else
{
if ( _Cache::insert( connection, self, req, path, parent ) == false )
return nullptr;
}
self->m_dbConection = connection;
return self;
}
......@@ -69,3 +83,11 @@ std::vector<FilePtr> Folder::files()
" WHERE folder_id = ?";
return SqliteTools::fetchAll<File, IFile>( m_dbConection, req, m_id );
}
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 SqliteTools::fetchOne<Folder>( m_dbConection, req, m_parent );
}
......@@ -31,20 +31,22 @@ class Folder : public IFolder, public Cache<Folder, IFolder, policy::FolderTable
public:
Folder(DBConnection dbConnection, sqlite3_stmt* stmt);
Folder( const std::string& path );
Folder(const std::string& path , unsigned int parent);
static bool createTable( DBConnection connection );
static FolderPtr create( DBConnection connection, const std::string& path );
static FolderPtr create(DBConnection connection, const std::string& path , unsigned int parent);
virtual unsigned int id() const override;
virtual const std::string& path() override;
virtual std::vector<FilePtr> files() override;
virtual FolderPtr parent() override;
private:
DBConnection m_dbConection;
unsigned int m_id;
std::string m_path;
unsigned int m_parent;
friend _Cache;
friend struct policy::FolderTable;
......
......@@ -106,18 +106,19 @@ FilePtr MediaLibrary::addFile( const std::string& path )
FolderPtr MediaLibrary::addFolder( const std::string& path )
{
std::queue<std::string> folders;
std::queue<std::pair<std::string, unsigned int>> folders;
FolderPtr root;
folders.emplace( path );
folders.emplace( path, 0 );
while ( folders.empty() == false )
{
std::unique_ptr<fs::IDirectory> dir;
auto currentFolder = folders.front();
folders.pop();
try
{
dir = m_fsFactory->createDirectory( folders.front() );
folders.pop();
dir = m_fsFactory->createDirectory( currentFolder.first );
}
catch ( std::runtime_error& )
{
......@@ -125,11 +126,10 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
// Otherwise, assume something went wrong in a subdirectory.
if (root == nullptr)
return nullptr;
folders.pop();
continue;
}
auto folder = Folder::create( m_dbConnection, dir->path() );
auto folder = Folder::create( m_dbConnection, dir->path(), currentFolder.second );
if ( folder == nullptr && root == nullptr )
return nullptr;
if ( root == nullptr )
......@@ -144,7 +144,7 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
std::cerr << "Failed to add file " << f << " to the media library" << std::endl;
}
for ( auto& f : dir->dirs() )
folders.emplace( f );
folders.emplace( f, folder->id() );
}
return root;
}
......
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