Commit 9d528469 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Propagate presence state to album tracks & albums

parent 0194d004
......@@ -44,7 +44,8 @@ Album::Album(DBConnection dbConnection, sqlite::Row& row)
>> m_shortSummary
>> m_artworkUrl
>> m_lastSyncDate
>> m_nbTracks;
>> m_nbTracks
>> m_isPresent;
}
Album::Album(const std::string& title )
......@@ -54,6 +55,7 @@ Album::Album(const std::string& title )
, m_releaseYear( ~0u )
, m_lastSyncDate( 0 )
, m_nbTracks( 0 )
, m_isPresent( true )
, m_tracksCached( false )
{
}
......@@ -64,6 +66,7 @@ Album::Album( const Artist* artist )
, m_releaseYear( ~0u )
, m_lastSyncDate( 0 )
, m_nbTracks( 0 )
, m_isPresent( true )
, m_tracksCached( false )
{
}
......@@ -249,7 +252,8 @@ bool Album::createTable(DBConnection dbConnection )
"short_summary TEXT,"
"artwork_url TEXT,"
"last_sync_date UNSIGNED INTEGER,"
"nb_tracks UNSIGNED INTEGER DEFAULT 0"
"nb_tracks UNSIGNED INTEGER DEFAULT 0,"
"is_present BOOLEAN NOT NULL DEFAULT 1"
")";
static const std::string reqRel = "CREATE TABLE IF NOT EXISTS AlbumArtistRelation("
"id_album INTEGER,"
......@@ -264,6 +268,18 @@ bool Album::createTable(DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, reqRel );
}
bool Album::createTriggers(DBConnection dbConnection)
{
static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_album_present AFTER UPDATE OF "
"is_present ON " + policy::AlbumTrackTable::Name +
" BEGIN "
" UPDATE " + policy::AlbumTable::Name + " SET is_present="
"(SELECT COUNT(id_track) FROM " + policy::AlbumTrackTable::Name + " WHERE album_id=new.album_id AND is_present=1) "
"WHERE id_album=new.album_id;"
" END";
return sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
std::shared_ptr<Album> Album::create(DBConnection dbConnection, const std::string& title )
{
auto album = std::make_shared<Album>( title );
......
......@@ -83,6 +83,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
bool removeArtist( Artist* artist );
static bool createTable( DBConnection dbConnection );
static bool createTriggers( DBConnection dbConnection );
static std::shared_ptr<Album> create(DBConnection dbConnection, const std::string& title );
static std::shared_ptr<Album> createUnknownAlbum( DBConnection dbConnection, const Artist* artist );
......@@ -96,6 +97,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
std::string m_artworkUrl;
time_t m_lastSyncDate;
unsigned int m_nbTracks;
bool m_isPresent;
mutable std::vector<MediaPtr> m_tracks;
mutable bool m_tracksCached;
......
......@@ -40,7 +40,8 @@ AlbumTrack::AlbumTrack(DBConnection dbConnection, sqlite::Row& row )
>> m_trackNumber
>> m_albumId
>> m_releaseYear
>> m_discNumber;
>> m_discNumber
>> m_isPresent;
}
//FIXME: constify media
......@@ -51,6 +52,7 @@ AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int alb
, m_albumId( albumId )
, m_releaseYear( 0 )
, m_discNumber( discNumber )
, m_isPresent( true )
{
}
......@@ -87,12 +89,19 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
"album_id UNSIGNED INTEGER NOT NULL,"
"release_year UNSIGNED INTEGER,"
"disc_number UNSIGNED INTEGER,"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
" ON DELETE CASCADE, "
"FOREIGN KEY (album_id) REFERENCES Album(id_album) "
" ON DELETE CASCADE"
")";
return sqlite::Tools::executeRequest( dbConnection, req );
static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_track_present AFTER UPDATE OF is_present "
"ON " + policy::MediaTable::Name +
" BEGIN"
" UPDATE " + policy::AlbumTrackTable::Name + " SET is_present = new.is_present WHERE media_id = new.id_media;"
" END";
return sqlite::Tools::executeRequest( dbConnection, req ) &&
sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb, unsigned int discNumber )
......
......@@ -75,6 +75,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
unsigned int m_albumId;
unsigned int m_releaseYear;
unsigned int m_discNumber;
bool m_isPresent;
std::weak_ptr<Album> m_album;
......
......@@ -129,6 +129,7 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
Label::createTable( m_dbConnection.get() ) &&
Album::createTable( m_dbConnection.get() ) &&
AlbumTrack::createTable( m_dbConnection.get() ) &&
Album::createTriggers( m_dbConnection.get() ) &&
Show::createTable( m_dbConnection.get() ) &&
ShowEpisode::createTable( m_dbConnection.get() ) &&
Movie::createTable( m_dbConnection.get() ) &&
......@@ -280,6 +281,7 @@ std::shared_ptr<Album> MediaLibrary::createAlbum(const std::string& title )
std::vector<AlbumPtr> MediaLibrary::albums()
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE is_present=1"
" ORDER BY title ASC";
return Album::fetchAll<IAlbum>( m_dbConnection.get(), req );
}
......
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