Show.cpp 4.31 KB
Newer Older
1
#include "Show.h"
2
#include "ShowEpisode.h"
3 4
#include "SqliteTools.h"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
5 6
const std::string policy::ShowTable::Name = "Show";
const std::string policy::ShowTable::CacheColumn = "id_show";
7
unsigned int Show::* const policy::ShowTable::PrimaryKey = &Show::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
8

9
Show::Show(DBConnection dbConnection, sqlite3_stmt* stmt)
10 11
    : m_dbConnection( dbConnection )
{
12 13 14 15 16 17 18
    m_id = Traits<unsigned int>::Load( stmt, 0 );
    m_name = Traits<std::string>::Load( stmt, 1 );
    m_releaseDate = Traits<unsigned int>::Load( stmt, 2 );
    m_shortSummary = Traits<std::string>::Load( stmt, 3 );
    m_artworkUrl = Traits<std::string>::Load( stmt, 4 );
    m_lastSyncDate = Traits<unsigned int>::Load( stmt, 5 );
    m_tvdbId = Traits<std::string>::Load( stmt, 6 );
19 20
}

21 22 23 24 25
Show::Show( const std::string& name )
    : m_id( 0 )
    , m_name( name )
    , m_releaseDate( 0 )
    , m_lastSyncDate( 0 )
26 27 28
{
}

29 30 31 32 33 34
unsigned int Show::id() const
{
    return m_id;
}

const std::string& Show::name() const
35 36 37 38
{
    return m_name;
}

39
time_t Show::releaseDate() const
40
{
41
    return m_releaseDate;
42 43
}

44 45 46 47 48 49 50 51 52 53 54
bool Show::setReleaseDate( time_t date )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET release_date = ? WHERE id_show = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
        return false;
    m_releaseDate = date;
    return true;
}

const std::string& Show::shortSummary() const
55 56 57 58
{
    return m_shortSummary;
}

59 60 61 62 63 64 65 66 67 68 69
bool Show::setShortSummary( const std::string& summary )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET short_summary = ? WHERE id_show = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
        return false;
    m_shortSummary = summary;
    return true;
}

const std::string& Show::artworkUrl() const
70 71 72 73
{
    return m_artworkUrl;
}

74 75 76 77 78 79 80 81 82
bool Show::setArtworkUrl( const std::string& artworkUrl )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET artwork_url = ? WHERE id_show = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, artworkUrl, m_id ) == false )
        return false;
    m_artworkUrl = artworkUrl;
    return true;
}
83

84
time_t Show::lastSyncDate() const
85 86 87 88 89 90 91 92 93
{
    return m_lastSyncDate;
}

const std::string& Show::tvdbId()
{
    return m_tvdbId;
}

94 95 96 97 98 99 100 101 102 103
bool Show::setTvdbId( const std::string& tvdbId )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET tvdb_id = ? WHERE id_show = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, tvdbId, m_id ) == false )
        return false;
    m_tvdbId = tvdbId;
    return true;
}

104 105 106 107 108 109 110 111 112 113 114 115
ShowEpisodePtr Show::addEpisode(const std::string& title, unsigned int episodeNumber)
{
    return ShowEpisode::create( m_dbConnection, title, episodeNumber, m_id );
}

bool Show::episodes( std::vector<ShowEpisodePtr>& episodes )
{
    static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
            + " WHERE show_id = ?";
    return SqliteTools::fetchAll<ShowEpisode>( m_dbConnection, req, episodes, m_id );
}

116 117 118 119 120 121 122 123 124 125 126 127 128
bool Show::destroy()
{
    std::vector<ShowEpisodePtr> eps;
    if ( episodes( eps ) == false )
        return false;
    //FIXME: This is suboptimal. Each episode::destroy() will fire a SQL request of its own
    for ( auto& t : eps )
    {
        t->destroy();
    }
    return _Cache::destroy( m_dbConnection, this );
}

129
bool Show::createTable(DBConnection dbConnection)
130
{
131
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowTable::Name + "("
132
                        "id_show INTEGER PRIMARY KEY AUTOINCREMENT,"
133 134 135
                        "name TEXT, "
                        "release_date UNSIGNED INTEGER,"
                        "short_summary TEXT,"
136 137 138 139
                        "artwork_url TEXT,"
                        "last_sync_date UNSIGNED INTEGER,"
                        "tvdb_id TEXT"
                    ")";
140
    return SqliteTools::executeRequest( dbConnection, req );
141
}
142

143
ShowPtr Show::create(DBConnection dbConnection, const std::string& name )
144 145 146 147 148 149 150 151 152
{
    auto show = std::make_shared<Show>( name );
    static const std::string req = "INSERT INTO " + policy::ShowTable::Name
            + "(name) VALUES(?)";
    if ( _Cache::insert( dbConnection, show, req, name ) == false )
        return nullptr;
    show->m_dbConnection = dbConnection;
    return show;
}