Label.cpp 2.45 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
10
11
const std::string policy::LabelTable::Name = "Label";
const std::string policy::LabelTable::CacheColumn = "id_label";

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

Label::Label( const std::string& name )
    : m_dbConnection( NULL )
    , m_id( 0 )
    , m_name( name )
    , m_files( NULL )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
26
27
}

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33
34
35
36
37
38

const std::string& Label::name()
{
    return m_name;
}

39
std::vector<FilePtr>& Label::files()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
40
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
41
    if ( m_files == nullptr )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
42
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
        m_files = new std::vector<std::shared_ptr<IFile>>;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44
        const char* req = "SELECT f.* FROM Files f "
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
45
46
                "LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
                "WHERE lfr.id_label = ?";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47
        SqliteTools::fetchAll<File>( m_dbConnection, req, m_id, *m_files );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
48
49
50
51
    }
    return *m_files;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52
53
bool Label::insert( sqlite3* dbConnection )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54
    assert( m_dbConnection == nullptr );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    assert( m_id == 0 );
    sqlite3_stmt* stmt;
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
    if ( sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL ) != SQLITE_OK )
    {
        std::cerr << "Failed to insert record: " << sqlite3_errmsg( dbConnection ) << std::endl;
        return false;
    }
    char* tmpName = strdup( m_name.c_str() );
    sqlite3_bind_text( stmt, 1, tmpName, -1, &free );
    if ( sqlite3_step( stmt ) != SQLITE_DONE )
        return false;
    m_dbConnection = dbConnection;
    m_id = sqlite3_last_insert_rowid( dbConnection );
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72
73
74
bool Label::createTable(sqlite3* dbConnection)
{
    const char* req = "CREATE TABLE IF NOT EXISTS Label("
75
                "id_label INTEGER PRIMARY KEY AUTOINCREMENT, "
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
76
77
                "name TEXT"
            ")";
78
    if ( SqliteTools::createTable( dbConnection, req ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
79
80
81
82
83
84
85
        return false;
    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);";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
86
    return SqliteTools::createTable( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88