Folder.cpp 1.82 KB
Newer Older
1
2
3
4
5
6
7
8
#include "Folder.h"
#include "File.h"

#include "SqliteTools.h"

namespace policy
{
    const std::string FolderTable::Name = "Folder";
9
    const std::string FolderTable::CacheColumn = "path";
10
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
11
12
13
14
15
16
17
18
19
20
21

    const FolderCache::KeyType&FolderCache::key(const std::shared_ptr<Folder>& self)
    {
        return self->path();
    }

    FolderCache::KeyType FolderCache::key( sqlite3_stmt* stmt )
    {
        return Traits<FolderCache::KeyType>::Load( stmt, 1 );
    }

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
}

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 + "("
39
            + "id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            "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 = ?";
70
    return SqliteTools::fetchAll<File, IFile>( m_dbConection, req, m_id );
71
}