AlbumTrack.cpp 7.6 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 "Artist.h"
26
#include "Media.h"
27
#include "Genre.h"
28
#include "database/SqliteTools.h"
29
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
30

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31
const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
32
const std::string policy::AlbumTrackTable::PrimaryKeyColumn = "id_track";
33
int64_t AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34

35
36
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
37
{
38
39
    row >> m_id
        >> m_mediaId
40
        >> m_artistId
41
        >> m_genreId
42
        >> m_trackNumber
43
        >> m_albumId
44
45
        >> m_discNumber
        >> m_isPresent;
46
47
}

48
AlbumTrack::AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, unsigned int trackNumber, int64_t albumId, unsigned int discNumber )
49
50
    : m_ml( ml )
    , m_id( 0 )
51
    , m_mediaId( mediaId )
52
53
    , m_artistId( 0 )
    , m_genreId( 0 )
54
55
    , m_trackNumber( trackNumber )
    , m_albumId( albumId )
56
    , m_discNumber( discNumber )
57
    , m_isPresent( true )
58
59
60
{
}

61
int64_t AlbumTrack::id() const
62
63
{
    return m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64
65
}

66
ArtistPtr AlbumTrack::artist() const
67
{
68
69
    if ( m_artistId == 0 )
        return nullptr;
70
    auto lock = m_artist.lock();
71
    if ( m_artist.isCached() == false )
72
    {
73
        m_artist = Artist::fetch( m_ml, m_artistId );
74
75
    }
    return m_artist.get();
76
77
}

78
bool AlbumTrack::setArtist( std::shared_ptr<Artist> artist )
79
80
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name +
81
82
            " SET artist_id = ? WHERE id_track = ?";
    if ( artist->id() == m_artistId )
83
        return true;
84
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artist->id(), m_id ) == false )
85
        return false;
86
    m_artistId = artist->id();
87
88
89
90
    m_artist = artist;
    return true;
}

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

116
    return sqlite::Tools::executeRequest( dbConnection, req ) &&
117
            sqlite::Tools::executeRequest( dbConnection, triggerReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
118
119
}

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

133
AlbumTrackPtr AlbumTrack::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
134
135
136
{
    static const std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
            " WHERE media_id = ?";
137
    return fetch( ml, req, mediaId );
138
139
}

140
std::vector<MediaPtr> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, medialibrary::SortingCriteria sort, bool desc )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
141
{
142
143
144
    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 ";
145
146
147
148
149
150
151
152
153
154
155
    switch ( sort )
    {
    case medialibrary::SortingCriteria::Duration:
        req += "m.duration";
        break;
    case medialibrary::SortingCriteria::InsertionDate:
        req += "m.insertion_date";
        break;
    case medialibrary::SortingCriteria::ReleaseDate:
        req += "m.release_date";
        break;
156
157
158
    case medialibrary::SortingCriteria::Alpha:
        req += "m.title";
        break;
159
    default:
160
        if ( desc == true )
161
            req += "t.artist_id DESC, t.album_id DESC, t.disc_number DESC, t.track_number DESC, m.filename";
162
        else
163
            req += "t.artist_id, t.album_id, t.disc_number, t.track_number, m.filename";
164
165
166
167
168
        break;
    }

    if ( desc == true )
        req += " DESC";
169
    return Media::fetchAll<IMedia>( ml, req, genreId );
170
171
172
173
174
175
176
}

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

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
196
197
198
199
200
unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

201
202
203
204
205
unsigned int AlbumTrack::discNumber() const
{
    return m_discNumber;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
206
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
207
{
208
209
    auto album = m_album.lock();
    if ( album == nullptr && m_albumId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
210
    {
211
        album = Album::fetch( m_ml, m_albumId );
212
        m_album = album;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
213
    }
214
    return album;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
215
}
216
217
218
219
220
221
222
223
224
225

std::shared_ptr<IMedia> AlbumTrack::media()
{
    auto lock = m_media.lock();
    if ( m_media.isCached() == false )
    {
        m_media = Media::fetch( m_ml, m_mediaId );
    }
    return m_media.get().lock();
}