AudioTrack.cpp 4.23 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
#include "AudioTrack.h"

25
26
#include "Media.h"

27
28
29
30
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;

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

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

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

73
74
75
76
77
78
79
80
81
82
unsigned int AudioTrack::sampleRate() const
{
    return m_sampleRate;
}

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

83
84
85
86
87
88
89
90
91
92
const std::string& AudioTrack::language() const
{
    return m_language;
}

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

93
94
bool AudioTrack::createTable( DBConnection dbConnection )
{
95
    //FIXME: Index on media_id ? Unless it's already implied by the foreign key
96
97
98
99
100
    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,"
101
102
                "samplerate UNSIGNED INTEGER,"
                "nb_channels UNSIGNED INTEGER,"
103
104
                "language TEXT,"
                "description TEXT,"
105
106
107
                "media_id UNSIGNED INT,"
                "FOREIGN KEY ( media_id ) REFERENCES " + policy::MediaTable::Name
                    + "( id_media ) ON DELETE CASCADE"
108
            ")";
109
    return sqlite::Tools::executeRequest( dbConnection, req );
110
111
}

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