AudioTrack.cpp 3.95 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
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26 27 28 29 30
#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 )
31 32 33 34 35
    , 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 ) )
36 37 38
{
}

39
AudioTrack::AudioTrack(const std::string& codec, unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
40 41 42
    : m_id( 0 )
    , m_codec( codec )
    , m_bitrate( bitrate )
43 44
    , m_sampleRate( sampleRate )
    , m_nbChannels( nbChannels )
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
}

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

63 64 65 66 67 68 69 70 71 72
unsigned int AudioTrack::sampleRate() const
{
    return m_sampleRate;
}

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

73 74 75 76 77 78 79
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,"
80 81
                "samplerate UNSIGNED INTEGER,"
                "nb_channels UNSIGNED INTEGER,"
82 83
                "UNIQUE ( codec, bitrate ) ON CONFLICT FAIL"
            ")";
84
    return sqlite::Tools::executeRequest( dbConnection, req );
85 86
}

87 88
AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& codec,
                unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
89 90
{
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
91
            + " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?";
92
    return AudioTrack::fetchOne( dbConnection, req, codec, bitrate, sampleRate, nbChannels );
93 94
}

95 96
AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec,
                unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels )
97 98
{
    static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
99 100 101
            + "(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 )
102 103 104 105
        return nullptr;
    track->m_dbConnection = dbConnection;
    return track;
}