Commit 86963ad8 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Artist: Use external thumbnail table to store artworks

parent 4f6ec86d
......@@ -44,7 +44,7 @@ Artist::Artist( MediaLibraryPtr ml, sqlite::Row& row )
row >> m_id
>> m_name
>> m_shortBio
>> m_artworkMrl
>> m_thumbnailId
>> m_nbAlbums
>> m_nbTracks
>> m_mbId
......@@ -55,6 +55,7 @@ Artist::Artist( MediaLibraryPtr ml, const std::string& name )
: m_ml( ml )
, m_id( 0 )
, m_name( name )
, m_thumbnailId( 0 )
, m_nbAlbums( 0 )
, m_nbTracks( 0 )
, m_isPresent( true )
......@@ -151,18 +152,40 @@ bool Artist::addMedia( Media& media )
const std::string& Artist::artworkMrl() const
{
return m_artworkMrl;
if ( m_thumbnailId == 0 )
return Thumbnail::EmptyMrl;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return Thumbnail::EmptyMrl;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get()->mrl();
}
bool Artist::setArtworkMrl( const std::string& artworkMrl )
{
if ( m_artworkMrl == artworkMrl )
return true;
if ( m_thumbnailId != 0 )
return Thumbnail::setMrlFromPrimaryKey( m_ml, m_thumbnail, m_thumbnailId,
artworkMrl );
std::unique_ptr<sqlite::Transaction> t;
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Artist );
if ( m_thumbnail.get() == nullptr )
return false;
static const std::string req = "UPDATE " + policy::ArtistTable::Name +
" SET artwork_mrl = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
" SET thumbnail_id = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_thumbnail.get()->id(), m_id ) == false )
return false;
m_artworkMrl = artworkMrl;
m_thumbnailId = m_thumbnail.get()->id();
if ( t != nullptr )
t->commit();
return true;
}
......@@ -245,11 +268,13 @@ void Artist::createTable( sqlite::Connection* dbConnection )
"id_artist INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT COLLATE NOCASE UNIQUE ON CONFLICT FAIL,"
"shortbio TEXT,"
"artwork_mrl TEXT,"
"thumbnail_id TEXT,"
"nb_albums UNSIGNED INT DEFAULT 0,"
"nb_tracks UNSIGNED INT DEFAULT 0,"
"mb_id TEXT,"
"is_present BOOLEAN NOT NULL DEFAULT 1"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"FOREIGN KEY(thumbnail_id) REFERENCES " + policy::ThumbnailTable::Name
+ "(id_thumbnail)"
")";
const std::string reqRel = "CREATE TABLE IF NOT EXISTS MediaArtistRelation("
"media_id INTEGER NOT NULL,"
......
......@@ -25,6 +25,7 @@
#include "database/DatabaseHelpers.h"
#include "medialibrary/IArtist.h"
#include "medialibrary/IMediaLibrary.h"
#include "utils/Cache.h"
namespace medialibrary
{
......@@ -32,6 +33,7 @@ namespace medialibrary
class Artist;
class Album;
class Media;
class Thumbnail;
namespace policy
{
......@@ -79,12 +81,14 @@ private:
int64_t m_id;
std::string m_name;
std::string m_shortBio;
std::string m_artworkMrl;
int64_t m_thumbnailId;
unsigned int m_nbAlbums;
unsigned int m_nbTracks;
bool m_isPresent;
std::string m_mbId;
mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
friend struct policy::ArtistTable;
};
......
......@@ -1059,6 +1059,7 @@ void MediaLibrary::migrateModel13to14()
// Re-create tables that we just removed
// We will run a re-scan, so we don't care about keeping their content
Album::createTable( getConn() );
Artist::createTable( getConn() );
// Re-create triggers removed in the process
Media::createTriggers( getConn() );
AlbumTrack::createTriggers( getConn() );
......
......@@ -57,3 +57,6 @@
"DROP TABLE " + AlbumTable::Name,
"DELETE FROM " + AlbumTable::Name + "Fts",
"DROP TABLE " + ArtistTable::Name,
"DELETE FROM " + ArtistTable::Name + "Fts",
"DELETE FROM MediaArtistRelation",
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