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

Handle label deletion

parent 3661fb88
......@@ -19,6 +19,8 @@ class IMediaLibrary
virtual FilePtr addFile( const std::string& path ) = 0;
virtual FilePtr file( const std::string& path ) = 0;
virtual LabelPtr createLabel( const std::string& label ) = 0;
virtual bool deleteLabel( const std::string& label ) = 0;
virtual bool deleteLabel( LabelPtr label ) = 0;
virtual bool files( std::vector<FilePtr>& res ) = 0;
};
......
......@@ -102,6 +102,19 @@ class Cache
return res;
}
static bool destroy( sqlite3* dbConnection, const std::shared_ptr<IMPL>& self )
{
std::lock_guard<std::mutex> lock( Mutex );
auto key = CACHEPOLICY::key( self );
auto it = Store.find( key );
if ( it != Store.end() )
Store.erase( it );
static const std::string req = "DELETE FROM " + TABLEPOLICY::Name + " WHERE " +
TABLEPOLICY::CacheColumn + " = ?";
return SqliteTools::destroy( dbConnection, req.c_str(), key );
}
private:
static std::unordered_map<typename CACHEPOLICY::KeyType, std::shared_ptr<IMPL> > Store;
static std::mutex Mutex;
......
......@@ -88,7 +88,7 @@ bool Label::createTable(sqlite3* dbConnection)
const std::string&policy::LabelCachePolicy::key( const std::shared_ptr<Label> self )
const std::string&policy::LabelCachePolicy::key( const std::shared_ptr<ILabel> self )
{
return self->name();
}
......
......@@ -21,7 +21,7 @@ struct LabelTable
struct LabelCachePolicy
{
typedef std::string KeyType;
static const std::string& key( const std::shared_ptr<Label> self );
static const std::string& key(const std::shared_ptr<ILabel> self );
static std::string key( sqlite3_stmt* stmt );
};
......
......@@ -60,3 +60,14 @@ LabelPtr MediaLibrary::createLabel( const std::string& label )
}
return l;
}
bool MediaLibrary::deleteLabel( const std::string& text )
{
auto label = Label::fetch( m_dbConnection, text );
return deleteLabel( label );
}
bool MediaLibrary::deleteLabel( LabelPtr label )
{
return Label::destroy( m_dbConnection, std::static_pointer_cast<Label>( label ) );
}
......@@ -13,8 +13,9 @@ class MediaLibrary : public IMediaLibrary
virtual bool files( std::vector<FilePtr>& res );
virtual FilePtr file( const std::string& path );
virtual FilePtr addFile( const std::string& path );
virtual LabelPtr createLabel(const std::string& label);
virtual LabelPtr createLabel( const std::string& label );
virtual bool deleteLabel(const std::string& text );
virtual bool deleteLabel( LabelPtr label );
private:
sqlite3* m_dbConnection;
};
......
......@@ -81,6 +81,17 @@ class SqliteTools
return result;
}
template <typename... Args>
static bool destroy( sqlite3* dbConnection, const char* req, const Args&... args )
{
auto stmt = executeRequest( dbConnection, req, args... );
if ( stmt == nullptr )
return false;
sqlite3_step( stmt );
sqlite3_finalize( stmt );
return sqlite3_changes( dbConnection ) > 0;
}
template <typename... Args>
static sqlite3_stmt* executeRequest( sqlite3* dbConnection, const char* req, const Args&... args )
{
......
......@@ -147,3 +147,28 @@ TEST_F( MLTest, FilesWithLabel )
ASSERT_TRUE( labelFile == f || labelFile == f3 );
}
}
TEST_F( MLTest, DeleteLabel )
{
auto f = ml->addFile( "/dev/null" );
auto l1 = ml->createLabel( "sea otter" );
auto l2 = ml->createLabel( "cony the cone" );
f->addLabel( l1 );
f->addLabel( l2 );
auto labels = f->labels();
ASSERT_EQ( labels.size(), 2u );
ml->deleteLabel( "sea otter" );
labels = f->labels();
ASSERT_EQ( labels.size(), 1u );
ml->deleteLabel( l2 );
labels = f->labels();
ASSERT_EQ( labels.size(), 0u );
// Nothing to delete anymore, this should fail gracefuly
bool res = ml->deleteLabel( l1 );
ASSERT_FALSE( res );
}
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