Label.cpp 3.54 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
#if HAVE_CONFIG_H
# include "config.h"
#endif

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27 28 29 30
#include <cassert>
#include <cstdlib>
#include <cstring>

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31
#include "Label.h"
32
#include "Media.h"
33
#include "database/SqliteTools.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34

35 36 37
namespace medialibrary
{

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
const std::string policy::LabelTable::Name = "Label";
39
const std::string policy::LabelTable::PrimaryKeyColumn = "id_label";
40
int64_t Label::* const policy::LabelTable::PrimaryKey = &Label::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
41

42 43
Label::Label(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44
{
45 46
    row >> m_id
        >> m_name;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47 48
}

49 50 51
Label::Label( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52 53
    , m_name( name )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54 55
}

56
int64_t Label::id() const
57 58 59 60
{
    return m_id;
}

61
const std::string& Label::name() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
62 63 64 65
{
    return m_name;
}

66
std::vector<MediaPtr> Label::files()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67
{
68
    static const std::string req = "SELECT f.* FROM " + policy::MediaTable::Name + " f "
69
            "INNER JOIN LabelFileRelation lfr ON lfr.media_id = f.id_media "
70
            "WHERE lfr.label_id = ?";
71
    return Media::fetchAll<IMedia>( m_ml, req, m_id );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72 73
}

74
LabelPtr Label::create( MediaLibraryPtr ml, const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
75
{
76
    auto self = std::make_shared<Label>( ml, name );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
77
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
78
    if ( insert( ml, self, req, self->m_name ) == false )
79 80
        return nullptr;
    return self;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
81 82
}

83
bool Label::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84
{
85
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::LabelTable::Name + "("
86
                "id_label INTEGER PRIMARY KEY AUTOINCREMENT, "
87
                "name TEXT UNIQUE ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88
            ")";
89
    const std::string relReq = "CREATE TABLE IF NOT EXISTS LabelFileRelation("
90 91 92 93 94
                "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);";
95
    const std::string ftsTrigger = "CREATE TRIGGER IF NOT EXISTS delete_label_fts "
96 97 98 99 100 101 102 103
            "BEFORE DELETE ON " + policy::LabelTable::Name +
            " BEGIN"
            " UPDATE " + policy::MediaTable::Name + "Fts SET labels = TRIM(REPLACE(labels, old.name, ''))"
            " WHERE labels MATCH old.name;"
            " END";
    return sqlite::Tools::executeRequest( dbConnection, req ) &&
            sqlite::Tools::executeRequest( dbConnection, relReq ) &&
            sqlite::Tools::executeRequest( dbConnection, ftsTrigger );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
104
}
105 106

}