Commit 547e4567 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Media: Save all fields at once.

Instead of having a per-field SQL request.
parent 118fcff7
...@@ -45,6 +45,7 @@ unsigned int Media::* const policy::MediaTable::PrimaryKey = &Media::m_id; ...@@ -45,6 +45,7 @@ unsigned int Media::* const policy::MediaTable::PrimaryKey = &Media::m_id;
Media::Media( DBConnection dbConnection, sqlite::Row& row ) Media::Media( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection ) : m_dbConnection( dbConnection )
, m_changed( false )
{ {
row >> m_id row >> m_id
>> m_type >> m_type
...@@ -73,6 +74,7 @@ Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& t ...@@ -73,6 +74,7 @@ Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& t
, m_lastModificationDate( file->lastModificationDate() ) , m_lastModificationDate( file->lastModificationDate() )
, m_isParsed( false ) , m_isParsed( false )
, m_title( title ) , m_title( title )
, m_changed( false )
{ {
} }
...@@ -111,16 +113,12 @@ const std::string& Media::artist() const ...@@ -111,16 +113,12 @@ const std::string& Media::artist() const
return m_artist; return m_artist;
} }
bool Media::setArtist(const std::string& artist) void Media::setArtist(const std::string& artist)
{ {
if ( m_artist == artist ) if ( m_artist == artist )
return true; return;
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET artist = ? "
"WHERE id_media = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist, m_id ) == false )
return false;
m_artist = artist; m_artist = artist;
return true; m_changed = true;
} }
int64_t Media::duration() const int64_t Media::duration() const
...@@ -128,14 +126,12 @@ int64_t Media::duration() const ...@@ -128,14 +126,12 @@ int64_t Media::duration() const
return m_duration; return m_duration;
} }
bool Media::setDuration( int64_t duration ) void Media::setDuration( int64_t duration )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET duration = ? " if ( m_duration == duration )
"WHERE id_media = ?"; return;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, duration, m_id ) == false )
return false;
m_duration = duration; m_duration = duration;
return true; m_changed = true;
} }
std::shared_ptr<IShowEpisode> Media::showEpisode() std::shared_ptr<IShowEpisode> Media::showEpisode()
...@@ -147,15 +143,13 @@ std::shared_ptr<IShowEpisode> Media::showEpisode() ...@@ -147,15 +143,13 @@ std::shared_ptr<IShowEpisode> Media::showEpisode()
return m_showEpisode; return m_showEpisode;
} }
bool Media::setShowEpisode(ShowEpisodePtr showEpisode) void Media::setShowEpisode( ShowEpisodePtr showEpisode )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name if ( showEpisode == nullptr || m_showEpisodeId == showEpisode->id() )
+ " SET show_episode_id = ? WHERE id_media = ?"; return;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, showEpisode->id(), m_id ) == false )
return false;
m_showEpisodeId = showEpisode->id(); m_showEpisodeId = showEpisode->id();
m_showEpisode = showEpisode; m_showEpisode = showEpisode;
return true; m_changed = true;
} }
std::vector<LabelPtr> Media::labels() std::vector<LabelPtr> Media::labels()
...@@ -185,15 +179,13 @@ MoviePtr Media::movie() ...@@ -185,15 +179,13 @@ MoviePtr Media::movie()
return m_movie; return m_movie;
} }
bool Media::setMovie( MoviePtr movie ) void Media::setMovie( MoviePtr movie )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name if ( movie == nullptr || m_movieId == movie->id() )
+ " SET movie_id = ? WHERE id_media = ?"; return;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, movie->id(), m_id ) == false )
return false;
m_movie = movie; m_movie = movie;
m_movieId = movie->id(); m_movieId = movie->id();
return true; m_changed = true;
} }
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps) bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
...@@ -227,13 +219,30 @@ const std::string &Media::snapshot() ...@@ -227,13 +219,30 @@ const std::string &Media::snapshot()
return m_snapshot; return m_snapshot;
} }
bool Media::setSnapshot(const std::string &snapshot) void Media::setSnapshot( const std::string& snapshot )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name if ( m_snapshot == snapshot )
+ " SET snapshot = ? WHERE id_media = ?"; return;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, snapshot, m_id ) == false )
return false;
m_snapshot = snapshot; m_snapshot = snapshot;
m_changed = true;
}
bool Media::save()
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"type = ?, duration = ?, play_count = ?, show_episode_id = ?, artist = ?,"
"movie_id = ?, last_modification_date = ?, snapshot = ?, parsed = ?, title = ? "
"WHERE id_media = ?";
if ( m_changed == false )
return true;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_type, m_duration, m_playCount,
sqlite::ForeignKey{ m_showEpisodeId }, m_artist,
sqlite::ForeignKey{ m_movieId }, m_lastModificationDate,
m_snapshot, m_isParsed, m_title, m_id ) == false )
{
return false;
}
m_changed = false;
return true; return true;
} }
...@@ -252,16 +261,12 @@ bool Media::isParsed() const ...@@ -252,16 +261,12 @@ bool Media::isParsed() const
return m_isParsed; return m_isParsed;
} }
bool Media::markParsed() void Media::markParsed()
{ {
if ( m_isParsed == true ) if ( m_isParsed == true )
return true; return;
static const std::string req = "UPDATE " + policy::MediaTable::Name
+ " SET parsed = ? WHERE id_media = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, true, m_id ) == false )
return false;
m_isParsed = true; m_isParsed = true;
return true; m_changed = true;
} }
unsigned int Media::id() const unsigned int Media::id() const
...@@ -274,15 +279,12 @@ IMedia::Type Media::type() ...@@ -274,15 +279,12 @@ IMedia::Type Media::type()
return m_type; return m_type;
} }
bool Media::setType( Type type ) void Media::setType( Type type )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name if ( m_type != type )
+ " SET type = ? WHERE id_media = ?"; return;
// We need to convert to an integer representation for the sqlite traits to work properly
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, type, m_id ) == false )
return false;
m_type = type; m_type = type;
return true; m_changed = true;
} }
const std::string &Media::title() const std::string &Media::title()
...@@ -290,14 +292,12 @@ const std::string &Media::title() ...@@ -290,14 +292,12 @@ const std::string &Media::title()
return m_title; return m_title;
} }
bool Media::setTitle( const std::string &title ) void Media::setTitle( const std::string &title )
{ {
static const std::string req = "UPDATE " + policy::MediaTable::Name if ( m_title == title )
+ " SET title = ? WHERE id_media = ?"; return;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, title, m_id ) == false )
return false;
m_title = title; m_title = title;
return true; m_changed = true;
} }
bool Media::createTable( DBConnection connection ) bool Media::createTable( DBConnection connection )
......
...@@ -66,24 +66,24 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable> ...@@ -66,24 +66,24 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
virtual unsigned int id() const override; virtual unsigned int id() const override;
virtual Type type() override; virtual Type type() override;
bool setType( Type type ); void setType( Type type );
virtual const std::string& title() override; virtual const std::string& title() override;
bool setTitle( const std::string& title ); void setTitle( const std::string& title );
virtual AlbumTrackPtr albumTrack() override; virtual AlbumTrackPtr albumTrack() override;
bool setAlbumTrack( AlbumTrackPtr albumTrack ); bool setAlbumTrack( AlbumTrackPtr albumTrack );
virtual const std::string& artist() const override; virtual const std::string& artist() const override;
bool setArtist( const std::string& artist ); void setArtist( const std::string& artist );
virtual int64_t duration() const override; virtual int64_t duration() const override;
bool setDuration( int64_t duration); void setDuration( int64_t duration);
virtual std::shared_ptr<IShowEpisode> showEpisode() override; virtual std::shared_ptr<IShowEpisode> showEpisode() override;
bool setShowEpisode( ShowEpisodePtr showEpisode ); void setShowEpisode( ShowEpisodePtr showEpisode );
virtual bool addLabel( LabelPtr label ) override; virtual bool addLabel( LabelPtr label ) override;
virtual bool removeLabel( LabelPtr label ) override; virtual bool removeLabel( LabelPtr label ) override;
virtual std::vector<LabelPtr> labels() override; virtual std::vector<LabelPtr> labels() override;
virtual int playCount() const override; virtual int playCount() const override;
virtual const std::string& mrl() const override; virtual const std::string& mrl() const override;
virtual MoviePtr movie() override; virtual MoviePtr movie() override;
bool setMovie( MoviePtr movie ); void setMovie( MoviePtr movie );
bool addVideoTrack( const std::string& codec, unsigned int width, bool addVideoTrack( const std::string& codec, unsigned int width,
unsigned int height, float fps ); unsigned int height, float fps );
virtual std::vector<VideoTrackPtr> videoTracks() override; virtual std::vector<VideoTrackPtr> videoTracks() override;
...@@ -91,7 +91,8 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable> ...@@ -91,7 +91,8 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
unsigned int nbChannels, const std::string& language, const std::string& desc ); unsigned int nbChannels, const std::string& language, const std::string& desc );
virtual std::vector<AudioTrackPtr> audioTracks() override; virtual std::vector<AudioTrackPtr> audioTracks() override;
virtual const std::string& snapshot() override; virtual const std::string& snapshot() override;
bool setSnapshot( const std::string& snapshot ); void setSnapshot( const std::string& snapshot );
bool save();
bool isStandAlone(); bool isStandAlone();
unsigned int lastModificationDate(); unsigned int lastModificationDate();
...@@ -99,7 +100,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable> ...@@ -99,7 +100,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
/// Explicitely mark a file as fully parsed, meaning no metadata service needs to run anymore. /// Explicitely mark a file as fully parsed, meaning no metadata service needs to run anymore.
//FIXME: This lacks granularity as we don't have a straight forward way to know which service //FIXME: This lacks granularity as we don't have a straight forward way to know which service
//needs to run or not. //needs to run or not.
bool markParsed(); void markParsed();
bool isParsed() const; bool isParsed() const;
private: private:
...@@ -125,6 +126,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable> ...@@ -125,6 +126,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
AlbumTrackPtr m_albumTrack; AlbumTrackPtr m_albumTrack;
ShowEpisodePtr m_showEpisode; ShowEpisodePtr m_showEpisode;
MoviePtr m_movie; MoviePtr m_movie;
bool m_changed;
friend struct policy::MediaTable; friend struct policy::MediaTable;
}; };
......
...@@ -130,7 +130,8 @@ IMetadataService::Status VLCMetadataService::handleMediaMeta( std::shared_ptr<Me ...@@ -130,7 +130,8 @@ IMetadataService::Status VLCMetadataService::handleMediaMeta( std::shared_ptr<Me
return Status::Fatal; return Status::Fatal;
} }
auto duration = vlcMedia.duration(); auto duration = vlcMedia.duration();
if ( media->setDuration( duration ) == false ) media->setDuration( duration );
if ( media->save() == false )
return Status::Error; return Status::Error;
return Status::Success; return Status::Success;
} }
......
...@@ -112,6 +112,7 @@ TEST_F( Medias, Duration ) ...@@ -112,6 +112,7 @@ TEST_F( Medias, Duration )
int64_t d = int64_t(1) << 40; int64_t d = int64_t(1) << 40;
f->setDuration( d ); f->setDuration( d );
f->save();
ASSERT_EQ( f->duration(), d ); ASSERT_EQ( f->duration(), d );
Reload(); Reload();
...@@ -129,6 +130,7 @@ TEST_F( Medias, Artist ) ...@@ -129,6 +130,7 @@ TEST_F( Medias, Artist )
std::string newArtist( "Rage Against The Otters" ); std::string newArtist( "Rage Against The Otters" );
f->setArtist( newArtist ); f->setArtist( newArtist );
f->save();
ASSERT_EQ( f->artist(), newArtist ); ASSERT_EQ( f->artist(), newArtist );
Reload(); Reload();
...@@ -145,6 +147,7 @@ TEST_F( Medias, Snapshot ) ...@@ -145,6 +147,7 @@ TEST_F( Medias, Snapshot )
std::string newSnapshot( "/path/to/snapshot" ); std::string newSnapshot( "/path/to/snapshot" );
f->setSnapshot( newSnapshot ); f->setSnapshot( newSnapshot );
f->save();
ASSERT_EQ( f->snapshot(), newSnapshot ); ASSERT_EQ( f->snapshot(), newSnapshot );
Reload(); Reload();
......
...@@ -110,6 +110,7 @@ TEST_F( Movies, AssignToFile ) ...@@ -110,6 +110,7 @@ TEST_F( Movies, AssignToFile )
ASSERT_EQ( f->movie(), nullptr ); ASSERT_EQ( f->movie(), nullptr );
f->setMovie( m ); f->setMovie( m );
f->save();
ASSERT_EQ( f->movie(), m ); ASSERT_EQ( f->movie(), m );
Reload(); Reload();
......
...@@ -132,6 +132,7 @@ TEST_F( Shows, FetchShowFromEpisode ) ...@@ -132,6 +132,7 @@ TEST_F( Shows, FetchShowFromEpisode )
auto e = s->addEpisode( "episode 1", 1 ); auto e = s->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file.avi", nullptr ); auto f = ml->addFile( "file.avi", nullptr );
f->setShowEpisode( e ); f->setShowEpisode( e );
f->save();
auto e2 = f->showEpisode(); auto e2 = f->showEpisode();
auto s2 = e2->show(); auto s2 = e2->show();
...@@ -141,6 +142,7 @@ TEST_F( Shows, FetchShowFromEpisode ) ...@@ -141,6 +142,7 @@ TEST_F( Shows, FetchShowFromEpisode )
Reload(); Reload();
f = std::static_pointer_cast<Media>( ml->file( "file.avi" ) ); f = std::static_pointer_cast<Media>( ml->file( "file.avi" ) );
ASSERT_NE( nullptr, f->showEpisode() );
s2 = f->showEpisode()->show(); s2 = f->showEpisode()->show();
ASSERT_NE( s2, nullptr ); ASSERT_NE( s2, nullptr );
ASSERT_EQ( s->name(), s2->name() ); ASSERT_EQ( s->name(), s2->name() );
...@@ -218,6 +220,7 @@ TEST_F( Shows, FileSetShowEpisode ) ...@@ -218,6 +220,7 @@ TEST_F( Shows, FileSetShowEpisode )
ASSERT_EQ( f->showEpisode(), nullptr ); ASSERT_EQ( f->showEpisode(), nullptr );
f->setShowEpisode( e ); f->setShowEpisode( e );
f->save();
ASSERT_EQ( f->showEpisode(), e ); ASSERT_EQ( f->showEpisode(), e );
Reload(); Reload();
......
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