File.cpp 5.71 KB
Newer Older
1
#include <cassert>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
2 3
#include <cstdlib>
#include <cstring>
4

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
5
#include "Album.h"
6
#include "AlbumTrack.h"
7
#include "File.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
8
#include "Label.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
9
#include "Movie.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
10
#include "ShowEpisode.h"
11 12
#include "SqliteTools.h"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
13 14
const std::string policy::FileTable::Name = "File";
const std::string policy::FileTable::CacheColumn = "mrl";
15
unsigned int File::* const policy::FileTable::PrimaryKey = &File::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
16

17
File::File( DBConnection dbConnection, sqlite3_stmt* stmt )
18 19 20 21 22
    : m_dbConnection( dbConnection )
{
    m_id = sqlite3_column_int( stmt, 0 );
    m_type = (Type)sqlite3_column_int( stmt, 1 );
    m_duration = sqlite3_column_int( stmt, 2 );
23
    m_albumTrackId = sqlite3_column_int( stmt, 3 );
24
    m_playCount = sqlite3_column_int( stmt, 4 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
25
    m_showEpisodeId = sqlite3_column_int( stmt, 5 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
26
    m_mrl = (const char*)sqlite3_column_text( stmt, 6 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27
    m_movieId = Traits<unsigned int>::Load( stmt, 7 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28 29 30
}

File::File( const std::string& mrl )
31
    : m_id( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32 33 34 35 36 37
    , m_type( UnknownType )
    , m_duration( 0 )
    , m_albumTrackId( 0 )
    , m_playCount( 0 )
    , m_showEpisodeId( 0 )
    , m_mrl( mrl )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
    , m_movieId( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39
{
40 41
}

42
FilePtr File::create( DBConnection dbConnection, const std::string& mrl )
43
{
44
    auto self = std::make_shared<File>( mrl );
45
    static const std::string req = "INSERT INTO " + policy::FileTable::Name +
46 47
            "(mrl) VALUES(?)";
    bool pKey = _Cache::insert( dbConnection, self, req, mrl );
48
    if ( pKey == false )
49 50 51
        return nullptr;
    self->m_dbConnection = dbConnection;
    return self;
52 53
}

54
AlbumTrackPtr File::albumTrack()
55
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
56
    if ( m_albumTrack == nullptr && m_albumTrackId != 0 )
57
    {
58
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, m_albumTrackId );
59 60
    }
    return m_albumTrack;
61 62
}

63 64 65 66 67 68 69 70 71 72 73
bool File::setAlbumTrack( AlbumTrackPtr albumTrack )
{
    static const std::string req = "UPDATE " + policy::FileTable::Name + " SET album_track_id = ? "
            "WHERE id_file = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, albumTrack->id(), m_id ) == false )
        return false;
    m_albumTrackId = albumTrack->id();
    m_albumTrack = albumTrack;
    return true;
}

74
unsigned int File::duration() const
75 76 77 78
{
    return m_duration;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
79
std::shared_ptr<IShowEpisode> File::showEpisode()
80
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
81
    if ( m_showEpisode == nullptr && m_showEpisodeId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
82
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
83
        m_showEpisode = ShowEpisode::fetch( m_dbConnection, m_showEpisodeId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84 85
    }
    return m_showEpisode;
86 87
}

88 89 90 91 92 93 94 95 96 97 98
bool File::setShowEpisode(ShowEpisodePtr showEpisode)
{
    static const std::string req = "UPDATE " + policy::FileTable::Name
            + " SET show_episode_id = ?  WHERE id_file = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, showEpisode->id(), m_id ) == false )
        return false;
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
    return true;
}

99
std::vector<std::shared_ptr<ILabel> > File::labels()
100
{
101
    std::vector<std::shared_ptr<ILabel> > labels;
102
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
103 104
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
            "WHERE lfr.id_file = ?";
105
    SqliteTools::fetchAll<Label>( m_dbConnection, req, labels, m_id );
106
    return labels;
107 108
}

109
int File::playCount() const
110
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
111
    return m_playCount;
112 113
}

114
const std::string& File::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
115 116 117 118
{
    return m_mrl;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
MoviePtr File::movie()
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

bool File::setMovie( MoviePtr movie )
{
    static const std::string req = "UPDATE " + policy::FileTable::Name
            + " SET movie_id = ? WHERE id_file = ?";
    if ( SqliteTools::executeUpdate( m_dbConnection, req, movie->id(), m_id ) == false )
        return false;
    m_movie = movie;
    m_movieId = movie->id();
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
139 140 141 142 143
unsigned int File::id() const
{
    return m_id;
}

144
bool File::createTable( DBConnection connection )
145
{
146
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FileTable::Name + "("
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
147
            "id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148 149
            "type INTEGER,"
            "duration UNSIGNED INTEGER,"
150
            "album_track_id UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
151 152
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
153
            "mrl TEXT UNIQUE ON CONFLICT FAIL,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
154
            "movie_id UNSIGNED INTEGER,"
155
            "FOREIGN KEY (album_track_id) REFERENCES " + policy::AlbumTrackTable::Name
156 157
            + "(id_track) ON DELETE CASCADE,"
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
158 159 160
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
            + "(id_movie) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
161
            ")";
162
    return SqliteTools::executeRequest( connection, req );
163
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
164

165 166
bool File::addLabel( LabelPtr label )
{
167
    if ( m_id == 0 || label->id() == 0 )
168 169 170 171 172
    {
        std::cerr << "Both file & label need to be inserted in database before being linked together" << std::endl;
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
173
    return SqliteTools::executeRequest( m_dbConnection, req, label->id(), m_id );
174 175 176 177
}

bool File::removeLabel( LabelPtr label )
{
178
    if ( m_id == 0 || label->id() == 0 )
179 180 181 182 183
    {
        std::cerr << "Can't unlink a label/file not inserted in database" << std::endl;
        return false;
    }
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_file = ?";
184
    return SqliteTools::executeDelete( m_dbConnection, req, label->id(), m_id );
185
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
186 187 188 189 190 191 192 193 194 195

const std::string& policy::FileCache::key(const std::shared_ptr<File> self )
{
    return self->mrl();
}

std::string policy::FileCache::key(sqlite3_stmt* stmt)
{
    return Traits<std::string>::Load( stmt, 6 );
}