Commit 015e2ae1 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

WIP

parent 9c90f0bd
Pipeline #7584 failed with stage
in 57 seconds
......@@ -42,7 +42,6 @@ namespace medialibrary
const std::string Album::Table::Name = "Album";
const std::string Album::Table::PrimaryKeyColumn = "id_album";
int64_t Album::* const Album::Table::PrimaryKey = &Album::m_id;
const std::string Album::ThumbnailTable::Name = "AlbumThumbnail";
Album::Album(MediaLibraryPtr ml, sqlite::Row& row)
: m_ml( ml )
......
......@@ -42,11 +42,6 @@ class Media;
class Album : public IAlbum, public DatabaseHelpers<Album>
{
struct ThumbnailTable
{
static const std::string Name;
};
public:
struct Table
{
......
......@@ -38,7 +38,6 @@ namespace medialibrary
const std::string Artist::Table::Name = "Artist";
const std::string Artist::Table::PrimaryKeyColumn = "id_artist";
int64_t Artist::*const Artist::Table::PrimaryKey = &Artist::m_id;
const std::string Artist::ThumbnailTable::Name = "ArtistThumbnail";
Artist::Artist( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
......
......@@ -36,11 +36,6 @@ class Media;
class Artist : public IArtist, public DatabaseHelpers<Artist>
{
struct ThumbnailTable
{
static const std::string Name;
};
public:
struct Table
{
......
......@@ -61,7 +61,6 @@ namespace medialibrary
const std::string Media::Table::Name = "Media";
const std::string Media::Table::PrimaryKeyColumn = "id_media";
int64_t Media::* const Media::Table::PrimaryKey = &Media::m_id;
const std::string Media::ThumbnailTable::Name = "MediaThumbnail";
Media::Media( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
......@@ -340,8 +339,8 @@ std::shared_ptr<Thumbnail> Media::thumbnail( ThumbnailSizeType sizeType ) const
{
auto idx = Thumbnail::SizeToInt(sizeType);
if ( m_thumbnails[idx] == nullptr )
m_thumbnails[idx] = Thumbnail::fetch( m_ml, ThumbnailTable::Name, m_id,
sizeType );
m_thumbnails[idx] = Thumbnail::fetch( m_ml, Thumbnail::EntityType::Media,
m_id, sizeType );
return m_thumbnails[idx];
}
......
......@@ -48,12 +48,6 @@ class Media : public IMedia,
public DatabaseHelpers<Media>,
public std::enable_shared_from_this<Media>
{
private:
struct ThumbnailTable
{
static const std::string Name;
};
public:
struct Table
{
......
......@@ -33,6 +33,7 @@ namespace medialibrary
const std::string Thumbnail::Table::Name = "Thumbnail";
const std::string Thumbnail::Table::PrimaryKeyColumn = "id_thumbnail";
int64_t Thumbnail::*const Thumbnail::Table::PrimaryKey = &Thumbnail::m_id;
const std::string Thumbnail::LinkingTable::Name = "ThumbnailLinking";
const std::string Thumbnail::EmptyMrl;
......@@ -103,8 +104,8 @@ bool Thumbnail::update( std::string mrl, bool isOwned )
return true;
}
bool Thumbnail::updateLinkRecord( int64_t entityId, Thumbnail::Origin origin,
const std::string& tableName )
bool Thumbnail::updateLinkRecord( int64_t entityId, EntityType type,
Thumbnail::Origin origin )
{
const std::string req = "UPDATE " + tableName + " SET "
"thumbnail_id = ?, origin = ? WHERE entity_id = ? AND size_type = ?";
......@@ -172,26 +173,6 @@ void Thumbnail::createTable( sqlite::Connection* dbConnection )
sqlite::Tools::executeRequest( dbConnection, req );
}
void Thumbnail::createLinkingTable( sqlite::Connection* dbConnection,
const std::string& tableName )
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + tableName +
"("
"entity_id UNSIGNED INTEGER NOT NULL,"
"size_type UNSIGNED INTEGER NOT NULL,"
"thumbnail_id UNSIGNED INTEGER NOT NULL,"
"origin UNSIGNED INT NOT NULL,"
"PRIMARY KEY(entity_id, size_type),"
"FOREIGN KEY(thumbnail_id) REFERENCES " +
Table::Name + "(id_thumbnail) ON DELETE CASCADE"
")";
const std::string indexReq = "CREATE INDEX IF NOT EXISTS "
"thumbnail_" + tableName + "_index ON " + Table::Name + "(id_thumbnail)";
sqlite::Tools::executeRequest( dbConnection, req );
sqlite::Tools::executeRequest( dbConnection, indexReq );
}
std::shared_ptr<Thumbnail> Thumbnail::create( MediaLibraryPtr ml, std::string mrl,
Thumbnail::Origin origin,
ThumbnailSizeType sizeType, bool isOwned )
......@@ -207,8 +188,8 @@ std::shared_ptr<Thumbnail> Thumbnail::create( MediaLibraryPtr ml, std::string mr
return self;
}
std::shared_ptr<Thumbnail> Thumbnail::fetch( MediaLibraryPtr ml, const std::string& entityTable,
int64_t entityId, ThumbnailSizeType sizeType )
std::shared_ptr<Thumbnail> Thumbnail::fetch( MediaLibraryPtr ml, EntityType type,
int64_t entityId, ThumbnailSizeType sizeType )
{
std::string req = "SELECT t.id_thumbnail, t.mrl, ent.origin, ent.size_type, t.is_generated "
"FROM " + Table::Name + " t "
......
......@@ -38,6 +38,10 @@ public:
static const std::string PrimaryKeyColumn;
static int64_t Thumbnail::*const PrimaryKey;
};
struct LinkingTable
{
static const std::string Name;
};
enum class Origin : uint8_t
{
......@@ -53,6 +57,12 @@ public:
/// An image (jpg or png) that was located in the album folder
CoverFile,
};
enum class EntityType
{
Media,
Album,
Artist,
};
Thumbnail( MediaLibraryPtr ml, sqlite::Row& row );
/**
......@@ -76,8 +86,8 @@ public:
/**
* @brief updateLinkEntity Updates the record linking an entity and a thumbnail
* @param entityId The target entity ID
* @param type The type of entity for this linking record
* @param sizeType The thumbnail size type
* @param tableName The table containing the linking records for this entity type
* @return true in case of success, false otherwise
*
* @warning This must be run in a transaction, as the associated thumbnail
......@@ -85,7 +95,7 @@ public:
* This is expected to be called when a new thumbnail had to be inserted, and
* the linking entity needs updating.
*/
bool updateLinkRecord( int64_t entityId, Origin origin, const std::string& tableName );
bool updateLinkRecord( int64_t entityId, EntityType type, Origin origin );
/**
* @brief insert Insert the thumbnail in database
* @return The new entity primary key, or 0 in case of failure
......@@ -95,21 +105,20 @@ public:
/**
* @brief insertLinkRecord Insert a new record to link an entity and a thumbnail
* @param entityId The target entity id
* @param type The type of entity to insert a linking record for
* @param origin The thumbnail origin
* @param tableName The table containing the linking records for this entity type
*
* This is expected to be called when a new thumbnail gets inserted, or
* when it can be shared with another entity.
*/
void insertLinkRecord( int64_t entityId, Origin origin,
const std::string& tableName );
void insertLinkRecord( int64_t entityId, EntityType type, Origin origin );
/**
* @brief unlinkThumbnail Removes the link between an entity and a thumbnail
* @param entityId The ID of the entity to unlink
* @param tableName The table containing the linking records for this entity type
* @param type The type of entity of the linking record to remove
*/
void unlinkThumbnail( int64_t entityId, const std::string& tableName );
void unlinkThumbnail( int64_t entityId, EntityType entityType );
/**
* @brief isValid Returns true if the thumbnail was correctly generated
*
......@@ -134,14 +143,6 @@ public:
bool isFailureRecord() const;
static void createTable( sqlite::Connection* dbConnection );
/**
* @brief createLinkingTable Creates a linking table for an entity type & its thumbnails
* @param dbConnection An sqlite connection
* @param entityIdFieldName The name of the field that will hold the entity id.
* Typically <entity_type>_id
*/
static void createLinkingTable( sqlite::Connection* dbConnection,
const std::string& tableName );
static std::shared_ptr<Thumbnail> create( MediaLibraryPtr ml, std::string mrl,
Origin origin, ThumbnailSizeType sizeType,
bool isOwned );
......@@ -149,7 +150,7 @@ public:
// the thumbnail table. We need to get data from both the thumbnail & linking
// tables.
static std::shared_ptr<Thumbnail> fetch( MediaLibraryPtr ml,
const std::string& entityTable,
EntityType type,
int64_t entityId,
ThumbnailSizeType sizeType );
......
......@@ -4,3 +4,19 @@
"mrl TEXT,"
"is_generated BOOLEAN NOT NULL"
")",
"CREATE TABLE IF NOT EXISTS " + Thumbnail::LinkingTable::Name +
"("
"entity_id UNSIGNED INTEGER NOT NULL,"
"entity_type UNSIGNED INTEGER NOT NULL,"
"size_type UNSIGNED INTEGER NOT NULL,"
"thumbnail_id UNSIGNED INTEGER NOT NULL,"
"origin UNSIGNED INT NOT NULL,"
"PRIMARY KEY(entity_id, entity_type, size_type),"
"FOREIGN KEY(thumbnail_id) REFERENCES " +
Thumbnail::Table::Name + "(id_thumbnail) ON DELETE CASCADE"
")",
"CREATE INDEX IF NOT EXISTS thumbnail_link_index "
"ON " + Thumbnail::Table::Name + "(id_thumbnail)",
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