Label.cpp 2.34 KB
Newer Older
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
1
2
3
4
#include <cassert>
#include <cstdlib>
#include <cstring>

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
5
#include "Label.h"
6
#include "File.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
7
8
#include "SqliteTools.h"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
9
const std::string policy::LabelTable::Name = "Label";
10
const std::string policy::LabelTable::CacheColumn = "name";
11
unsigned int Label::* const policy::LabelTable::PrimaryKey = &Label::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
12

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
13
14
15
Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt )
    : m_dbConnection( dbConnection )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
16
17
18
19
20
    m_id = sqlite3_column_int( stmt, 0 );
    m_name = (const char*)sqlite3_column_text( stmt, 1 );
}

Label::Label( const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
21
    : m_dbConnection( nullptr )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
22
23
24
    , m_id( 0 )
    , m_name( name )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
25
26
}

27
28
29
30
31
unsigned int Label::id() const
{
    return m_id;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32
33
34
35
36
const std::string& Label::name()
{
    return m_name;
}

37
bool Label::files( std::vector<FilePtr>& files )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
{
39
40
41
    static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name + " f "
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
            "WHERE lfr.id_label = ?";
42
    return SqliteTools::fetchAll<File>( m_dbConnection, req, files, m_id );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
44
}

45
LabelPtr Label::create( sqlite3* dbConnection, const std::string& name )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46
{
47
    auto self = std::make_shared<Label>( name );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
48
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
49
    if ( _Cache::insert( dbConnection, self, req, self->m_name ) == false )
50
51
52
        return nullptr;
    self->m_dbConnection = dbConnection;
    return self;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53
54
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
55
56
bool Label::createTable(sqlite3* dbConnection)
{
57
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::LabelTable::Name + "("
58
                "id_label INTEGER PRIMARY KEY AUTOINCREMENT, "
59
                "name TEXT UNIQUE ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
            ")";
61
    if ( SqliteTools::executeRequest( dbConnection, req ) == false )
62
        return false;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
63
64
65
66
67
68
    req = "CREATE TABLE IF NOT EXISTS LabelFileRelation("
                "id_label INTEGER,"
                "id_file INTEGER,"
            "PRIMARY KEY (id_label, id_file)"
            "FOREIGN KEY(id_label) REFERENCES Label(id_label) ON DELETE CASCADE,"
            "FOREIGN KEY(id_file) REFERENCES File(id_file) ON DELETE CASCADE);";
69
    return SqliteTools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
71

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72
const std::string&policy::LabelCachePolicy::key( const std::shared_ptr<ILabel> self )
73
74
75
76
77
78
79
80
{
    return self->name();
}

std::string policy::LabelCachePolicy::key(sqlite3_stmt* stmt)
{
    return Traits<KeyType>::Load( stmt, 1 );
}