Folder.cpp 1.59 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Folder.h"
#include "File.h"

#include "SqliteTools.h"

namespace policy
{
    const std::string FolderTable::Name = "Folder";
    const std::string FolderTable::CacheColumn = "id_folder";
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
}

Folder::Folder( DBConnection dbConnection, sqlite3_stmt* stmt )
    : m_dbConection( dbConnection )
{
    m_id = Traits<unsigned int>::Load( stmt, 0 );
    m_path = Traits<std::string>::Load( stmt, 1 );
}

Folder::Folder( const std::string& path )
    : m_path( path )
{
}

bool Folder::createTable(DBConnection connection)
{
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name + "("
            + policy::FolderTable::CacheColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            "path TEXT UNIQUE ON CONFLICT FAIL"
            ")";
    return SqliteTools::executeRequest( connection, req );
}

FolderPtr Folder::create(DBConnection connection, const std::string& path)
{
    auto self = std::make_shared<Folder>( path );
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
            "(path) VALUES(?)";
    if ( _Cache::insert( connection, self, req, path ) == false )
        return nullptr;
    self->m_dbConection = connection;
    return self;
}

unsigned int Folder::id() const
{
    return m_id;
}

const std::string& Folder::path()
{
    return m_path;
}

std::vector<FilePtr> Folder::files()
{
    static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name +
        " WHERE f.id_folder = ?";
59
    return SqliteTools::fetchAll<File, IFile>( m_dbConection, req, m_id );
60
}