AlbumTrack.cpp 8.87 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
    int64_t dummyDuration;
46
47
    row >> m_id
        >> m_mediaId
48
        >> dummyDuration
49
        >> m_artistId
50
        >> m_genreId
51
        >> m_trackNumber
52
        >> m_albumId
53
54
        >> m_discNumber
        >> m_isPresent;
55
56
}

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

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

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

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

101
void AlbumTrack::createTable( sqlite::Connection* dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
{
103
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
104
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
105
                "media_id INTEGER,"
106
                "duration INTEGER NOT NULL,"
107
                "artist_id UNSIGNED INTEGER,"
108
                "genre_id INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
109
110
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
111
                "disc_number UNSIGNED INTEGER,"
112
                "is_present BOOLEAN NOT NULL DEFAULT 1,"
113
                "FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
114
115
116
                    " ON DELETE CASCADE,"
                "FOREIGN KEY (artist_id) REFERENCES " + policy::ArtistTable::Name + "(id_artist)"
                    " ON DELETE CASCADE,"
117
                "FOREIGN KEY (genre_id) REFERENCES " + policy::GenreTable::Name + "(id_genre),"
118
119
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) "
                    " ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
120
            ")";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
121
122
123
124
125
126
127
128
129
130
131
    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";
    const std::string indexReq = "CREATE INDEX IF NOT EXISTS "
            "album_media_artist_genre_album_idx ON " +
            policy::AlbumTrackTable::Name +
            "(media_id, artist_id, genre_id, album_id)";
132

133
134
135
    sqlite::Tools::executeRequest( dbConnection, req );
    sqlite::Tools::executeRequest( dbConnection, triggerReq );
    sqlite::Tools::executeRequest( dbConnection, indexReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
136
137
}

138
std::shared_ptr<AlbumTrack> AlbumTrack::create( MediaLibraryPtr ml, int64_t albumId,
139
                                                std::shared_ptr<Media> media, unsigned int trackNb,
140
141
                                                unsigned int discNumber, int64_t artistId, int64_t genreId,
                                                int64_t duration )
142
{
143
    auto self = std::make_shared<AlbumTrack>( ml, media->id(), artistId, genreId, trackNb, albumId, discNumber );
144
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
145
146
147
            + "(media_id, duration, artist_id, genre_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?, ?, ?, ?)";
    if ( insert( ml, self, req, media->id(), duration >= 0 ? duration : 0, sqlite::ForeignKey( artistId ),
                 sqlite::ForeignKey( genreId ), trackNb, albumId, discNumber ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148
        return nullptr;
149
    self->m_media = media;
150
151
152
    return self;
}

153
AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
154
155
156
{
    static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
            " WHERE media_id = ?";
157
    return fetch( ml, req, mediaId );
158
159
}

160
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
161
{
162
163
164
    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 ";
165
166
    switch ( sort )
    {
167
    case SortingCriteria::Duration:
168
169
        req += "m.duration";
        break;
170
    case SortingCriteria::InsertionDate:
171
172
        req += "m.insertion_date";
        break;
173
    case SortingCriteria::ReleaseDate:
174
175
        req += "m.release_date";
        break;
176
    case SortingCriteria::Alpha:
177
178
        req += "m.title";
        break;
179
    default:
180
        if ( desc == true )
181
            req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
182
        else
183
            req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
184
185
186
187
188
        break;
    }

    if ( desc == true )
        req += " DESC";
189
    return Media::fetchAll<IMedia>( ml, req, genreId );
190
191
}

192
GenrePtr AlbumTrack::genre()
193
194
195
196
{
    auto l = m_genre.lock();
    if ( m_genre.isCached() == false )
    {
197
        m_genre = Genre::fetch( m_ml, m_genreId );
198
199
    }
    return m_genre.get();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
200
201
}

202
bool AlbumTrack::setGenre( std::shared_ptr<Genre> genre )
203
{
204
205
206
207
208
209
210
211
    // We need to fetch the old genre entity now, in case it gets deleted through
    // the nbTracks reaching 0 trigger.
    if ( m_genreId > 0 )
    {
        auto l = m_genre.lock();
        if ( m_genre.isCached() == false )
            m_genre = Genre::fetch( m_ml, m_genreId );
    }
212
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
213
            + " SET genre_id = ? WHERE id_track = ?";
214
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req,
215
216
                                       sqlite::ForeignKey( genre != nullptr ? genre->id() : 0 ),
                                       m_id ) == false )
217
        return false;
218
219
220
221
222
223
224
225
226
227
228
229
230
    {
        auto l = m_genre.lock();
        if ( m_genreId > 0 )
            m_genre.get()->updateCachedNbTracks( -1 );
        m_genre = genre;
    }
    if ( genre != nullptr )
    {
        genre->updateCachedNbTracks( 1 );
        m_genreId = genre->id();
    }
    else
        m_genreId = 0;
231
232
233
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
234
235
236
237
238
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

239
240
241
242
243
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

244
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
245
{
246
247
248
249
250
251
252
253
    // "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
254
}
255

256
std::shared_ptr<IMedia> AlbumTrack::media()
257
258
259
260
261
262
263
264
{
    auto lock = m_media.lock();
    if ( m_media.isCached() == false )
    {
        m_media = Media::fetch( m_ml, m_mediaId );
    }
    return m_media.get().lock();
}
265
266

}