Show.cpp 8.03 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
#include "Show.h"
28 29

#include "Media.h"
30
#include "ShowEpisode.h"
31
#include "MediaLibrary.h"
32

33
#include "database/SqliteTools.h"
34
#include "database/SqliteQuery.h"
35

36 37 38
namespace medialibrary
{

39 40 41
const std::string Show::Table::Name = "Show";
const std::string Show::Table::PrimaryKeyColumn = "id_show";
int64_t Show::* const Show::Table::PrimaryKey = &Show::m_id;
42

43 44
Show::Show( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
45
{
46
    row >> m_id
47
        >> m_title
48 49
        >> m_releaseDate
        >> m_shortSummary
50
        >> m_artworkMrl
51
        >> m_tvdbId;
52 53
}

54 55 56
Show::Show( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
57
    , m_title( name )
58
    , m_releaseDate( 0 )
59 60 61
{
}

62
int64_t Show::id() const
63 64 65 66
{
    return m_id;
}

67
const std::string& Show::title() const
68
{
69
    return m_title;
70 71
}

72
time_t Show::releaseDate() const
73
{
74
    return m_releaseDate;
75 76
}

77 78
bool Show::setReleaseDate( time_t date )
{
79
    static const std::string req = "UPDATE " + Show::Table::Name
80
            + " SET release_date = ? WHERE id_show = ?";
81
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, date, m_id ) == false )
82 83 84 85 86 87
        return false;
    m_releaseDate = date;
    return true;
}

const std::string& Show::shortSummary() const
88 89 90 91
{
    return m_shortSummary;
}

92 93
bool Show::setShortSummary( const std::string& summary )
{
94
    static const std::string req = "UPDATE " + Show::Table::Name
95
            + " SET short_summary = ? WHERE id_show = ?";
96
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, summary, m_id ) == false )
97 98 99 100 101
        return false;
    m_shortSummary = summary;
    return true;
}

102
const std::string& Show::artworkMrl() const
103
{
104
    return m_artworkMrl;
105 106
}

107
bool Show::setArtworkMrl( const std::string& artworkMrl )
108
{
109
    static const std::string req = "UPDATE " + Show::Table::Name
110
            + " SET artwork_mrl = ? WHERE id_show = ?";
111
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
112
        return false;
113
    m_artworkMrl = artworkMrl;
114 115
    return true;
}
116

117
const std::string& Show::tvdbId() const
118 119 120 121
{
    return m_tvdbId;
}

122 123
bool Show::setTvdbId( const std::string& tvdbId )
{
124
    static const std::string req = "UPDATE " + Show::Table::Name
125
            + " SET tvdb_id = ? WHERE id_show = ?";
126
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, tvdbId, m_id ) == false )
127 128 129 130 131
        return false;
    m_tvdbId = tvdbId;
    return true;
}

132
std::shared_ptr<ShowEpisode> Show::addEpisode( Media& media, unsigned int episodeNumber)
133
{
134
    auto episode = ShowEpisode::create( m_ml, media.id(), episodeNumber, m_id );
135
    media.setShowEpisode( episode );
136
    media.save();
137
    return episode;
138 139
}

140
Query<IMedia> Show::episodes( const QueryParameters* params ) const
141
{
142 143
    std::string req = "FROM " + Media::Table::Name + " med "
            " INNER JOIN " + ShowEpisode::Table::Name + " ep ON ep.media_id = med.id_media "
144 145
            + " WHERE ep.show_id = ?";
    std::string orderBy = " ORDER BY ";
146 147 148 149 150
    auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
    auto desc = params != nullptr ? params->desc : false;
    switch ( sort )
    {
        case SortingCriteria::Alpha:
151
            orderBy += "med.name";
152
            if ( desc == true )
153
                orderBy += " DESC";
154 155 156
            break;
        default:
            if ( desc == true )
157
                orderBy += "ep.season_number DESC, ep.episode_number DESC";
158
            else
159
                orderBy += "ep.season_number, ep.episode_number";
160 161 162
            break;

    }
163 164
    return make_query<Media, IMedia>( m_ml, "*", std::move( req ),
                                      std::move( orderBy), m_id );
165 166
}

