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

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

10
AlbumTrack::AlbumTrack( DBConnection dbConnection, sqlite3_stmt* stmt )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
11
    : m_dbConnection( dbConnection )
12
    , m_album( nullptr )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
13
{
14 15 16 17 18 19 20 21
    m_id = Traits<unsigned int>::Load( stmt, 0 );
    m_title = Traits<std::string>::Load( stmt, 1 );
    m_genre = Traits<std::string>::Load( stmt, 2 );
    m_trackNumber = Traits<unsigned int>::Load( stmt, 3 );
    m_albumId = Traits<unsigned int>::Load( stmt, 4 );
}

AlbumTrack::AlbumTrack( const std::string& title, unsigned int trackNumber, unsigned int albumId )
22
    : m_id( 0 )
23 24 25 26 27 28 29 30 31 32
    , m_title( title )
    , m_trackNumber( trackNumber )
    , m_albumId( albumId )
    , m_album( nullptr )
{
}

unsigned int AlbumTrack::id() const
{
    return m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33 34
}

35
bool AlbumTrack::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36 37
{
    const char* req = "CREATE TABLE IF NOT EXISTS AlbumTrack ("
38
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39 40 41 42
                "title TEXT,"
                "genre TEXT,"
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
43
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44
            ")";
45
    return SqliteTools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46 47
}

48
AlbumTrackPtr AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, const std::string& name, unsigned int trackNb)
49 50 51 52
{
    auto self = std::make_shared<AlbumTrack>( name, trackNb, albumId );
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
            + "(title, track_number, album_id) VALUES(?, ?, ?)";
53
    if ( _Cache::insert( dbConnection, self, req, name, trackNb, albumId ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54
        return nullptr;
55 56 57 58
    self->m_dbConnection = dbConnection;
    return self;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59 60 61 62 63
const std::string& AlbumTrack::genre()
{
    return m_genre;
}

64 65 66 67 68 69 70 71 72 73
bool AlbumTrack::setGenre(const std::string& genre)
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
            + " SET genre = ? WHERE id_track = ? ";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, genre, m_id ) == false )
        return false;
    m_genre = genre;
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74 75 76 77 78 79 80 81 82 83
const std::string& AlbumTrack::title()
{
    return m_title;
}

unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
85
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
86
    if ( m_album == nullptr && m_albumId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88
        m_album = Album::fetch( m_dbConnection, m_albumId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
89 90 91
    }
    return m_album;
}
92 93 94 95 96 97 98 99 100 101 102

bool AlbumTrack::destroy()
{
    // Manually remove Files from cache, and let foreign key handling delete them from the DB
    std::vector<FilePtr> fs;
    if ( files( fs ) == false )
        return false;
    if ( fs.size() == 0 )
        std::cerr << "No files found for AlbumTrack " << m_id << std::endl;
    for ( auto& f : fs )
    {
103 104 105
        // Ignore failures to discard from cache, we might want to discard records from
        // cache in a near future to avoid running out of memory on mobile devices
        File::discard( std::static_pointer_cast<File>( f ) );
106 107 108 109 110 111 112 113 114 115
    }
    return _Cache::destroy( m_dbConnection, this );
}

bool AlbumTrack::files(std::vector<FilePtr>& files)
{
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
            + " WHERE album_track_id = ? ";
    return SqliteTools::fetchAll<File>( m_dbConnection, req, files, m_id );
}