AlbumTrack.cpp 4.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
23
24
#include "AlbumTrack.h"
#include "Album.h"
25
#include "Media.h"
26
#include "database/SqliteTools.h"
27
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
29
30
const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
const std::string policy::AlbumTrackTable::CacheColumn = "id_track";
31
unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32

33
AlbumTrack::AlbumTrack(DBConnection dbConnection, sqlite::Row& row )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34
    : m_dbConnection( dbConnection )
35
    , m_album( nullptr )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
{
37
38
    row >> m_id
        >> m_mediaId
39
        >> m_artist
40
41
42
        >> m_genre
        >> m_trackNumber
        >> m_albumId;
43
44
}

45
46
//FIXME: constify media
AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int albumId )
47
    : m_id( 0 )
48
    , m_mediaId( media->id() )
49
50
51
52
53
54
55
56
57
    , 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
58
59
}

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const std::string&AlbumTrack::artist() const
{
    return m_artist;
}

bool AlbumTrack::setArtist( const std::string& artist )
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name +
            " SET artist = ? WHERE id_track = ?";
    if ( artist == m_artist )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist, m_id ) == false )
        return false;
    m_artist = artist;
    return true;
}

77
bool AlbumTrack::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
78
{
79
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
80
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
81
                "media_id INTEGER,"
82
                "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
83
84
85
                "genre TEXT,"
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
86
87
88
89
                "FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
                    " ON DELETE CASCADE, "
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) "
                    " ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
90
            ")";
91
    return sqlite::Tools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
92
93
}

94
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb)
95
{
96
    auto self = std::make_shared<AlbumTrack>( media, trackNb, albumId );
97
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
98
99
            + "(media_id, track_number, album_id) VALUES(?, ?, ?)";
    if ( _Cache::insert( dbConnection, self, req, media->id(), trackNb, albumId ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
100
        return nullptr;
101
102
103
104
    self->m_dbConnection = dbConnection;
    return self;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
105
106
107
108
109
const std::string& AlbumTrack::genre()
{
    return m_genre;
}

110
111
112
113
bool AlbumTrack::setGenre(const std::string& genre)
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
            + " SET genre = ? WHERE id_track = ? ";
114
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, genre, m_id ) == false )
115
116
117
118
119
        return false;
    m_genre = genre;
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
120
121
122
123
124
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
125
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
126
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
127
    if ( m_album == nullptr && m_albumId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
128
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
129
        m_album = Album::fetch( m_dbConnection, m_albumId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
130
131
132
    }
    return m_album;
}