AlbumTrack.cpp 8.88 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 "database/SqliteQuery.h"
34
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35

36
37
38
namespace medialibrary
{

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

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

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

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

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

89
90
91
92
93
int64_t AlbumTrack::artistId() const
{
    return m_artistId;
}

94
void AlbumTrack::createTable( sqlite::Connection* dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
95
{
96
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
97
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
98
                "media_id INTEGER,"
99
                "duration INTEGER NOT NULL,"
100
                "artist_id UNSIGNED INTEGER,"
101
                "genre_id INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
103
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
104
                "disc_number UNSIGNED INTEGER,"
105
                "is_present BOOLEAN NOT NULL DEFAULT 1,"
106
                "FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
107
108
109
                    " ON DELETE CASCADE,"
                "FOREIGN KEY (artist_id) REFERENCES " + policy::ArtistTable::Name + "(id_artist)"
                    " ON DELETE CASCADE,"
110
                "FOREIGN KEY (genre_id) REFERENCES " + policy::GenreTable::Name + "(id_genre),"
111
112
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) "
                    " ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
113
            ")";
114
115
116
117
118
119

    sqlite::Tools::executeRequest( dbConnection, req );
}

void AlbumTrack::createTriggers(sqlite::Connection* dbConnection)
{
120
    const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_track_present "
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
121
122
123
124
125
126
127
128
129
130
            "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)";
131
132
    sqlite::Tools::executeRequest( dbConnection, triggerReq );
    sqlite::Tools::executeRequest( dbConnection, indexReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133
134
}

135
std::shared_ptr<AlbumTrack> AlbumTrack::create( MediaLibraryPtr ml, int64_t albumId,
136
                                                std::shared_ptr<Media> media, unsigned int trackNb,
137
138
                                                unsigned int discNumber, int64_t artistId, int64_t genreId,
                                                int64_t duration )
139
{
140
    auto self = std::make_shared<AlbumTrack>( ml, media->id(), artistId, genreId, trackNb, albumId, discNumber );
141
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
142
143
144
            + "(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
145
        return nullptr;
146
    self->m_media = media;
147
148
149
    return self;
}

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

157
Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
158
{
159
    std::string req = "FROM " + policy::MediaTable::Name + " m"
160
            " INNER JOIN " + policy::AlbumTrackTable::Name + " t ON m.id_media = t.media_id"
161
            " WHERE t.genre_id = ? AND m.is_present = 1 ORDER BY ";
162
163
    auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
    auto desc = params != nullptr ? params->desc : false;
164
165
    switch ( sort )
    {
166
    case SortingCriteria::Duration:
167
168
        req += "m.duration";
        break;
169
    case SortingCriteria::InsertionDate:
170
171
        req += "m.insertion_date";
        break;
172
    case SortingCriteria::ReleaseDate:
173
174
        req += "m.release_date";
        break;
175
    case SortingCriteria::Alpha:
176
177
        req += "m.title";
        break;
178
    default:
179
        if ( desc == true )
180
            req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
181
        else
182
            req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
183
184
185
186
187
        break;
    }

    if ( desc == true )
        req += " DESC";
188
    return make_query<Media, IMedia>( ml, "m.*", std::move( req ), genreId );
189
190
}

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

201
202
203
204
205
int64_t AlbumTrack::genreId() const
{
    return m_genreId;
}

206
bool AlbumTrack::setGenre( std::shared_ptr<Genre> genre )
207
{
208
209
210
211
212
213
214
215
    // 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 );
    }
216
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
217
            + " SET genre_id = ? WHERE id_track = ?";
218
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req,
219
220
                                       sqlite::ForeignKey( genre != nullptr ? genre->id() : 0 ),
                                       m_id ) == false )
221
        return false;
222
223
224
225
226
227
228
229
230
231
232
233
234
    {
        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;
235
236
237
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
238
239
240
241
242
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

243
244
245
246
247
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

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

260
261
262
263
264
int64_t AlbumTrack::albumId() const
{
    return m_albumId;
}

265
std::shared_ptr<IMedia> AlbumTrack::media()
266
267
268
269
270
271
272
273
{
    auto lock = m_media.lock();
    if ( m_media.isCached() == false )
    {
        m_media = Media::fetch( m_ml, m_mediaId );
    }
    return m_media.get().lock();
}
274
275

}