AudioTrack.cpp 2.88 KB
Newer Older
1 2 3 4 5 6 7 8
#include "AudioTrack.h"

const std::string policy::AudioTrackTable::Name = "AudioTrack";
const std::string policy::AudioTrackTable::CacheColumn  = "id_track";
unsigned int AudioTrack::* const policy::AudioTrackTable::PrimaryKey = &AudioTrack::m_id;

AudioTrack::AudioTrack( DBConnection dbConnection, sqlite3_stmt* stmt )
    : m_dbConnection( dbConnection )
9 10 11 12 13
    , m_id( sqlite::Traits<unsigned int>::Load( stmt, 0 ) )
    , m_codec( sqlite::Traits<std::string>::Load( stmt, 1 ) )
    , m_bitrate( sqlite::Traits<unsigned int>::Load( stmt, 2 ) )
    , m_sampleRate( sqlite::Traits<unsigned int>::Load( stmt, 3 ) )
    , m_nbChannels( sqlite::Traits<unsigned int>::Load( stmt, 4 ) )
14 15 16
{
}

17
AudioTrack::AudioTrack(const std::string& codec, unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
18 19 20
    : m_id( 0 )
    , m_codec( codec )
    , m_bitrate( bitrate )
21 22
    , m_sampleRate( sampleRate )
    , m_nbChannels( nbChannels )
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
}

unsigned int AudioTrack::id() const
{
    return m_id;
}

const std::string&AudioTrack::codec() const
{
    return m_codec;
}

unsigned int AudioTrack::bitrate() const
{
    return m_bitrate;
}

41 42 43 44 45 46 47 48 49 50
unsigned int AudioTrack::sampleRate() const
{
    return m_sampleRate;
}

unsigned int AudioTrack::nbChannels() const
{
    return m_nbChannels;
}

51 52 53 54 55 56 57
bool AudioTrack::createTable( DBConnection dbConnection )
{
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AudioTrackTable::Name
            + "(" +
                policy::AudioTrackTable::CacheColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                "codec TEXT,"
                "bitrate UNSIGNED INTEGER,"
58 59
                "samplerate UNSIGNED INTEGER,"
                "nb_channels UNSIGNED INTEGER,"
60 61
                "UNIQUE ( codec, bitrate ) ON CONFLICT FAIL"
            ")";
62
    return sqlite::Tools::executeRequest( dbConnection, req );
63 64
}

65 66
AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& codec,
                unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
67 68
{
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
69
            + " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?";
70
    return sqlite::Tools::fetchOne<AudioTrack>( dbConnection, req, codec, bitrate, sampleRate, nbChannels );
71 72
}

73 74
AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec,
                unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels )
75 76
{
    static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
77 78 79
            + "(codec, bitrate, samplerate, nb_channels) VALUES(?, ?, ?, ?)";
    auto track = std::make_shared<AudioTrack>( codec, bitrate, sampleRate, nbChannels );
    if ( _Cache::insert( dbConnection, track, req, codec, bitrate, sampleRate, nbChannels ) == false )
80 81 82 83
        return nullptr;
    track->m_dbConnection = dbConnection;
    return track;
}