From 8aeae86110ea6ccda18dc5364a55a9b8d6362c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Beauz=C3=A9e-Luyssen?= Date: Fri, 23 May 2014 19:21:43 +0300 Subject: [PATCH] Labels: Do not store a list of files. --- include/ILabel.h | 2 +- src/Label.cpp | 17 +++++------------ src/Label.h | 3 +-- test/Labels.cpp | 9 +++++++-- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/ILabel.h b/include/ILabel.h index 93f9b3575..740729e1f 100644 --- a/include/ILabel.h +++ b/include/ILabel.h @@ -13,7 +13,7 @@ class ILabel virtual unsigned int id() const = 0; virtual const std::string& name() = 0; - virtual std::vector& files() = 0; + virtual bool files( std::vector& files ) = 0; }; #endif // ILABEL_H diff --git a/src/Label.cpp b/src/Label.cpp index e167029a3..346877b1c 100644 --- a/src/Label.cpp +++ b/src/Label.cpp @@ -12,7 +12,6 @@ unsigned int Label::* const policy::LabelTable::PrimaryKey = &Label::m_id; Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt ) : m_dbConnection( dbConnection ) - , m_files( NULL ) { m_id = sqlite3_column_int( stmt, 0 ); m_name = (const char*)sqlite3_column_text( stmt, 1 ); @@ -22,7 +21,6 @@ Label::Label( const std::string& name ) : m_dbConnection( NULL ) , m_id( 0 ) , m_name( name ) - , m_files( NULL ) { } @@ -37,17 +35,12 @@ const std::string& Label::name() return m_name; } -std::vector& Label::files() +bool Label::files( std::vector& files ) { - if ( m_files == nullptr ) - { - m_files = new std::vector>; - 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 = ?"; - SqliteTools::fetchAll( m_dbConnection, req.c_str(), *m_files, m_id ); - } - return *m_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 SqliteTools::fetchAll( m_dbConnection, req.c_str(), files, m_id ); } LabelPtr Label::create( sqlite3* dbConnection, const std::string& name ) diff --git a/src/Label.h b/src/Label.h index 87018965e..3943499a5 100644 --- a/src/Label.h +++ b/src/Label.h @@ -39,7 +39,7 @@ class Label : public ILabel, public Cache& files(); + virtual bool files( std::vector& files ); static LabelPtr create(sqlite3* dbConnection, const std::string& name ); static bool createTable( sqlite3* dbConnection ); @@ -47,7 +47,6 @@ class Label : public ILabel, public Cache>* m_files; friend class Cache; friend class policy::LabelTable; diff --git a/test/Labels.cpp b/test/Labels.cpp index bef12fa7f..39c0ace96 100644 --- a/test/Labels.cpp +++ b/test/Labels.cpp @@ -104,8 +104,13 @@ TEST_F( Labels, Files ) f2->addLabel( l2 ); f3->addLabel( l1 ); - auto label1Files = l1->files(); - auto label2Files = l2->files(); + std::vector label1Files; + std::vector label2Files; + + bool res = l1->files( label1Files ); + ASSERT_TRUE( res ); + res = l2->files( label2Files ); + ASSERT_TRUE( res ); ASSERT_EQ( label1Files.size(), 2u ); ASSERT_EQ( label2Files.size(), 1u ); -- GitLab