Commit 08af48cd authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

AlbumTrack: Store the duration of a track in the track entity

It's more reliable than using the media as duration source, since the
media might not exist when reacting to a track disappearance
It also speeds up a bit the album modification triggers since we don't
need an inner select to get the added/deleted track duration
parent 87a35bf0
......@@ -234,7 +234,7 @@ std::vector<MediaPtr> Album::cachedTracks() const
std::shared_ptr<AlbumTrack> Album::addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, int64_t genreId )
{
auto track = AlbumTrack::create( m_ml, m_id, media, trackNb, discNumber, artistId, genreId );
auto track = AlbumTrack::create( m_ml, m_id, media, trackNb, discNumber, artistId, genreId, media->duration() );
if ( track == nullptr )
return nullptr;
media->setAlbumTrack( track );
......@@ -382,7 +382,7 @@ bool Album::createTriggers(DBConnection dbConnection)
" UPDATE " + policy::AlbumTable::Name +
" SET"
" nb_tracks = nb_tracks - 1,"
" duration = duration - max(0, (SELECT duration FROM " + policy::MediaTable::Name + " WHERE id_media=old.media_id))"
" duration = duration - old.duration"
" WHERE id_album = old.album_id;"
" DELETE FROM " + policy::AlbumTable::Name +
" WHERE id_album=old.album_id AND nb_tracks = 0;"
......@@ -391,7 +391,7 @@ bool Album::createTriggers(DBConnection dbConnection)
" AFTER INSERT ON " + policy::AlbumTrackTable::Name +
" BEGIN"
" UPDATE " + policy::AlbumTable::Name +
" SET duration = duration + max(0, (SELECT duration FROM " + policy::MediaTable::Name + " WHERE id_media=new.media_id)),"
" SET duration = duration + new.duration,"
" nb_tracks = nb_tracks + 1"
" WHERE id_album = new.album_id;"
" END";
......
......@@ -42,8 +42,10 @@ int64_t AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
int64_t dummyDuration;
row >> m_id
>> m_mediaId
>> dummyDuration
>> m_artistId
>> m_genreId
>> m_trackNumber
......@@ -52,7 +54,8 @@ AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
>> m_isPresent;
}
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, int64_t artistId, int64_t genreId, unsigned int trackNumber, int64_t albumId, unsigned int discNumber )
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, int64_t artistId, int64_t genreId,
unsigned int trackNumber, int64_t albumId, unsigned int discNumber )
: m_ml( ml )
, m_id( 0 )
, m_mediaId( mediaId )
......@@ -100,6 +103,7 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
"id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
"media_id INTEGER,"
"duration INTEGER NOT NULL,"
"artist_id UNSIGNED INTEGER,"
"genre_id INTEGER,"
"track_number UNSIGNED INTEGER,"
......@@ -129,13 +133,14 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
std::shared_ptr<AlbumTrack> AlbumTrack::create( MediaLibraryPtr ml, int64_t albumId,
std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, int64_t genreId )
unsigned int discNumber, int64_t artistId, int64_t genreId,
int64_t duration )
{
auto self = std::make_shared<AlbumTrack>( ml, media->id(), artistId, genreId, trackNb, albumId, discNumber );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(media_id, artist_id, genre_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?, ?, ?)";
if ( insert( ml, self, req, media->id(), sqlite::ForeignKey( artistId ), sqlite::ForeignKey( genreId ),
trackNb, albumId, discNumber ) == false )
+ "(media_id, duration, artist_id, genre_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?, ?, ?, ?)";
if ( insert( ml, self, req, media->id(), duration >= 0 ? duration : 0, sqlite::ForeignKey( artistId ),
sqlite::ForeignKey( genreId ), trackNb, albumId, discNumber ) == false )
return nullptr;
self->m_media = media;
return self;
......
......@@ -55,8 +55,8 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
{
public:
AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row );
AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, int64_t artistId, int64_t genreId, unsigned int trackNumber,
int64_t albumId, unsigned int discNumber);
AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, int64_t artistId, int64_t genreId,
unsigned int trackNumber, int64_t albumId, unsigned int discNumber );
virtual int64_t id() const override;
virtual ArtistPtr artist() const override;
......@@ -70,7 +70,9 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
static bool createTable( DBConnection dbConnection );
static std::shared_ptr<AlbumTrack> create(MediaLibraryPtr ml, int64_t albumId,
std::shared_ptr<Media> media, unsigned int trackNb , unsigned int discNumber, int64_t artistId , int64_t genreId);
std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, int64_t genreId,
int64_t duration );
static AlbumTrackPtr fromMedia( MediaLibraryPtr ml, int64_t mediaId );
static std::vector<MediaPtr> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
static std::vector<MediaPtr> search(DBConnection dbConn, const std::string& title );
......
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