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

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

34
AlbumTrack::AlbumTrack( DBConnection dbConnection, sqlite3_stmt* stmt )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35
    : m_dbConnection( dbConnection )
36
    , m_album( nullptr )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
37
{
38
39
40
41
    m_id = sqlite::Traits<unsigned int>::Load( stmt, 0 );
    m_title = sqlite::Traits<std::string>::Load( stmt, 1 );
    m_genre = sqlite::Traits<std::string>::Load( stmt, 2 );
    m_trackNumber = sqlite::Traits<unsigned int>::Load( stmt, 3 );
42
    m_albumId = sqlite::Traits<unsigned int>::Load( stmt, 4 );
43
44
45
}

AlbumTrack::AlbumTrack( const std::string& title, unsigned int trackNumber, unsigned int albumId )
46
    : m_id( 0 )
47
48
49
50
51
52
53
54
55
56
    , m_title( title )
    , m_trackNumber( trackNumber )
    , m_albumId( albumId )
    , m_album( nullptr )
{
}

unsigned int AlbumTrack::id() const
{
    return m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
57
58
}

59
bool AlbumTrack::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
{
61
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AlbumTrackTable::Name + "("
62
                "id_track INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
63
64
65
66
                "title TEXT,"
                "genre TEXT,"
                "track_number UNSIGNED INTEGER,"
                "album_id UNSIGNED INTEGER NOT NULL,"
67
                "FOREIGN KEY (album_id) REFERENCES Album(id_album) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
68
            ")";
69
    static const std::string reqRel = "CREATE TABLE IF NOT EXISTS TrackArtistRelation("
70
                "id_track INTEGER NOT NULL,"
71
72
73
74
75
76
77
78
79
                "id_artist INTEGER,"
                "PRIMARY KEY (id_track, id_artist),"
                "FOREIGN KEY(id_track) REFERENCES " + policy::AlbumTrackTable::Name + "("
                    + policy::AlbumTrackTable::CacheColumn + ") ON DELETE CASCADE,"
                "FOREIGN KEY(id_artist) REFERENCES " + policy::ArtistTable::Name + "("
                    + policy::ArtistTable::CacheColumn + ") ON DELETE CASCADE"
            ")";
    return sqlite::Tools::executeRequest( dbConnection, req ) &&
            sqlite::Tools::executeRequest( dbConnection, reqRel );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
80
81
}

82
AlbumTrackPtr AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, const std::string& name, unsigned int trackNb)
83
84
85
86
{
    auto self = std::make_shared<AlbumTrack>( name, trackNb, albumId );
    static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
            + "(title, track_number, album_id) VALUES(?, ?, ?)";
87
    if ( _Cache::insert( dbConnection, self, req, name, trackNb, albumId ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88
        return nullptr;
89
90
91
92
    self->m_dbConnection = dbConnection;
    return self;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
93
94
95
96
97
const std::string& AlbumTrack::genre()
{
    return m_genre;
}

98
99
100
101
bool AlbumTrack::setGenre(const std::string& genre)
{
    static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
            + " SET genre = ? WHERE id_track = ? ";
102
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, genre, m_id ) == false )
103
104
105
106
107
        return false;
    m_genre = genre;
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108
109
110
111
112
113
114
115
116
117
const std::string& AlbumTrack::title()
{
    return m_title;
}

unsigned int AlbumTrack::trackNumber()
{
    return m_trackNumber;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
118
std::shared_ptr<IAlbum> AlbumTrack::album()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
119
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
120
    if ( m_album == nullptr && m_albumId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
121
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
122
        m_album = Album::fetch( m_dbConnection, m_albumId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
123
124
125
    }
    return m_album;
}
126
127
128
129

bool AlbumTrack::destroy()
{
    // Manually remove Files from cache, and let foreign key handling delete them from the DB
130
    auto fs = files();
131
    if ( fs.size() == 0 )
132
        LOG_WARN( "No files found for AlbumTrack ", m_id );
133
134
    for ( auto& f : fs )
    {
135
136
137
        // Ignore failures to discard from cache, we might want to discard records from
        // cache in a near future to avoid running out of memory on mobile devices
        File::discard( std::static_pointer_cast<File>( f ) );
138
139
140
141
    }
    return _Cache::destroy( m_dbConnection, this );
}

142
bool AlbumTrack::addArtist( ArtistPtr artist )
143
{
144
    static const std::string req = "INSERT INTO TrackArtistRelation VALUES(?, ?)";
145
146
147
    // If track's ID is 0, the request will fail due to table constraints
    sqlite::ForeignKey artistForeignKey( artist != nullptr ? artist->id() : 0 );
    return sqlite::Tools::executeRequest( m_dbConnection, req, m_id, artistForeignKey );
148
149
}

150
std::vector<ArtistPtr> AlbumTrack::artists() const
151
{
152
153
154
    static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
            "LEFT JOIN TrackArtistRelation tar ON tar.id_artist = art.id_artist "
            "WHERE tar.id_track = ?";
155
    return Artist::fetchAll( m_dbConnection, req, m_id );
156
157
}

158
std::vector<FilePtr> AlbumTrack::files()
159
160
161
{
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
            + " WHERE album_track_id = ? ";
162
    return File::fetchAll( m_dbConnection, req, m_id );
163
}