Genre.cpp 4.92 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 47 48 49
{
    row >> m_id
        >> m_name;
}

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

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

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

67
std::vector<ArtistPtr> Genre::artists( SortingCriteria, bool desc ) const
68
{
69
    std::string req = "SELECT a.* FROM " + policy::ArtistTable::Name + " a "
70
            "INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
71 72 73 74
            "WHERE att.genre_id = ? GROUP BY att.artist_id"
            " ORDER BY a.name";
    if ( desc == true )
        req += " DESC";
75
    return Artist::fetchAll<IArtist>( m_ml, req, m_id );
76 77
}

78
std::vector<MediaPtr> Genre::tracks( SortingCriteria sort, bool desc ) const
79
{
80
    return AlbumTrack::fromGenre( m_ml, m_id, sort, desc );
81 82
}

83
std::vector<AlbumPtr> Genre::albums( SortingCriteria sort, bool desc ) const
84
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
85
    return Album::fromGenre( m_ml, m_id, sort, desc );
86 87
}

88 89 90 91 92 93 94
bool Genre::createTable( DBConnection dbConn )
{
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::GenreTable::Name +
        "("
            "id_genre INTEGER PRIMARY KEY AUTOINCREMENT,"
            "name TEXT UNIQUE ON CONFLICT FAIL"
        ")";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    static const std::string vtableReq = "CREATE VIRTUAL TABLE IF NOT EXISTS "
                + policy::GenreTable::Name + "Fts USING FTS3("
                "name"
            ")";

    static const std::string vtableInsertTrigger = "CREATE TRIGGER IF NOT EXISTS insert_genre_fts"
            " AFTER INSERT ON " + policy::GenreTable::Name +
            " BEGIN"
            " INSERT INTO " + policy::GenreTable::Name + "Fts(rowid,name) VALUES(new.id_genre, new.name);"
            " END";
    static const std::string vtableDeleteTrigger = "CREATE TRIGGER IF NOT EXISTS delete_genre_fts"
            " BEFORE DELETE ON " + policy::GenreTable::Name +
            " BEGIN"
            " DELETE FROM " + policy::GenreTable::Name + "Fts WHERE rowid = old.id_genre;"
            " END";
    return sqlite::Tools::executeRequest( dbConn, req ) &&
            sqlite::Tools::executeRequest( dbConn, vtableReq ) &&
            sqlite::Tools::executeRequest( dbConn, vtableInsertTrigger ) &&
            sqlite::Tools::executeRequest( dbConn, vtableDeleteTrigger );
114 115
}

116
std::shared_ptr<Genre> Genre::create( MediaLibraryPtr ml, const std::string& name )
117 118 119
{
    static const std::string req = "INSERT INTO " + policy::GenreTable::Name + "(name)"
            "VALUES(?)";
120
    auto self = std::make_shared<Genre>( ml, name );
121
    if ( insert( ml, self, req, name ) == false )
122 123 124 125
        return nullptr;
    return self;
}

126
std::shared_ptr<Genre> Genre::fromName( MediaLibraryPtr ml, const std::string& name )
127 128
{
    static const std::string req = "SELECT * FROM " + policy::GenreTable::Name + " WHERE name = ?";
129
    return fetch( ml, req, name );
130 131
}

132
std::vector<GenrePtr> Genre::search( MediaLibraryPtr ml, const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133 134 135
{
    static const std::string req = "SELECT * FROM " + policy::GenreTable::Name + " WHERE id_genre IN "
            "(SELECT rowid FROM " + policy::GenreTable::Name + "Fts WHERE name MATCH ?)";
136
    return fetchAll<IGenre>( ml, req, name + "*" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
137 138
}

139
std::vector<GenrePtr> Genre::listAll( MediaLibraryPtr ml, SortingCriteria, bool desc )
140 141 142 143 144 145 146
{
    std::string req = "SELECT * FROM " + policy::GenreTable::Name + " ORDER BY name";
    if ( desc == true )
        req += " DESC";
    return fetchAll<IGenre>( ml, req );
}

147
}