Label.cpp 2.32 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

13
Label::Label( DBConnection dbConnection, sqlite3_stmt* stmt )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
14
15
    : 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 )
21
    : m_id( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
22
23
    , m_name( name )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
24
25
}

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

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

36
bool Label::files( std::vector<FilePtr>& files )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
37
{
38
39
40
    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 = ?";
41
    return SqliteTools::fetchAll<File>( m_dbConnection, req, files, m_id );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
42
43
}

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

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

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

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