Commit 00b6f548 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Store a pointer to MediaLibrary instead of DBConnection

This allows us to fetch more resources, and will prevent obscure bugs if
the connection is reopened, as the old pointer wouldn't be valid
parent ae711267
......@@ -42,6 +42,7 @@ class ILogger;
class IArtist;
class IPlaylist;
class SqliteConnection;
class MediaLibrary;
using AlbumPtr = std::shared_ptr<IAlbum>;
using AlbumTrackPtr = std::shared_ptr<IAlbumTrack>;
......@@ -58,6 +59,8 @@ using ShowEpisodePtr = std::shared_ptr<IShowEpisode>;
using ShowPtr = std::shared_ptr<IShow>;
using VideoTrackPtr = std::shared_ptr<IVideoTrack>;
using MediaLibraryPtr = const MediaLibrary*;
typedef SqliteConnection* DBConnection;
enum class LogLevel
......
......@@ -34,8 +34,8 @@ const std::string policy::AlbumTable::Name = "Album";
const std::string policy::AlbumTable::PrimaryKeyColumn = "id_album";
unsigned int Album::* const policy::AlbumTable::PrimaryKey = &Album::m_id;
Album::Album(DBConnection dbConnection, sqlite::Row& row)
: m_dbConnection( dbConnection )
Album::Album(MediaLibraryPtr ml, sqlite::Row& row)
: m_ml( ml )
{
row >> m_id
>> m_title
......@@ -99,7 +99,7 @@ bool Album::setReleaseYear( unsigned int date, bool force )
}
static const std::string req = "UPDATE " + policy::AlbumTable::Name
+ " SET release_year = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, date, m_id ) == false )
return false;
m_releaseYear = date;
return true;
......@@ -114,7 +114,7 @@ bool Album::setShortSummary( const std::string& summary )
{
static const std::string req = "UPDATE " + policy::AlbumTable::Name
+ " SET short_summary = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, summary, m_id ) == false )
return false;
m_shortSummary = summary;
return true;
......@@ -129,7 +129,7 @@ bool Album::setArtworkMrl( const std::string& artworkMrl )
{
static const std::string req = "UPDATE " + policy::AlbumTable::Name
+ " SET artwork_mrl = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artworkMrl, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
return false;
m_artworkMrl = artworkMrl;
return true;
......@@ -142,7 +142,7 @@ std::vector<MediaPtr> Album::tracks() const
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present = 1 ORDER BY att.disc_number, att.track_number";
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_ml, req, m_id );
}
std::vector<std::shared_ptr<IMedia> > Album::tracks( GenrePtr genre ) const
......@@ -154,7 +154,7 @@ std::vector<std::shared_ptr<IMedia> > Album::tracks( GenrePtr genre ) const
" WHERE att.album_id = ? AND med.is_present = 1"
" AND genre_id = ?"
" ORDER BY att.disc_number, att.track_number";
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id, genre->id() );
return Media::fetchAll<IMedia>( m_ml, req, m_id, genre->id() );
}
std::vector<MediaPtr> Album::cachedTracks() const
......@@ -167,16 +167,16 @@ std::vector<MediaPtr> Album::cachedTracks() const
std::shared_ptr<AlbumTrack> Album::addTrack( std::shared_ptr<Media> media, unsigned int trackNb, unsigned int discNumber )
{
auto t = m_dbConnection->newTransaction();
auto t = m_ml->getConn()->newTransaction();
auto track = AlbumTrack::create( m_dbConnection, m_id, *media, trackNb, discNumber );
auto track = AlbumTrack::create( m_ml, m_id, *media, trackNb, discNumber );
if ( track == nullptr )
return nullptr;
media->setAlbumTrack( track );
// Assume the media will be saved by the caller
static const std::string req = "UPDATE " + policy::AlbumTable::Name +
" SET nb_tracks = nb_tracks + 1 WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_id ) == false )
return nullptr;
m_nbTracks++;
t->commit();
......@@ -202,7 +202,7 @@ ArtistPtr Album::albumArtist() const
{
if ( m_artistId == 0 )
return nullptr;
return Artist::fetch( m_dbConnection, m_artistId );
return Artist::fetch( m_ml, m_artistId );
}
bool Album::setAlbumArtist( Artist* artist )
......@@ -213,18 +213,18 @@ bool Album::setAlbumArtist( Artist* artist )
return false;
static const std::string req = "UPDATE " + policy::AlbumTable::Name + " SET "
"artist_id = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist->id(), m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artist->id(), m_id ) == false )
return false;
if ( m_artistId != 0 )
{
auto previousArtist = Artist::fetch( m_dbConnection, m_artistId );
auto previousArtist = Artist::fetch( m_ml, m_artistId );
previousArtist->updateNbAlbum( -1 );
}
m_artistId = artist->id();
artist->updateNbAlbum( 1 );
static const std::string ftsReq = "UPDATE " + policy::AlbumTable::Name + "Fts SET "
" artist = ? WHERE rowid = ?";
sqlite::Tools::executeUpdate( m_dbConnection, ftsReq, artist->name(), m_id );
sqlite::Tools::executeUpdate( m_ml->getConn(), ftsReq, artist->name(), m_id );
return true;
}
......@@ -233,7 +233,7 @@ std::vector<ArtistPtr> Album::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"INNER JOIN AlbumArtistRelation aar ON aar.artist_id = art.id_artist "
"WHERE aar.album_id = ?";
return Artist::fetchAll<IArtist>( m_dbConnection, req, m_id );
return Artist::fetchAll<IArtist>( m_ml, req, m_id );
}
bool Album::addArtist( std::shared_ptr<Artist> artist )
......@@ -244,14 +244,14 @@ bool Album::addArtist( std::shared_ptr<Artist> artist )
LOG_ERROR("Both artist & album need to be inserted in database before being linked together" );
return false;
}
return sqlite::Tools::insert( m_dbConnection, req, m_id, artist->id() ) != 0;
return sqlite::Tools::insert( m_ml->getConn(), req, m_id, artist->id() ) != 0;
}
bool Album::removeArtist(Artist* artist)
{
static const std::string req = "DELETE FROM AlbumArtistRelation WHERE album_id = ? "
"AND id_artist = ?";
return sqlite::Tools::executeDelete( m_dbConnection, req, m_id, artist->id() );
return sqlite::Tools::executeDelete( m_ml->getConn(), req, m_id, artist->id() );
}
bool Album::createTable(DBConnection dbConnection )
......@@ -317,33 +317,33 @@ bool Album::createTriggers(DBConnection dbConnection)
sqlite::Tools::executeRequest( dbConnection, vtriggerDelete );
}
std::shared_ptr<Album> Album::create(DBConnection dbConnection, const std::string& title )
std::shared_ptr<Album> Album::create( MediaLibraryPtr ml, const std::string& title )
{
auto album = std::make_shared<Album>( title );
static const std::string req = "INSERT INTO " + policy::AlbumTable::Name +
"(id_album, title) VALUES(NULL, ?)";
if ( insert( dbConnection, album, req, title ) == false )
if ( insert( ml, album, req, title ) == false )
return nullptr;
album->m_dbConnection = dbConnection;
album->m_ml = ml;
return album;
}
std::shared_ptr<Album> Album::createUnknownAlbum( DBConnection dbConnection, const Artist* artist )
std::shared_ptr<Album> Album::createUnknownAlbum( MediaLibraryPtr ml, const Artist* artist )
{
auto album = std::make_shared<Album>( artist );
static const std::string req = "INSERT INTO " + policy::AlbumTable::Name +
"(id_album, artist_id) VALUES(NULL, ?)";
if ( insert( dbConnection, album, req, artist->id() ) == false )
if ( insert( ml, album, req, artist->id() ) == false )
return nullptr;
album->m_dbConnection = dbConnection;
album->m_ml = ml;
return album;
}
std::vector<AlbumPtr> Album::search( DBConnection dbConn, const std::string& pattern )
std::vector<AlbumPtr> Album::search( MediaLibraryPtr ml, const std::string& pattern )
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " WHERE id_album IN "
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH ?)"
"AND is_present = 1";
return fetchAll<IAlbum>( dbConn, req, pattern + "*" );
return fetchAll<IAlbum>( ml, req, pattern + "*" );
}
......@@ -51,7 +51,7 @@ struct AlbumTable
class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
{
public:
Album( DBConnection dbConnection, sqlite::Row& row );
Album( MediaLibraryPtr ml, sqlite::Row& row );
Album( const std::string& title );
Album( const Artist* artist );
......@@ -96,17 +96,17 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
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 );
static std::shared_ptr<Album> create( MediaLibraryPtr ml, const std::string& title );
static std::shared_ptr<Album> createUnknownAlbum( MediaLibraryPtr ml, const Artist* artist );
///
/// \brief search search for an album, through its albumartist or title
/// \param pattern A pattern representing the title, or the name of the main artist
/// \return
///
static std::vector<AlbumPtr> search(DBConnection dbConn, const std::string& pattern );
static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern );
protected:
DBConnection m_dbConnection;
MediaLibraryPtr m_ml;
unsigned int m_id;
std::string m_title;
unsigned int m_artistId;
......
......@@ -32,8 +32,8 @@ const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
const std::string policy::AlbumTrackTable::PrimaryKeyColumn = "id_track";
unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m_id;
AlbumTrack::AlbumTrack( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection )
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
row >> m_id
>> m_mediaId
......@@ -69,7 +69,7 @@ ArtistPtr AlbumTrack::artist() const
auto lock = m_artist.lock();
if ( m_artist.isCached() == false )
{
m_artist = Artist::fetch( m_dbConnection, m_artistId );
m_artist = Artist::fetch( m_ml, m_artistId );
}
return m_artist.get();
}
......@@ -80,7 +80,7 @@ bool AlbumTrack::setArtist( std::shared_ptr<Artist> artist )
" SET artist_id = ? WHERE id_track = ?";
if ( artist->id() == m_artistId )
return true;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist->id(), m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artist->id(), m_id ) == false )
return false;
m_artistId = artist->id();
m_artist = artist;
......@@ -117,29 +117,29 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
std::shared_ptr<AlbumTrack> AlbumTrack::create( DBConnection dbConnection, unsigned int albumId, const Media& media, unsigned int trackNb, unsigned int discNumber )
std::shared_ptr<AlbumTrack> AlbumTrack::create( MediaLibraryPtr ml, unsigned int albumId, const Media& media, unsigned int trackNb, unsigned int discNumber )
{
auto self = std::make_shared<AlbumTrack>( media.id(), trackNb, albumId, discNumber );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(media_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?)";
if ( insert( dbConnection, self, req, media.id(), trackNb, albumId, discNumber ) == false )
if ( insert( ml, self, req, media.id(), trackNb, albumId, discNumber ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
self->m_ml = ml;
return self;
}
AlbumTrackPtr AlbumTrack::fromMedia( DBConnection dbConnection, unsigned int mediaId )
AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, unsigned int mediaId )
{
static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
" WHERE media_id = ?";
return fetch( dbConnection, req, mediaId );
return fetch( ml, req, mediaId );
}
std::vector<AlbumTrackPtr> AlbumTrack::fromGenre( DBConnection dbConn, unsigned int genreId )
std::vector<AlbumTrackPtr> AlbumTrack::fromGenre( MediaLibraryPtr ml, unsigned int genreId )
{
static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
" WHERE genre_id = ?";
return fetchAll<IAlbumTrack>( dbConn, req, genreId );
return fetchAll<IAlbumTrack>( ml, req, genreId );
}
GenrePtr AlbumTrack::genre()
......@@ -147,7 +147,7 @@ GenrePtr AlbumTrack::genre()
auto l = m_genre.lock();
if ( m_genre.isCached() == false )
{
m_genre = Genre::fetch( m_dbConnection, m_genreId );
m_genre = Genre::fetch( m_ml, m_genreId );
}
return m_genre.get();
}
......@@ -156,7 +156,7 @@ bool AlbumTrack::setGenre( std::shared_ptr<Genre> genre )
{
static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
+ " SET genre_id = ? WHERE id_track = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req,
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req,
sqlite::ForeignKey( genre != nullptr ? genre->id() : 0 ),
m_id ) == false )
return false;
......@@ -182,7 +182,7 @@ bool AlbumTrack::setReleaseYear(unsigned int year)
return true;
static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name +
" SET release_year = ? WHERE id_track = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, year, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, year, m_id ) == false )
return false;
m_releaseYear = year;
return true;
......@@ -198,7 +198,7 @@ std::shared_ptr<IAlbum> AlbumTrack::album()
auto album = m_album.lock();
if ( album == nullptr && m_albumId != 0 )
{
album = Album::fetch( m_dbConnection, m_albumId );
album = Album::fetch( m_ml, m_albumId );
m_album = album;
}
return album;
......
......@@ -51,7 +51,7 @@ struct AlbumTrackTable
class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy::AlbumTrackTable>
{
public:
AlbumTrack( DBConnection dbConnection, sqlite::Row& row );
AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row );
AlbumTrack( unsigned int mediaId, unsigned int trackNumber, unsigned int albumId , unsigned int discNumber);
virtual unsigned int id() const override;
......@@ -66,14 +66,14 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
virtual std::shared_ptr<IAlbum> album() override;
static bool createTable( DBConnection dbConnection );
static std::shared_ptr<AlbumTrack> create(DBConnection dbConnection, unsigned int albumId,
static std::shared_ptr<AlbumTrack> create( MediaLibraryPtr ml, unsigned int albumId,
const Media& media, unsigned int trackNb , unsigned int discNumber );
static AlbumTrackPtr fromMedia( DBConnection dbConnection, unsigned int mediaId );
static std::vector<AlbumTrackPtr> fromGenre( DBConnection dbConn, unsigned int genreId );
static AlbumTrackPtr fromMedia( MediaLibraryPtr ml, unsigned int mediaId );
static std::vector<AlbumTrackPtr> fromGenre( MediaLibraryPtr ml, unsigned int genreId );
static std::vector<MediaPtr> search(DBConnection dbConn, const std::string& title );
private:
DBConnection m_dbConnection;
MediaLibraryPtr m_ml;
unsigned int m_id;
unsigned int m_mediaId;
unsigned int m_artistId;
......
......@@ -32,8 +32,8 @@ const std::string policy::ArtistTable::PrimaryKeyColumn = "id_artist";
unsigned int Artist::*const policy::ArtistTable::PrimaryKey = &Artist::m_id;
Artist::Artist( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection )
Artist::Artist( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
row >> m_id
>> m_name
......@@ -71,7 +71,7 @@ bool Artist::setShortBio(const std::string &shortBio)
{
static const std::string req = "UPDATE " + policy::ArtistTable::Name
+ " SET shortbio = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, shortBio, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, shortBio, m_id ) == false )
return false;
m_shortBio = shortBio;
return true;
......@@ -83,7 +83,7 @@ std::vector<AlbumPtr> Artist::albums() const
return {};
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
"WHERE artist_id = ? ORDER BY release_year, title";
return Album::fetchAll<IAlbum>( m_dbConnection, req, m_id );
return Album::fetchAll<IAlbum>( m_ml, req, m_id );
}
std::vector<MediaPtr> Artist::media() const
......@@ -91,7 +91,7 @@ std::vector<MediaPtr> Artist::media() const
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
"INNER JOIN MediaArtistRelation mar ON mar.media_id = med.id_media "
"WHERE mar.artist_id = ? AND med.is_present = 1";
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_ml, req, m_id );
}
bool Artist::addMedia( Media& media )
......@@ -99,7 +99,7 @@ bool Artist::addMedia( Media& media )
static const std::string req = "INSERT INTO MediaArtistRelation VALUES(?, ?)";
// If track's ID is 0, the request will fail due to table constraints
sqlite::ForeignKey artistForeignKey( m_id );
return sqlite::Tools::insert( m_dbConnection, req, media.id(), artistForeignKey ) != 0;
return sqlite::Tools::insert( m_ml->getConn(), req, media.id(), artistForeignKey ) != 0;
}
const std::string& Artist::artworkMrl() const
......@@ -113,7 +113,7 @@ bool Artist::setArtworkMrl( const std::string& artworkMrl )
return true;
static const std::string req = "UPDATE " + policy::ArtistTable::Name +
" SET artwork_mrl = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artworkMrl, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
return false;
m_artworkMrl = artworkMrl;
return true;
......@@ -126,7 +126,7 @@ bool Artist::updateNbAlbum( int increment )
static const std::string req = "UPDATE " + policy::ArtistTable::Name +
" SET nb_albums = nb_albums + ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, increment, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, increment, m_id ) == false )
return false;
m_nbAlbums += increment;
return true;
......@@ -136,15 +136,15 @@ std::shared_ptr<Album> Artist::unknownAlbum()
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE artist_id = ? AND title IS NULL";
auto album = Album::fetch( m_dbConnection, req, m_id );
auto album = Album::fetch( m_ml, req, m_id );
if ( album == nullptr )
{
album = Album::createUnknownAlbum( m_dbConnection, this );
album = Album::createUnknownAlbum( m_ml, this );
if ( album == nullptr )
return nullptr;
if ( updateNbAlbum( 1 ) == false )
{
Album::destroy( m_dbConnection, album->id() );
Album::destroy( m_ml, album->id() );
return nullptr;
}
}
......@@ -162,7 +162,7 @@ bool Artist::setMusicBrainzId( const std::string& mbId )
+ " SET mb_id = ? WHERE id_artist = ?";
if ( mbId == m_mbId )
return true;
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, mbId, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, mbId, m_id ) == false )
return false;
m_mbId = mbId;
return true;
......@@ -236,22 +236,22 @@ bool Artist::createDefaultArtists( DBConnection dbConnection )
return true;
}
std::shared_ptr<Artist> Artist::create( DBConnection dbConnection, const std::string &name )
std::shared_ptr<Artist> Artist::create( MediaLibraryPtr ml, const std::string &name )
{
auto artist = std::make_shared<Artist>( name );
static const std::string req = "INSERT INTO " + policy::ArtistTable::Name +
"(id_artist, name) VALUES(NULL, ?)";
if ( insert( dbConnection, artist, req, name ) == false )
if ( insert( ml, artist, req, name ) == false )
return nullptr;
artist->m_dbConnection = dbConnection;
artist->m_ml = ml;
return artist;
}
std::vector<ArtistPtr> Artist::search( DBConnection dbConnection, const std::string& name )
std::vector<ArtistPtr> Artist::search( MediaLibraryPtr ml, const std::string& name )
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name + " WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH ?)"
"AND is_present != 0";
return fetchAll<IArtist>( dbConnection, req, name + "*" );
return fetchAll<IArtist>( ml, req, name + "*" );
}
......@@ -43,7 +43,7 @@ struct ArtistTable
class Artist : public IArtist, public DatabaseHelpers<Artist, policy::ArtistTable>
{
public:
Artist( DBConnection dbConnection, sqlite::Row& row );
Artist(MediaLibraryPtr ml, sqlite::Row& row );
Artist( const std::string& name );
virtual unsigned int id() const override;
......@@ -63,11 +63,11 @@ public:
static bool createTable( DBConnection dbConnection );
static bool createTriggers( DBConnection dbConnection );
static bool createDefaultArtists( DBConnection dbConnection );
static std::shared_ptr<Artist> create( DBConnection dbConnection, const std::string& name );
static std::vector<ArtistPtr> search( DBConnection dbConnection, const std::string& name );
static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name );
static std::vector<ArtistPtr> search( MediaLibraryPtr ml, const std::string& name );
private:
DBConnection m_dbConnection;
MediaLibraryPtr m_ml;
unsigned int m_id;
std::string m_name;
std::string m_shortBio;
......
......@@ -28,8 +28,8 @@ const std::string policy::AudioTrackTable::Name = "AudioTrack";
const std::string policy::AudioTrackTable::PrimaryKeyColumn = "id_track";
unsigned int AudioTrack::* const policy::AudioTrackTable::PrimaryKey = &AudioTrack::m_id;
AudioTrack::AudioTrack( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection )
AudioTrack::AudioTrack( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
row >> m_id
>> m_codec
......@@ -109,15 +109,15 @@ bool AudioTrack::createTable( DBConnection dbConnection )
return sqlite::Tools::executeRequest( dbConnection, req );
}
std::shared_ptr<AudioTrack> AudioTrack::create( DBConnection dbConnection, const std::string& codec,
unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels,
std::shared_ptr<AudioTrack> AudioTrack::create( MediaLibraryPtr ml, const std::string& codec,
unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels,
const std::string& language, const std::string& desc, unsigned int mediaId )
{
static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
+ "(codec, bitrate, samplerate, nb_channels, language, description, media_id) VALUES(?, ?, ?, ?, ?, ?, ?)";
auto track = std::make_shared<AudioTrack>( codec, bitrate, sampleRate, nbChannels, language, desc, mediaId );
if ( insert( dbConnection, track, req, codec, bitrate, sampleRate, nbChannels, language, desc, mediaId ) == false )
if ( insert( ml, track, req, codec, bitrate, sampleRate, nbChannels, language, desc, mediaId ) == false )
return nullptr;
track->m_dbConnection = dbConnection;
track->m_ml = ml;
return track;
}
......@@ -42,7 +42,7 @@ struct AudioTrackTable
class AudioTrack : public IAudioTrack, public DatabaseHelpers<AudioTrack, policy::AudioTrackTable>
{
public:
AudioTrack( DBConnection dbConnection, sqlite::Row& row );
AudioTrack(MediaLibraryPtr ml, sqlite::Row& row );
AudioTrack(const std::string& codec, unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels, const std::string& language, const std::string& desc , unsigned int mediaId);
virtual unsigned int id() const override;
......@@ -54,12 +54,12 @@ class AudioTrack : public IAudioTrack, public DatabaseHelpers<AudioTrack, policy
virtual const std::string& description() const override;
static bool createTable( DBConnection dbConnection );
static std::shared_ptr<AudioTrack> create( DBConnection dbConnection, const std::string& codec,
static std::shared_ptr<AudioTrack> create( MediaLibraryPtr ml, const std::string& codec,
unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels,
const std::string& language, const std::string& desc, unsigned int mediaId );
private:
DBConnection m_dbConnection;
MediaLibraryPtr m_ml;
unsigned int m_id;
std::string m_codec;
unsigned int m_bitrate;
......
......@@ -26,8 +26,8 @@ const std::string policy::DeviceTable::Name = "Device";
const std::string policy::DeviceTable::PrimaryKeyColumn = "id_device";
unsigned int Device::* const policy::DeviceTable::PrimaryKey = &Device::m_id;
Device::Device( DBConnection dbConnection, sqlite::Row& row )
: m_dbConn( dbConnection )
Device::Device(MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
row >> m_id
>> m_uuid
......@@ -69,19 +69,19 @@ void Device::setPresent(bool value)
{
static const std::string req = "UPDATE " + policy::DeviceTable::Name +
" SET is_present = ? WHERE id_device = ?";
if ( sqlite::Tools::executeUpdate( m_dbConn, req, value, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, value, m_id ) == false )
return;
m_isPresent = value;
}
std::shared_ptr<Device> Device::create( DBConnection dbConnection, const std::string& uuid, bool isRemovable )
std::shared_ptr<Device> Device::create( MediaLibraryPtr ml, const std::string& uuid, bool isRemovable )
{
static const std::string req = "INSERT INTO " + policy::DeviceTable::Name
+ "(uuid, is_removable, is_present) VALUES(?, ?, ?)";
auto self = std::make_shared<Device>( uuid, isRemovable );
if ( insert( dbConnection, self, req, uuid, isRemovable, self->isPresent() ) == false )
if ( insert( ml, self, req, uuid, isRemovable, self->isPresent() ) == false )
return nullptr;
self->m_dbConn = dbConnection;
self->m_ml = ml;
return self;
}
......@@ -96,10 +96,10 @@ bool Device::createTable(DBConnection connection)
return sqlite::Tools::executeRequest( connection, req );
}
std::shared_ptr<Device> Device::fromUuid( DBConnection dbConnection, const std::string& uuid )
std::shared_ptr<Device> Device::fromUuid( MediaLibraryPtr ml, const std::string& uuid )
{
static const std::string req = "SELECT * FROM " + policy::DeviceTable::Name +
" WHERE uuid = ?";
return fetch( dbConnection, req, uuid );
return fetch( ml, req, uuid );
}
......@@ -41,19 +41,19 @@ class Device : public DatabaseHelpers<Device, policy::DeviceTable>
{
public:
Device( const std::string& uuid, bool isRemovable );
Device( DBConnection dbConnection, sqlite::Row& row );
Device( MediaLibraryPtr ml, sqlite::Row& row );
unsigned int id() const;
const std::string& uuid() const;
bool isRemovable() const;
bool isPresent() const;
void setPresent( bool value );
static std::shared_ptr<Device> create( DBConnection dbConnection, const std::string& uuid, bool isRemovable );
static std::shared_ptr<Device> create( MediaLibraryPtr ml, const std::string& uuid, bool isRemovable );
static bool createTable( DBConnection connection );
static std::shared_ptr<Device> fromUuid( DBConnection dbConnection, const std::string& uuid );
static std::shared_ptr<Device> fromUuid( MediaLibraryPtr ml, const std::string& uuid );
private:
DBConnection m_dbConn;
MediaLibraryPtr m_ml;
// This is a database ID
unsigned int m_id;
// This is a unique ID on the system side, in the /dev/disk/by-uuid sense.
......
......@@ -29,8 +29,8 @@ const std::string policy::FileTable::Name = "File";
const std::string policy::FileTable::PrimaryKeyColumn = "id_file";
unsigned int File::* const policy::FileTable::PrimaryKey = &File::m_id;
File::File( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection )
File::File( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
{
row >> m_id
>> m_mediaId
......@@ -69,7 +69,7 @@ const std::string& File::mrl() const
auto lock = m_fullPath.lock();
if ( m_fullPath.isCached() )
return m_fullPath;
auto folder = Folder::fetch( m_dbConnection, m_folderId );
auto folder = Folder::fetch( m_ml, m_folderId );
if ( folder == nullptr )
return m_mrl;
m_fullPath = folder->path() + m_mrl;
......@@ -96,14 +96,14 @@ std::shared_ptr<Media> File::media() const
auto lock = m_media.lock();
if ( m_media.isCached() == false )
{
m_media = Media::fetch( m_dbConnection, m_mediaId );
m_media = Media::fetch( m_ml, m_mediaId );
}
return m_media.get().lock();
}
bool File::destroy()
{
return DatabaseHelpers::destroy( m_dbConnection, m_id );
return DatabaseHelpers::destroy( m_ml, m_id );
}
void File::markParsed()
......@@ -111,7 +111,7 @@ void File::markParsed()
if ( m_isParsed == true )
return;
static const std::string req = "UPDATE " + policy::FileTable::Name + " SET parsed = 1 WHERE id_file = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_id ) == false )
return;
m_isParsed = true;
}
......@@ -146,16 +146,16 @@ bool File::createTable( DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, indexReq );
}
std::shared_ptr<File> File::create( DBConnection dbConnection, unsigned int mediaId, Type type, const fs::IFile& fileFs, unsigned int folderId, bool isRemovable )
std::shared_ptr<File> File::create( MediaLibraryPtr ml, unsigned int mediaId, Type type, const fs::IFile& fileFs, unsigned int folderId, bool isRemovable )
{
auto self = std::make_shared<File>( mediaId, type, fileFs, folderId, isRemovable );
static const std::string req = "INSERT INTO " + policy::FileTable::Name +
"(media_id, mrl, type, folder_id, last_modification_date, is_removable) VALUES(?, ?, ?, ?, ?, ?)";
if ( insert( dbConnection, self, req, mediaId, self->m_mrl, type, sqlite::ForeignKey( folderId ),
if ( insert( ml, self, req, mediaId, self->m_mrl, type, sqlite::ForeignKey( folderId ),
self->m_lastModificationDate, isRemovable ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
self->m_ml = ml;
self->m_fullPath = fileFs.fullPath();
return self;
}
......@@ -44,7 +44,7 @@ struct FileTable
class File : public IFile, public DatabaseHelpers<File, policy::FileTable>
{
public:
File( DBConnection dbConnection, sqlite::Row& row );
File( MediaLibraryPtr ml, sqlite::Row& row );
File( unsigned int mediaId, Type type, const fs::IFile& file, unsigned int folderId, bool isRemovable );
virtual unsigned int id() const override;