AlbumTrack.cpp 7.68 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.
 *****************************************************************************/

23
24
25
26
#if HAVE_CONFIG_H
# include "config.h"
#endif

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27
28
#include "AlbumTrack.h"
#include "Album.h"
29
#include "Artist.h"
30
#include "Media.h"
31
#include "Genre.h"
32
#include "database/SqliteTools.h"
33
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34

35
36
37
namespace medialibrary
{

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
39
const std::string policy::AlbumTrackTable::PrimaryKeyColumn = "id_track";
40
int64_t AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
41

42
43
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44
{
45
46
    row >> m_id
        >> m_mediaId
47
        >> m_artistId
48
        >> m_genreId
49
        >> m_trackNumber
50
        >> m_albumId
51
52
        >> m_discNumber
        >> m_isPresent;
53
54
}

55
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, unsigned int trackNumber, int64_t albumId, unsigned int discNumber )
56
57
    : m_ml( ml )
    , m_id( 0 )
58
    , m_mediaId( mediaId )
59
60
    , m_artistId( 0 )
    , m_genreId( 0 )
61
62
    , m_trackNumber( trackNumber )
    , m_albumId( albumId )
63
    , m_discNumber( discNumber )
64
    , m_isPresent( true )
65
66
67
{
}

68
int64_t AlbumTrack::id() const
69
70
{
    return m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
71
72
}

73
ArtistPtr AlbumTrack::artist() const
74
{
75
76
    if ( m_artistId == 0 )
        return nullptr;
77
    auto lock = m_artist.lock();
78
    if ( m_artist.isCached() == false )
79
    {
80
        m_artist = Artist::fetch( m_ml, m_artistId );
81
82
    }
    return m_artist.get();
83
84
}

85
bool AlbumTrack::setArtist( std::shared_ptr<Artist> artist )
86
87
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name +
88
89
            " SET artist_id = ? WHERE id_track = ?";
    if ( artist->id() == m_artistId )
90
        return true;
91
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artist->id(), m_id ) == false )
92
        return false;
93
    m_artistId = artist->id();
94
95
96
97
    m_artist = artist;
    return true;
}

98
bool AlbumTrack::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
99
{
100
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
101
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
102
                "media_id INTEGER,"
103
                "artist_id UNSIGNED INTEGER,"
104
                "genre_id INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
105
106
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
107
                "disc_number UNSIGNED INTEGER,"
108
                "is_present BOOLEAN NOT NULL DEFAULT 1,"
109
                "FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
110
111
112
                    " ON DELETE CASCADE,"
                "FOREIGN KEY (artist_id) REFERENCES " + policy::ArtistTable::Name + "(id_artist)"
                    " ON DELETE CASCADE,"
113
                "FOREIGN KEY (genre_id) REFERENCES " + policy::GenreTable::Name + "(id_genre),"
114
115
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) "
                    " ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
116
            ")";
117
118
119
120
121
    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";
122

123
    return sqlite::Tools::executeRequest( dbConnection, req ) &&
124
            sqlite::Tools::executeRequest( dbConnection, triggerReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
125
126
}

127
std::shared_ptr<AlbumTrack> AlbumTrack::create( MediaLibraryPtr ml, int64_t albumId,
128
129
                                                std::shared_ptr<Media> media, unsigned int trackNb,
                                                unsigned int discNumber )
130
{
131
    auto self = std::make_shared<AlbumTrack>( ml, media->id(), trackNb, albumId, discNumber );
132
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
133
            + "(media_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?)";
134
    if ( insert( ml, self, req, media->id(), trackNb, albumId, discNumber ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
135
        return nullptr;
136
    self->m_media = media;
137
138
139
    return self;
}

140
AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
141
142
143
{
    static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
            " WHERE media_id = ?";
144
    return fetch( ml, req, mediaId );
145
146
}

147
std::vector<MediaPtr> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148
{
149
150
151
    std::string req = "SELECT m.* FROM " + policy::MediaTable::Name + " m"
            " INNER JOIN " + policy::AlbumTrackTable::Name + " t ON m.id_media = t.media_id"
            " WHERE t.genre_id = ? ORDER BY ";
152
153
    switch ( sort )
    {
154
    case SortingCriteria::Duration:
155
156
        req += "m.duration";
        break;
157
    case SortingCriteria::InsertionDate:
158
159
        req += "m.insertion_date";
        break;
160
    case SortingCriteria::ReleaseDate:
161
162
        req += "m.release_date";
        break;
163
    case SortingCriteria::Alpha:
164
165
        req += "m.title";
        break;
166
    default:
167
        if ( desc == true )
168
            req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
169
        else
170
            req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
171
172
173
174
175
        break;
    }

    if ( desc == true )
        req += " DESC";
176
    return Media::fetchAll<IMedia>( ml, req, genreId );
177
178
}

179
GenrePtr AlbumTrack::genre()
180
181
182
183
{
    auto l = m_genre.lock();
    if ( m_genre.isCached() == false )
    {
184
        m_genre = Genre::fetch( m_ml, m_genreId );
185
186
    }
    return m_genre.get();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
187
188
}

189
bool AlbumTrack::setGenre( std::shared_ptr<Genre> genre )
190
191
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
192
            + " SET genre_id = ? WHERE id_track = ?";
193
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req,
194
195
                                       sqlite::ForeignKey( genre != nullptr ? genre->id() : 0 ),
                                       m_id ) == false )
196
        return false;
197
    auto l = m_genre.lock();
198
    m_genre = genre;
199
    m_genreId = genre != nullptr ? genre->id() : 0;
200
201
202
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
203
204
205
206
207
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

208
209
210
211
212
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

213
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
214
{
215
216
217
218
219
220
221
222
    // "Fail" early in case there's no album to fetch
    if ( m_albumId == 0 )
        return nullptr;

    auto lock = m_album.lock();
    if ( m_album.isCached() == false )
        m_album = Album::fetch( m_ml, m_albumId );
    return m_album.get().lock();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
223
}
224

225
std::shared_ptr<IMedia> AlbumTrack::media()
226
227
228
229
230
231
232
233
{
    auto lock = m_media.lock();
    if ( m_media.isCached() == false )
    {
        m_media = Media::fetch( m_ml, m_mediaId );
    }
    return m_media.get().lock();
}
234
235

}