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

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

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

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

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

151
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
152
{
153
154
    std::string req = "SELECT m.* FROM " + policy::MediaTable::Name + " m"
            " INNER JOIN " + policy::AlbumTrackTable::Name + " t ON m.id_media = t.media_id"
155
            " WHERE t.genre_id = ? AND m.is_present = 1 ORDER BY ";
156
157
    switch ( sort )
    {
158
    case SortingCriteria::Duration:
159
160
        req += "m.duration";
        break;
161
    case SortingCriteria::InsertionDate:
162
163
        req += "m.insertion_date";
        break;
164
    case SortingCriteria::ReleaseDate:
165
166
        req += "m.release_date";
        break;
167
    case SortingCriteria::Alpha:
168
169
        req += "m.title";
        break;
170
    default:
171
        if ( desc == true )
172
            req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
173
        else
174
            req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
175
176
177
178
179
        break;
    }

    if ( desc == true )
        req += " DESC";
180
    return Media::fetchAll<IMedia>( ml, req, genreId );
181
182
}

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

193
bool AlbumTrack::setGenre( std::shared_ptr<Genre> genre )
194
{
195
196
197
198
199
200
201
202
    // 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 );
    }
203
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
204
            + " SET genre_id = ? WHERE id_track = ?";
205
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req,
206
207
                                       sqlite::ForeignKey( genre != nullptr ? genre->id() : 0 ),
                                       m_id ) == false )
208
        return false;
209
210
211
212
213
214
215
216
217
218
219
220
221
    {
        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;
222
223
224
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
225
226
227
228
229
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

230
231
232
233
234
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

235
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
236
{
237
238
239
240
241
242
243
244
    // "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
245
}
246

247
std::shared_ptr<IMedia> AlbumTrack::media()
248
249
250
251
252
253
254
255
{
    auto lock = m_media.lock();
    if ( m_media.isCached() == false )
    {
        m_media = Media::fetch( m_ml, m_mediaId );
    }
    return m_media.get().lock();
}
256
257

}