AlbumTrack.cpp 5.64 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
const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
30
const std::string policy::AlbumTrackTable::PrimaryKeyColumn = "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
35
    : m_dbConnection( dbConnection )
{
36
37
    row >> m_id
        >> m_mediaId
38
        >> m_artist
39
40
        >> m_genre
        >> m_trackNumber
41
        >> m_albumId
42
        >> m_releaseYear
43
44
        >> m_discNumber
        >> m_isPresent;
45
46
}

47
//FIXME: constify media
48
AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int albumId, unsigned int discNumber )
49
    : m_id( 0 )
50
    , m_mediaId( media->id() )
51
52
    , m_trackNumber( trackNumber )
    , m_albumId( albumId )
53
    , m_releaseYear( 0 )
54
    , m_discNumber( discNumber )
55
    , m_isPresent( true )
56
57
58
59
60
61
{
}

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

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
}

81
bool AlbumTrack::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
82
{
83
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
84
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
85
                "media_id INTEGER,"
86
                "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87
88
89
                "genre TEXT,"
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
90
                "release_year UNSIGNED INTEGER,"
91
                "disc_number UNSIGNED INTEGER,"
92
                "is_present BOOLEAN NOT NULL DEFAULT 1,"
93
94
95
96
                "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
97
            ")";
98
99
100
101
102
103
104
    static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_track_present AFTER UPDATE OF is_present "
            "ON " + policy::MediaTable::Name +
            " BEGIN"
            " UPDATE " + policy::AlbumTrackTable::Name + " SET is_present = new.is_present WHERE media_id = new.id_media;"
            " END";
    return sqlite::Tools::executeRequest( dbConnection, req ) &&
            sqlite::Tools::executeRequest( dbConnection, triggerReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
105
106
}

107
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb, unsigned int discNumber )
108
{
109
    auto self = std::make_shared<AlbumTrack>( media, trackNb, albumId, discNumber );
110
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
111
            + "(media_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?)";
112
    if ( insert( dbConnection, self, req, media->id(), trackNb, albumId, discNumber ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
113
        return nullptr;
114
115
116
117
    self->m_dbConnection = dbConnection;
    return self;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
118
119
120
121
122
const std::string& AlbumTrack::genre()
{
    return m_genre;
}

123
124
125
bool AlbumTrack::setGenre(const std::string& genre)
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
126
            + " SET genre = ? WHERE id_track = ?";
127
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, genre, m_id ) == false )
128
129
130
131
132
        return false;
    m_genre = genre;
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133
134
135
136
137
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
unsigned int AlbumTrack::releaseYear() const
{
    return m_releaseYear;
}

bool AlbumTrack::setReleaseYear(unsigned int year)
{
    if ( m_releaseYear == 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 )
        return false;
    m_releaseYear = year;
    return true;
}

155
156
157
158
159
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
160
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
161
{
162
163
    auto album = m_album.lock();
    if ( album == nullptr && m_albumId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
164
    {
165
166
        album = Album::fetch( m_dbConnection, m_albumId );
        m_album = album;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
167
    }
168
    return album;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
169
}