Album.cpp 4.27 KB
Newer Older
1
#include "Album.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
2
#include "AlbumTrack.h"
3 4 5

#include "SqliteTools.h"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6 7
const std::string policy::AlbumTable::Name = "Album";
const std::string policy::AlbumTable::CacheColumn = "id_album";
8
unsigned int Album::* const policy::AlbumTable::PrimaryKey = &Album::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
9

10
Album::Album(DBConnection dbConnection, sqlite3_stmt* stmt)
11 12
    : m_dbConnection( dbConnection )
{
13
    m_id = sqlite3_column_int( stmt, 0 );
14
    m_name = Traits<std::string>::Load( stmt, 1 );
15
    m_releaseDate = sqlite3_column_int( stmt, 2 );
16 17
    m_shortSummary = Traits<std::string>::Load( stmt, 3 );
    m_artworkUrl = Traits<std::string>::Load( stmt, 4 );
18
    m_lastSyncDate = sqlite3_column_int( stmt, 5 );
19 20 21 22
    m_id3tag = Traits<std::string>::Load( stmt, 6 );
}

Album::Album( const std::string& id3tag )
23
    : m_id( 0 )
24
    , m_releaseDate( 0 )
25 26 27 28 29 30 31 32
    , m_lastSyncDate( 0 )
    , m_id3tag( id3tag )
{
}

unsigned int Album::id() const
{
    return m_id;
33 34
}

35
const std::string& Album::name() const
36 37 38 39
{
    return m_name;
}

40
bool Album::setName( const std::string& name )
41
{
42 43 44 45 46 47
    static const std::string& req = "UPDATE " + policy::AlbumTable::Name
            + " SET name = ? WHERE id_album = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, name, m_id ) == false )
        return false;
    m_name = name;
    return true;
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
time_t Album::releaseDate() const
{
    return m_releaseDate;
}

bool Album::setReleaseDate( time_t date )
{
    static const std::string& req = "UPDATE " + policy::AlbumTable::Name
            + " SET release_date = ? WHERE id_album = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
        return false;
    m_releaseDate = date;
    return true;
}

const std::string& Album::shortSummary() const
66 67 68 69
{
    return m_shortSummary;
}

70 71 72 73 74 75 76 77 78 79 80
bool Album::setShortSummary( const std::string& summary )
{
    static const std::string& req = "UPDATE " + policy::AlbumTable::Name
            + " SET short_summary = ? WHERE id_album = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
        return false;
    m_shortSummary = summary;
    return true;
}

const std::string& Album::artworkUrl() const
81 82 83 84
{
    return m_artworkUrl;
}

85 86 87 88 89 90 91 92 93 94 95
bool Album::setArtworkUrl( const std::string& artworkUrl )
{
    static const std::string& req = "UPDATE " + policy::AlbumTable::Name
            + " SET artwork_url = ? WHERE id_album = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, artworkUrl, m_id ) == false )
        return false;
    m_artworkUrl = artworkUrl;
    return true;
}

time_t Album::lastSyncDate() const
96 97 98 99
{
    return m_lastSyncDate;
}

100
bool Album::tracks( std::vector<std::shared_ptr<IAlbumTrack> >& tracks ) const
101
{
102 103
    static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name
            + " WHERE album_id = ?";
104 105 106 107 108 109
    return SqliteTools::fetchAll<AlbumTrack>( m_dbConnection, req, tracks, m_id );
}

AlbumTrackPtr Album::addTrack( const std::string& name, unsigned int trackNb )
{
    return AlbumTrack::create( m_dbConnection, m_id, name, trackNb );
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124
bool Album::destroy()
{
    std::vector<AlbumTrackPtr> ts;
    if ( tracks( ts ) == false )
        return false;
    //FIXME: Have a single request to fetch all files at once, instead of having one per track
    for ( auto& t : ts )
    {
        t->destroy();
    }
    return _Cache::destroy( m_dbConnection, this );
}

125
bool Album::createTable(DBConnection dbConnection )
126
{
127 128 129
    static const std::string req = "CREATE TABLE IF NOT EXISTS " +
            policy::AlbumTable::Name +
            "("
130 131
                "id_album INTEGER PRIMARY KEY AUTOINCREMENT,"
                "name TEXT,"
132
                "release_date UNSIGNED INTEGER,"
133 134
                "short_summary TEXT,"
                "artwork_url TEXT,"
135 136
                "UNSIGNED INTEGER last_sync_date,"
                "id3tag TEXT UNIQUE ON CONFLICT FAIL"
137
            ")";
138 139 140
   return SqliteTools::executeRequest( dbConnection, req );
}

141
AlbumPtr Album::create(DBConnection dbConnection, const std::string& id3Tag )
142 143 144 145
{
    auto album = std::make_shared<Album>( id3Tag );
    static const std::string& req = "INSERT INTO " + policy::AlbumTable::Name +
            "(id_album, id3tag) VALUES(NULL, ?)";
146
    if ( _Cache::insert( dbConnection, album, req, id3Tag ) == false )
147 148 149
        return nullptr;
    album->m_dbConnection = dbConnection;
    return album;
150
}