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