Genre.cpp 6.67 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

27
28
#include "Genre.h"

29
#include "Album.h"
30
#include "AlbumTrack.h"
31
#include "Artist.h"
32

33
34
35
namespace medialibrary
{

36
37
38
39
namespace policy
{
const std::string GenreTable::Name = "Genre";
const std::string GenreTable::PrimaryKeyColumn = "id_genre";
40
int64_t Genre::* const GenreTable::PrimaryKey = &Genre::m_id;
41
42
}

43
44
Genre::Genre( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
45
46
{
    row >> m_id
47
48
        >> m_name
        >> m_nbTracks;
49
50
}

51
52
Genre::Genre( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
53
    , m_id( 0 )
54
    , m_name( name )
55
    , m_nbTracks( 0 )
56
57
58
{
}

59
int64_t Genre::id() const
60
61
62
63
64
65
66
67
68
{
    return m_id;
}

const std::string& Genre::name() const
{
    return m_name;
}

69
70
71
72
73
74
75
76
77
78
uint32_t Genre::nbTracks() const
{
    return m_nbTracks;
}

void Genre::updateCachedNbTracks( int increment )
{
    m_nbTracks += increment;
}

79
std::vector<ArtistPtr> Genre::artists( SortingCriteria, bool desc ) const
80
{
81
    std::string req = "SELECT a.* FROM " + policy::ArtistTable::Name + " a "
82
            "INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
83
84
85
86
            "WHERE att.genre_id = ? GROUP BY att.artist_id"
            " ORDER BY a.name";
    if ( desc == true )
        req += " DESC";
87
    return Artist::fetchAll<IArtist>( m_ml, req, m_id );
88
89
}

90
std::vector<MediaPtr> Genre::tracks( SortingCriteria sort, bool desc ) const
91
{
92
    return AlbumTrack::fromGenre( m_ml, m_id, sort, desc );
93
94
}

95
std::vector<AlbumPtr> Genre::albums( SortingCriteria sort, bool desc ) const
96
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
97
    return Album::fromGenre( m_ml, m_id, sort, desc );
98
99
}

100
void Genre::createTable( sqlite::Connection* dbConn )
101
{
102
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::GenreTable::Name +
103
104
        "("
            "id_genre INTEGER PRIMARY KEY AUTOINCREMENT,"
105
            "name TEXT COLLATE NOCASE UNIQUE ON CONFLICT FAIL,"
106
            "nb_tracks INTEGER NOT NULL DEFAULT 0"
107
        ")";
108
    const std::string vtableReq = "CREATE VIRTUAL TABLE IF NOT EXISTS "
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
109
110
111
112
                + policy::GenreTable::Name + "Fts USING FTS3("
                "name"
            ")";

113
114
115
116
117
118
    sqlite::Tools::executeRequest( dbConn, req );
    sqlite::Tools::executeRequest( dbConn, vtableReq );
}

void Genre::createTriggers( sqlite::Connection* dbConn )
{
119
    const std::string vtableInsertTrigger = "CREATE TRIGGER IF NOT EXISTS insert_genre_fts"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
120
121
122
123
            " AFTER INSERT ON " + policy::GenreTable::Name +
            " BEGIN"
            " INSERT INTO " + policy::GenreTable::Name + "Fts(rowid,name) VALUES(new.id_genre, new.name);"
            " END";
124
    const std::string vtableDeleteTrigger = "CREATE TRIGGER IF NOT EXISTS delete_genre_fts"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
125
126
127
128
            " BEFORE DELETE ON " + policy::GenreTable::Name +
            " BEGIN"
            " DELETE FROM " + policy::GenreTable::Name + "Fts WHERE rowid = old.id_genre;"
            " END";
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    const std::string onGenreChanged = "CREATE TRIGGER IF NOT EXISTS on_track_genre_changed AFTER UPDATE OF "
            " genre_id ON " + policy::AlbumTrackTable::Name +
            " BEGIN"
            " UPDATE " + policy::GenreTable::Name + " SET nb_tracks = nb_tracks + 1 WHERE id_genre = new.genre_id;"
            " UPDATE " + policy::GenreTable::Name + " SET nb_tracks = nb_tracks - 1 WHERE id_genre = old.genre_id;"
            " DELETE FROM " + policy::GenreTable::Name + " WHERE nb_tracks = 0;"
            " END";
    const std::string onTrackCreated = "CREATE TRIGGER IF NOT EXISTS update_genre_on_new_track"
            " AFTER INSERT ON " + policy::AlbumTrackTable::Name +
            " WHEN new.genre_id IS NOT NULL"
            " BEGIN"
            " UPDATE " + policy::GenreTable::Name + " SET nb_tracks = nb_tracks + 1 WHERE id_genre = new.genre_id;"
            " END";
    const std::string onTrackDeleted = "CREATE TRIGGER IF NOT EXISTS update_genre_on_track_deleted"
            " AFTER DELETE ON " + policy::AlbumTrackTable::Name +
            " WHEN old.genre_id IS NOT NULL"
            " BEGIN"
            " UPDATE " + policy::GenreTable::Name + " SET nb_tracks = nb_tracks - 1 WHERE id_genre = old.genre_id;"
            " DELETE FROM " + policy::GenreTable::Name + " WHERE nb_tracks = 0;"
            " END";

150
151
    sqlite::Tools::executeRequest( dbConn, vtableInsertTrigger );
    sqlite::Tools::executeRequest( dbConn, vtableDeleteTrigger );
152
153
154
    sqlite::Tools::executeRequest( dbConn, onGenreChanged );
    sqlite::Tools::executeRequest( dbConn, onTrackCreated );
    sqlite::Tools::executeRequest( dbConn, onTrackDeleted );
155
156
}

157
std::shared_ptr<Genre> Genre::create( MediaLibraryPtr ml, const std::string& name )
158
159
160
{
    static const std::string req = "INSERT INTO " + policy::GenreTable::Name + "(name)"
            "VALUES(?)";
161
    auto self = std::make_shared<Genre>( ml, name );
162
    if ( insert( ml, self, req, name ) == false )
163
164
165
166
        return nullptr;
    return self;
}

167
std::shared_ptr<Genre> Genre::fromName( MediaLibraryPtr ml, const std::string& name )
168
169
{
    static const std::string req = "SELECT * FROM " + policy::GenreTable::Name + " WHERE name = ?";
170
    return fetch( ml, req, name );
171
172
}

173
std::vector<GenrePtr> Genre::search( MediaLibraryPtr ml, const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
174
175
{
    static const std::string req = "SELECT * FROM " + policy::GenreTable::Name + " WHERE id_genre IN "
176
177
            "(SELECT rowid FROM " + policy::GenreTable::Name + "Fts WHERE name MATCH '*' || ? || '*')";
    return fetchAll<IGenre>( ml, req, name );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
178
179
}

180
std::vector<GenrePtr> Genre::listAll( MediaLibraryPtr ml, SortingCriteria, bool desc )
181
182
183
184
185
186
187
{
    std::string req = "SELECT * FROM " + policy::GenreTable::Name + " ORDER BY name";
    if ( desc == true )
        req += " DESC";
    return fetchAll<IGenre>( ml, req );
}

188
}