AudioTrack.cpp 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#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 )
    , m_id( Traits<unsigned int>::Load( stmt, 0 ) )
    , m_codec( Traits<std::string>::Load( stmt, 1 ) )
    , m_bitrate( Traits<unsigned int>::Load( stmt, 2 ) )
{
}

AudioTrack::AudioTrack( const std::string& codec, unsigned int bitrate )
    : m_id( 0 )
    , m_codec( codec )
    , m_bitrate( bitrate )
{
}

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

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,"
                "UNIQUE ( codec, bitrate ) ON CONFLICT FAIL"
            ")";
    return SqliteTools::executeRequest( dbConnection, req );
}

AudioTrackPtr AudioTrack::fetch( DBConnection dbConnection, const std::string& codec, unsigned int bitrate )
{
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
            + " WHERE codec = ? AND bitrate = ?";
    return SqliteTools::fetchOne<AudioTrack>( dbConnection, req, codec, bitrate );
}

AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec, unsigned int bitrate )
{
    static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
            + "(codec, bitrate) VALUES(?, ?)";
    auto track = std::make_shared<AudioTrack>( codec, bitrate );
    if ( _Cache::insert( dbConnection, track, req, codec, bitrate ) == false )
        return nullptr;
    track->m_dbConnection = dbConnection;
    return track;
}