ShowEpisode.cpp 5.58 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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27
#include "ShowEpisode.h"
28
#include "database/SqliteTools.h"
29
#include "database/SqliteQuery.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
30
#include "Show.h"
31
#include "Media.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32

33 34 35
namespace medialibrary
{

36 37 38
const std::string ShowEpisode::Table::Name = "ShowEpisode";
const std::string ShowEpisode::Table::PrimaryKeyColumn = "show_id";
int64_t ShowEpisode::* const ShowEpisode::Table::PrimaryKey = &ShowEpisode::m_id;
39

40 41
ShowEpisode::ShowEpisode( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
42 43 44 45 46 47 48
    , m_id( row.extract<decltype(m_id)>() )
    , m_mediaId( row.extract<decltype(m_mediaId)>() )
    , m_episodeNumber( row.extract<decltype(m_episodeNumber)>() )
    , m_seasonNumber( row.extract<decltype(m_seasonNumber)>() )
    , m_shortSummary( row.extract<decltype(m_shortSummary)>() )
    , m_tvdbId( row.extract<decltype(m_tvdbId)>() )
    , m_showId( row.extract<decltype(m_showId)>() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
49 50 51
{
}

52 53
ShowEpisode::ShowEpisode( MediaLibraryPtr ml, int64_t mediaId,
                          unsigned int episodeNumber, int64_t showId )
54 55
    : m_ml( ml )
    , m_id( 0 )
56
    , m_mediaId( mediaId )
57 58 59 60 61 62
    , m_episodeNumber( episodeNumber )
    , m_seasonNumber( 0 )
    , m_showId( showId )
{
}

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

68
unsigned int ShowEpisode::episodeNumber() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
69 70 71 72
{
    return m_episodeNumber;
}

73
unsigned int ShowEpisode::seasonNumber() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74 75 76 77
{
    return m_seasonNumber;
}

78 79
bool ShowEpisode::setSeasonNumber( unsigned int seasonNumber )
{
80
    static const std::string req = "UPDATE " + ShowEpisode::Table::Name
81
            + " SET season_number = ? WHERE id_episode = ?";
82
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, seasonNumber, m_id ) == false )
83 84 85 86 87 88
        return false;
    m_seasonNumber = seasonNumber;
    return true;
}

const std::string& ShowEpisode::shortSummary() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
89 90 91 92
{
    return m_shortSummary;
}

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

const std::string& ShowEpisode::tvdbId() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
104 105 106 107
{
    return m_tvdbId;
}

108 109
bool ShowEpisode::setTvdbId( const std::string& tvdbId )
{
110
    static const std::string req = "UPDATE " + ShowEpisode::Table::Name
111
            + " SET tvdb_id = ? WHERE id_episode = ?";
112
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, tvdbId, m_id ) == false )
113 114 115 116 117
        return false;
    m_tvdbId = tvdbId;
    return true;
}

118
std::shared_ptr<IShow> ShowEpisode::show()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
119
{
120
    if ( m_show == nullptr && m_showId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
121
    {
122
        m_show = Show::fetch( m_ml, m_showId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
123 124 125 126
    }
    return m_show;
}

127
void ShowEpisode::createTable( sqlite::Connection* dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
128
{
129
    const std::string req = "CREATE TABLE IF NOT EXISTS " + ShowEpisode::Table::Name
130
            + "("
131
                "id_episode INTEGER PRIMARY KEY AUTOINCREMENT,"
132
                "media_id UNSIGNED INTEGER NOT NULL,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133 134 135 136 137
                "episode_number UNSIGNED INT,"
                "season_number UNSIGNED INT,"
                "episode_summary TEXT,"
                "tvdb_id TEXT,"
                "show_id UNSIGNED INT,"
138
                "FOREIGN KEY(media_id) REFERENCES " + Media::Table::Name
139
                    + "(id_media) ON DELETE CASCADE,"
140
                "FOREIGN KEY(show_id) REFERENCES " + Show::Table::Name
141
                    + "(id_show) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
142
            ")";
143
    const std::string indexReq = "CREATE INDEX IF NOT EXISTS show_episode_media_show_idx ON " +
144
            ShowEpisode::Table::Name + "(media_id, show_id)";
145 146
    sqlite::Tools::executeRequest( dbConnection, req );
    sqlite::Tools::executeRequest( dbConnection, indexReq );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
147
}
148

149 150
std::shared_ptr<ShowEpisode> ShowEpisode::create( MediaLibraryPtr ml, int64_t mediaId,
                                                  unsigned int episodeNumber, int64_t showId )
151
{
152
    auto episode = std::make_shared<ShowEpisode>( ml, mediaId, episodeNumber, showId );
153
    static const std::string req = "INSERT INTO " + ShowEpisode::Table::Name
154 155
            + "(media_id, episode_number, show_id) VALUES(?, ?, ?)";
    if ( insert( ml, episode, req, mediaId, episodeNumber, showId ) == false )
156 157 158
        return nullptr;
    return episode;
}
159

160
ShowEpisodePtr ShowEpisode::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
161
{
162
    static const std::string req = "SELECT * FROM " + ShowEpisode::Table::Name + " WHERE media_id = ?";
163
    return fetch( ml, req, mediaId );
164
}
165 166

}