167 168 169 170 171 172
Query<IMedia> Show::searchEpisodes( const std::string& pattern,
                                    const QueryParameters* params ) const
{
    return Media::searchShowEpisodes( m_ml, pattern, m_id, params );
}

173 174 175 176 177 178 179 180 181 182
uint32_t Show::nbSeasons() const
{
    return 0;
}

uint32_t Show::nbEpisodes() const
{
    return 0;
}

183
void Show::createTable( sqlite::Connection* dbConnection )
184
{
185
    const std::string req = "CREATE TABLE IF NOT EXISTS " + Show::Table::Name + "("
186
                        "id_show INTEGER PRIMARY KEY AUTOINCREMENT,"
187
                        "title TEXT, "
188 189
                        "release_date UNSIGNED INTEGER,"
                        "short_summary TEXT,"
190
                        "artwork_mrl TEXT,"
191 192
                        "tvdb_id TEXT"
                    ")";
193
    const std::string reqFts = "CREATE VIRTUAL TABLE IF NOT EXISTS " +
194
                Show::Table::Name + "Fts USING FTS3"
195 196 197
            "("
                "title"
            ")";
198
    sqlite::Tools::executeRequest( dbConnection, req );
199 200 201 202 203 204
    sqlite::Tools::executeRequest( dbConnection, reqFts );
}

void Show::createTriggers( sqlite::Connection* dbConnection )
{
    const std::string insertTrigger = "CREATE TRIGGER IF NOT EXISTS insert_show_fts"
205
            " AFTER INSERT ON " + Show::Table::Name +
206
            " BEGIN"
207
            " INSERT INTO " + Show::Table::Name + "Fts(rowid,title) VALUES(new.id_show, new.title);"
208 209
            " END";
    const std::string deleteTrigger = "CREATE TRIGGER IF NOT EXISTS delete_show_fts"
210
            " BEFORE DELETE ON " + Show::Table::Name +
211
            " BEGIN"
212
            " DELETE FROM " + Show::Table::Name + "Fts WHERE rowid = old.id_show;"
213 214 215
            " END";
    sqlite::Tools::executeRequest( dbConnection, insertTrigger );
    sqlite::Tools::executeRequest( dbConnection, deleteTrigger );
216
}
217

218
std::shared_ptr<Show> Show::create( MediaLibraryPtr ml, const std::string& name )
219
{
220
    auto show = std::make_shared<Show>( ml, name );
221
    static const std::string req = "INSERT INTO " + Show::Table::Name
222
            + "(title) VALUES(?)";
223
    if ( insert( ml, show, req, name ) == false )
224 225 226
        return nullptr;
    return show;
}
227

228 229
Query<IShow> Show::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
230
    std::string req = "FROM " + Show::Table::Name;
231
    return make_query<Show, IShow>( ml, "*", std::move( req ), orderBy( params ) );
232 233 234 235 236
}

std::string Show::orderBy( const QueryParameters* params )
{
    std::string req = " ORDER BY ";
237 238 239 240 241 242 243 244 245
    SortingCriteria sort = params != nullptr ? params->sort : SortingCriteria::Default;
    switch ( sort )
    {
        case SortingCriteria::ReleaseDate:
            req += "release_date";
            break;
        case SortingCriteria::Default:
        case SortingCriteria::Alpha:
        default:
246
            req += "title";
247 248 249 250 251
            break;

    }
    if ( params != nullptr && params->desc == true )
        req += " DESC";
252
    return req;
253 254
}

255 256 257
Query<IShow> Show::search( MediaLibraryPtr ml, const std::string& pattern,
                           const QueryParameters* params )
{
258 259 260
    std::string req = "FROM " + Show::Table::Name + " WHERE id_show IN"
            "(SELECT rowid FROM " + Show::Table::Name + "Fts WHERE " +
            Show::Table::Name + "Fts MATCH '*' || ? || '*')";
261 262
    return make_query<Show, IShow>( ml, "*", std::move( req ),
                                    orderBy( params ), pattern );
263 264
}

265
}