AudioTrack.cpp 4.53 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
#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;

29
AudioTrack::AudioTrack( DBConnection dbConnection, sqlite::Row& row )
30
31
    : m_dbConnection( dbConnection )
{
32
33
34
35
    row >> m_id
        >> m_codec
        >> m_bitrate
        >> m_sampleRate
36
37
38
        >> m_nbChannels
        >> m_language
        >> m_description;
39
40
}

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

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

68
69
70
71
72
73
74
75
76
77
unsigned int AudioTrack::sampleRate() const
{
    return m_sampleRate;
}

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

78
79
80
81
82
83
84
85
86
87
const std::string& AudioTrack::language() const
{
    return m_language;
}

const std::string& AudioTrack::description() const
{
    return m_description;
}

88
89
90
91
92
93
94
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,"
95
96
                "samplerate UNSIGNED INTEGER,"
                "nb_channels UNSIGNED INTEGER,"
97
98
99
                "language TEXT,"
                "description TEXT,"
                "UNIQUE ( codec, bitrate, samplerate, nb_channels, language, description ) ON CONFLICT FAIL"
100
            ")";
101
    return sqlite::Tools::executeRequest( dbConnection, req );
102
103
}

104
std::shared_ptr<AudioTrack> AudioTrack::fetch(DBConnection dbConnection, const std::string& codec,
105
106
                unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels,
                                              const std::string& language, const std::string& desc )
107
108
{
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
109
110
111
            + " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?"
            " AND language = ? AND description = ?";
    return AudioTrack::fetchOne( dbConnection, req, codec, bitrate, sampleRate, nbChannels, language, desc );
112
113
}

114
std::shared_ptr<AudioTrack> AudioTrack::create( DBConnection dbConnection, const std::string& codec,
115
116
                unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels,
                                                const std::string& language, const std::string& desc )
117
118
{
    static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
119
120
121
            + "(codec, bitrate, samplerate, nb_channels, language, description) VALUES(?, ?, ?, ?, ?, ?)";
    auto track = std::make_shared<AudioTrack>( codec, bitrate, sampleRate, nbChannels, language, desc );
    if ( _Cache::insert( dbConnection, track, req, codec, bitrate, sampleRate, nbChannels, language, desc ) == false )
122
123
124
125
        return nullptr;
    track->m_dbConnection = dbConnection;
    return track;
}