Label.cpp 3.12 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.
 *****************************************************************************/

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
23 24 25 26
#include <cassert>
#include <cstdlib>
#include <cstring>

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27
#include "Label.h"
28
#include "Media.h"
29
#include "database/SqliteTools.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
30

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31
const std::string policy::LabelTable::Name = "Label";
32
const std::string policy::LabelTable::PrimaryKeyColumn = "id_label";
33
unsigned int Label::* const policy::LabelTable::PrimaryKey = &Label::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34

35
Label::Label( DBConnection dbConnection, sqlite::Row& row )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36 37
    : m_dbConnection( dbConnection )
{
38 39
    row >> m_id
        >> m_name;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
40 41 42
}

Label::Label( const std::string& name )
43
    : m_id( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44 45
    , m_name( name )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46 47
}

48 49 50 51 52
unsigned int Label::id() const
{
    return m_id;
}

53
const std::string& Label::name() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54 55 56 57
{
    return m_name;
}

58
std::vector<MediaPtr> Label::files()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
{
60
    static const std::string req = "SELECT f.* FROM " + policy::MediaTable::Name + " f "
61 62
            "LEFT JOIN LabelFileRelation lfr ON lfr.media_id = f.id_media "
            "WHERE lfr.label_id = ?";
63
    return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64 65
}

66
LabelPtr Label::create(DBConnection dbConnection, const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67
{
68
    auto self = std::make_shared<Label>( name );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
69
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
70
    if ( insert( dbConnection, self, req, self->m_name ) == false )
71 72 73
        return nullptr;
    self->m_dbConnection = dbConnection;
    return self;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74 75
}

76
bool Label::createTable(DBConnection dbConnection)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
77
{
78
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::LabelTable::Name + "("
79
                "id_label INTEGER PRIMARY KEY AUTOINCREMENT, "
80
                "name TEXT UNIQUE ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
81
            ")";
82
    if ( sqlite::Tools::executeRequest( dbConnection, req ) == false )
83
        return false;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84
    req = "CREATE TABLE IF NOT EXISTS LabelFileRelation("
85 86 87 88 89
                "label_id INTEGER,"
                "media_id INTEGER,"
            "PRIMARY KEY (label_id, media_id),"
            "FOREIGN KEY(label_id) REFERENCES Label(id_label) ON DELETE CASCADE,"
            "FOREIGN KEY(media_id) REFERENCES Media(id_media) ON DELETE CASCADE);";
90
    return sqlite::Tools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
91
}