File.cpp 2.51 KB
Newer Older
1 2
#include <cassert>

3
#include "File.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
4
#include "Label.h"
5
#include "SqliteTools.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6
#include "Album.h"
7 8 9

File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
    : m_dbConnection( dbConnection )
10 11 12
    , m_albumTrack( NULL )
    , m_showEpisode( NULL )
    , m_labels( NULL )
13 14 15 16
{
    m_id = sqlite3_column_int( stmt, 0 );
    m_type = (Type)sqlite3_column_int( stmt, 1 );
    m_duration = sqlite3_column_int( stmt, 2 );
17
    m_albumTrackId = sqlite3_column_int( stmt, 3 );
18 19
}

20 21
File::File()
    : m_dbConnection( NULL )
22 23 24 25
    , m_id( 0 )
{
}

26
bool File::insert( sqlite3* dbConnection )
27
{
28 29
    assert( m_dbConnection == NULL );
    m_dbConnection = dbConnection;
30
    sqlite3_stmt* stmt;
31
    std::string req = "INSERT INTO File VALUES(NULL, ?, ?, ?)";
32 33 34 35
    if ( sqlite3_prepare_v2( m_dbConnection, req.c_str(), -1, &stmt, NULL ) != SQLITE_OK )
        return false;
    sqlite3_bind_int( stmt, 1, m_type );
    sqlite3_bind_int( stmt, 2, m_duration );
36
    sqlite3_bind_int( stmt, 3, m_albumTrackId );
37 38 39 40 41 42
    if ( sqlite3_step( stmt ) != SQLITE_DONE )
        return false;
    m_id = sqlite3_last_insert_rowid( m_dbConnection );
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
IAlbumTrack* File::albumTrack()
44
{
45 46 47 48 49
    if ( m_albumTrack == NULL && m_albumTrackId != 0 )
    {
        m_albumTrack = AlbumTrack::fetch( m_albumTrackId );
    }
    return m_albumTrack;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
}

const std::string&File::artworkUrl()
{
}

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

IShowEpisode*File::showEpisode()
{
}

std::vector<ILabel*> File::labels()
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    if ( m_labels == NULL )
    {
        m_labels = new std::vector<ILabel*>;
        const char* req = "SELECT * FROM Labels l"
                "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = f.id_label "
                "WHERE lfr.id_file = ?";
        sqlite3_stmt* stmt;
        int res = sqlite3_prepare_v2( m_dbConnection, req, -1, &stmt, NULL );
        if ( res != SQLITE_OK )
            return *m_labels;
        sqlite3_bind_int( stmt, 1, m_id );
        res = sqlite3_step( stmt );
        while ( res == SQLITE_ROW )
        {
            ILabel* l = new Label( m_dbConnection, stmt );
            m_labels->push_back( l );
            res = sqlite3_step( stmt );
        }
        sqlite3_finalize( stmt );
    }
    return *m_labels;
88 89 90 91
}

bool File::CreateTable(sqlite3* connection)
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
92
    const char* req = "CREATE TABLE IF NOT EXISTS File("
93
            "id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
94 95
            "type INTEGER, duration UNSIGNED INTEGER,"
            "album_track_id UNSIGNED INTEGER)";
96 97
    return SqliteTools::CreateTable( connection, req );
}