File.cpp 4.09 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 "ShowEpisode.h"
10 11
#include "SqliteTools.h"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
12 13 14
const std::string policy::FileTable::Name = "File";
const std::string policy::FileTable::CacheColumn = "mrl";

15 16 17 18 19 20
File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
    : 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 );
21
    m_albumTrackId = sqlite3_column_int( stmt, 3 );
22
    m_playCount = sqlite3_column_int( stmt, 4 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
23
    m_showEpisodeId = sqlite3_column_int( stmt, 5 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
24 25 26 27 28
    m_mrl = (const char*)sqlite3_column_text( stmt, 6 );
}

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

39
FilePtr File::create( sqlite3* dbConnection, const std::string& mrl )
40
{
41
    auto self = std::make_shared<File>( mrl );
42 43
    static const std::string req = "INSERT INTO " + policy::FileTable::Name +
            " VALUES(NULL, ?, ?, ?, ?, ?, ?)";
44 45 46 47 48 49 50
    auto pKey = _Cache::insert( dbConnection, self, req.c_str(), (int)self->m_type, self->m_duration,
        self->m_albumTrackId, self->m_playCount, self->m_showEpisodeId, self->m_mrl );
    if ( pKey == 0 )
        return nullptr;
    self->m_id = pKey;
    self->m_dbConnection = dbConnection;
    return self;
51 52
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53
std::shared_ptr<IAlbumTrack> File::albumTrack()
54
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
55
    if ( m_albumTrack == nullptr && m_albumTrackId != 0 )
56
    {
57
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, m_albumTrackId );
58 59
    }
    return m_albumTrack;
60 61 62 63 64 65 66
}

unsigned int File::duration()
{
    return m_duration;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67
std::shared_ptr<IShowEpisode> File::showEpisode()
68
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
69
    if ( m_showEpisode == nullptr && m_showEpisodeId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
71
        m_showEpisode = ShowEpisode::fetch( m_dbConnection, m_showEpisodeId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72 73
    }
    return m_showEpisode;
74 75
}

76
std::vector<std::shared_ptr<ILabel> > File::labels()
77
{
78
    std::vector<std::shared_ptr<ILabel> > labels;
79
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
80 81
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
            "WHERE lfr.id_file = ?";
82
    SqliteTools::fetchAll<Label>( m_dbConnection, req.c_str(), labels, m_id );
83
    return labels;
84 85
}

86 87
int File::playCount()
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88
    return m_playCount;
89 90
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
91 92 93 94 95
const std::string& File::mrl()
{
    return m_mrl;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
96 97 98 99 100
unsigned int File::id() const
{
    return m_id;
}

101
bool File::createTable(sqlite3* connection)
102
{
103
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FileTable::Name + "("
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
104
            "id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
105 106
            "type INTEGER,"
            "duration UNSIGNED INTEGER,"
107
            "album_track_id UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108 109
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
110
            "mrl TEXT UNIQUE ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
111
            ")";
112
    return SqliteTools::executeRequest( connection, req.c_str() );
113
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
114

115 116 117 118 119 120 121 122
bool File::addLabel( LabelPtr label )
{
    if ( m_dbConnection == nullptr || m_id == 0 || label->id() == 0)
    {
        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(?, ?)";
123
    return SqliteTools::executeRequest( m_dbConnection, req, label->id(), m_id );
124 125 126 127 128 129 130 131 132 133
}

bool File::removeLabel( LabelPtr label )
{
    if ( m_dbConnection == nullptr || m_id == 0 || label->id() == 0 )
    {
        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 = ?";
134
    return SqliteTools::executeDelete( m_dbConnection, req, label->id(), m_id );
135
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
136 137 138 139 140 141 142 143 144 145

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 );
}