Commit 2f075671 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Playlist: Remove playlist_update_nb_media_on_media_change trigger

parent a2056ca7
......@@ -1327,7 +1327,9 @@ bool Media::setTypeInternal( IMedia::Type type )
"WHERE id_media = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, type, m_id ) == false )
return false;
auto previousType = m_type;
m_type = type;
Playlist::onMediaTypeChanged( m_ml, *this, previousType );
return true;
}
......@@ -2506,6 +2508,11 @@ bool Media::setPresent( bool present )
return false;
genre->onMediaPresenceChanged( *this );
}
if ( m_nbPlaylists > 0 )
{
if ( Playlist::onMediaPresenceChanged( m_ml, *this ) == false )
return false;
}
return true;
}
......
......@@ -517,6 +517,76 @@ std::string Playlist::mrl() const
}
}
bool Playlist::onMediaPresenceChanged( MediaLibraryPtr ml, const Media& m )
{
std::string field;
switch ( m.type() )
{
case IMedia::Type::Audio:
field = "nb_present_audio";
break;
case IMedia::Type::Video:
field = "nb_present_video";
break;
case IMedia::Type::Unknown:
field = "nb_present_unknown";
break;
}
auto sign = m.isPresent() == true ? 1 : -1;
const std::string req = "UPDATE " + Table::Name + " SET " +
field + " = " + field + " + items.count * ?"
" FROM (SELECT COUNT(media_id) AS count, playlist_id"
" FROM " + MediaRelationTable::Name +
" WHERE media_id = ?"
" GROUP BY playlist_id"
") AS items"
" WHERE id_playlist = items.playlist_id;";
return sqlite::Tools::executeUpdate( ml->getConn(), req, sign, m.id() );
}
bool Playlist::onMediaTypeChanged( MediaLibraryPtr ml, const Media& m, IMedia::Type oldType )
{
std::string oldField;
std::string newField;
switch ( oldType )
{
case IMedia::Type::Audio:
oldField = "audio";
break;
case IMedia::Type::Video:
oldField = "video";
break;
case IMedia::Type::Unknown:
oldField = "unknown";
break;
}
switch ( m.type() )
{
case IMedia::Type::Audio:
newField = "audio";
break;
case IMedia::Type::Video:
newField = "video";
break;
case IMedia::Type::Unknown:
newField = "unknown";
break;
}
auto presenceMultiplier = m.isPresent() == true ? 1 : 0;
const std::string req = "UPDATE " + Table::Name + " SET " +
"nb_" + oldField + " = nb_" + oldField + " - items.count,"
"nb_present_" + oldField + " = nb_present_" + oldField + " - items.count * ?1,"
"nb_" + newField + " = nb_" + newField + " + items.count,"
"nb_present_" + newField + " = nb_present_" + newField + " + items.count * ?1"
" FROM (SELECT COUNT(media_id) AS count, playlist_id"
" FROM " + MediaRelationTable::Name +
" WHERE media_id = ?2"
" GROUP BY playlist_id"
") AS items"
" WHERE id_playlist = items.playlist_id;";
return sqlite::Tools::executeUpdate( ml->getConn(), req, presenceMultiplier, m.id() );
}
void Playlist::createTable( sqlite::Connection* dbConn )
{
std::string reqs[] = {
......@@ -551,9 +621,6 @@ void Playlist::createTriggers( sqlite::Connection* dbConn )
sqlite::Tools::executeRequest( dbConn,
trigger( Triggers::UpdateDurationOnMediaChange,
Settings::DbModelVersion ) );
sqlite::Tools::executeRequest( dbConn,
trigger( Triggers::UpdateNbMediaOnMediaChange,
Settings::DbModelVersion ) );
sqlite::Tools::executeRequest( dbConn,
trigger( Triggers::CascadeFileDeletion,
Settings::DbModelVersion ) );
......@@ -954,6 +1021,7 @@ std::string Playlist::trigger( Triggers trigger, uint32_t dbModel )
" END";
case Triggers::UpdateNbMediaOnMediaChange:
assert( dbModel >= 33 );
assert( dbModel < 37 );
return "CREATE TRIGGER " + triggerName( trigger, dbModel ) +
" AFTER UPDATE OF type, is_present ON " + Media::Table::Name +
" WHEN old.type != new.type OR old.is_present != new.is_present AND"
......@@ -1079,6 +1147,7 @@ std::string Playlist::triggerName(Playlist::Triggers trigger, uint32_t dbModel)
return "playlist_update_duration_on_media_change";
case Triggers::UpdateNbMediaOnMediaChange:
assert( dbModel >= 33 );
assert( dbModel < 37 );
return "playlist_update_nb_media_on_media_change";
case Triggers::CascadeFileDeletion:
assert( dbModel >= 34 );
......@@ -1184,7 +1253,6 @@ bool Playlist::checkDbModel(MediaLibraryPtr ml)
checkTrigger( Triggers::DeleteFts ) &&
checkTrigger( Triggers::UpdateNbMediaOnMediaDeletion ) &&
checkTrigger( Triggers::UpdateDurationOnMediaChange ) &&
checkTrigger( Triggers::UpdateNbMediaOnMediaChange ) &&
checkTrigger( Triggers::CascadeFileDeletion ) &&
checkIndex( Indexes::PlaylistIdPosition ) &&
checkIndex( Indexes::PlaylistRelMediaId );
......
......@@ -113,6 +113,8 @@ public:
virtual bool remove( uint32_t position ) override;
virtual bool isReadOnly() const override;
virtual std::string mrl() const override;
static bool onMediaPresenceChanged( MediaLibraryPtr ml, const Media& m );
static bool onMediaTypeChanged( MediaLibraryPtr ml, const Media& m, IMedia::Type oldType );
static void createTable( sqlite::Connection* dbConn );
static void createTriggers( sqlite::Connection* dbConn );
......
......@@ -3,3 +3,4 @@
"DROP TRIGGER album_is_present",
"DROP TRIGGER artist_has_tracks_present",
"DROP TRIGGER genre_update_is_present",
"DROP TRIGGER playlist_update_nb_media_on_media_change",
......@@ -82,7 +82,6 @@ namespace
"media_group_update_nb_media_types",
"playlist_cascade_file_deletion",
"playlist_update_duration_on_media_change",
"playlist_update_nb_media_on_media_change",
"playlist_update_nb_media_on_media_deletion",
"show_decrement_nb_episode",
"show_increment_nb_episode",
......
Supports Markdown
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